From ff1403c0a1e9a97e55db19528e1ebd3ebea7c00d Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 11 May 2023 12:32:19 +0200 Subject: [PATCH] fix(node): expose channels in worker_threads (#19086) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR ensures that node's `worker_threads` module exports `MessageChannel`, `MessagePort` and the `BroadcastChannel` API. Fixing these won't make `esbuild` work, but brings us one step closer 🎉 Fixes #19028 . --- cli/tests/node_compat/config.jsonc | 2 ++ .../test-worker-threads-broadcast-channel.js | 9 +++++++++ .../test-worker-threads-message-channel.js | 10 ++++++++++ ext/node/polyfills/worker_threads.ts | 17 +++++++---------- 4 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 cli/tests/node_compat/test/parallel/test-worker-threads-broadcast-channel.js create mode 100644 cli/tests/node_compat/test/parallel/test-worker-threads-message-channel.js diff --git a/cli/tests/node_compat/config.jsonc b/cli/tests/node_compat/config.jsonc index 87530d4f5c..8fbc3e921d 100644 --- a/cli/tests/node_compat/config.jsonc +++ b/cli/tests/node_compat/config.jsonc @@ -655,6 +655,8 @@ "test-whatwg-url-override-hostname.js", "test-whatwg-url-properties.js", "test-whatwg-url-toascii.js", + "test-worker-threads-broadcast-channel.js", + "test-worker-threads-message-channel.js", "test-zlib-close-after-error.js", "test-zlib-close-after-write.js", "test-zlib-convenience-methods.js", diff --git a/cli/tests/node_compat/test/parallel/test-worker-threads-broadcast-channel.js b/cli/tests/node_compat/test/parallel/test-worker-threads-broadcast-channel.js new file mode 100644 index 0000000000..a8fd3ff0e8 --- /dev/null +++ b/cli/tests/node_compat/test/parallel/test-worker-threads-broadcast-channel.js @@ -0,0 +1,9 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +"use strict"; + +const assert = require("assert/strict"); +const worker_threads = require("worker_threads"); + +assert.equal(BroadcastChannel, worker_threads.BroadcastChannel); diff --git a/cli/tests/node_compat/test/parallel/test-worker-threads-message-channel.js b/cli/tests/node_compat/test/parallel/test-worker-threads-message-channel.js new file mode 100644 index 0000000000..b831ed3fee --- /dev/null +++ b/cli/tests/node_compat/test/parallel/test-worker-threads-message-channel.js @@ -0,0 +1,10 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +"use strict"; + +const assert = require("assert/strict"); +const worker_threads = require("worker_threads"); + +assert.equal(MessageChannel, worker_threads.MessageChannel); +assert.equal(MessagePort, worker_threads.MessagePort); diff --git a/ext/node/polyfills/worker_threads.ts b/ext/node/polyfills/worker_threads.ts index cc9529fbd7..2c13e4bc8d 100644 --- a/ext/node/polyfills/worker_threads.ts +++ b/ext/node/polyfills/worker_threads.ts @@ -4,6 +4,8 @@ import { resolve, toFileUrl } from "ext:deno_node/path.ts"; import { notImplemented } from "ext:deno_node/_utils.ts"; import { EventEmitter } from "ext:deno_node/events.ts"; +import { BroadcastChannel } from "ext:deno_broadcast_channel/01_broadcast_channel.js"; +import { MessageChannel, MessagePort } from "ext:deno_web/13_message_port.js"; const environmentData = new Map(); let threads = 0; @@ -204,12 +206,6 @@ export function setEnvironmentData(key: unknown, value?: unknown) { } } -// deno-lint-ignore no-explicit-any -const _MessagePort: typeof MessagePort = (globalThis as any).MessagePort; -const _MessageChannel: typeof MessageChannel = - // deno-lint-ignore no-explicit-any - (globalThis as any).MessageChannel; -export const BroadcastChannel = globalThis.BroadcastChannel; export const SHARE_ENV = Symbol.for("nodejs.worker_threads.SHARE_ENV"); export function markAsUntransferable() { notImplemented("markAsUntransferable"); @@ -221,9 +217,10 @@ export function receiveMessageOnPort() { notImplemented("receiveMessageOnPort"); } export { - _MessageChannel as MessageChannel, - _MessagePort as MessagePort, _Worker as Worker, + BroadcastChannel, + MessageChannel, + MessagePort, parentPort, threadId, workerData, @@ -233,8 +230,8 @@ export default { markAsUntransferable, moveMessagePortToContext, receiveMessageOnPort, - MessagePort: _MessagePort, - MessageChannel: _MessageChannel, + MessagePort, + MessageChannel, BroadcastChannel, Worker: _Worker, getEnvironmentData,