From 37d2943e421a01db3e7e95ab5824fbfbb771cc5c Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Sun, 22 May 2022 11:03:24 -0700 Subject: [PATCH] server: fix threading lib --- server/src/threading.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/server/src/threading.ts b/server/src/threading.ts index 9e9c0ffd8..810dc9494 100644 --- a/server/src/threading.ts +++ b/server/src/threading.ts @@ -4,12 +4,20 @@ import v8 from 'v8'; export async function newThread(thread: () => Promise): Promise; export async function newThread(params: V, thread: (params: V) => Promise): Promise; +export async function newThread(modules: M, params: V, thread: (params: M & V) => Promise): Promise; export async function newThread(...args: any[]): Promise { - let thread: () => Promise = args[1]; let params: { [key: string]: any } = {}; - if (thread) { + let modules: { [key: string]: any } = {}; + let thread: () => Promise; + if (args[2]) { + modules = args[0] + params = args[1]; + thread = args[2]; + } + else if (args[1]) { params = args[0]; + thread = args[1]; } else { thread = args[0]; @@ -31,13 +39,17 @@ export async function newThread(...args: any[]): Promise { reject?.(e); } }); + mainPeer.transportSafeArgumentTypes.add(Buffer.name); worker_threads.parentPort.on('message', (message: any) => mainPeer.handleMessage(v8.deserialize(message))); - mainPeer.params.eval = async (script: string, paramNames: string[], ...paramValues: any[]) => { + mainPeer.params.eval = async (script: string, moduleNames: string[], paramNames: string[], ...paramValues: any[]) => { const f = vm.compileFunction(`return (${script})`, paramNames, { filename: 'script.js', }); const params: any = {}; + for (const module of moduleNames) { + params[module] = global.require(module); + } for (let i = 0; i < paramNames.length; i++) { params[paramNames[i]] = paramValues[i]; } @@ -66,13 +78,15 @@ export async function newThread(...args: any[]): Promise { reject?.(e); } }); + threadPeer.transportSafeArgumentTypes.add(Buffer.name); worker.on('message', (message: any) => threadPeer.handleMessage(v8.deserialize(message))); const e = await threadPeer.getParam('eval'); + const moduleNames = Object.keys(modules); const paramNames = Object.keys(params); const paramValues = Object.values(params); try { - return await e(thread.toString(), paramNames, ...paramValues); + return await e(thread.toString(), moduleNames, paramNames, ...paramValues); } finally { worker.terminate();