Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
ReflectApply,
Symbol,
SymbolFor,
SymbolToStringTag,
} = primordials;

const {
Expand Down Expand Up @@ -208,6 +209,19 @@ ObjectDefineProperty(MessagePort.prototype, inspect.custom, {
},
});

ObjectDefineProperty(MessagePort.prototype, SymbolToStringTag, {
__proto__: null,
configurable: true,
value: 'MessagePort',
});

ObjectDefineProperty(MessageChannel.prototype, SymbolToStringTag, {
__proto__: null,
configurable: true,
value: 'MessageChannel',
});


function setupPortReferencing(port, eventEmitter, eventName) {
// Keep track of whether there are any workerMessage listeners:
// If there are some, ref() the channel so it keeps the event loop alive.
Expand Down Expand Up @@ -464,6 +478,11 @@ ObjectDefineProperties(BroadcastChannel.prototype, {
name: kEnumerableProperty,
close: kEnumerableProperty,
postMessage: kEnumerableProperty,
[SymbolToStringTag]: {
__proto__: null,
configurable: true,
value: 'BroadcastChannel',
},
});

defineEventHandler(BroadcastChannel.prototype, 'message');
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-eventtarget-memoryleakwarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ common.expectWarning({
'EventTarget. MaxListeners is 2. Use events.setMaxListeners() ' +
'to increase limit'],
['Possible EventTarget memory leak detected. 3 foo listeners added to ' +
'[MessagePort [EventTarget]]. ' +
'[MessagePort]. ' +
'MaxListeners is 2. ' +
'Use events.setMaxListeners() to increase ' +
'limit'],
['Possible EventTarget memory leak detected. 3 foo listeners added to ' +
'[MessagePort [EventTarget]]. ' +
'[MessagePort]. ' +
'MaxListeners is 2. ' +
'Use events.setMaxListeners() to increase ' +
'limit'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async_hooks.createHook({
init: common.mustCall((id, type, triggerId, resource) => {
assert.strictEqual(
util.inspect(resource),
'MessagePort [EventTarget] { active: true, refed: false }');
'MessagePort { active: true, refed: false }');
}, 2)
}).enable();

Expand Down
38 changes: 38 additions & 0 deletions test/parallel/test-worker-messaging-string-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

require('../common');
const assert = require('assert');
const { MessageChannel, MessagePort, BroadcastChannel } = require('worker_threads');

const classesToBeTested = [
MessageChannel,
MessagePort,
BroadcastChannel,
];

for (const cls of classesToBeTested) {
assert.strictEqual(cls.prototype[Symbol.toStringTag], cls.name);
assert.deepStrictEqual(
Object.getOwnPropertyDescriptor(cls.prototype, Symbol.toStringTag),
{ configurable: true, enumerable: false, value: cls.name, writable: false }
);
}

const channel = new MessageChannel();
assert.strictEqual(Object.prototype.toString.call(channel), '[object MessageChannel]');
assert.strictEqual(Object.prototype.toString.call(channel.port1), '[object MessagePort]');
assert.strictEqual(Object.prototype.toString.call(channel.port2), '[object MessagePort]');

const broadcast = new BroadcastChannel('test');
assert.strictEqual(Object.prototype.toString.call(broadcast), '[object BroadcastChannel]');
broadcast.close();

// Test globals
assert.strictEqual(globalThis.MessageChannel, MessageChannel);
assert.strictEqual(globalThis.MessagePort, MessagePort);
assert.strictEqual(globalThis.BroadcastChannel, BroadcastChannel);
assert.strictEqual(Object.prototype.toString.call(new globalThis.MessageChannel()), '[object MessageChannel]');
assert.strictEqual(Object.prototype.toString.call(new globalThis.MessageChannel().port1), '[object MessagePort]');
const globalBroadcast = new globalThis.BroadcastChannel('test-global');
assert.strictEqual(Object.prototype.toString.call(globalBroadcast), '[object BroadcastChannel]');
globalBroadcast.close();