Skip to content

Commit

Permalink
Add additional processing imports
Browse files Browse the repository at this point in the history
  • Loading branch information
aquarat committed May 3, 2022
1 parent 30bfd85 commit 0988c33
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
28 changes: 25 additions & 3 deletions packages/airnode-node/src/api/unsafe-evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,19 @@ import v8 from 'v8';
import vm from 'vm';
import worker_threads from 'worker_threads';
import zlib from 'zlib';
import axios from 'axios';
import ethers from 'ethers';
import abi from '@api3/airnode-abi';
import adapter from '@api3/airnode-adapter';
import admin from '@api3/airnode-admin';
import node from '@api3/airnode-node';
import ois from '@api3/airnode-ois';
import operation from '@api3/airnode-operation';
import protocol from '@api3/airnode-protocol';
import utilities from '@api3/airnode-utilities';
import validator from '@api3/airnode-validator';

const buildInNodeModules = {
const builtInNodeModules = {
assert,
async_hooks,
buffer,
Expand Down Expand Up @@ -72,6 +83,17 @@ const buildInNodeModules = {
vm,
worker_threads,
zlib,
ethers,
abi,
adapter,
admin,
node,
ois,
operation,
protocol,
utilities,
validator,
axios,
};

/**
Expand All @@ -80,7 +102,7 @@ const buildInNodeModules = {
export const unsafeEvaluate = (input: any, code: string, timeout: number) => {
const vmContext = {
input,
...buildInNodeModules,
...builtInNodeModules,
deferredOutput: undefined,
};

Expand Down Expand Up @@ -110,7 +132,7 @@ export const unsafeEvaluateAsync = async (input: any, code: string, timeout: num
input,
resolve,
reject,
...buildInNodeModules,
...builtInNodeModules,
};

vm.runInNewContext(code, vmContext, { displayErrors: true, timeout });
Expand Down
16 changes: 9 additions & 7 deletions packages/airnode-utilities/src/caching/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs, { Dirent } from 'fs';
import fs, { Dirent, PathLike, Stats } from 'fs';
import { caching, CACHE_BASE_PATH } from './index';

describe('caching utils', () => {
Expand Down Expand Up @@ -39,9 +39,10 @@ describe('caching utils', () => {
readdirSyncSpy.mockReturnValueOnce(files as unknown as Dirent[]);

const statSyncSpy = jest.spyOn(fs, 'statSync');
// @ts-ignore
statSyncSpy.mockImplementation((file: string) =>
filesStatData.find((statData) => file.indexOf(statData.file) > -1)

statSyncSpy.mockImplementation(
(file: PathLike) =>
filesStatData.find((statData) => file.toString().indexOf(statData.file) > -1)! as unknown as Stats
);

const rmSyncSpy = jest.spyOn(fs, 'rmSync');
Expand All @@ -62,9 +63,10 @@ describe('caching utils', () => {
readdirSyncSpy.mockReturnValueOnce(files as unknown as Dirent[]);

const statSyncSpy = jest.spyOn(fs, 'statSync');
// @ts-ignore
statSyncSpy.mockImplementation((file: string) =>
filesStatData.find((statData) => file.indexOf(statData.file) > -1)

statSyncSpy.mockImplementation(
(file: PathLike) =>
filesStatData.find((statData) => file.toString().indexOf(statData.file) > -1)! as unknown as Stats
);

const rmSyncSpy = jest.spyOn(fs, 'rmSync');
Expand Down
19 changes: 18 additions & 1 deletion packages/airnode-utilities/src/caching/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@ import { join } from 'path';
import { logger } from '../logging';

export const CACHE_BASE_PATH = `/tmp/airnode-cache`;

/**
* Lambda, when last tested circa November 2021, persists /tmp contents for 2.5 hours.
*/
export const CACHE_MAX_FILESYSTEM_AGE_MINUTES = 60;
export const CACHE_MAX_FILES = 20_000; // (500 (cache size) * 1024 (KB) ) / 4 (KB, fs "cluster size" = 130k

/**
* This is the maximum number of files allowed in the cache.
* Lambda's /tmp mountpoint can handle approximately 130k files of minimum size (4KB).
* 4 KB is a common sector size for modern filesystems and is used on Lambda's /tmp mountpoint.
*
* 512 MB (size of Lambda /tmp) * 1024 / 4 KB (minimum size) = ~130k files
*
* Some URLs detailing the 4 KB minimum file size:
* ext4: https://askubuntu.com/questions/186813/why-does-every-directory-have-a-size-4096-bytes-4-k
* tmpfs: https://www.linuxquestions.org/questions/linux-server-73/tmpfs-block-size-4175501796/
* ntfs: https://support.microsoft.com/en-us/topic/default-cluster-size-for-ntfs-fat-and-exfat-9772e6f1-e31a-00d7-e18f-73169155af95#:~:text=By%20default%2C%20the%20maximum%20cluster,have%20a%20larger%20cluster%20size.
*/
export const CACHE_MAX_FILES = 20_000;

export const isJest = () => process.env.JEST_WORKER_ID !== undefined;

Expand Down

0 comments on commit 0988c33

Please sign in to comment.