diff --git a/benchmark/fixtures/echo.worker.js b/benchmark/fixtures/echo.worker.js index fac5551a66b0fd..167a28ad23e830 100644 --- a/benchmark/fixtures/echo.worker.js +++ b/benchmark/fixtures/echo.worker.js @@ -1,6 +1,6 @@ 'use strict'; -const { parentPort } = require('worker'); +const { parentPort } = require('worker_threads'); parentPort.on('message', (msg) => { parentPort.postMessage(msg); diff --git a/benchmark/worker/echo.js b/benchmark/worker/echo.js index 6ab471a0a8480e..32c4bddf7d5f77 100644 --- a/benchmark/worker/echo.js +++ b/benchmark/worker/echo.js @@ -12,7 +12,7 @@ const bench = common.createBenchmark(main, { const workerPath = path.resolve(__dirname, '..', 'fixtures', 'echo.worker.js'); function main(conf) { - const { Worker } = require('worker'); + const { Worker } = require('worker_threads'); const n = +conf.n; const workers = +conf.workers; diff --git a/doc/api/_toc.md b/doc/api/_toc.md index 1b2fdea26e46eb..e307d52ae8544e 100644 --- a/doc/api/_toc.md +++ b/doc/api/_toc.md @@ -53,7 +53,7 @@ * [Utilities](util.html) * [V8](v8.html) * [VM](vm.html) -* [Worker](worker.html) +* [Worker Threads](worker_threads.html) * [ZLIB](zlib.html)
diff --git a/doc/api/all.md b/doc/api/all.md index 6f0a21dd092105..47216b695d3351 100644 --- a/doc/api/all.md +++ b/doc/api/all.md @@ -46,5 +46,5 @@ @include util @include v8 @include vm -@include worker +@include worker_threads @include zlib diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 29dd5259415ac3..6e5971c51d10c9 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -728,4 +728,4 @@ never be called. [Hook Callbacks]: #async_hooks_hook_callbacks [PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk [promise execution tracking]: #async_hooks_promise_execution_tracking -[`Worker`]: worker.html#worker_worker +[`Worker`]: worker_threads.html#worker_threads_class_worker diff --git a/doc/api/process.md b/doc/api/process.md index 0a9c52a44c3e23..92b39d6da4a97d 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -2018,7 +2018,7 @@ cases: [`ChildProcess`]: child_process.html#child_process_class_childprocess [`Error`]: errors.html#errors_class_error [`EventEmitter`]: events.html#events_class_eventemitter -[`Worker`]: worker.html#worker_worker +[`Worker`]: worker_threads.html#worker_threads_class_worker [`console.error()`]: console.html#console_console_error_data_args [`console.log()`]: console.html#console_console_log_data_args [`domain`]: domain.html diff --git a/doc/api/worker.md b/doc/api/worker_threads.md similarity index 88% rename from doc/api/worker.md rename to doc/api/worker_threads.md index 2fa55cfa2ddadf..2a6384be2cafbb 100644 --- a/doc/api/worker.md +++ b/doc/api/worker_threads.md @@ -1,4 +1,4 @@ -# Worker +# Worker Threads @@ -9,7 +9,7 @@ on independent threads, and to create message channels between them. It can be accessed using: ```js -const worker = require('worker'); +const worker = require('worker_threads'); ``` Workers are useful for performing CPU-intensive JavaScript operations; do not @@ -23,7 +23,9 @@ share memory efficiently by transferring `ArrayBuffer` instances or sharing ## Example ```js -const { Worker, isMainThread, parentPort, workerData } = require('worker'); +const { + Worker, isMainThread, parentPort, workerData +} = require('worker_threads'); if (isMainThread) { module.exports = async function parseJSAsync(script) { @@ -104,7 +106,7 @@ yields an object with `port1` and `port2` properties, which refer to linked [`MessagePort`][] instances. ```js -const { MessageChannel } = require('worker'); +const { MessageChannel } = require('worker_threads'); const { port1, port2 } = new MessageChannel(); port1.on('message', (message) => console.log('received', message)); @@ -241,8 +243,8 @@ Notable differences inside a Worker environment are: - The [`process.stdin`][], [`process.stdout`][] and [`process.stderr`][] may be redirected by the parent thread. -- The [`require('worker').isMainThread`][] property is set to `false`. -- The [`require('worker').parentPort`][] message port is available, +- The [`require('worker_threads').isMainThread`][] property is set to `false`. +- The [`require('worker_threads').parentPort`][] message port is available, - [`process.exit()`][] does not stop the whole program, just the single thread, and [`process.abort()`][] is not available. - [`process.chdir()`][] and `process` methods that set group or user ids @@ -283,7 +285,9 @@ For example: ```js const assert = require('assert'); -const { Worker, MessageChannel, MessagePort, isMainThread } = require('worker'); +const { + Worker, MessageChannel, MessagePort, isMainThread +} = require('worker_threads'); if (isMainThread) { const worker = new Worker(__filename); const subChannel = new MessageChannel(); @@ -292,7 +296,7 @@ if (isMainThread) { console.log('received:', value); }); } else { - require('worker').once('workerMessage', (value) => { + require('worker_threads').once('workerMessage', (value) => { assert(value.hereIsYourPort instanceof MessagePort); value.hereIsYourPort.postMessage('the worker is sending this'); value.hereIsYourPort.close(); @@ -309,9 +313,9 @@ if (isMainThread) { * `eval` {boolean} If true, interpret the first argument to the constructor as a script that is executed once the worker is online. * `data` {any} Any JavaScript value that will be cloned and made - available as [`require('worker').workerData`][]. The cloning will occur as - described in the [HTML structured clone algorithm][], and an error will be - thrown if the object cannot be cloned (e.g. because it contains + available as [`require('worker_threads').workerData`][]. The cloning will + occur as described in the [HTML structured clone algorithm][], and an error + will be thrown if the object cannot be cloned (e.g. because it contains `function`s). * stdin {boolean} If this is set to `true`, then `worker.stdin` will provide a writable stream whose contents will appear as `process.stdin` @@ -351,8 +355,8 @@ added: REPLACEME * `value` {any} The transmitted value The `'message'` event is emitted when the worker thread has invoked -[`require('worker').postMessage()`][]. See the [`port.on('message')`][] event -for more details. +[`require('worker_threads').postMessage()`][]. See the [`port.on('message')`][] +event for more details. ### Event: 'online'