mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 08:33:43 -05:00
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.
This commit is contained in:
parent
92a8ada719
commit
86bc7a4381
3 changed files with 19 additions and 22 deletions
|
@ -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,
|
||||
|
|
|
@ -9,7 +9,7 @@ const deferred = createDeferred();
|
|||
const worker = new workerThreads.Worker(
|
||||
import.meta.resolve("./node_worker_message_port_1.cjs"),
|
||||
{
|
||||
workerData: { workerPort },
|
||||
workerData: workerPort,
|
||||
transferList: [workerPort],
|
||||
},
|
||||
);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const { parentPort, workerData } = require("worker_threads");
|
||||
|
||||
parentPort.on("message", (msg) => {
|
||||
const workerPort = workerData.workerPort;
|
||||
const workerPort = workerData;
|
||||
parentPort.postMessage("Hello from worker on parentPort!");
|
||||
workerPort.postMessage("Hello from worker on workerPort!");
|
||||
workerPort.on("close", () => console.log("worker port closed"));
|
||||
|
|
Loading…
Reference in a new issue