Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 03fbc9e

Browse filesBrowse files
addaleaxtargos
authored andcommitted
worker: rename to worker_threads
PR-URL: #20876 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
1 parent 6caa354 commit 03fbc9e
Copy full SHA for 03fbc9e
Expand file treeCollapse file tree

33 files changed

+71
-66
lines changed
Open diff view settings
Collapse file

‎benchmark/fixtures/echo.worker.js‎

Copy file name to clipboardExpand all lines: benchmark/fixtures/echo.worker.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { parentPort } = require('worker');
3+
const { parentPort } = require('worker_threads');
44

55
parentPort.on('message', (msg) => {
66
parentPort.postMessage(msg);
Collapse file

‎benchmark/worker/echo.js‎

Copy file name to clipboardExpand all lines: benchmark/worker/echo.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const bench = common.createBenchmark(main, {
1212
const workerPath = path.resolve(__dirname, '..', 'fixtures', 'echo.worker.js');
1313

1414
function main(conf) {
15-
const { Worker } = require('worker');
15+
const { Worker } = require('worker_threads');
1616

1717
const n = +conf.n;
1818
const workers = +conf.workers;
Collapse file

‎doc/api/_toc.md‎

Copy file name to clipboardExpand all lines: doc/api/_toc.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* [Utilities](util.html)
5454
* [V8](v8.html)
5555
* [VM](vm.html)
56-
* [Worker](worker.html)
56+
* [Worker Threads](worker_threads.html)
5757
* [ZLIB](zlib.html)
5858

5959
<div class="line"></div>
Collapse file

‎doc/api/all.md‎

Copy file name to clipboardExpand all lines: doc/api/all.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@
4646
@include util
4747
@include v8
4848
@include vm
49-
@include worker
49+
@include worker_threads
5050
@include zlib
Collapse file

‎doc/api/async_hooks.md‎

Copy file name to clipboardExpand all lines: doc/api/async_hooks.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -728,4 +728,4 @@ never be called.
728728
[Hook Callbacks]: #async_hooks_hook_callbacks
729729
[PromiseHooks]: https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk
730730
[promise execution tracking]: #async_hooks_promise_execution_tracking
731-
[`Worker`]: worker.html#worker_worker
731+
[`Worker`]: worker_threads.html#worker_threads_class_worker
Collapse file

‎doc/api/process.md‎

Copy file name to clipboardExpand all lines: doc/api/process.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,7 @@ cases:
20182018
[`ChildProcess`]: child_process.html#child_process_class_childprocess
20192019
[`Error`]: errors.html#errors_class_error
20202020
[`EventEmitter`]: events.html#events_class_eventemitter
2021-
[`Worker`]: worker.html#worker_worker
2021+
[`Worker`]: worker_threads.html#worker_threads_class_worker
20222022
[`console.error()`]: console.html#console_console_error_data_args
20232023
[`console.log()`]: console.html#console_console_log_data_args
20242024
[`domain`]: domain.html
Collapse file
+33-29Lines changed: 33 additions & 29 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Worker
1+
# Worker Threads
22

33
<!--introduced_in=REPLACEME-->
44

@@ -9,7 +9,7 @@ on independent threads, and to create message channels between them. It
99
can be accessed using:
1010

1111
```js
12-
const worker = require('worker');
12+
const worker = require('worker_threads');
1313
```
1414

1515
Workers are useful for performing CPU-intensive JavaScript operations; do not
@@ -23,7 +23,9 @@ share memory efficiently by transferring `ArrayBuffer` instances or sharing
2323
## Example
2424

2525
```js
26-
const { Worker, isMainThread, parentPort, workerData } = require('worker');
26+
const {
27+
Worker, isMainThread, parentPort, workerData
28+
} = require('worker_threads');
2729

2830
if (isMainThread) {
2931
module.exports = async function parseJSAsync(script) {
@@ -104,7 +106,7 @@ yields an object with `port1` and `port2` properties, which refer to linked
104106
[`MessagePort`][] instances.
105107

106108
```js
107-
const { MessageChannel } = require('worker');
109+
const { MessageChannel } = require('worker_threads');
108110

109111
const { port1, port2 } = new MessageChannel();
110112
port1.on('message', (message) => console.log('received', message));
@@ -241,8 +243,8 @@ Notable differences inside a Worker environment are:
241243

242244
- The [`process.stdin`][], [`process.stdout`][] and [`process.stderr`][]
243245
may be redirected by the parent thread.
244-
- The [`require('worker').isMainThread`][] property is set to `false`.
245-
- The [`require('worker').parentPort`][] message port is available,
246+
- The [`require('worker_threads').isMainThread`][] property is set to `false`.
247+
- The [`require('worker_threads').parentPort`][] message port is available,
246248
- [`process.exit()`][] does not stop the whole program, just the single thread,
247249
and [`process.abort()`][] is not available.
248250
- [`process.chdir()`][] and `process` methods that set group or user ids
@@ -283,7 +285,9 @@ For example:
283285

284286
```js
285287
const assert = require('assert');
286-
const { Worker, MessageChannel, MessagePort, isMainThread } = require('worker');
288+
const {
289+
Worker, MessageChannel, MessagePort, isMainThread
290+
} = require('worker_threads');
287291
if (isMainThread) {
288292
const worker = new Worker(__filename);
289293
const subChannel = new MessageChannel();
@@ -292,7 +296,7 @@ if (isMainThread) {
292296
console.log('received:', value);
293297
});
294298
} else {
295-
require('worker').once('workerMessage', (value) => {
299+
require('worker_threads').once('workerMessage', (value) => {
296300
assert(value.hereIsYourPort instanceof MessagePort);
297301
value.hereIsYourPort.postMessage('the worker is sending this');
298302
value.hereIsYourPort.close();
@@ -309,9 +313,9 @@ if (isMainThread) {
309313
* `eval` {boolean} If true, interpret the first argument to the constructor
310314
as a script that is executed once the worker is online.
311315
* `data` {any} Any JavaScript value that will be cloned and made
312-
available as [`require('worker').workerData`][]. The cloning will occur as
313-
described in the [HTML structured clone algorithm][], and an error will be
314-
thrown if the object cannot be cloned (e.g. because it contains
316+
available as [`require('worker_threads').workerData`][]. The cloning will
317+
occur as described in the [HTML structured clone algorithm][], and an error
318+
will be thrown if the object cannot be cloned (e.g. because it contains
315319
`function`s).
316320
* stdin {boolean} If this is set to `true`, then `worker.stdin` will
317321
provide a writable stream whose contents will appear as `process.stdin`
@@ -351,8 +355,8 @@ added: REPLACEME
351355
* `value` {any} The transmitted value
352356

353357
The `'message'` event is emitted when the worker thread has invoked
354-
[`require('worker').postMessage()`][]. See the [`port.on('message')`][] event
355-
for more details.
358+
[`require('worker_threads').postMessage()`][]. See the [`port.on('message')`][]
359+
event for more details.
356360

357361
### Event: 'online'
358362
<!-- YAML
@@ -371,8 +375,8 @@ added: REPLACEME
371375
* `transferList` {Object[]}
372376

373377
Send a message to the worker that will be received via
374-
[`require('worker').on('workerMessage')`][]. See [`port.postMessage()`][] for
375-
more details.
378+
[`require('worker_threads').on('workerMessage')`][].
379+
See [`port.postMessage()`][] for more details.
376380

377381
### worker.ref()
378382
<!-- YAML
@@ -444,7 +448,7 @@ added: REPLACEME
444448
* {integer}
445449

446450
An integer identifier for the referenced thread. Inside the worker thread,
447-
it is available as [`require('worker').threadId`][].
451+
it is available as [`require('worker_threads').threadId`][].
448452

449453
### worker.unref()
450454
<!-- YAML
@@ -457,14 +461,14 @@ active handle in the event system. If the worker is already `unref()`ed calling
457461

458462
[`Buffer`]: buffer.html
459463
[`EventEmitter`]: events.html
460-
[`MessagePort`]: #worker_class_messageport
461-
[`port.postMessage()`]: #worker_port_postmessage_value_transferlist
462-
[`Worker`]: #worker_class_worker
463-
[`worker.terminate()`]: #worker_worker_terminate_callback
464-
[`worker.postMessage()`]: #worker_worker_postmessage_value_transferlist_1
465-
[`worker.on('message')`]: #worker_event_message_1
466-
[`worker.threadId`]: #worker_worker_threadid_1
467-
[`port.on('message')`]: #worker_event_message
464+
[`MessagePort`]: #worker_threads_class_messageport
465+
[`port.postMessage()`]: #worker_threads_port_postmessage_value_transferlist
466+
[`Worker`]: #worker_threads_class_worker
467+
[`worker.terminate()`]: #worker_threads_worker_terminate_callback
468+
[`worker.postMessage()`]: #worker_threads_worker_postmessage_value_transferlist_1
469+
[`worker.on('message')`]: #worker_threads_event_message_1
470+
[`worker.threadId`]: #worker_threads_worker_threadid_1
471+
[`port.on('message')`]: #worker_threads_event_message
468472
[`process.exit()`]: process.html#process_process_exit_code
469473
[`process.abort()`]: process.html#process_process_abort
470474
[`process.chdir()`]: process.html#process_process_chdir_directory
@@ -473,11 +477,11 @@ active handle in the event system. If the worker is already `unref()`ed calling
473477
[`process.stderr`]: process.html#process_process_stderr
474478
[`process.stdout`]: process.html#process_process_stdout
475479
[`process.title`]: process.html#process_process_title
476-
[`require('worker').workerData`]: #worker_worker_workerdata
477-
[`require('worker').on('workerMessage')`]: #worker_event_workermessage
478-
[`require('worker').postMessage()`]: #worker_worker_postmessage_value_transferlist
479-
[`require('worker').isMainThread`]: #worker_worker_ismainthread
480-
[`require('worker').threadId`]: #worker_worker_threadid
480+
[`require('worker_threads').workerData`]: #worker_threads_worker_workerdata
481+
[`require('worker_threads').on('workerMessage')`]: #worker_threads_event_workermessage
482+
[`require('worker_threads').postMessage()`]: #worker_threads_worker_postmessage_value_transferlist
483+
[`require('worker_threads').isMainThread`]: #worker_threads_worker_ismainthread
484+
[`require('worker_threads').threadId`]: #worker_threads_worker_threadid
481485
[`cluster` module]: cluster.html
482486
[`inspector`]: inspector.html
483487
[v8.serdes]: v8.html#v8_serialization_api
Collapse file

‎lib/internal/bootstrap/loaders.js‎

Copy file name to clipboardExpand all lines: lib/internal/bootstrap/loaders.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@
195195

196196
NativeModule.isInternal = function(id) {
197197
return id.startsWith('internal/') ||
198-
(id === 'worker' && !process.binding('config').experimentalWorker);
198+
(id === 'worker_threads' &&
199+
!process.binding('config').experimentalWorker);
199200
};
200201
}
201202

Collapse file

‎lib/internal/modules/cjs/helpers.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/helpers.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const builtinLibs = [
106106
];
107107

108108
if (process.binding('config').experimentalWorker) {
109-
builtinLibs.push('worker');
109+
builtinLibs.push('worker_threads');
110110
builtinLibs.sort();
111111
}
112112

Collapse file

‎lib/internal/worker.js‎

Copy file name to clipboardExpand all lines: lib/internal/worker.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ function setupChild(evalScript) {
387387
debug(`[${threadId}] is setting up worker child environment`);
388388
const port = getEnvMessagePort();
389389

390-
const publicWorker = require('worker');
390+
const publicWorker = require('worker_threads');
391391

392392
port.on('message', (message) => {
393393
if (message.type === 'loadScript') {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.