diff --git a/server/src/rpc-peer-eval.ts b/server/src/rpc-peer-eval.ts index 5402edd4b3..213463f565 100644 --- a/server/src/rpc-peer-eval.ts +++ b/server/src/rpc-peer-eval.ts @@ -1,27 +1,22 @@ -import type { CompileFunctionOptions } from 'vm'; -import { RpcPeer } from "./rpc"; +import type { RpcPeer } from "./rpc"; -type CompileFunction = (code: string, params?: ReadonlyArray, options?: CompileFunctionOptions) => Function; +export interface CompileFunctionOptions { + filename?: string; +} function compileFunction(code: string, params?: ReadonlyArray, options?: CompileFunctionOptions): any { params = params || []; - const f = `(function(${params.join(',')}) {;${code};})`; + if (options?.filename) + code = `${code}\n//# sourceURL=${options.filename}\n`; + const f = `(function(${params.join(',')}) {;${code}\n;})`; return eval(f); } export function evalLocal(peer: RpcPeer, script: string, filename?: string, coercedParams?: { [name: string]: any }): T { const params = Object.assign({}, peer.params, coercedParams); - let compile: CompileFunction; - try { - // prevent bundlers from trying to include non-existent vm module. - compile = module[`require`]('vm').compileFunction; - } - catch (e) { - compile = compileFunction; - } - const f = compile(script, Object.keys(params), { + const f = compileFunction(script, Object.keys(params), { filename, }); const value = f(...Object.values(params)); return value; -} \ No newline at end of file +}