From ce4d3adf5029278090bdfca8e2b8a2fc384b9cc6 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 21 Dec 2021 12:26:14 -0800 Subject: [PATCH] worker: expose BroadcastChannel as a global Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/41271 Reviewed-By: Antoine du Hamel Reviewed-By: Matteo Collina --- .eslintrc.js | 1 + doc/api/globals.md | 8 ++++++++ doc/api/worker_threads.md | 2 +- lib/.eslintrc.yaml | 2 ++ lib/internal/bootstrap/node.js | 2 ++ lib/internal/worker/io.js | 2 +- 6 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 890c1663025cf8..9ab6638b10e4af 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -368,6 +368,7 @@ module.exports = { Event: 'readable', EventTarget: 'readable', MessageChannel: 'readable', + BroadcastChannel: 'readable', MessageEvent: 'readable', MessagePort: 'readable', TextEncoder: 'readable', diff --git a/doc/api/globals.md b/doc/api/globals.md index f3a1d46d251300..787841f0c9d3c6 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -239,6 +239,14 @@ added: v16.0.0 Global alias for [`buffer.atob()`][]. +## `BroadcastChannel` + + + +See {BroadcastChannel}. + ## `btoa(data)` diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index bf8b55a34435b4..c5e1058a3068e1 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -51,6 +51,8 @@ rules: # disabled with --without-intl build flag. - name: Intl message: "Use `const { Intl } = globalThis;` instead of the global." + - name: BroadcastChannel + message: "Use `const { BroadcastChannel } = require('internal/worker/io');` instead of the global." - name: MessageChannel message: "Use `const { MessageChannel } = require('internal/worker/io');` instead of the global." - name: MessageEvent diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index b621a17c0d9e3c..331006fee03bad 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -251,10 +251,12 @@ if (!config.noBrowserGlobals) { MessageChannel, MessagePort, MessageEvent, + BroadcastChannel, } = require('internal/worker/io'); exposeInterface(globalThis, 'MessageChannel', MessageChannel); exposeInterface(globalThis, 'MessagePort', MessagePort); exposeInterface(globalThis, 'MessageEvent', MessageEvent); + exposeInterface(globalThis, 'BroadcastChannel', BroadcastChannel); // https://html.spec.whatwg.org/multipage/webappapis.html#windoworworkerglobalscope const timers = require('timers'); diff --git a/lib/internal/worker/io.js b/lib/internal/worker/io.js index 0379e5f26c8b54..0f9f2d777a695c 100644 --- a/lib/internal/worker/io.js +++ b/lib/internal/worker/io.js @@ -456,7 +456,7 @@ class BroadcastChannel extends EventTarget { /** * - * @param {*} message + * @param {any} message * @returns {void} */ postMessage(message) {