Skip to content

Commit

Permalink
fix: add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle committed Jun 3, 2024
1 parent 5be45fd commit d198252
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
34 changes: 34 additions & 0 deletions src/utils/safe-stringify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const replacer = (key: string, value: unknown) => {
if (typeof value === 'object') {
if (key === 'globalThis') {
return '[globalThis]';
}
if (key === 'global') {
return '[global]';
}
if (key === 'GLOBAL') {
return '[GLOBAL]';
}
if (key === 'process') {
return '[process]';
}
if (key === 'win32') {
// path.win32
return '[win32]';
}
if (key === 'posix') {
// path.posix
return '[posix]';
}
}
if (typeof value === 'function') {
return '[Function]';
}
if (typeof value === 'bigint') {
return '[bigint]';
}
return value;
};
export function safeStringify(obj: unknown) {
return JSON.stringify(obj, replacer);
}
27 changes: 18 additions & 9 deletions src/utils/static-eval.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import { safeStringify } from './safe-stringify';
import { EvaluatedValue, StaticValue, ConditionalValue, Node } from './types';
import { URL } from 'url';
type Walk = (node: Node) => EvaluatedValue;
type Walk = (node: Node) => Promise<EvaluatedValue>;
type State = { computeBranches: boolean; vars: Record<string, any> };

const walkCache = new Map<string, EvaluatedValue>();

export async function evaluate(
ast: Node,
vars = {},
computeBranches = true,
ast: Readonly<Node>,
vars: Readonly<Record<string, string>> = {},
computeBranches: Readonly<boolean> = true,
): Promise<EvaluatedValue> {
const state: State = {
computeBranches,
vars,
};
const stateCacheKey = safeStringify(state);
return walk(ast);

// walk returns:
// 1. Single known value: { value: value }
// 2. Conditional value: { test, then, else }
// 3. Unknown value: undefined
function walk(node: Node) {
async function walk(node: Node) {
const walkCacheKey = stateCacheKey + safeStringify(node);
let result = walkCache.get(walkCacheKey);
if (result) {
return result;
}
const visitor = visitors[node.type];
if (visitor) {
return visitor.call(state, node, walk);
result = await visitor.call(state, node, walk);
}
return undefined;
walkCache.set(walkCacheKey, result);
return result;
}
}

Expand Down Expand Up @@ -359,7 +368,7 @@ const visitors: Record<
if (typeof obj.value === 'string' && node.property.name === 'concat') {
return {
value: {
[FUNCTION]: (...args: string[]) => obj.value.concat(args),
[FUNCTION]: (...args: string[]) => obj.value.concat(...args),
},
};
}
Expand Down
2 changes: 2 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const stat = gracefulFS.promises.stat;
const readlink = gracefulFS.promises.readlink;
const readFile = gracefulFS.promises.readFile;

jest.setTimeout(5_000);

global._unit = true;

const nodeGypTests = [
Expand Down

0 comments on commit d198252

Please sign in to comment.