Skip to content

Commit 28dc74e

Browse files
committed
worker: define Symbol.toStringTag on messaging prototypes
Ensure compliance with the HTML and WebIDL specifications by defining Symbol.toStringTag on MessageChannel.prototype, MessagePort.prototype, and BroadcastChannel.prototype. Fixes: #65527 Signed-off-by: Santusht kotai <115890693+santusht06@users.noreply.github.com>
1 parent 984e46f commit 28dc74e

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

lib/internal/worker/io.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
ReflectApply,
1616
Symbol,
1717
SymbolFor,
18+
SymbolToStringTag,
1819
} = primordials;
1920

2021
const {
@@ -208,6 +209,19 @@ ObjectDefineProperty(MessagePort.prototype, inspect.custom, {
208209
},
209210
});
210211

212+
ObjectDefineProperty(MessagePort.prototype, SymbolToStringTag, {
213+
__proto__: null,
214+
configurable: true,
215+
value: 'MessagePort',
216+
});
217+
218+
ObjectDefineProperty(MessageChannel.prototype, SymbolToStringTag, {
219+
__proto__: null,
220+
configurable: true,
221+
value: 'MessageChannel',
222+
});
223+
224+
211225
function setupPortReferencing(port, eventEmitter, eventName) {
212226
// Keep track of whether there are any workerMessage listeners:
213227
// If there are some, ref() the channel so it keeps the event loop alive.
@@ -464,6 +478,11 @@ ObjectDefineProperties(BroadcastChannel.prototype, {
464478
name: kEnumerableProperty,
465479
close: kEnumerableProperty,
466480
postMessage: kEnumerableProperty,
481+
[SymbolToStringTag]: {
482+
__proto__: null,
483+
configurable: true,
484+
value: 'BroadcastChannel',
485+
},
467486
});
468487

469488
defineEventHandler(BroadcastChannel.prototype, 'message');

test/parallel/test-worker-message-port-inspect-during-init-hook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async_hooks.createHook({
1212
init: common.mustCall((id, type, triggerId, resource) => {
1313
assert.strictEqual(
1414
util.inspect(resource),
15-
'MessagePort [EventTarget] { active: true, refed: false }');
15+
'MessagePort { active: true, refed: false }');
1616
}, 2)
1717
}).enable();
1818

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const { MessageChannel, MessagePort, BroadcastChannel } = require('worker_threads');
6+
7+
const classesToBeTested = [
8+
MessageChannel,
9+
MessagePort,
10+
BroadcastChannel,
11+
];
12+
13+
for (const cls of classesToBeTested) {
14+
assert.strictEqual(cls.prototype[Symbol.toStringTag], cls.name);
15+
assert.deepStrictEqual(
16+
Object.getOwnPropertyDescriptor(cls.prototype, Symbol.toStringTag),
17+
{ configurable: true, enumerable: false, value: cls.name, writable: false }
18+
);
19+
}
20+
21+
const channel = new MessageChannel();
22+
assert.strictEqual(Object.prototype.toString.call(channel), '[object MessageChannel]');
23+
assert.strictEqual(Object.prototype.toString.call(channel.port1), '[object MessagePort]');
24+
assert.strictEqual(Object.prototype.toString.call(channel.port2), '[object MessagePort]');
25+
26+
const broadcast = new BroadcastChannel('test');
27+
assert.strictEqual(Object.prototype.toString.call(broadcast), '[object BroadcastChannel]');
28+
broadcast.close();
29+
30+
// Test globals
31+
assert.strictEqual(globalThis.MessageChannel, MessageChannel);
32+
assert.strictEqual(globalThis.MessagePort, MessagePort);
33+
assert.strictEqual(globalThis.BroadcastChannel, BroadcastChannel);
34+
assert.strictEqual(Object.prototype.toString.call(new globalThis.MessageChannel()), '[object MessageChannel]');
35+
assert.strictEqual(Object.prototype.toString.call(new globalThis.MessageChannel().port1), '[object MessagePort]');
36+
const globalBroadcast = new globalThis.BroadcastChannel('test-global');
37+
assert.strictEqual(Object.prototype.toString.call(globalBroadcast), '[object BroadcastChannel]');
38+
globalBroadcast.close();

0 commit comments

Comments
 (0)