@@ -1310,6 +1310,105 @@ changes:
13101310A browser-compatible implementation of {WebSocket}. Disable this API
13111311with the [ ` --no-experimental-websocket ` ] [ ] CLI flag.
13121312
1313+ ## Class: ` Worker `
1314+
1315+ <!-- YAML
1316+ added: REPLACEME
1317+ -->
1318+
1319+ > Stability: 1 - Experimental. Enable this API with the
1320+ > [ ` --experimental-web-worker ` ] [ ] CLI flag.
1321+
1322+ A mostly browser-compatible implementation of Web Workers of the [ HTML Standard] [ ] ,
1323+ implemented on top of [ ` node:worker_threads ` ] [ ] . Threads created with it
1324+ are given the {DedicatedWorkerGlobalScope} API (` self ` ,
1325+ ` name ` , ` location ` , ` navigator ` , ` postMessage() ` , ` close() ` , and
1326+ ` importScripts() ` ), in addition to the usual Node.js globals, such as ` process ` .
1327+
1328+ ``` js
1329+ // worker.js
1330+ addEventListener (' message' , (event ) => {
1331+ postMessage (` ${ event .data } from ${ name} !` );
1332+ });
1333+ ```
1334+
1335+ ``` js
1336+ // main.js
1337+ const worker = new Worker (' ./worker.js' , { name: ' greeter' });
1338+
1339+ worker .addEventListener (' message' , (event ) => {
1340+ console .log (event .data ); // Prints: Hello from greeter!
1341+ worker .terminate ();
1342+ });
1343+
1344+ worker .postMessage (' Hello' );
1345+ ```
1346+
1347+ Because their lifetime and sharing model depend on origins and
1348+ browsing contexts, Node.js does not currently implement ` SharedWorker ` .
1349+
1350+ ### Loading worker scripts
1351+
1352+ Worker scripts are read synchronously from the local file system or from
1353+ memory rather than fetched over the network, which changes which URLs are
1354+ accepted and how failures are reported:
1355+
1356+ * ` new Worker() ` and ` importScripts() ` accept only ` file: ` , ` data: ` , and
1357+ ` blob: ` URLs. Any other scheme makes ` new Worker() ` throw a
1358+ ` NotSupportedError ` and ` importScripts() ` throw a ` NetworkError ` .
1359+ * A script that cannot be read makes ` importScripts() ` throw a ` NetworkError ` ;
1360+ for ` new Worker() ` it fires an ` error ` event at the ` Worker ` object.
1361+ * Redirects, the ` nosniff ` check, and HTTP MIME type validation do not apply.
1362+ MIME types are validated only for ` data: ` and ` blob: ` URLs. The
1363+ ` credentials ` option is validated for API compatibility but has no effect,
1364+ since no network request is made.
1365+ * On the main thread, relative script URLs are resolved against the current
1366+ working directory, because there is no document base URL. Within a worker
1367+ they are resolved against the worker's own URL (as is done in the spec).
1368+ * For ` blob: ` URLs, the script must be held in memory, so blobs backed by a file,
1369+ such as those returned by [ ` fs.openAsBlob() ` ] [ ] , cannot be used.
1370+
1371+ ### Differences from the HTML Standard
1372+
1373+ Besides script loading, mentioned above:
1374+
1375+ * Node.js has no origin model, so same-origin and cross-origin distinctions do
1376+ not exist and ` location.origin ` is ` 'null' ` for every supported scheme.
1377+ * ` close() ` terminates the worker immediately instead of following the
1378+ specification's "closing flag" algorithm, so code remaining in the current
1379+ task after ` close() ` is not executed.
1380+ * The worker global is the normal Node.js global object with
1381+ ` DedicatedWorkerGlobalScope ` inserted into its prototype chain, rather than
1382+ a fresh global created from the interface. Node.js globals such as
1383+ ` process ` , ` Buffer ` , and ` require() ` remain available to worker scripts.
1384+ * ` ErrorEvent ` s dispatched at ` Worker ` instances include ` message ` and
1385+ ` error ` , but ` filename ` , ` lineno ` , and ` colno ` are always ` '' ` , ` 0 ` , and
1386+ ` 0 ` . An uncaught exception terminates the worker thread, and an unhandled
1387+ ` error ` event is not propagated further: it neither reaches the parent's
1388+ global scope nor affects the exit code of the process.
1389+ * The following {WorkerGlobalScope} events are never dispatched, although
1390+ their handler properties exist: ` languagechange ` , ` online ` , and ` offline ` ,
1391+ since these concepts do not exist in Node.js; ` rejectionhandled ` and
1392+ ` unhandledrejection ` , since Node.js exposes the equivalent does not
1393+ implement the ` PromiseRejectionEvent ` interface or the per-rejection
1394+ ` preventDefault() ` behavior required by the HTML Standard.
1395+
1396+ ### Web Workers and ` node:worker_threads `
1397+
1398+ Every Web Worker is backed by a [ ` node:worker_threads ` ] [ ] {Worker}, so the
1399+ two APIs share their threading, structured clone, and transfer semantics.
1400+ Inside a worker, \[ ` worker_threads.parentPort ` ] \[ ] is the port behind
1401+ ` self.postMessage() ` and the worker's ` message ` events, ` isMainThread ` is
1402+ ` false ` , and ` workerData ` is ` undefined ` .
1403+
1404+ As a rule of thumb, use [ ` node:worker_threads ` ] [ ] directly when a program
1405+ needs ` workerData ` , a custom ` env ` or ` execArgv ` , resource limits, stdio
1406+ redirection, the ` 'online' ` and ` 'exit' ` events, or ` worker.threadId ` ;
1407+ ` Worker ` accepts only the ` name ` , ` type ` , and ` credentials ` options and,
1408+ per the specification, its ` terminate() ` returns ` undefined ` , rather than
1409+ a promise. Threads started through [ ` node:worker_threads ` ] [ ] are ordinary
1410+ Node.js threads and do not get the worker global scope APIs.
1411+
13131412## Class: ` WritableStream `
13141413
13151414<!-- YAML
@@ -1355,10 +1454,12 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
13551454[ CommonJS module ] : modules.md
13561455[ CommonJS modules ] : modules.md
13571456[ ECMAScript module ] : esm.md
1457+ [ HTML Standard ] : https://html.spec.whatwg.org/multipage/workers.html
13581458[ Navigator API ] : https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
13591459[ RFC 5646 ] : https://www.rfc-editor.org/rfc/rfc5646.txt
13601460[ Web Crypto API ] : webcrypto.md
13611461[ `--experimental-eventsource` ] : cli.md#--experimental-eventsource
1462+ [ `--experimental-web-worker` ] : cli.md#--experimental-web-worker
13621463[ `--localstorage-file` ] : cli.md#--localstorage-filefile
13631464[ `--no-experimental-global-navigator` ] : cli.md#--no-experimental-global-navigator
13641465[ `--no-experimental-websocket` ] : cli.md#--no-experimental-websocket
@@ -1410,9 +1511,11 @@ A browser-compatible implementation of [`WritableStreamDefaultWriter`][].
14101511[ `console` ] : console.md
14111512[ `exports` ] : modules.md#exports
14121513[ `fetch()` ] : https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch
1514+ [ `fs.openAsBlob()` ] : fs.md#fsopenasblobpath-options
14131515[ `globalThis` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
14141516[ `localStorage` ] : https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
14151517[ `module` ] : modules.md#module
1518+ [ `node:worker_threads` ] : worker_threads.md
14161519[ `perf_hooks.performance` ] : perf_hooks.md#perf_hooksperformance
14171520[ `process.nextTick()` ] : process.md#processnexttickcallback-args
14181521[ `process` object ] : process.md#process
0 commit comments