From 86bc7a43810846fc66bf06b7577490f01ead1918 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Wed, 3 Apr 2024 16:42:16 +0530 Subject: fix(ext/node): patch MessagePort if provided as workerData (#23198) MessagePort if directly assigned to workerData property instead of embedding it in an object then it is not patched to a NodeMessagePort. This commit fixes the bug. --- ext/node/polyfills/worker_threads.ts | 37 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'ext') diff --git a/ext/node/polyfills/worker_threads.ts b/ext/node/polyfills/worker_threads.ts index 49562d892..c34f1fe23 100644 --- a/ext/node/polyfills/worker_threads.ts +++ b/ext/node/polyfills/worker_threads.ts @@ -343,14 +343,7 @@ internals.__initWorkerThreads = ( defaultExport.parentPort = parentPort; defaultExport.threadId = threadId; - for (const obj in workerData as Record) { - if (ObjectPrototypeIsPrototypeOf(MessagePortPrototype, workerData[obj])) { - workerData[obj] = webMessagePortToNodeMessagePort( - workerData[obj] as MessagePort, - ); - break; - } - } + workerData = patchMessagePortIfFound(workerData); parentPort.off = parentPort.removeListener = function ( this: ParentPort, @@ -369,18 +362,7 @@ internals.__initWorkerThreads = ( // deno-lint-ignore no-explicit-any const _listener = (ev: any) => { let message = ev.data; - if (ObjectPrototypeIsPrototypeOf(MessagePortPrototype, message)) { - message = webMessagePortToNodeMessagePort(message); - } else { - for (const obj in message) { - if ( - ObjectPrototypeIsPrototypeOf(MessagePortPrototype, message[obj]) - ) { - message[obj] = webMessagePortToNodeMessagePort(message[obj]); - break; - } - } - } + message = patchMessagePortIfFound(message); return listener(message); }; listeners.set(listener, _listener); @@ -481,6 +463,21 @@ function webMessagePortToNodeMessagePort(port: MessagePort) { return port; } +// deno-lint-ignore no-explicit-any +function patchMessagePortIfFound(data: any) { + if (ObjectPrototypeIsPrototypeOf(MessagePortPrototype, data)) { + data = webMessagePortToNodeMessagePort(data); + } else { + for (const obj in data as Record) { + if (ObjectPrototypeIsPrototypeOf(MessagePortPrototype, data[obj])) { + data[obj] = webMessagePortToNodeMessagePort(data[obj] as MessagePort); + break; + } + } + } + return data; +} + export { BroadcastChannel, MessagePort, -- cgit v1.2.3