11cb0ef41Sopenharmony_ci# Worker threads 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v10.5.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci> Stability: 2 - Stable 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- source_link=lib/worker_threads.js --> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciThe `node:worker_threads` module enables the use of threads that execute 101cb0ef41Sopenharmony_ciJavaScript in parallel. To access it: 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci```js 131cb0ef41Sopenharmony_ciconst worker = require('node:worker_threads'); 141cb0ef41Sopenharmony_ci``` 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciWorkers (threads) are useful for performing CPU-intensive JavaScript operations. 171cb0ef41Sopenharmony_ciThey do not help much with I/O-intensive work. The Node.js built-in 181cb0ef41Sopenharmony_ciasynchronous I/O operations are more efficient than Workers can be. 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciUnlike `child_process` or `cluster`, `worker_threads` can share memory. They do 211cb0ef41Sopenharmony_ciso by transferring `ArrayBuffer` instances or sharing `SharedArrayBuffer` 221cb0ef41Sopenharmony_ciinstances. 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci```js 251cb0ef41Sopenharmony_ciconst { 261cb0ef41Sopenharmony_ci Worker, isMainThread, parentPort, workerData, 271cb0ef41Sopenharmony_ci} = require('node:worker_threads'); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciif (isMainThread) { 301cb0ef41Sopenharmony_ci module.exports = function parseJSAsync(script) { 311cb0ef41Sopenharmony_ci return new Promise((resolve, reject) => { 321cb0ef41Sopenharmony_ci const worker = new Worker(__filename, { 331cb0ef41Sopenharmony_ci workerData: script, 341cb0ef41Sopenharmony_ci }); 351cb0ef41Sopenharmony_ci worker.on('message', resolve); 361cb0ef41Sopenharmony_ci worker.on('error', reject); 371cb0ef41Sopenharmony_ci worker.on('exit', (code) => { 381cb0ef41Sopenharmony_ci if (code !== 0) 391cb0ef41Sopenharmony_ci reject(new Error(`Worker stopped with exit code ${code}`)); 401cb0ef41Sopenharmony_ci }); 411cb0ef41Sopenharmony_ci }); 421cb0ef41Sopenharmony_ci }; 431cb0ef41Sopenharmony_ci} else { 441cb0ef41Sopenharmony_ci const { parse } = require('some-js-parsing-library'); 451cb0ef41Sopenharmony_ci const script = workerData; 461cb0ef41Sopenharmony_ci parentPort.postMessage(parse(script)); 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci``` 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciThe above example spawns a Worker thread for each `parseJSAsync()` call. In 511cb0ef41Sopenharmony_cipractice, use a pool of Workers for these kinds of tasks. Otherwise, the 521cb0ef41Sopenharmony_cioverhead of creating Workers would likely exceed their benefit. 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciWhen implementing a worker pool, use the [`AsyncResource`][] API to inform 551cb0ef41Sopenharmony_cidiagnostic tools (e.g. to provide asynchronous stack traces) about the 561cb0ef41Sopenharmony_cicorrelation between tasks and their outcomes. See 571cb0ef41Sopenharmony_ci["Using `AsyncResource` for a `Worker` thread pool"][async-resource-worker-pool] 581cb0ef41Sopenharmony_ciin the `async_hooks` documentation for an example implementation. 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ciWorker threads inherit non-process-specific options by default. Refer to 611cb0ef41Sopenharmony_ci[`Worker constructor options`][] to know how to customize worker thread options, 621cb0ef41Sopenharmony_cispecifically `argv` and `execArgv` options. 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci## `worker.getEnvironmentData(key)` 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci<!-- YAML 671cb0ef41Sopenharmony_ciadded: 681cb0ef41Sopenharmony_ci - v15.12.0 691cb0ef41Sopenharmony_ci - v14.18.0 701cb0ef41Sopenharmony_cichanges: 711cb0ef41Sopenharmony_ci - version: v17.5.0 721cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41272 731cb0ef41Sopenharmony_ci description: No longer experimental. 741cb0ef41Sopenharmony_ci--> 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci* `key` {any} Any arbitrary, cloneable JavaScript value that can be used as a 771cb0ef41Sopenharmony_ci {Map} key. 781cb0ef41Sopenharmony_ci* Returns: {any} 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ciWithin a worker thread, `worker.getEnvironmentData()` returns a clone 811cb0ef41Sopenharmony_ciof data passed to the spawning thread's `worker.setEnvironmentData()`. 821cb0ef41Sopenharmony_ciEvery new `Worker` receives its own copy of the environment data 831cb0ef41Sopenharmony_ciautomatically. 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci```js 861cb0ef41Sopenharmony_ciconst { 871cb0ef41Sopenharmony_ci Worker, 881cb0ef41Sopenharmony_ci isMainThread, 891cb0ef41Sopenharmony_ci setEnvironmentData, 901cb0ef41Sopenharmony_ci getEnvironmentData, 911cb0ef41Sopenharmony_ci} = require('node:worker_threads'); 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ciif (isMainThread) { 941cb0ef41Sopenharmony_ci setEnvironmentData('Hello', 'World!'); 951cb0ef41Sopenharmony_ci const worker = new Worker(__filename); 961cb0ef41Sopenharmony_ci} else { 971cb0ef41Sopenharmony_ci console.log(getEnvironmentData('Hello')); // Prints 'World!'. 981cb0ef41Sopenharmony_ci} 991cb0ef41Sopenharmony_ci``` 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci## `worker.isMainThread` 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci<!-- YAML 1041cb0ef41Sopenharmony_ciadded: v10.5.0 1051cb0ef41Sopenharmony_ci--> 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci* {boolean} 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ciIs `true` if this code is not running inside of a [`Worker`][] thread. 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ci```js 1121cb0ef41Sopenharmony_ciconst { Worker, isMainThread } = require('node:worker_threads'); 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ciif (isMainThread) { 1151cb0ef41Sopenharmony_ci // This re-loads the current file inside a Worker instance. 1161cb0ef41Sopenharmony_ci new Worker(__filename); 1171cb0ef41Sopenharmony_ci} else { 1181cb0ef41Sopenharmony_ci console.log('Inside Worker!'); 1191cb0ef41Sopenharmony_ci console.log(isMainThread); // Prints 'false'. 1201cb0ef41Sopenharmony_ci} 1211cb0ef41Sopenharmony_ci``` 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci## `worker.markAsUntransferable(object)` 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci<!-- YAML 1261cb0ef41Sopenharmony_ciadded: 1271cb0ef41Sopenharmony_ci - v14.5.0 1281cb0ef41Sopenharmony_ci - v12.19.0 1291cb0ef41Sopenharmony_ci--> 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ciMark an object as not transferable. If `object` occurs in the transfer list of 1321cb0ef41Sopenharmony_cia [`port.postMessage()`][] call, it is ignored. 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ciIn particular, this makes sense for objects that can be cloned, rather than 1351cb0ef41Sopenharmony_citransferred, and which are used by other objects on the sending side. 1361cb0ef41Sopenharmony_ciFor example, Node.js marks the `ArrayBuffer`s it uses for its 1371cb0ef41Sopenharmony_ci[`Buffer` pool][`Buffer.allocUnsafe()`] with this. 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ciThis operation cannot be undone. 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci```js 1421cb0ef41Sopenharmony_ciconst { MessageChannel, markAsUntransferable } = require('node:worker_threads'); 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ciconst pooledBuffer = new ArrayBuffer(8); 1451cb0ef41Sopenharmony_ciconst typedArray1 = new Uint8Array(pooledBuffer); 1461cb0ef41Sopenharmony_ciconst typedArray2 = new Float64Array(pooledBuffer); 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_cimarkAsUntransferable(pooledBuffer); 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ciconst { port1 } = new MessageChannel(); 1511cb0ef41Sopenharmony_ciport1.postMessage(typedArray1, [ typedArray1.buffer ]); 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci// The following line prints the contents of typedArray1 -- it still owns 1541cb0ef41Sopenharmony_ci// its memory and has been cloned, not transferred. Without 1551cb0ef41Sopenharmony_ci// `markAsUntransferable()`, this would print an empty Uint8Array. 1561cb0ef41Sopenharmony_ci// typedArray2 is intact as well. 1571cb0ef41Sopenharmony_ciconsole.log(typedArray1); 1581cb0ef41Sopenharmony_ciconsole.log(typedArray2); 1591cb0ef41Sopenharmony_ci``` 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ciThere is no equivalent to this API in browsers. 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci## `worker.moveMessagePortToContext(port, contextifiedSandbox)` 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ci<!-- YAML 1661cb0ef41Sopenharmony_ciadded: v11.13.0 1671cb0ef41Sopenharmony_ci--> 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_ci* `port` {MessagePort} The message port to transfer. 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ci* `contextifiedSandbox` {Object} A [contextified][] object as returned by the 1721cb0ef41Sopenharmony_ci `vm.createContext()` method. 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci* Returns: {MessagePort} 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ciTransfer a `MessagePort` to a different [`vm`][] Context. The original `port` 1771cb0ef41Sopenharmony_ciobject is rendered unusable, and the returned `MessagePort` instance 1781cb0ef41Sopenharmony_citakes its place. 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ciThe returned `MessagePort` is an object in the target context and 1811cb0ef41Sopenharmony_ciinherits from its global `Object` class. Objects passed to the 1821cb0ef41Sopenharmony_ci[`port.onmessage()`][] listener are also created in the target context 1831cb0ef41Sopenharmony_ciand inherit from its global `Object` class. 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ciHowever, the created `MessagePort` no longer inherits from 1861cb0ef41Sopenharmony_ci[`EventTarget`][], and only [`port.onmessage()`][] can be used to receive 1871cb0ef41Sopenharmony_cievents using it. 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci## `worker.parentPort` 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci<!-- YAML 1921cb0ef41Sopenharmony_ciadded: v10.5.0 1931cb0ef41Sopenharmony_ci--> 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci* {null|MessagePort} 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ciIf this thread is a [`Worker`][], this is a [`MessagePort`][] 1981cb0ef41Sopenharmony_ciallowing communication with the parent thread. Messages sent using 1991cb0ef41Sopenharmony_ci`parentPort.postMessage()` are available in the parent thread 2001cb0ef41Sopenharmony_ciusing `worker.on('message')`, and messages sent from the parent thread 2011cb0ef41Sopenharmony_ciusing `worker.postMessage()` are available in this thread using 2021cb0ef41Sopenharmony_ci`parentPort.on('message')`. 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ci```js 2051cb0ef41Sopenharmony_ciconst { Worker, isMainThread, parentPort } = require('node:worker_threads'); 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ciif (isMainThread) { 2081cb0ef41Sopenharmony_ci const worker = new Worker(__filename); 2091cb0ef41Sopenharmony_ci worker.once('message', (message) => { 2101cb0ef41Sopenharmony_ci console.log(message); // Prints 'Hello, world!'. 2111cb0ef41Sopenharmony_ci }); 2121cb0ef41Sopenharmony_ci worker.postMessage('Hello, world!'); 2131cb0ef41Sopenharmony_ci} else { 2141cb0ef41Sopenharmony_ci // When a message from the parent thread is received, send it back: 2151cb0ef41Sopenharmony_ci parentPort.once('message', (message) => { 2161cb0ef41Sopenharmony_ci parentPort.postMessage(message); 2171cb0ef41Sopenharmony_ci }); 2181cb0ef41Sopenharmony_ci} 2191cb0ef41Sopenharmony_ci``` 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci## `worker.receiveMessageOnPort(port)` 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ci<!-- YAML 2241cb0ef41Sopenharmony_ciadded: v12.3.0 2251cb0ef41Sopenharmony_cichanges: 2261cb0ef41Sopenharmony_ci - version: v15.12.0 2271cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37535 2281cb0ef41Sopenharmony_ci description: The port argument can also refer to a `BroadcastChannel` now. 2291cb0ef41Sopenharmony_ci--> 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ci* `port` {MessagePort|BroadcastChannel} 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci* Returns: {Object|undefined} 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ciReceive a single message from a given `MessagePort`. If no message is available, 2361cb0ef41Sopenharmony_ci`undefined` is returned, otherwise an object with a single `message` property 2371cb0ef41Sopenharmony_cithat contains the message payload, corresponding to the oldest message in the 2381cb0ef41Sopenharmony_ci`MessagePort`'s queue. 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci```js 2411cb0ef41Sopenharmony_ciconst { MessageChannel, receiveMessageOnPort } = require('node:worker_threads'); 2421cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 2431cb0ef41Sopenharmony_ciport1.postMessage({ hello: 'world' }); 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ciconsole.log(receiveMessageOnPort(port2)); 2461cb0ef41Sopenharmony_ci// Prints: { message: { hello: 'world' } } 2471cb0ef41Sopenharmony_ciconsole.log(receiveMessageOnPort(port2)); 2481cb0ef41Sopenharmony_ci// Prints: undefined 2491cb0ef41Sopenharmony_ci``` 2501cb0ef41Sopenharmony_ci 2511cb0ef41Sopenharmony_ciWhen this function is used, no `'message'` event is emitted and the 2521cb0ef41Sopenharmony_ci`onmessage` listener is not invoked. 2531cb0ef41Sopenharmony_ci 2541cb0ef41Sopenharmony_ci## `worker.resourceLimits` 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_ci<!-- YAML 2571cb0ef41Sopenharmony_ciadded: 2581cb0ef41Sopenharmony_ci - v13.2.0 2591cb0ef41Sopenharmony_ci - v12.16.0 2601cb0ef41Sopenharmony_ci--> 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ci* {Object} 2631cb0ef41Sopenharmony_ci * `maxYoungGenerationSizeMb` {number} 2641cb0ef41Sopenharmony_ci * `maxOldGenerationSizeMb` {number} 2651cb0ef41Sopenharmony_ci * `codeRangeSizeMb` {number} 2661cb0ef41Sopenharmony_ci * `stackSizeMb` {number} 2671cb0ef41Sopenharmony_ci 2681cb0ef41Sopenharmony_ciProvides the set of JS engine resource constraints inside this Worker thread. 2691cb0ef41Sopenharmony_ciIf the `resourceLimits` option was passed to the [`Worker`][] constructor, 2701cb0ef41Sopenharmony_cithis matches its values. 2711cb0ef41Sopenharmony_ci 2721cb0ef41Sopenharmony_ciIf this is used in the main thread, its value is an empty object. 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_ci## `worker.SHARE_ENV` 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ci<!-- YAML 2771cb0ef41Sopenharmony_ciadded: v11.14.0 2781cb0ef41Sopenharmony_ci--> 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci* {symbol} 2811cb0ef41Sopenharmony_ci 2821cb0ef41Sopenharmony_ciA special value that can be passed as the `env` option of the [`Worker`][] 2831cb0ef41Sopenharmony_ciconstructor, to indicate that the current thread and the Worker thread should 2841cb0ef41Sopenharmony_cishare read and write access to the same set of environment variables. 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci```js 2871cb0ef41Sopenharmony_ciconst { Worker, SHARE_ENV } = require('node:worker_threads'); 2881cb0ef41Sopenharmony_cinew Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV }) 2891cb0ef41Sopenharmony_ci .on('exit', () => { 2901cb0ef41Sopenharmony_ci console.log(process.env.SET_IN_WORKER); // Prints 'foo'. 2911cb0ef41Sopenharmony_ci }); 2921cb0ef41Sopenharmony_ci``` 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ci## `worker.setEnvironmentData(key[, value])` 2951cb0ef41Sopenharmony_ci 2961cb0ef41Sopenharmony_ci<!-- YAML 2971cb0ef41Sopenharmony_ciadded: 2981cb0ef41Sopenharmony_ci - v15.12.0 2991cb0ef41Sopenharmony_ci - v14.18.0 3001cb0ef41Sopenharmony_cichanges: 3011cb0ef41Sopenharmony_ci - version: v17.5.0 3021cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41272 3031cb0ef41Sopenharmony_ci description: No longer experimental. 3041cb0ef41Sopenharmony_ci--> 3051cb0ef41Sopenharmony_ci 3061cb0ef41Sopenharmony_ci* `key` {any} Any arbitrary, cloneable JavaScript value that can be used as a 3071cb0ef41Sopenharmony_ci {Map} key. 3081cb0ef41Sopenharmony_ci* `value` {any} Any arbitrary, cloneable JavaScript value that will be cloned 3091cb0ef41Sopenharmony_ci and passed automatically to all new `Worker` instances. If `value` is passed 3101cb0ef41Sopenharmony_ci as `undefined`, any previously set value for the `key` will be deleted. 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ciThe `worker.setEnvironmentData()` API sets the content of 3131cb0ef41Sopenharmony_ci`worker.getEnvironmentData()` in the current thread and all new `Worker` 3141cb0ef41Sopenharmony_ciinstances spawned from the current context. 3151cb0ef41Sopenharmony_ci 3161cb0ef41Sopenharmony_ci## `worker.threadId` 3171cb0ef41Sopenharmony_ci 3181cb0ef41Sopenharmony_ci<!-- YAML 3191cb0ef41Sopenharmony_ciadded: v10.5.0 3201cb0ef41Sopenharmony_ci--> 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ci* {integer} 3231cb0ef41Sopenharmony_ci 3241cb0ef41Sopenharmony_ciAn integer identifier for the current thread. On the corresponding worker object 3251cb0ef41Sopenharmony_ci(if there is any), it is available as [`worker.threadId`][]. 3261cb0ef41Sopenharmony_ciThis value is unique for each [`Worker`][] instance inside a single process. 3271cb0ef41Sopenharmony_ci 3281cb0ef41Sopenharmony_ci## `worker.workerData` 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci<!-- YAML 3311cb0ef41Sopenharmony_ciadded: v10.5.0 3321cb0ef41Sopenharmony_ci--> 3331cb0ef41Sopenharmony_ci 3341cb0ef41Sopenharmony_ciAn arbitrary JavaScript value that contains a clone of the data passed 3351cb0ef41Sopenharmony_cito this thread's `Worker` constructor. 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ciThe data is cloned as if using [`postMessage()`][`port.postMessage()`], 3381cb0ef41Sopenharmony_ciaccording to the [HTML structured clone algorithm][]. 3391cb0ef41Sopenharmony_ci 3401cb0ef41Sopenharmony_ci```js 3411cb0ef41Sopenharmony_ciconst { Worker, isMainThread, workerData } = require('node:worker_threads'); 3421cb0ef41Sopenharmony_ci 3431cb0ef41Sopenharmony_ciif (isMainThread) { 3441cb0ef41Sopenharmony_ci const worker = new Worker(__filename, { workerData: 'Hello, world!' }); 3451cb0ef41Sopenharmony_ci} else { 3461cb0ef41Sopenharmony_ci console.log(workerData); // Prints 'Hello, world!'. 3471cb0ef41Sopenharmony_ci} 3481cb0ef41Sopenharmony_ci``` 3491cb0ef41Sopenharmony_ci 3501cb0ef41Sopenharmony_ci## Class: `BroadcastChannel extends EventTarget` 3511cb0ef41Sopenharmony_ci 3521cb0ef41Sopenharmony_ci<!-- YAML 3531cb0ef41Sopenharmony_ciadded: v15.4.0 3541cb0ef41Sopenharmony_cichanges: 3551cb0ef41Sopenharmony_ci - version: v18.0.0 3561cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41271 3571cb0ef41Sopenharmony_ci description: No longer experimental. 3581cb0ef41Sopenharmony_ci--> 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_ciInstances of `BroadcastChannel` allow asynchronous one-to-many communication 3611cb0ef41Sopenharmony_ciwith all other `BroadcastChannel` instances bound to the same channel name. 3621cb0ef41Sopenharmony_ci 3631cb0ef41Sopenharmony_ci```js 3641cb0ef41Sopenharmony_ci'use strict'; 3651cb0ef41Sopenharmony_ci 3661cb0ef41Sopenharmony_ciconst { 3671cb0ef41Sopenharmony_ci isMainThread, 3681cb0ef41Sopenharmony_ci BroadcastChannel, 3691cb0ef41Sopenharmony_ci Worker, 3701cb0ef41Sopenharmony_ci} = require('node:worker_threads'); 3711cb0ef41Sopenharmony_ci 3721cb0ef41Sopenharmony_ciconst bc = new BroadcastChannel('hello'); 3731cb0ef41Sopenharmony_ci 3741cb0ef41Sopenharmony_ciif (isMainThread) { 3751cb0ef41Sopenharmony_ci let c = 0; 3761cb0ef41Sopenharmony_ci bc.onmessage = (event) => { 3771cb0ef41Sopenharmony_ci console.log(event.data); 3781cb0ef41Sopenharmony_ci if (++c === 10) bc.close(); 3791cb0ef41Sopenharmony_ci }; 3801cb0ef41Sopenharmony_ci for (let n = 0; n < 10; n++) 3811cb0ef41Sopenharmony_ci new Worker(__filename); 3821cb0ef41Sopenharmony_ci} else { 3831cb0ef41Sopenharmony_ci bc.postMessage('hello from every worker'); 3841cb0ef41Sopenharmony_ci bc.close(); 3851cb0ef41Sopenharmony_ci} 3861cb0ef41Sopenharmony_ci``` 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ci### `new BroadcastChannel(name)` 3891cb0ef41Sopenharmony_ci 3901cb0ef41Sopenharmony_ci<!-- YAML 3911cb0ef41Sopenharmony_ciadded: v15.4.0 3921cb0ef41Sopenharmony_ci--> 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ci* `name` {any} The name of the channel to connect to. Any JavaScript value 3951cb0ef41Sopenharmony_ci that can be converted to a string using `` `${name}` `` is permitted. 3961cb0ef41Sopenharmony_ci 3971cb0ef41Sopenharmony_ci### `broadcastChannel.close()` 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_ci<!-- YAML 4001cb0ef41Sopenharmony_ciadded: v15.4.0 4011cb0ef41Sopenharmony_ci--> 4021cb0ef41Sopenharmony_ci 4031cb0ef41Sopenharmony_ciCloses the `BroadcastChannel` connection. 4041cb0ef41Sopenharmony_ci 4051cb0ef41Sopenharmony_ci### `broadcastChannel.onmessage` 4061cb0ef41Sopenharmony_ci 4071cb0ef41Sopenharmony_ci<!-- YAML 4081cb0ef41Sopenharmony_ciadded: v15.4.0 4091cb0ef41Sopenharmony_ci--> 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ci* Type: {Function} Invoked with a single `MessageEvent` argument 4121cb0ef41Sopenharmony_ci when a message is received. 4131cb0ef41Sopenharmony_ci 4141cb0ef41Sopenharmony_ci### `broadcastChannel.onmessageerror` 4151cb0ef41Sopenharmony_ci 4161cb0ef41Sopenharmony_ci<!-- YAML 4171cb0ef41Sopenharmony_ciadded: v15.4.0 4181cb0ef41Sopenharmony_ci--> 4191cb0ef41Sopenharmony_ci 4201cb0ef41Sopenharmony_ci* Type: {Function} Invoked with a received message cannot be 4211cb0ef41Sopenharmony_ci deserialized. 4221cb0ef41Sopenharmony_ci 4231cb0ef41Sopenharmony_ci### `broadcastChannel.postMessage(message)` 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_ci<!-- YAML 4261cb0ef41Sopenharmony_ciadded: v15.4.0 4271cb0ef41Sopenharmony_ci--> 4281cb0ef41Sopenharmony_ci 4291cb0ef41Sopenharmony_ci* `message` {any} Any cloneable JavaScript value. 4301cb0ef41Sopenharmony_ci 4311cb0ef41Sopenharmony_ci### `broadcastChannel.ref()` 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_ci<!-- YAML 4341cb0ef41Sopenharmony_ciadded: v15.4.0 4351cb0ef41Sopenharmony_ci--> 4361cb0ef41Sopenharmony_ci 4371cb0ef41Sopenharmony_ciOpposite of `unref()`. Calling `ref()` on a previously `unref()`ed 4381cb0ef41Sopenharmony_ciBroadcastChannel does _not_ let the program exit if it's the only active handle 4391cb0ef41Sopenharmony_cileft (the default behavior). If the port is `ref()`ed, calling `ref()` again 4401cb0ef41Sopenharmony_cihas no effect. 4411cb0ef41Sopenharmony_ci 4421cb0ef41Sopenharmony_ci### `broadcastChannel.unref()` 4431cb0ef41Sopenharmony_ci 4441cb0ef41Sopenharmony_ci<!-- YAML 4451cb0ef41Sopenharmony_ciadded: v15.4.0 4461cb0ef41Sopenharmony_ci--> 4471cb0ef41Sopenharmony_ci 4481cb0ef41Sopenharmony_ciCalling `unref()` on a BroadcastChannel allows the thread to exit if this 4491cb0ef41Sopenharmony_ciis the only active handle in the event system. If the BroadcastChannel is 4501cb0ef41Sopenharmony_cialready `unref()`ed calling `unref()` again has no effect. 4511cb0ef41Sopenharmony_ci 4521cb0ef41Sopenharmony_ci## Class: `MessageChannel` 4531cb0ef41Sopenharmony_ci 4541cb0ef41Sopenharmony_ci<!-- YAML 4551cb0ef41Sopenharmony_ciadded: v10.5.0 4561cb0ef41Sopenharmony_ci--> 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_ciInstances of the `worker.MessageChannel` class represent an asynchronous, 4591cb0ef41Sopenharmony_citwo-way communications channel. 4601cb0ef41Sopenharmony_ciThe `MessageChannel` has no methods of its own. `new MessageChannel()` 4611cb0ef41Sopenharmony_ciyields an object with `port1` and `port2` properties, which refer to linked 4621cb0ef41Sopenharmony_ci[`MessagePort`][] instances. 4631cb0ef41Sopenharmony_ci 4641cb0ef41Sopenharmony_ci```js 4651cb0ef41Sopenharmony_ciconst { MessageChannel } = require('node:worker_threads'); 4661cb0ef41Sopenharmony_ci 4671cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 4681cb0ef41Sopenharmony_ciport1.on('message', (message) => console.log('received', message)); 4691cb0ef41Sopenharmony_ciport2.postMessage({ foo: 'bar' }); 4701cb0ef41Sopenharmony_ci// Prints: received { foo: 'bar' } from the `port1.on('message')` listener 4711cb0ef41Sopenharmony_ci``` 4721cb0ef41Sopenharmony_ci 4731cb0ef41Sopenharmony_ci## Class: `MessagePort` 4741cb0ef41Sopenharmony_ci 4751cb0ef41Sopenharmony_ci<!-- YAML 4761cb0ef41Sopenharmony_ciadded: v10.5.0 4771cb0ef41Sopenharmony_cichanges: 4781cb0ef41Sopenharmony_ci - version: 4791cb0ef41Sopenharmony_ci - v14.7.0 4801cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/34057 4811cb0ef41Sopenharmony_ci description: This class now inherits from `EventTarget` rather than 4821cb0ef41Sopenharmony_ci from `EventEmitter`. 4831cb0ef41Sopenharmony_ci--> 4841cb0ef41Sopenharmony_ci 4851cb0ef41Sopenharmony_ci* Extends: {EventTarget} 4861cb0ef41Sopenharmony_ci 4871cb0ef41Sopenharmony_ciInstances of the `worker.MessagePort` class represent one end of an 4881cb0ef41Sopenharmony_ciasynchronous, two-way communications channel. It can be used to transfer 4891cb0ef41Sopenharmony_cistructured data, memory regions and other `MessagePort`s between different 4901cb0ef41Sopenharmony_ci[`Worker`][]s. 4911cb0ef41Sopenharmony_ci 4921cb0ef41Sopenharmony_ciThis implementation matches [browser `MessagePort`][]s. 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci### Event: `'close'` 4951cb0ef41Sopenharmony_ci 4961cb0ef41Sopenharmony_ci<!-- YAML 4971cb0ef41Sopenharmony_ciadded: v10.5.0 4981cb0ef41Sopenharmony_ci--> 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ciThe `'close'` event is emitted once either side of the channel has been 5011cb0ef41Sopenharmony_cidisconnected. 5021cb0ef41Sopenharmony_ci 5031cb0ef41Sopenharmony_ci```js 5041cb0ef41Sopenharmony_ciconst { MessageChannel } = require('node:worker_threads'); 5051cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 5061cb0ef41Sopenharmony_ci 5071cb0ef41Sopenharmony_ci// Prints: 5081cb0ef41Sopenharmony_ci// foobar 5091cb0ef41Sopenharmony_ci// closed! 5101cb0ef41Sopenharmony_ciport2.on('message', (message) => console.log(message)); 5111cb0ef41Sopenharmony_ciport2.on('close', () => console.log('closed!')); 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_ciport1.postMessage('foobar'); 5141cb0ef41Sopenharmony_ciport1.close(); 5151cb0ef41Sopenharmony_ci``` 5161cb0ef41Sopenharmony_ci 5171cb0ef41Sopenharmony_ci### Event: `'message'` 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci<!-- YAML 5201cb0ef41Sopenharmony_ciadded: v10.5.0 5211cb0ef41Sopenharmony_ci--> 5221cb0ef41Sopenharmony_ci 5231cb0ef41Sopenharmony_ci* `value` {any} The transmitted value 5241cb0ef41Sopenharmony_ci 5251cb0ef41Sopenharmony_ciThe `'message'` event is emitted for any incoming message, containing the cloned 5261cb0ef41Sopenharmony_ciinput of [`port.postMessage()`][]. 5271cb0ef41Sopenharmony_ci 5281cb0ef41Sopenharmony_ciListeners on this event receive a clone of the `value` parameter as passed 5291cb0ef41Sopenharmony_cito `postMessage()` and no further arguments. 5301cb0ef41Sopenharmony_ci 5311cb0ef41Sopenharmony_ci### Event: `'messageerror'` 5321cb0ef41Sopenharmony_ci 5331cb0ef41Sopenharmony_ci<!-- YAML 5341cb0ef41Sopenharmony_ciadded: 5351cb0ef41Sopenharmony_ci - v14.5.0 5361cb0ef41Sopenharmony_ci - v12.19.0 5371cb0ef41Sopenharmony_ci--> 5381cb0ef41Sopenharmony_ci 5391cb0ef41Sopenharmony_ci* `error` {Error} An Error object 5401cb0ef41Sopenharmony_ci 5411cb0ef41Sopenharmony_ciThe `'messageerror'` event is emitted when deserializing a message failed. 5421cb0ef41Sopenharmony_ci 5431cb0ef41Sopenharmony_ciCurrently, this event is emitted when there is an error occurring while 5441cb0ef41Sopenharmony_ciinstantiating the posted JS object on the receiving end. Such situations 5451cb0ef41Sopenharmony_ciare rare, but can happen, for instance, when certain Node.js API objects 5461cb0ef41Sopenharmony_ciare received in a `vm.Context` (where Node.js APIs are currently 5471cb0ef41Sopenharmony_ciunavailable). 5481cb0ef41Sopenharmony_ci 5491cb0ef41Sopenharmony_ci### `port.close()` 5501cb0ef41Sopenharmony_ci 5511cb0ef41Sopenharmony_ci<!-- YAML 5521cb0ef41Sopenharmony_ciadded: v10.5.0 5531cb0ef41Sopenharmony_ci--> 5541cb0ef41Sopenharmony_ci 5551cb0ef41Sopenharmony_ciDisables further sending of messages on either side of the connection. 5561cb0ef41Sopenharmony_ciThis method can be called when no further communication will happen over this 5571cb0ef41Sopenharmony_ci`MessagePort`. 5581cb0ef41Sopenharmony_ci 5591cb0ef41Sopenharmony_ciThe [`'close'` event][] is emitted on both `MessagePort` instances that 5601cb0ef41Sopenharmony_ciare part of the channel. 5611cb0ef41Sopenharmony_ci 5621cb0ef41Sopenharmony_ci### `port.postMessage(value[, transferList])` 5631cb0ef41Sopenharmony_ci 5641cb0ef41Sopenharmony_ci<!-- YAML 5651cb0ef41Sopenharmony_ciadded: v10.5.0 5661cb0ef41Sopenharmony_cichanges: 5671cb0ef41Sopenharmony_ci - version: 5681cb0ef41Sopenharmony_ci - v15.14.0 5691cb0ef41Sopenharmony_ci - v14.18.0 5701cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37917 5711cb0ef41Sopenharmony_ci description: Add 'BlockList' to the list of cloneable types. 5721cb0ef41Sopenharmony_ci - version: 5731cb0ef41Sopenharmony_ci - v15.9.0 5741cb0ef41Sopenharmony_ci - v14.18.0 5751cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/37155 5761cb0ef41Sopenharmony_ci description: Add 'Histogram' types to the list of cloneable types. 5771cb0ef41Sopenharmony_ci - version: v15.6.0 5781cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/36804 5791cb0ef41Sopenharmony_ci description: Added `X509Certificate` to the list of cloneable types. 5801cb0ef41Sopenharmony_ci - version: v15.0.0 5811cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/35093 5821cb0ef41Sopenharmony_ci description: Added `CryptoKey` to the list of cloneable types. 5831cb0ef41Sopenharmony_ci - version: 5841cb0ef41Sopenharmony_ci - v14.5.0 5851cb0ef41Sopenharmony_ci - v12.19.0 5861cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33360 5871cb0ef41Sopenharmony_ci description: Added `KeyObject` to the list of cloneable types. 5881cb0ef41Sopenharmony_ci - version: 5891cb0ef41Sopenharmony_ci - v14.5.0 5901cb0ef41Sopenharmony_ci - v12.19.0 5911cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/33772 5921cb0ef41Sopenharmony_ci description: Added `FileHandle` to the list of transferable types. 5931cb0ef41Sopenharmony_ci--> 5941cb0ef41Sopenharmony_ci 5951cb0ef41Sopenharmony_ci* `value` {any} 5961cb0ef41Sopenharmony_ci* `transferList` {Object\[]} 5971cb0ef41Sopenharmony_ci 5981cb0ef41Sopenharmony_ciSends a JavaScript value to the receiving side of this channel. 5991cb0ef41Sopenharmony_ci`value` is transferred in a way which is compatible with 6001cb0ef41Sopenharmony_cithe [HTML structured clone algorithm][]. 6011cb0ef41Sopenharmony_ci 6021cb0ef41Sopenharmony_ciIn particular, the significant differences to `JSON` are: 6031cb0ef41Sopenharmony_ci 6041cb0ef41Sopenharmony_ci* `value` may contain circular references. 6051cb0ef41Sopenharmony_ci* `value` may contain instances of builtin JS types such as `RegExp`s, 6061cb0ef41Sopenharmony_ci `BigInt`s, `Map`s, `Set`s, etc. 6071cb0ef41Sopenharmony_ci* `value` may contain typed arrays, both using `ArrayBuffer`s 6081cb0ef41Sopenharmony_ci and `SharedArrayBuffer`s. 6091cb0ef41Sopenharmony_ci* `value` may contain [`WebAssembly.Module`][] instances. 6101cb0ef41Sopenharmony_ci* `value` may not contain native (C++-backed) objects other than: 6111cb0ef41Sopenharmony_ci * {CryptoKey}s, 6121cb0ef41Sopenharmony_ci * {FileHandle}s, 6131cb0ef41Sopenharmony_ci * {Histogram}s, 6141cb0ef41Sopenharmony_ci * {KeyObject}s, 6151cb0ef41Sopenharmony_ci * {MessagePort}s, 6161cb0ef41Sopenharmony_ci * {net.BlockList}s, 6171cb0ef41Sopenharmony_ci * {net.SocketAddress}es, 6181cb0ef41Sopenharmony_ci * {X509Certificate}s. 6191cb0ef41Sopenharmony_ci 6201cb0ef41Sopenharmony_ci```js 6211cb0ef41Sopenharmony_ciconst { MessageChannel } = require('node:worker_threads'); 6221cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 6231cb0ef41Sopenharmony_ci 6241cb0ef41Sopenharmony_ciport1.on('message', (message) => console.log(message)); 6251cb0ef41Sopenharmony_ci 6261cb0ef41Sopenharmony_ciconst circularData = {}; 6271cb0ef41Sopenharmony_cicircularData.foo = circularData; 6281cb0ef41Sopenharmony_ci// Prints: { foo: [Circular] } 6291cb0ef41Sopenharmony_ciport2.postMessage(circularData); 6301cb0ef41Sopenharmony_ci``` 6311cb0ef41Sopenharmony_ci 6321cb0ef41Sopenharmony_ci`transferList` may be a list of [`ArrayBuffer`][], [`MessagePort`][], and 6331cb0ef41Sopenharmony_ci[`FileHandle`][] objects. 6341cb0ef41Sopenharmony_ciAfter transferring, they are not usable on the sending side of the channel 6351cb0ef41Sopenharmony_cianymore (even if they are not contained in `value`). Unlike with 6361cb0ef41Sopenharmony_ci[child processes][], transferring handles such as network sockets is currently 6371cb0ef41Sopenharmony_cinot supported. 6381cb0ef41Sopenharmony_ci 6391cb0ef41Sopenharmony_ciIf `value` contains [`SharedArrayBuffer`][] instances, those are accessible 6401cb0ef41Sopenharmony_cifrom either thread. They cannot be listed in `transferList`. 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ci`value` may still contain `ArrayBuffer` instances that are not in 6431cb0ef41Sopenharmony_ci`transferList`; in that case, the underlying memory is copied rather than moved. 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ci```js 6461cb0ef41Sopenharmony_ciconst { MessageChannel } = require('node:worker_threads'); 6471cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 6481cb0ef41Sopenharmony_ci 6491cb0ef41Sopenharmony_ciport1.on('message', (message) => console.log(message)); 6501cb0ef41Sopenharmony_ci 6511cb0ef41Sopenharmony_ciconst uint8Array = new Uint8Array([ 1, 2, 3, 4 ]); 6521cb0ef41Sopenharmony_ci// This posts a copy of `uint8Array`: 6531cb0ef41Sopenharmony_ciport2.postMessage(uint8Array); 6541cb0ef41Sopenharmony_ci// This does not copy data, but renders `uint8Array` unusable: 6551cb0ef41Sopenharmony_ciport2.postMessage(uint8Array, [ uint8Array.buffer ]); 6561cb0ef41Sopenharmony_ci 6571cb0ef41Sopenharmony_ci// The memory for the `sharedUint8Array` is accessible from both the 6581cb0ef41Sopenharmony_ci// original and the copy received by `.on('message')`: 6591cb0ef41Sopenharmony_ciconst sharedUint8Array = new Uint8Array(new SharedArrayBuffer(4)); 6601cb0ef41Sopenharmony_ciport2.postMessage(sharedUint8Array); 6611cb0ef41Sopenharmony_ci 6621cb0ef41Sopenharmony_ci// This transfers a freshly created message port to the receiver. 6631cb0ef41Sopenharmony_ci// This can be used, for example, to create communication channels between 6641cb0ef41Sopenharmony_ci// multiple `Worker` threads that are children of the same parent thread. 6651cb0ef41Sopenharmony_ciconst otherChannel = new MessageChannel(); 6661cb0ef41Sopenharmony_ciport2.postMessage({ port: otherChannel.port1 }, [ otherChannel.port1 ]); 6671cb0ef41Sopenharmony_ci``` 6681cb0ef41Sopenharmony_ci 6691cb0ef41Sopenharmony_ciThe message object is cloned immediately, and can be modified after 6701cb0ef41Sopenharmony_ciposting without having side effects. 6711cb0ef41Sopenharmony_ci 6721cb0ef41Sopenharmony_ciFor more information on the serialization and deserialization mechanisms 6731cb0ef41Sopenharmony_cibehind this API, see the [serialization API of the `node:v8` module][v8.serdes]. 6741cb0ef41Sopenharmony_ci 6751cb0ef41Sopenharmony_ci#### Considerations when transferring TypedArrays and Buffers 6761cb0ef41Sopenharmony_ci 6771cb0ef41Sopenharmony_ciAll `TypedArray` and `Buffer` instances are views over an underlying 6781cb0ef41Sopenharmony_ci`ArrayBuffer`. That is, it is the `ArrayBuffer` that actually stores 6791cb0ef41Sopenharmony_cithe raw data while the `TypedArray` and `Buffer` objects provide a 6801cb0ef41Sopenharmony_ciway of viewing and manipulating the data. It is possible and common 6811cb0ef41Sopenharmony_cifor multiple views to be created over the same `ArrayBuffer` instance. 6821cb0ef41Sopenharmony_ciGreat care must be taken when using a transfer list to transfer an 6831cb0ef41Sopenharmony_ci`ArrayBuffer` as doing so causes all `TypedArray` and `Buffer` 6841cb0ef41Sopenharmony_ciinstances that share that same `ArrayBuffer` to become unusable. 6851cb0ef41Sopenharmony_ci 6861cb0ef41Sopenharmony_ci```js 6871cb0ef41Sopenharmony_ciconst ab = new ArrayBuffer(10); 6881cb0ef41Sopenharmony_ci 6891cb0ef41Sopenharmony_ciconst u1 = new Uint8Array(ab); 6901cb0ef41Sopenharmony_ciconst u2 = new Uint16Array(ab); 6911cb0ef41Sopenharmony_ci 6921cb0ef41Sopenharmony_ciconsole.log(u2.length); // prints 5 6931cb0ef41Sopenharmony_ci 6941cb0ef41Sopenharmony_ciport.postMessage(u1, [u1.buffer]); 6951cb0ef41Sopenharmony_ci 6961cb0ef41Sopenharmony_ciconsole.log(u2.length); // prints 0 6971cb0ef41Sopenharmony_ci``` 6981cb0ef41Sopenharmony_ci 6991cb0ef41Sopenharmony_ciFor `Buffer` instances, specifically, whether the underlying 7001cb0ef41Sopenharmony_ci`ArrayBuffer` can be transferred or cloned depends entirely on how 7011cb0ef41Sopenharmony_ciinstances were created, which often cannot be reliably determined. 7021cb0ef41Sopenharmony_ci 7031cb0ef41Sopenharmony_ciAn `ArrayBuffer` can be marked with [`markAsUntransferable()`][] to indicate 7041cb0ef41Sopenharmony_cithat it should always be cloned and never transferred. 7051cb0ef41Sopenharmony_ci 7061cb0ef41Sopenharmony_ciDepending on how a `Buffer` instance was created, it may or may 7071cb0ef41Sopenharmony_cinot own its underlying `ArrayBuffer`. An `ArrayBuffer` must not 7081cb0ef41Sopenharmony_cibe transferred unless it is known that the `Buffer` instance 7091cb0ef41Sopenharmony_ciowns it. In particular, for `Buffer`s created from the internal 7101cb0ef41Sopenharmony_ci`Buffer` pool (using, for instance `Buffer.from()` or `Buffer.allocUnsafe()`), 7111cb0ef41Sopenharmony_citransferring them is not possible and they are always cloned, 7121cb0ef41Sopenharmony_ciwhich sends a copy of the entire `Buffer` pool. 7131cb0ef41Sopenharmony_ciThis behavior may come with unintended higher memory 7141cb0ef41Sopenharmony_ciusage and possible security concerns. 7151cb0ef41Sopenharmony_ci 7161cb0ef41Sopenharmony_ciSee [`Buffer.allocUnsafe()`][] for more details on `Buffer` pooling. 7171cb0ef41Sopenharmony_ci 7181cb0ef41Sopenharmony_ciThe `ArrayBuffer`s for `Buffer` instances created using 7191cb0ef41Sopenharmony_ci`Buffer.alloc()` or `Buffer.allocUnsafeSlow()` can always be 7201cb0ef41Sopenharmony_citransferred but doing so renders all other existing views of 7211cb0ef41Sopenharmony_cithose `ArrayBuffer`s unusable. 7221cb0ef41Sopenharmony_ci 7231cb0ef41Sopenharmony_ci#### Considerations when cloning objects with prototypes, classes, and accessors 7241cb0ef41Sopenharmony_ci 7251cb0ef41Sopenharmony_ciBecause object cloning uses the [HTML structured clone algorithm][], 7261cb0ef41Sopenharmony_cinon-enumerable properties, property accessors, and object prototypes are 7271cb0ef41Sopenharmony_cinot preserved. In particular, [`Buffer`][] objects will be read as 7281cb0ef41Sopenharmony_ciplain [`Uint8Array`][]s on the receiving side, and instances of JavaScript 7291cb0ef41Sopenharmony_ciclasses will be cloned as plain JavaScript objects. 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ci```js 7321cb0ef41Sopenharmony_ciconst b = Symbol('b'); 7331cb0ef41Sopenharmony_ci 7341cb0ef41Sopenharmony_ciclass Foo { 7351cb0ef41Sopenharmony_ci #a = 1; 7361cb0ef41Sopenharmony_ci constructor() { 7371cb0ef41Sopenharmony_ci this[b] = 2; 7381cb0ef41Sopenharmony_ci this.c = 3; 7391cb0ef41Sopenharmony_ci } 7401cb0ef41Sopenharmony_ci 7411cb0ef41Sopenharmony_ci get d() { return 4; } 7421cb0ef41Sopenharmony_ci} 7431cb0ef41Sopenharmony_ci 7441cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 7451cb0ef41Sopenharmony_ci 7461cb0ef41Sopenharmony_ciport1.onmessage = ({ data }) => console.log(data); 7471cb0ef41Sopenharmony_ci 7481cb0ef41Sopenharmony_ciport2.postMessage(new Foo()); 7491cb0ef41Sopenharmony_ci 7501cb0ef41Sopenharmony_ci// Prints: { c: 3 } 7511cb0ef41Sopenharmony_ci``` 7521cb0ef41Sopenharmony_ci 7531cb0ef41Sopenharmony_ciThis limitation extends to many built-in objects, such as the global `URL` 7541cb0ef41Sopenharmony_ciobject: 7551cb0ef41Sopenharmony_ci 7561cb0ef41Sopenharmony_ci```js 7571cb0ef41Sopenharmony_ciconst { port1, port2 } = new MessageChannel(); 7581cb0ef41Sopenharmony_ci 7591cb0ef41Sopenharmony_ciport1.onmessage = ({ data }) => console.log(data); 7601cb0ef41Sopenharmony_ci 7611cb0ef41Sopenharmony_ciport2.postMessage(new URL('https://example.org')); 7621cb0ef41Sopenharmony_ci 7631cb0ef41Sopenharmony_ci// Prints: { } 7641cb0ef41Sopenharmony_ci``` 7651cb0ef41Sopenharmony_ci 7661cb0ef41Sopenharmony_ci### `port.hasRef()` 7671cb0ef41Sopenharmony_ci 7681cb0ef41Sopenharmony_ci<!-- YAML 7691cb0ef41Sopenharmony_ciadded: v18.1.0 7701cb0ef41Sopenharmony_ci--> 7711cb0ef41Sopenharmony_ci 7721cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 7731cb0ef41Sopenharmony_ci 7741cb0ef41Sopenharmony_ci* Returns: {boolean} 7751cb0ef41Sopenharmony_ci 7761cb0ef41Sopenharmony_ciIf true, the `MessagePort` object will keep the Node.js event loop active. 7771cb0ef41Sopenharmony_ci 7781cb0ef41Sopenharmony_ci### `port.ref()` 7791cb0ef41Sopenharmony_ci 7801cb0ef41Sopenharmony_ci<!-- YAML 7811cb0ef41Sopenharmony_ciadded: v10.5.0 7821cb0ef41Sopenharmony_ci--> 7831cb0ef41Sopenharmony_ci 7841cb0ef41Sopenharmony_ciOpposite of `unref()`. Calling `ref()` on a previously `unref()`ed port does 7851cb0ef41Sopenharmony_ci_not_ let the program exit if it's the only active handle left (the default 7861cb0ef41Sopenharmony_cibehavior). If the port is `ref()`ed, calling `ref()` again has no effect. 7871cb0ef41Sopenharmony_ci 7881cb0ef41Sopenharmony_ciIf listeners are attached or removed using `.on('message')`, the port 7891cb0ef41Sopenharmony_ciis `ref()`ed and `unref()`ed automatically depending on whether 7901cb0ef41Sopenharmony_cilisteners for the event exist. 7911cb0ef41Sopenharmony_ci 7921cb0ef41Sopenharmony_ci### `port.start()` 7931cb0ef41Sopenharmony_ci 7941cb0ef41Sopenharmony_ci<!-- YAML 7951cb0ef41Sopenharmony_ciadded: v10.5.0 7961cb0ef41Sopenharmony_ci--> 7971cb0ef41Sopenharmony_ci 7981cb0ef41Sopenharmony_ciStarts receiving messages on this `MessagePort`. When using this port 7991cb0ef41Sopenharmony_cias an event emitter, this is called automatically once `'message'` 8001cb0ef41Sopenharmony_cilisteners are attached. 8011cb0ef41Sopenharmony_ci 8021cb0ef41Sopenharmony_ciThis method exists for parity with the Web `MessagePort` API. In Node.js, 8031cb0ef41Sopenharmony_ciit is only useful for ignoring messages when no event listener is present. 8041cb0ef41Sopenharmony_ciNode.js also diverges in its handling of `.onmessage`. Setting it 8051cb0ef41Sopenharmony_ciautomatically calls `.start()`, but unsetting it lets messages queue up 8061cb0ef41Sopenharmony_ciuntil a new handler is set or the port is discarded. 8071cb0ef41Sopenharmony_ci 8081cb0ef41Sopenharmony_ci### `port.unref()` 8091cb0ef41Sopenharmony_ci 8101cb0ef41Sopenharmony_ci<!-- YAML 8111cb0ef41Sopenharmony_ciadded: v10.5.0 8121cb0ef41Sopenharmony_ci--> 8131cb0ef41Sopenharmony_ci 8141cb0ef41Sopenharmony_ciCalling `unref()` on a port allows the thread to exit if this is the only 8151cb0ef41Sopenharmony_ciactive handle in the event system. If the port is already `unref()`ed calling 8161cb0ef41Sopenharmony_ci`unref()` again has no effect. 8171cb0ef41Sopenharmony_ci 8181cb0ef41Sopenharmony_ciIf listeners are attached or removed using `.on('message')`, the port is 8191cb0ef41Sopenharmony_ci`ref()`ed and `unref()`ed automatically depending on whether 8201cb0ef41Sopenharmony_cilisteners for the event exist. 8211cb0ef41Sopenharmony_ci 8221cb0ef41Sopenharmony_ci## Class: `Worker` 8231cb0ef41Sopenharmony_ci 8241cb0ef41Sopenharmony_ci<!-- YAML 8251cb0ef41Sopenharmony_ciadded: v10.5.0 8261cb0ef41Sopenharmony_ci--> 8271cb0ef41Sopenharmony_ci 8281cb0ef41Sopenharmony_ci* Extends: {EventEmitter} 8291cb0ef41Sopenharmony_ci 8301cb0ef41Sopenharmony_ciThe `Worker` class represents an independent JavaScript execution thread. 8311cb0ef41Sopenharmony_ciMost Node.js APIs are available inside of it. 8321cb0ef41Sopenharmony_ci 8331cb0ef41Sopenharmony_ciNotable differences inside a Worker environment are: 8341cb0ef41Sopenharmony_ci 8351cb0ef41Sopenharmony_ci* The [`process.stdin`][], [`process.stdout`][], and [`process.stderr`][] 8361cb0ef41Sopenharmony_ci streams may be redirected by the parent thread. 8371cb0ef41Sopenharmony_ci* The [`require('node:worker_threads').isMainThread`][] property is set to `false`. 8381cb0ef41Sopenharmony_ci* The [`require('node:worker_threads').parentPort`][] message port is available. 8391cb0ef41Sopenharmony_ci* [`process.exit()`][] does not stop the whole program, just the single thread, 8401cb0ef41Sopenharmony_ci and [`process.abort()`][] is not available. 8411cb0ef41Sopenharmony_ci* [`process.chdir()`][] and `process` methods that set group or user ids 8421cb0ef41Sopenharmony_ci are not available. 8431cb0ef41Sopenharmony_ci* [`process.env`][] is a copy of the parent thread's environment variables, 8441cb0ef41Sopenharmony_ci unless otherwise specified. Changes to one copy are not visible in other 8451cb0ef41Sopenharmony_ci threads, and are not visible to native add-ons (unless 8461cb0ef41Sopenharmony_ci [`worker.SHARE_ENV`][] is passed as the `env` option to the 8471cb0ef41Sopenharmony_ci [`Worker`][] constructor). On Windows, unlike the main thread, a copy of the 8481cb0ef41Sopenharmony_ci environment variables operates in a case-sensitive manner. 8491cb0ef41Sopenharmony_ci* [`process.title`][] cannot be modified. 8501cb0ef41Sopenharmony_ci* Signals are not delivered through [`process.on('...')`][Signals events]. 8511cb0ef41Sopenharmony_ci* Execution may stop at any point as a result of [`worker.terminate()`][] 8521cb0ef41Sopenharmony_ci being invoked. 8531cb0ef41Sopenharmony_ci* IPC channels from parent processes are not accessible. 8541cb0ef41Sopenharmony_ci* The [`trace_events`][] module is not supported. 8551cb0ef41Sopenharmony_ci* Native add-ons can only be loaded from multiple threads if they fulfill 8561cb0ef41Sopenharmony_ci [certain conditions][Addons worker support]. 8571cb0ef41Sopenharmony_ci 8581cb0ef41Sopenharmony_ciCreating `Worker` instances inside of other `Worker`s is possible. 8591cb0ef41Sopenharmony_ci 8601cb0ef41Sopenharmony_ciLike [Web Workers][] and the [`node:cluster` module][], two-way communication 8611cb0ef41Sopenharmony_cican be achieved through inter-thread message passing. Internally, a `Worker` has 8621cb0ef41Sopenharmony_cia built-in pair of [`MessagePort`][]s that are already associated with each 8631cb0ef41Sopenharmony_ciother when the `Worker` is created. While the `MessagePort` object on the parent 8641cb0ef41Sopenharmony_ciside is not directly exposed, its functionalities are exposed through 8651cb0ef41Sopenharmony_ci[`worker.postMessage()`][] and the [`worker.on('message')`][] event 8661cb0ef41Sopenharmony_cion the `Worker` object for the parent thread. 8671cb0ef41Sopenharmony_ci 8681cb0ef41Sopenharmony_ciTo create custom messaging channels (which is encouraged over using the default 8691cb0ef41Sopenharmony_ciglobal channel because it facilitates separation of concerns), users can create 8701cb0ef41Sopenharmony_cia `MessageChannel` object on either thread and pass one of the 8711cb0ef41Sopenharmony_ci`MessagePort`s on that `MessageChannel` to the other thread through a 8721cb0ef41Sopenharmony_cipre-existing channel, such as the global one. 8731cb0ef41Sopenharmony_ci 8741cb0ef41Sopenharmony_ciSee [`port.postMessage()`][] for more information on how messages are passed, 8751cb0ef41Sopenharmony_ciand what kind of JavaScript values can be successfully transported through 8761cb0ef41Sopenharmony_cithe thread barrier. 8771cb0ef41Sopenharmony_ci 8781cb0ef41Sopenharmony_ci```js 8791cb0ef41Sopenharmony_ciconst assert = require('node:assert'); 8801cb0ef41Sopenharmony_ciconst { 8811cb0ef41Sopenharmony_ci Worker, MessageChannel, MessagePort, isMainThread, parentPort, 8821cb0ef41Sopenharmony_ci} = require('node:worker_threads'); 8831cb0ef41Sopenharmony_ciif (isMainThread) { 8841cb0ef41Sopenharmony_ci const worker = new Worker(__filename); 8851cb0ef41Sopenharmony_ci const subChannel = new MessageChannel(); 8861cb0ef41Sopenharmony_ci worker.postMessage({ hereIsYourPort: subChannel.port1 }, [subChannel.port1]); 8871cb0ef41Sopenharmony_ci subChannel.port2.on('message', (value) => { 8881cb0ef41Sopenharmony_ci console.log('received:', value); 8891cb0ef41Sopenharmony_ci }); 8901cb0ef41Sopenharmony_ci} else { 8911cb0ef41Sopenharmony_ci parentPort.once('message', (value) => { 8921cb0ef41Sopenharmony_ci assert(value.hereIsYourPort instanceof MessagePort); 8931cb0ef41Sopenharmony_ci value.hereIsYourPort.postMessage('the worker is sending this'); 8941cb0ef41Sopenharmony_ci value.hereIsYourPort.close(); 8951cb0ef41Sopenharmony_ci }); 8961cb0ef41Sopenharmony_ci} 8971cb0ef41Sopenharmony_ci``` 8981cb0ef41Sopenharmony_ci 8991cb0ef41Sopenharmony_ci### `new Worker(filename[, options])` 9001cb0ef41Sopenharmony_ci 9011cb0ef41Sopenharmony_ci<!-- YAML 9021cb0ef41Sopenharmony_ciadded: v10.5.0 9031cb0ef41Sopenharmony_cichanges: 9041cb0ef41Sopenharmony_ci - version: v18.16.0 9051cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/46832 9061cb0ef41Sopenharmony_ci description: Added support for a `name` option, which allows 9071cb0ef41Sopenharmony_ci adding a name to worker title for debugging. 9081cb0ef41Sopenharmony_ci - version: v14.9.0 9091cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/34584 9101cb0ef41Sopenharmony_ci description: The `filename` parameter can be a WHATWG `URL` object using 9111cb0ef41Sopenharmony_ci `data:` protocol. 9121cb0ef41Sopenharmony_ci - version: v14.9.0 9131cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/34394 9141cb0ef41Sopenharmony_ci description: The `trackUnmanagedFds` option was set to `true` by default. 9151cb0ef41Sopenharmony_ci - version: 9161cb0ef41Sopenharmony_ci - v14.6.0 9171cb0ef41Sopenharmony_ci - v12.19.0 9181cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/34303 9191cb0ef41Sopenharmony_ci description: The `trackUnmanagedFds` option was introduced. 9201cb0ef41Sopenharmony_ci - version: 9211cb0ef41Sopenharmony_ci - v13.13.0 9221cb0ef41Sopenharmony_ci - v12.17.0 9231cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/32278 9241cb0ef41Sopenharmony_ci description: The `transferList` option was introduced. 9251cb0ef41Sopenharmony_ci - version: 9261cb0ef41Sopenharmony_ci - v13.12.0 9271cb0ef41Sopenharmony_ci - v12.17.0 9281cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/31664 9291cb0ef41Sopenharmony_ci description: The `filename` parameter can be a WHATWG `URL` object using 9301cb0ef41Sopenharmony_ci `file:` protocol. 9311cb0ef41Sopenharmony_ci - version: 9321cb0ef41Sopenharmony_ci - v13.4.0 9331cb0ef41Sopenharmony_ci - v12.16.0 9341cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/30559 9351cb0ef41Sopenharmony_ci description: The `argv` option was introduced. 9361cb0ef41Sopenharmony_ci - version: 9371cb0ef41Sopenharmony_ci - v13.2.0 9381cb0ef41Sopenharmony_ci - v12.16.0 9391cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/26628 9401cb0ef41Sopenharmony_ci description: The `resourceLimits` option was introduced. 9411cb0ef41Sopenharmony_ci--> 9421cb0ef41Sopenharmony_ci 9431cb0ef41Sopenharmony_ci* `filename` {string|URL} The path to the Worker's main script or module. Must 9441cb0ef41Sopenharmony_ci be either an absolute path or a relative path (i.e. relative to the 9451cb0ef41Sopenharmony_ci current working directory) starting with `./` or `../`, or a WHATWG `URL` 9461cb0ef41Sopenharmony_ci object using `file:` or `data:` protocol. 9471cb0ef41Sopenharmony_ci When using a [`data:` URL][], the data is interpreted based on MIME type using 9481cb0ef41Sopenharmony_ci the [ECMAScript module loader][]. 9491cb0ef41Sopenharmony_ci If `options.eval` is `true`, this is a string containing JavaScript code 9501cb0ef41Sopenharmony_ci rather than a path. 9511cb0ef41Sopenharmony_ci* `options` {Object} 9521cb0ef41Sopenharmony_ci * `argv` {any\[]} List of arguments which would be stringified and appended to 9531cb0ef41Sopenharmony_ci `process.argv` in the worker. This is mostly similar to the `workerData` 9541cb0ef41Sopenharmony_ci but the values are available on the global `process.argv` as if they 9551cb0ef41Sopenharmony_ci were passed as CLI options to the script. 9561cb0ef41Sopenharmony_ci * `env` {Object} If set, specifies the initial value of `process.env` inside 9571cb0ef41Sopenharmony_ci the Worker thread. As a special value, [`worker.SHARE_ENV`][] may be used 9581cb0ef41Sopenharmony_ci to specify that the parent thread and the child thread should share their 9591cb0ef41Sopenharmony_ci environment variables; in that case, changes to one thread's `process.env` 9601cb0ef41Sopenharmony_ci object affect the other thread as well. **Default:** `process.env`. 9611cb0ef41Sopenharmony_ci * `eval` {boolean} If `true` and the first argument is a `string`, interpret 9621cb0ef41Sopenharmony_ci the first argument to the constructor as a script that is executed once the 9631cb0ef41Sopenharmony_ci worker is online. 9641cb0ef41Sopenharmony_ci * `execArgv` {string\[]} List of node CLI options passed to the worker. 9651cb0ef41Sopenharmony_ci V8 options (such as `--max-old-space-size`) and options that affect the 9661cb0ef41Sopenharmony_ci process (such as `--title`) are not supported. If set, this is provided 9671cb0ef41Sopenharmony_ci as [`process.execArgv`][] inside the worker. By default, options are 9681cb0ef41Sopenharmony_ci inherited from the parent thread. 9691cb0ef41Sopenharmony_ci * `stdin` {boolean} If this is set to `true`, then `worker.stdin` 9701cb0ef41Sopenharmony_ci provides a writable stream whose contents appear as `process.stdin` 9711cb0ef41Sopenharmony_ci inside the Worker. By default, no data is provided. 9721cb0ef41Sopenharmony_ci * `stdout` {boolean} If this is set to `true`, then `worker.stdout` is 9731cb0ef41Sopenharmony_ci not automatically piped through to `process.stdout` in the parent. 9741cb0ef41Sopenharmony_ci * `stderr` {boolean} If this is set to `true`, then `worker.stderr` is 9751cb0ef41Sopenharmony_ci not automatically piped through to `process.stderr` in the parent. 9761cb0ef41Sopenharmony_ci * `workerData` {any} Any JavaScript value that is cloned and made 9771cb0ef41Sopenharmony_ci available as [`require('node:worker_threads').workerData`][]. The cloning 9781cb0ef41Sopenharmony_ci occurs as described in the [HTML structured clone algorithm][], and an error 9791cb0ef41Sopenharmony_ci is thrown if the object cannot be cloned (e.g. because it contains 9801cb0ef41Sopenharmony_ci `function`s). 9811cb0ef41Sopenharmony_ci * `trackUnmanagedFds` {boolean} If this is set to `true`, then the Worker 9821cb0ef41Sopenharmony_ci tracks raw file descriptors managed through [`fs.open()`][] and 9831cb0ef41Sopenharmony_ci [`fs.close()`][], and closes them when the Worker exits, similar to other 9841cb0ef41Sopenharmony_ci resources like network sockets or file descriptors managed through 9851cb0ef41Sopenharmony_ci the [`FileHandle`][] API. This option is automatically inherited by all 9861cb0ef41Sopenharmony_ci nested `Worker`s. **Default:** `true`. 9871cb0ef41Sopenharmony_ci * `transferList` {Object\[]} If one or more `MessagePort`-like objects 9881cb0ef41Sopenharmony_ci are passed in `workerData`, a `transferList` is required for those 9891cb0ef41Sopenharmony_ci items or [`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`][] is thrown. 9901cb0ef41Sopenharmony_ci See [`port.postMessage()`][] for more information. 9911cb0ef41Sopenharmony_ci * `resourceLimits` {Object} An optional set of resource limits for the new JS 9921cb0ef41Sopenharmony_ci engine instance. Reaching these limits leads to termination of the `Worker` 9931cb0ef41Sopenharmony_ci instance. These limits only affect the JS engine, and no external data, 9941cb0ef41Sopenharmony_ci including no `ArrayBuffer`s. Even if these limits are set, the process may 9951cb0ef41Sopenharmony_ci still abort if it encounters a global out-of-memory situation. 9961cb0ef41Sopenharmony_ci * `maxOldGenerationSizeMb` {number} The maximum size of the main heap in 9971cb0ef41Sopenharmony_ci MB. If the command-line argument [`--max-old-space-size`][] is set, it 9981cb0ef41Sopenharmony_ci overrides this setting. 9991cb0ef41Sopenharmony_ci * `maxYoungGenerationSizeMb` {number} The maximum size of a heap space for 10001cb0ef41Sopenharmony_ci recently created objects. If the command-line argument 10011cb0ef41Sopenharmony_ci [`--max-semi-space-size`][] is set, it overrides this setting. 10021cb0ef41Sopenharmony_ci * `codeRangeSizeMb` {number} The size of a pre-allocated memory range 10031cb0ef41Sopenharmony_ci used for generated code. 10041cb0ef41Sopenharmony_ci * `stackSizeMb` {number} The default maximum stack size for the thread. 10051cb0ef41Sopenharmony_ci Small values may lead to unusable Worker instances. **Default:** `4`. 10061cb0ef41Sopenharmony_ci * `name` {string} An optional `name` to be appended to the worker title 10071cb0ef41Sopenharmony_ci for debugging/identification purposes, making the final title as 10081cb0ef41Sopenharmony_ci `[worker ${id}] ${name}`. **Default:** `''`. 10091cb0ef41Sopenharmony_ci 10101cb0ef41Sopenharmony_ci### Event: `'error'` 10111cb0ef41Sopenharmony_ci 10121cb0ef41Sopenharmony_ci<!-- YAML 10131cb0ef41Sopenharmony_ciadded: v10.5.0 10141cb0ef41Sopenharmony_ci--> 10151cb0ef41Sopenharmony_ci 10161cb0ef41Sopenharmony_ci* `err` {Error} 10171cb0ef41Sopenharmony_ci 10181cb0ef41Sopenharmony_ciThe `'error'` event is emitted if the worker thread throws an uncaught 10191cb0ef41Sopenharmony_ciexception. In that case, the worker is terminated. 10201cb0ef41Sopenharmony_ci 10211cb0ef41Sopenharmony_ci### Event: `'exit'` 10221cb0ef41Sopenharmony_ci 10231cb0ef41Sopenharmony_ci<!-- YAML 10241cb0ef41Sopenharmony_ciadded: v10.5.0 10251cb0ef41Sopenharmony_ci--> 10261cb0ef41Sopenharmony_ci 10271cb0ef41Sopenharmony_ci* `exitCode` {integer} 10281cb0ef41Sopenharmony_ci 10291cb0ef41Sopenharmony_ciThe `'exit'` event is emitted once the worker has stopped. If the worker 10301cb0ef41Sopenharmony_ciexited by calling [`process.exit()`][], the `exitCode` parameter is the 10311cb0ef41Sopenharmony_cipassed exit code. If the worker was terminated, the `exitCode` parameter is 10321cb0ef41Sopenharmony_ci`1`. 10331cb0ef41Sopenharmony_ci 10341cb0ef41Sopenharmony_ciThis is the final event emitted by any `Worker` instance. 10351cb0ef41Sopenharmony_ci 10361cb0ef41Sopenharmony_ci### Event: `'message'` 10371cb0ef41Sopenharmony_ci 10381cb0ef41Sopenharmony_ci<!-- YAML 10391cb0ef41Sopenharmony_ciadded: v10.5.0 10401cb0ef41Sopenharmony_ci--> 10411cb0ef41Sopenharmony_ci 10421cb0ef41Sopenharmony_ci* `value` {any} The transmitted value 10431cb0ef41Sopenharmony_ci 10441cb0ef41Sopenharmony_ciThe `'message'` event is emitted when the worker thread has invoked 10451cb0ef41Sopenharmony_ci[`require('node:worker_threads').parentPort.postMessage()`][]. 10461cb0ef41Sopenharmony_ciSee the [`port.on('message')`][] event for more details. 10471cb0ef41Sopenharmony_ci 10481cb0ef41Sopenharmony_ciAll messages sent from the worker thread are emitted before the 10491cb0ef41Sopenharmony_ci[`'exit'` event][] is emitted on the `Worker` object. 10501cb0ef41Sopenharmony_ci 10511cb0ef41Sopenharmony_ci### Event: `'messageerror'` 10521cb0ef41Sopenharmony_ci 10531cb0ef41Sopenharmony_ci<!-- YAML 10541cb0ef41Sopenharmony_ciadded: 10551cb0ef41Sopenharmony_ci - v14.5.0 10561cb0ef41Sopenharmony_ci - v12.19.0 10571cb0ef41Sopenharmony_ci--> 10581cb0ef41Sopenharmony_ci 10591cb0ef41Sopenharmony_ci* `error` {Error} An Error object 10601cb0ef41Sopenharmony_ci 10611cb0ef41Sopenharmony_ciThe `'messageerror'` event is emitted when deserializing a message failed. 10621cb0ef41Sopenharmony_ci 10631cb0ef41Sopenharmony_ci### Event: `'online'` 10641cb0ef41Sopenharmony_ci 10651cb0ef41Sopenharmony_ci<!-- YAML 10661cb0ef41Sopenharmony_ciadded: v10.5.0 10671cb0ef41Sopenharmony_ci--> 10681cb0ef41Sopenharmony_ci 10691cb0ef41Sopenharmony_ciThe `'online'` event is emitted when the worker thread has started executing 10701cb0ef41Sopenharmony_ciJavaScript code. 10711cb0ef41Sopenharmony_ci 10721cb0ef41Sopenharmony_ci### `worker.getHeapSnapshot()` 10731cb0ef41Sopenharmony_ci 10741cb0ef41Sopenharmony_ci<!-- YAML 10751cb0ef41Sopenharmony_ciadded: 10761cb0ef41Sopenharmony_ci - v13.9.0 10771cb0ef41Sopenharmony_ci - v12.17.0 10781cb0ef41Sopenharmony_ci--> 10791cb0ef41Sopenharmony_ci 10801cb0ef41Sopenharmony_ci* Returns: {Promise} A promise for a Readable Stream containing 10811cb0ef41Sopenharmony_ci a V8 heap snapshot 10821cb0ef41Sopenharmony_ci 10831cb0ef41Sopenharmony_ciReturns a readable stream for a V8 snapshot of the current state of the Worker. 10841cb0ef41Sopenharmony_ciSee [`v8.getHeapSnapshot()`][] for more details. 10851cb0ef41Sopenharmony_ci 10861cb0ef41Sopenharmony_ciIf the Worker thread is no longer running, which may occur before the 10871cb0ef41Sopenharmony_ci[`'exit'` event][] is emitted, the returned `Promise` is rejected 10881cb0ef41Sopenharmony_ciimmediately with an [`ERR_WORKER_NOT_RUNNING`][] error. 10891cb0ef41Sopenharmony_ci 10901cb0ef41Sopenharmony_ci### `worker.performance` 10911cb0ef41Sopenharmony_ci 10921cb0ef41Sopenharmony_ci<!-- YAML 10931cb0ef41Sopenharmony_ciadded: 10941cb0ef41Sopenharmony_ci - v15.1.0 10951cb0ef41Sopenharmony_ci - v14.17.0 10961cb0ef41Sopenharmony_ci - v12.22.0 10971cb0ef41Sopenharmony_ci--> 10981cb0ef41Sopenharmony_ci 10991cb0ef41Sopenharmony_ciAn object that can be used to query performance information from a worker 11001cb0ef41Sopenharmony_ciinstance. Similar to [`perf_hooks.performance`][]. 11011cb0ef41Sopenharmony_ci 11021cb0ef41Sopenharmony_ci#### `performance.eventLoopUtilization([utilization1[, utilization2]])` 11031cb0ef41Sopenharmony_ci 11041cb0ef41Sopenharmony_ci<!-- YAML 11051cb0ef41Sopenharmony_ciadded: 11061cb0ef41Sopenharmony_ci - v15.1.0 11071cb0ef41Sopenharmony_ci - v14.17.0 11081cb0ef41Sopenharmony_ci - v12.22.0 11091cb0ef41Sopenharmony_ci--> 11101cb0ef41Sopenharmony_ci 11111cb0ef41Sopenharmony_ci* `utilization1` {Object} The result of a previous call to 11121cb0ef41Sopenharmony_ci `eventLoopUtilization()`. 11131cb0ef41Sopenharmony_ci* `utilization2` {Object} The result of a previous call to 11141cb0ef41Sopenharmony_ci `eventLoopUtilization()` prior to `utilization1`. 11151cb0ef41Sopenharmony_ci* Returns {Object} 11161cb0ef41Sopenharmony_ci * `idle` {number} 11171cb0ef41Sopenharmony_ci * `active` {number} 11181cb0ef41Sopenharmony_ci * `utilization` {number} 11191cb0ef41Sopenharmony_ci 11201cb0ef41Sopenharmony_ciThe same call as [`perf_hooks` `eventLoopUtilization()`][], except the values 11211cb0ef41Sopenharmony_ciof the worker instance are returned. 11221cb0ef41Sopenharmony_ci 11231cb0ef41Sopenharmony_ciOne difference is that, unlike the main thread, bootstrapping within a worker 11241cb0ef41Sopenharmony_ciis done within the event loop. So the event loop utilization is 11251cb0ef41Sopenharmony_ciimmediately available once the worker's script begins execution. 11261cb0ef41Sopenharmony_ci 11271cb0ef41Sopenharmony_ciAn `idle` time that does not increase does not indicate that the worker is 11281cb0ef41Sopenharmony_cistuck in bootstrap. The following examples shows how the worker's entire 11291cb0ef41Sopenharmony_cilifetime never accumulates any `idle` time, but is still be able to process 11301cb0ef41Sopenharmony_cimessages. 11311cb0ef41Sopenharmony_ci 11321cb0ef41Sopenharmony_ci```js 11331cb0ef41Sopenharmony_ciconst { Worker, isMainThread, parentPort } = require('node:worker_threads'); 11341cb0ef41Sopenharmony_ci 11351cb0ef41Sopenharmony_ciif (isMainThread) { 11361cb0ef41Sopenharmony_ci const worker = new Worker(__filename); 11371cb0ef41Sopenharmony_ci setInterval(() => { 11381cb0ef41Sopenharmony_ci worker.postMessage('hi'); 11391cb0ef41Sopenharmony_ci console.log(worker.performance.eventLoopUtilization()); 11401cb0ef41Sopenharmony_ci }, 100).unref(); 11411cb0ef41Sopenharmony_ci return; 11421cb0ef41Sopenharmony_ci} 11431cb0ef41Sopenharmony_ci 11441cb0ef41Sopenharmony_ciparentPort.on('message', () => console.log('msg')).unref(); 11451cb0ef41Sopenharmony_ci(function r(n) { 11461cb0ef41Sopenharmony_ci if (--n < 0) return; 11471cb0ef41Sopenharmony_ci const t = Date.now(); 11481cb0ef41Sopenharmony_ci while (Date.now() - t < 300); 11491cb0ef41Sopenharmony_ci setImmediate(r, n); 11501cb0ef41Sopenharmony_ci})(10); 11511cb0ef41Sopenharmony_ci``` 11521cb0ef41Sopenharmony_ci 11531cb0ef41Sopenharmony_ciThe event loop utilization of a worker is available only after the [`'online'` 11541cb0ef41Sopenharmony_cievent][] emitted, and if called before this, or after the [`'exit'` 11551cb0ef41Sopenharmony_cievent][], then all properties have the value of `0`. 11561cb0ef41Sopenharmony_ci 11571cb0ef41Sopenharmony_ci### `worker.postMessage(value[, transferList])` 11581cb0ef41Sopenharmony_ci 11591cb0ef41Sopenharmony_ci<!-- YAML 11601cb0ef41Sopenharmony_ciadded: v10.5.0 11611cb0ef41Sopenharmony_ci--> 11621cb0ef41Sopenharmony_ci 11631cb0ef41Sopenharmony_ci* `value` {any} 11641cb0ef41Sopenharmony_ci* `transferList` {Object\[]} 11651cb0ef41Sopenharmony_ci 11661cb0ef41Sopenharmony_ciSend a message to the worker that is received via 11671cb0ef41Sopenharmony_ci[`require('node:worker_threads').parentPort.on('message')`][]. 11681cb0ef41Sopenharmony_ciSee [`port.postMessage()`][] for more details. 11691cb0ef41Sopenharmony_ci 11701cb0ef41Sopenharmony_ci### `worker.ref()` 11711cb0ef41Sopenharmony_ci 11721cb0ef41Sopenharmony_ci<!-- YAML 11731cb0ef41Sopenharmony_ciadded: v10.5.0 11741cb0ef41Sopenharmony_ci--> 11751cb0ef41Sopenharmony_ci 11761cb0ef41Sopenharmony_ciOpposite of `unref()`, calling `ref()` on a previously `unref()`ed worker does 11771cb0ef41Sopenharmony_ci_not_ let the program exit if it's the only active handle left (the default 11781cb0ef41Sopenharmony_cibehavior). If the worker is `ref()`ed, calling `ref()` again has 11791cb0ef41Sopenharmony_cino effect. 11801cb0ef41Sopenharmony_ci 11811cb0ef41Sopenharmony_ci### `worker.resourceLimits` 11821cb0ef41Sopenharmony_ci 11831cb0ef41Sopenharmony_ci<!-- YAML 11841cb0ef41Sopenharmony_ciadded: 11851cb0ef41Sopenharmony_ci - v13.2.0 11861cb0ef41Sopenharmony_ci - v12.16.0 11871cb0ef41Sopenharmony_ci--> 11881cb0ef41Sopenharmony_ci 11891cb0ef41Sopenharmony_ci* {Object} 11901cb0ef41Sopenharmony_ci * `maxYoungGenerationSizeMb` {number} 11911cb0ef41Sopenharmony_ci * `maxOldGenerationSizeMb` {number} 11921cb0ef41Sopenharmony_ci * `codeRangeSizeMb` {number} 11931cb0ef41Sopenharmony_ci * `stackSizeMb` {number} 11941cb0ef41Sopenharmony_ci 11951cb0ef41Sopenharmony_ciProvides the set of JS engine resource constraints for this Worker thread. 11961cb0ef41Sopenharmony_ciIf the `resourceLimits` option was passed to the [`Worker`][] constructor, 11971cb0ef41Sopenharmony_cithis matches its values. 11981cb0ef41Sopenharmony_ci 11991cb0ef41Sopenharmony_ciIf the worker has stopped, the return value is an empty object. 12001cb0ef41Sopenharmony_ci 12011cb0ef41Sopenharmony_ci### `worker.stderr` 12021cb0ef41Sopenharmony_ci 12031cb0ef41Sopenharmony_ci<!-- YAML 12041cb0ef41Sopenharmony_ciadded: v10.5.0 12051cb0ef41Sopenharmony_ci--> 12061cb0ef41Sopenharmony_ci 12071cb0ef41Sopenharmony_ci* {stream.Readable} 12081cb0ef41Sopenharmony_ci 12091cb0ef41Sopenharmony_ciThis is a readable stream which contains data written to [`process.stderr`][] 12101cb0ef41Sopenharmony_ciinside the worker thread. If `stderr: true` was not passed to the 12111cb0ef41Sopenharmony_ci[`Worker`][] constructor, then data is piped to the parent thread's 12121cb0ef41Sopenharmony_ci[`process.stderr`][] stream. 12131cb0ef41Sopenharmony_ci 12141cb0ef41Sopenharmony_ci### `worker.stdin` 12151cb0ef41Sopenharmony_ci 12161cb0ef41Sopenharmony_ci<!-- YAML 12171cb0ef41Sopenharmony_ciadded: v10.5.0 12181cb0ef41Sopenharmony_ci--> 12191cb0ef41Sopenharmony_ci 12201cb0ef41Sopenharmony_ci* {null|stream.Writable} 12211cb0ef41Sopenharmony_ci 12221cb0ef41Sopenharmony_ciIf `stdin: true` was passed to the [`Worker`][] constructor, this is a 12231cb0ef41Sopenharmony_ciwritable stream. The data written to this stream will be made available in 12241cb0ef41Sopenharmony_cithe worker thread as [`process.stdin`][]. 12251cb0ef41Sopenharmony_ci 12261cb0ef41Sopenharmony_ci### `worker.stdout` 12271cb0ef41Sopenharmony_ci 12281cb0ef41Sopenharmony_ci<!-- YAML 12291cb0ef41Sopenharmony_ciadded: v10.5.0 12301cb0ef41Sopenharmony_ci--> 12311cb0ef41Sopenharmony_ci 12321cb0ef41Sopenharmony_ci* {stream.Readable} 12331cb0ef41Sopenharmony_ci 12341cb0ef41Sopenharmony_ciThis is a readable stream which contains data written to [`process.stdout`][] 12351cb0ef41Sopenharmony_ciinside the worker thread. If `stdout: true` was not passed to the 12361cb0ef41Sopenharmony_ci[`Worker`][] constructor, then data is piped to the parent thread's 12371cb0ef41Sopenharmony_ci[`process.stdout`][] stream. 12381cb0ef41Sopenharmony_ci 12391cb0ef41Sopenharmony_ci### `worker.terminate()` 12401cb0ef41Sopenharmony_ci 12411cb0ef41Sopenharmony_ci<!-- YAML 12421cb0ef41Sopenharmony_ciadded: v10.5.0 12431cb0ef41Sopenharmony_cichanges: 12441cb0ef41Sopenharmony_ci - version: v12.5.0 12451cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/28021 12461cb0ef41Sopenharmony_ci description: This function now returns a Promise. 12471cb0ef41Sopenharmony_ci Passing a callback is deprecated, and was useless up to this 12481cb0ef41Sopenharmony_ci version, as the Worker was actually terminated synchronously. 12491cb0ef41Sopenharmony_ci Terminating is now a fully asynchronous operation. 12501cb0ef41Sopenharmony_ci--> 12511cb0ef41Sopenharmony_ci 12521cb0ef41Sopenharmony_ci* Returns: {Promise} 12531cb0ef41Sopenharmony_ci 12541cb0ef41Sopenharmony_ciStop all JavaScript execution in the worker thread as soon as possible. 12551cb0ef41Sopenharmony_ciReturns a Promise for the exit code that is fulfilled when the 12561cb0ef41Sopenharmony_ci[`'exit'` event][] is emitted. 12571cb0ef41Sopenharmony_ci 12581cb0ef41Sopenharmony_ci### `worker.threadId` 12591cb0ef41Sopenharmony_ci 12601cb0ef41Sopenharmony_ci<!-- YAML 12611cb0ef41Sopenharmony_ciadded: v10.5.0 12621cb0ef41Sopenharmony_ci--> 12631cb0ef41Sopenharmony_ci 12641cb0ef41Sopenharmony_ci* {integer} 12651cb0ef41Sopenharmony_ci 12661cb0ef41Sopenharmony_ciAn integer identifier for the referenced thread. Inside the worker thread, 12671cb0ef41Sopenharmony_ciit is available as [`require('node:worker_threads').threadId`][]. 12681cb0ef41Sopenharmony_ciThis value is unique for each `Worker` instance inside a single process. 12691cb0ef41Sopenharmony_ci 12701cb0ef41Sopenharmony_ci### `worker.unref()` 12711cb0ef41Sopenharmony_ci 12721cb0ef41Sopenharmony_ci<!-- YAML 12731cb0ef41Sopenharmony_ciadded: v10.5.0 12741cb0ef41Sopenharmony_ci--> 12751cb0ef41Sopenharmony_ci 12761cb0ef41Sopenharmony_ciCalling `unref()` on a worker allows the thread to exit if this is the only 12771cb0ef41Sopenharmony_ciactive handle in the event system. If the worker is already `unref()`ed calling 12781cb0ef41Sopenharmony_ci`unref()` again has no effect. 12791cb0ef41Sopenharmony_ci 12801cb0ef41Sopenharmony_ci## Notes 12811cb0ef41Sopenharmony_ci 12821cb0ef41Sopenharmony_ci### Synchronous blocking of stdio 12831cb0ef41Sopenharmony_ci 12841cb0ef41Sopenharmony_ci`Worker`s utilize message passing via {MessagePort} to implement interactions 12851cb0ef41Sopenharmony_ciwith `stdio`. This means that `stdio` output originating from a `Worker` can 12861cb0ef41Sopenharmony_ciget blocked by synchronous code on the receiving end that is blocking the 12871cb0ef41Sopenharmony_ciNode.js event loop. 12881cb0ef41Sopenharmony_ci 12891cb0ef41Sopenharmony_ci```mjs 12901cb0ef41Sopenharmony_ciimport { 12911cb0ef41Sopenharmony_ci Worker, 12921cb0ef41Sopenharmony_ci isMainThread, 12931cb0ef41Sopenharmony_ci} from 'worker_threads'; 12941cb0ef41Sopenharmony_ci 12951cb0ef41Sopenharmony_ciif (isMainThread) { 12961cb0ef41Sopenharmony_ci new Worker(new URL(import.meta.url)); 12971cb0ef41Sopenharmony_ci for (let n = 0; n < 1e10; n++) { 12981cb0ef41Sopenharmony_ci // Looping to simulate work. 12991cb0ef41Sopenharmony_ci } 13001cb0ef41Sopenharmony_ci} else { 13011cb0ef41Sopenharmony_ci // This output will be blocked by the for loop in the main thread. 13021cb0ef41Sopenharmony_ci console.log('foo'); 13031cb0ef41Sopenharmony_ci} 13041cb0ef41Sopenharmony_ci``` 13051cb0ef41Sopenharmony_ci 13061cb0ef41Sopenharmony_ci```cjs 13071cb0ef41Sopenharmony_ci'use strict'; 13081cb0ef41Sopenharmony_ci 13091cb0ef41Sopenharmony_ciconst { 13101cb0ef41Sopenharmony_ci Worker, 13111cb0ef41Sopenharmony_ci isMainThread, 13121cb0ef41Sopenharmony_ci} = require('node:worker_threads'); 13131cb0ef41Sopenharmony_ci 13141cb0ef41Sopenharmony_ciif (isMainThread) { 13151cb0ef41Sopenharmony_ci new Worker(__filename); 13161cb0ef41Sopenharmony_ci for (let n = 0; n < 1e10; n++) { 13171cb0ef41Sopenharmony_ci // Looping to simulate work. 13181cb0ef41Sopenharmony_ci } 13191cb0ef41Sopenharmony_ci} else { 13201cb0ef41Sopenharmony_ci // This output will be blocked by the for loop in the main thread. 13211cb0ef41Sopenharmony_ci console.log('foo'); 13221cb0ef41Sopenharmony_ci} 13231cb0ef41Sopenharmony_ci``` 13241cb0ef41Sopenharmony_ci 13251cb0ef41Sopenharmony_ci### Launching worker threads from preload scripts 13261cb0ef41Sopenharmony_ci 13271cb0ef41Sopenharmony_ciTake care when launching worker threads from preload scripts (scripts loaded 13281cb0ef41Sopenharmony_ciand run using the `-r` command line flag). Unless the `execArgv` option is 13291cb0ef41Sopenharmony_ciexplicitly set, new Worker threads automatically inherit the command line flags 13301cb0ef41Sopenharmony_cifrom the running process and will preload the same preload scripts as the main 13311cb0ef41Sopenharmony_cithread. If the preload script unconditionally launches a worker thread, every 13321cb0ef41Sopenharmony_cithread spawned will spawn another until the application crashes. 13331cb0ef41Sopenharmony_ci 13341cb0ef41Sopenharmony_ci[Addons worker support]: addons.md#worker-support 13351cb0ef41Sopenharmony_ci[ECMAScript module loader]: esm.md#data-imports 13361cb0ef41Sopenharmony_ci[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm 13371cb0ef41Sopenharmony_ci[Signals events]: process.md#signal-events 13381cb0ef41Sopenharmony_ci[Web Workers]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API 13391cb0ef41Sopenharmony_ci[`'close'` event]: #event-close 13401cb0ef41Sopenharmony_ci[`'exit'` event]: #event-exit 13411cb0ef41Sopenharmony_ci[`'online'` event]: #event-online 13421cb0ef41Sopenharmony_ci[`--max-old-space-size`]: cli.md#--max-old-space-sizesize-in-megabytes 13431cb0ef41Sopenharmony_ci[`--max-semi-space-size`]: cli.md#--max-semi-space-sizesize-in-megabytes 13441cb0ef41Sopenharmony_ci[`ArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer 13451cb0ef41Sopenharmony_ci[`AsyncResource`]: async_hooks.md#class-asyncresource 13461cb0ef41Sopenharmony_ci[`Buffer.allocUnsafe()`]: buffer.md#static-method-bufferallocunsafesize 13471cb0ef41Sopenharmony_ci[`Buffer`]: buffer.md 13481cb0ef41Sopenharmony_ci[`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`]: errors.md#err_missing_message_port_in_transfer_list 13491cb0ef41Sopenharmony_ci[`ERR_WORKER_NOT_RUNNING`]: errors.md#err_worker_not_running 13501cb0ef41Sopenharmony_ci[`EventTarget`]: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget 13511cb0ef41Sopenharmony_ci[`FileHandle`]: fs.md#class-filehandle 13521cb0ef41Sopenharmony_ci[`MessagePort`]: #class-messageport 13531cb0ef41Sopenharmony_ci[`SharedArrayBuffer`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer 13541cb0ef41Sopenharmony_ci[`Uint8Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array 13551cb0ef41Sopenharmony_ci[`WebAssembly.Module`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Module 13561cb0ef41Sopenharmony_ci[`Worker constructor options`]: #new-workerfilename-options 13571cb0ef41Sopenharmony_ci[`Worker`]: #class-worker 13581cb0ef41Sopenharmony_ci[`data:` URL]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs 13591cb0ef41Sopenharmony_ci[`fs.close()`]: fs.md#fsclosefd-callback 13601cb0ef41Sopenharmony_ci[`fs.open()`]: fs.md#fsopenpath-flags-mode-callback 13611cb0ef41Sopenharmony_ci[`markAsUntransferable()`]: #workermarkasuntransferableobject 13621cb0ef41Sopenharmony_ci[`node:cluster` module]: cluster.md 13631cb0ef41Sopenharmony_ci[`perf_hooks.performance`]: perf_hooks.md#perf_hooksperformance 13641cb0ef41Sopenharmony_ci[`perf_hooks` `eventLoopUtilization()`]: perf_hooks.md#performanceeventlooputilizationutilization1-utilization2 13651cb0ef41Sopenharmony_ci[`port.on('message')`]: #event-message 13661cb0ef41Sopenharmony_ci[`port.onmessage()`]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/onmessage 13671cb0ef41Sopenharmony_ci[`port.postMessage()`]: #portpostmessagevalue-transferlist 13681cb0ef41Sopenharmony_ci[`process.abort()`]: process.md#processabort 13691cb0ef41Sopenharmony_ci[`process.chdir()`]: process.md#processchdirdirectory 13701cb0ef41Sopenharmony_ci[`process.env`]: process.md#processenv 13711cb0ef41Sopenharmony_ci[`process.execArgv`]: process.md#processexecargv 13721cb0ef41Sopenharmony_ci[`process.exit()`]: process.md#processexitcode 13731cb0ef41Sopenharmony_ci[`process.stderr`]: process.md#processstderr 13741cb0ef41Sopenharmony_ci[`process.stdin`]: process.md#processstdin 13751cb0ef41Sopenharmony_ci[`process.stdout`]: process.md#processstdout 13761cb0ef41Sopenharmony_ci[`process.title`]: process.md#processtitle 13771cb0ef41Sopenharmony_ci[`require('node:worker_threads').isMainThread`]: #workerismainthread 13781cb0ef41Sopenharmony_ci[`require('node:worker_threads').parentPort.on('message')`]: #event-message 13791cb0ef41Sopenharmony_ci[`require('node:worker_threads').parentPort.postMessage()`]: #workerpostmessagevalue-transferlist 13801cb0ef41Sopenharmony_ci[`require('node:worker_threads').parentPort`]: #workerparentport 13811cb0ef41Sopenharmony_ci[`require('node:worker_threads').threadId`]: #workerthreadid 13821cb0ef41Sopenharmony_ci[`require('node:worker_threads').workerData`]: #workerworkerdata 13831cb0ef41Sopenharmony_ci[`trace_events`]: tracing.md 13841cb0ef41Sopenharmony_ci[`v8.getHeapSnapshot()`]: v8.md#v8getheapsnapshot 13851cb0ef41Sopenharmony_ci[`vm`]: vm.md 13861cb0ef41Sopenharmony_ci[`worker.SHARE_ENV`]: #workershare_env 13871cb0ef41Sopenharmony_ci[`worker.on('message')`]: #event-message_1 13881cb0ef41Sopenharmony_ci[`worker.postMessage()`]: #workerpostmessagevalue-transferlist 13891cb0ef41Sopenharmony_ci[`worker.terminate()`]: #workerterminate 13901cb0ef41Sopenharmony_ci[`worker.threadId`]: #workerthreadid_1 13911cb0ef41Sopenharmony_ci[async-resource-worker-pool]: async_context.md#using-asyncresource-for-a-worker-thread-pool 13921cb0ef41Sopenharmony_ci[browser `MessagePort`]: https://developer.mozilla.org/en-US/docs/Web/API/MessagePort 13931cb0ef41Sopenharmony_ci[child processes]: child_process.md 13941cb0ef41Sopenharmony_ci[contextified]: vm.md#what-does-it-mean-to-contextify-an-object 13951cb0ef41Sopenharmony_ci[v8.serdes]: v8.md#serialization-api 1396