summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/worker_threads.ts
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2024-04-03 16:42:16 +0530
committerGitHub <noreply@github.com>2024-04-03 11:12:16 +0000
commit86bc7a43810846fc66bf06b7577490f01ead1918 (patch)
treeab0061bb1c35010363f36ebe86ea97e55f7c507b /ext/node/polyfills/worker_threads.ts
parent92a8ada7194ac013f8c34824f435f09b1f52ca5b (diff)
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.
Diffstat (limited to 'ext/node/polyfills/worker_threads.ts')
-rw-r--r--ext/node/polyfills/worker_threads.ts37
1 files changed, 17 insertions, 20 deletions
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<string, unknown>) {
- 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<string, unknown>) {
+ if (ObjectPrototypeIsPrototypeOf(MessagePortPrototype, data[obj])) {
+ data[obj] = webMessagePortToNodeMessagePort(data[obj] as MessagePort);
+ break;
+ }
+ }
+ }
+ return data;
+}
+
export {
BroadcastChannel,
MessagePort,