From 149675cfb399c7e9aaa44217ed985bf02b59b865 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sun, 4 Aug 2024 00:24:57 -0700 Subject: [PATCH] server: fix closure capture --- server/src/rpc-peer-eval.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/server/src/rpc-peer-eval.ts b/server/src/rpc-peer-eval.ts index 4c74e6504..c49851981 100644 --- a/server/src/rpc-peer-eval.ts +++ b/server/src/rpc-peer-eval.ts @@ -7,9 +7,12 @@ export interface CompileFunctionOptions { function compileFunction(): any { // this is a hacky way of preventing the closure from capturing the code variable which may be a large blob. try { - // "new Function" can't be used because it injects newlines per parameter. - // this causes source mapping to get misaligned. - return eval((globalThis as any).compileFunctionShim); + // "new Function" can't be used directly because it injects newlines per parameter, + // which causes source mapping to get misaligned. + // However, using eval inside a function works, because there are no parameters, + // and the "new Function" addresses the closure capture issue. + const f = new Function('return eval(globalThis.compileFunctionShim)'); + return f(); } finally { delete (globalThis as any).compileFunctionShim;