Skip to content

Commit b8eec15

Browse files
committed
server: revert and use a global shim
1 parent ddb93b2 commit b8eec15

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

server/src/rpc-peer-eval.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ export interface CompileFunctionOptions {
44
filename?: string;
55
}
66

7-
function compileFunction(code: string, params?: ReadonlyArray<string>, options?: CompileFunctionOptions): any {
8-
params = params || [];
9-
if (options?.filename)
10-
code = `${code}\n//# sourceURL=${options.filename}\n`;
11-
const f = `(function(${params.join(',')}) {;${code}\n;})`;
12-
return eval(f);
7+
function compileFunction(): any {
8+
// this is a hacky way of preventing the closure from capturing the code variable which may be a large blob.
9+
try {
10+
// "new Function" can't be used because it injects newlines per parameter.
11+
// this causes source mapping to get misaligned.
12+
return eval((globalThis as any).compileFunctionShim);
13+
}
14+
finally {
15+
delete (globalThis as any).compileFunctionShim;
16+
}
1317
}
1418

1519
export function evalLocal<T>(peer: RpcPeer, script: string, filename?: string, coercedParams?: { [name: string]: any }): T {
1620
const params = Object.assign({}, peer.params, coercedParams);
17-
const f = compileFunction(script, Object.keys(params), {
18-
filename,
19-
});
21+
let code = script;
22+
if (filename)
23+
code = `${code}\n//# sourceURL=${filename}\n`;
24+
(globalThis as any).compileFunctionShim = `(function(${Object.keys(params).join(',')}) {;${code}\n;})`;
25+
const f = compileFunction();
2026
const value = f(...Object.values(params));
2127
return value;
2228
}

0 commit comments

Comments
 (0)