diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index f113bb2d967805..c237b9638fc877 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -179,9 +179,13 @@ if (isMainThread) { ## `worker.receiveMessageOnPort(port)` -* `port` {MessagePort} +* `port` {MessagePort|BroadcastChannel} * Returns: {Object|undefined} diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index aae2dc8a39e585..c08e4b677b5dca 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -333,7 +333,7 @@ function createWorkerStdio() { } function receiveMessageOnPort(port) { - const message = receiveMessageOnPort_(port); + const message = receiveMessageOnPort_(port[kHandle] ?? port); if (message === noMessageSymbol) return undefined; return { message }; } diff --git a/test/parallel/test-worker-broadcastchannel.js b/test/parallel/test-worker-broadcastchannel.js index b26fbc3769d3f3..4212e5bf2f7afb 100644 --- a/test/parallel/test-worker-broadcastchannel.js +++ b/test/parallel/test-worker-broadcastchannel.js @@ -4,6 +4,7 @@ const common = require('../common'); const { BroadcastChannel, Worker, + receiveMessageOnPort } = require('worker_threads'); const assert = require('assert'); @@ -140,3 +141,13 @@ assert.throws(() => new BroadcastChannel(), { message: /BroadcastChannel is closed/ }); } + +{ + const bc1 = new BroadcastChannel('channel4'); + const bc2 = new BroadcastChannel('channel4'); + bc1.postMessage('some data'); + assert.strictEqual(receiveMessageOnPort(bc2).message, 'some data'); + assert.strictEqual(receiveMessageOnPort(bc2), undefined); + bc1.close(); + bc2.close(); +}