From 5b43aad0d91a22518fda18136cead7f4ebd2d829 Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Tue, 25 Aug 2026 13:10:33 +0500 Subject: [PATCH 1/2] worker: add Symbol.toStringTag to MessageChannel and MessagePort Signed-off-by: Lazizbek Ergashev --- lib/internal/worker/io.js | 13 +++++++++++++ .../test-eventtarget-memoryleakwarning.js | 4 ++-- test/parallel/test-messagechannel-string-tag.js | 17 +++++++++++++++++ ...ker-message-port-inspect-during-init-hook.js | 2 +- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-messagechannel-string-tag.js diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 011c00b0149c..f9dcd70da9aa 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -15,6 +15,7 @@ const { ReflectApply, Symbol, SymbolFor, + SymbolToStringTag, } = primordials; const { @@ -208,6 +209,18 @@ 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. diff --git a/test/parallel/test-eventtarget-memoryleakwarning.js b/test/parallel/test-eventtarget-memoryleakwarning.js index 38fc5efc9bd2..c8c1faa2175b 100644 --- a/test/parallel/test-eventtarget-memoryleakwarning.js +++ b/test/parallel/test-eventtarget-memoryleakwarning.js @@ -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'], diff --git a/test/parallel/test-messagechannel-string-tag.js b/test/parallel/test-messagechannel-string-tag.js new file mode 100644 index 000000000000..d824f3e4ffc2 --- /dev/null +++ b/test/parallel/test-messagechannel-string-tag.js @@ -0,0 +1,17 @@ +'use strict'; + +require('../common'); + +const assert = require('assert'); + +const classesToBeTested = [ MessageChannel, MessagePort ]; + +classesToBeTested.forEach((cls) => { + 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]'); diff --git a/test/parallel/test-worker-message-port-inspect-during-init-hook.js b/test/parallel/test-worker-message-port-inspect-during-init-hook.js index 8f9678de1e97..d5784dceb71d 100644 --- a/test/parallel/test-worker-message-port-inspect-during-init-hook.js +++ b/test/parallel/test-worker-message-port-inspect-during-init-hook.js @@ -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(); From 070a5ecce3d715898d827c2a5571964111381afb Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Tue, 25 Aug 2026 15:02:46 +0500 Subject: [PATCH 2/2] worker: add Symbol.toStringTag to BroadcastChannel Signed-off-by: Lazizbek Ergashev --- lib/internal/worker/io.js | 11 +++++++++++ ...nel-string-tag.js => test-messaging-string-tag.js} | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) rename test/parallel/{test-messagechannel-string-tag.js => test-messaging-string-tag.js} (69%) diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index f9dcd70da9aa..cdca2d848848 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -211,12 +211,16 @@ ObjectDefineProperty(MessagePort.prototype, inspect.custom, { ObjectDefineProperty(MessagePort.prototype, SymbolToStringTag, { __proto__: null, + writable: false, + enumerable: false, configurable: true, value: 'MessagePort', }); ObjectDefineProperty(MessageChannel.prototype, SymbolToStringTag, { __proto__: null, + writable: false, + enumerable: false, configurable: true, value: 'MessageChannel', }); @@ -474,6 +478,13 @@ class BroadcastChannel extends EventTarget { } ObjectDefineProperties(BroadcastChannel.prototype, { + [SymbolToStringTag]: { + __proto__: null, + writable: false, + enumerable: false, + configurable: true, + value: 'BroadcastChannel', + }, name: kEnumerableProperty, close: kEnumerableProperty, postMessage: kEnumerableProperty, diff --git a/test/parallel/test-messagechannel-string-tag.js b/test/parallel/test-messaging-string-tag.js similarity index 69% rename from test/parallel/test-messagechannel-string-tag.js rename to test/parallel/test-messaging-string-tag.js index d824f3e4ffc2..77f9484ef55b 100644 --- a/test/parallel/test-messagechannel-string-tag.js +++ b/test/parallel/test-messaging-string-tag.js @@ -4,7 +4,7 @@ require('../common'); const assert = require('assert'); -const classesToBeTested = [ MessageChannel, MessagePort ]; +const classesToBeTested = [ MessageChannel, MessagePort, BroadcastChannel ]; classesToBeTested.forEach((cls) => { assert.strictEqual(cls.prototype[Symbol.toStringTag], cls.name); @@ -15,3 +15,7 @@ classesToBeTested.forEach((cls) => { const channel = new MessageChannel(); assert.strictEqual(Object.prototype.toString.call(channel), '[object MessageChannel]'); assert.strictEqual(Object.prototype.toString.call(channel.port1), '[object MessagePort]'); + +const broadcastChannel = new BroadcastChannel('string-tag'); +assert.strictEqual(Object.prototype.toString.call(broadcastChannel), '[object BroadcastChannel]'); +broadcastChannel.close();