Skip to content

Commit 98aaa6a

Browse files
worker: populate BroadcastChannel MessageEvent source
Populate the MessageEvent source property with the sender's worker thread ID for BroadcastChannel messages originating from worker threads. Fixes: #59053 Signed-off-by: Sudhansu Bandha <bandhasudhansu@gmail.com>
1 parent c5635b8 commit 98aaa6a

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

lib/internal/worker/io.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const {
4848
} = internalBinding('messaging');
4949
const {
5050
getEnvMessagePort,
51+
threadId,
5152
} = internalBinding('worker');
5253

5354
const { Readable, Writable } = require('stream');
@@ -346,7 +347,7 @@ function receiveMessageOnPort(port) {
346347
}
347348

348349
function onMessageEvent(type, data) {
349-
this.dispatchEvent(lazyMessageEvent(type, { data }));
350+
this.dispatchEvent(lazyMessageEvent(type, { data: data.value, source: data.source }));
350351
}
351352

352353
function isBroadcastChannel(value) {
@@ -419,13 +420,17 @@ class BroadcastChannel extends EventTarget {
419420
* @returns {void}
420421
*/
421422
postMessage(message) {
423+
const broadcastMessage = {
424+
source: threadId,
425+
value: message,
426+
};
422427
if (!isBroadcastChannel(this))
423428
throw new ERR_INVALID_THIS('BroadcastChannel');
424429
if (arguments.length === 0)
425430
throw new ERR_MISSING_ARGS('message');
426431
if (this[kHandle] === undefined)
427432
throw new DOMException('BroadcastChannel is closed.', 'InvalidStateError');
428-
if (this[kHandle].postMessage(message) === undefined)
433+
if (this[kHandle].postMessage(broadcastMessage) === undefined)
429434
throw new DOMException('Message could not be posted.');
430435
}
431436

test/parallel/test-worker-broadcastchannel.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ assert.throws(() => new BroadcastChannel(), {
147147
const bc1 = new BroadcastChannel('channel4');
148148
const bc2 = new BroadcastChannel('channel4');
149149
bc1.postMessage('some data');
150-
assert.strictEqual(receiveMessageOnPort(bc2).message, 'some data');
150+
assert.strictEqual(receiveMessageOnPort(bc2).message.value, 'some data');
151151
assert.strictEqual(receiveMessageOnPort(bc2), undefined);
152152
bc1.close();
153153
bc2.close();
@@ -183,3 +183,32 @@ assert.throws(() => new BroadcastChannel(), {
183183
"BroadcastChannel { name: 'channel5', active: false }"
184184
);
185185
}
186+
187+
{
188+
const bc = new BroadcastChannel('worker-source');
189+
190+
new Worker(`
191+
const assert = require('assert');
192+
const { BroadcastChannel, threadId } = require('worker_threads');
193+
194+
const bc = new BroadcastChannel('worker-source');
195+
196+
bc.onmessage = (evt) => {
197+
assert.strictEqual(evt.data, 'from-main');
198+
assert.strictEqual(evt.source, 0);
199+
200+
bc.close();
201+
};
202+
203+
bc.postMessage('from-worker');
204+
`, { eval: true });
205+
206+
bc.onmessage = common.mustCall((evt) => {
207+
assert.strictEqual(evt.data, 'from-worker');
208+
209+
assert.ok(evt.source > 0);
210+
211+
bc.postMessage('from-main');
212+
bc.close();
213+
});
214+
}

0 commit comments

Comments
 (0)