Skip to content

Commit 03fbc9e

Browse files
addaleaxtargos
authored andcommitted
worker: rename to worker_threads
PR-URL: #20876 Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Shingo Inoue <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: John-David Dalton <[email protected]> Reviewed-By: Gus Caplan <[email protected]>
1 parent 6caa354 commit 03fbc9e

33 files changed

+71
-66
lines changed

benchmark/fixtures/echo.worker.js

+1-1
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);

benchmark/worker/echo.js

+1-1
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;

doc/api/_toc.md

+1-1
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>

doc/api/all.md

+1-1
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

doc/api/async_hooks.md

+1-1
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

doc/api/process.md

+1-1
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

doc/api/worker.md doc/api/worker_threads.md

+33-29
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

lib/internal/bootstrap/loaders.js

+2-1
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

lib/internal/modules/cjs/helpers.js

+1-1
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

lib/internal/worker.js

+1-1
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') {
File renamed without changes.

node.gyp

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
'lib/util.js',
7979
'lib/v8.js',
8080
'lib/vm.js',
81-
'lib/worker.js',
81+
'lib/worker_threads.js',
8282
'lib/zlib.js',
8383
'lib/internal/assert.js',
8484
'lib/internal/async_hooks.js',

test/common/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Object.defineProperty(exports, 'PORT', {
4848

4949
exports.isMainThread = (() => {
5050
try {
51-
return require('worker').isMainThread;
51+
return require('worker_threads').isMainThread;
5252
} catch {
5353
// Worker module not enabled → only a single main thread exists.
5454
return true;

test/fixtures/worker-script.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import worker from 'worker';
1+
import worker from 'worker_threads';
22

33
worker.parentPort.postMessage('Hello, world!');

test/parallel/test-message-channel-sharedarraybuffer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
const common = require('../common');
55
const assert = require('assert');
6-
const { Worker } = require('worker');
6+
const { Worker } = require('worker_threads');
77

88
{
99
const sharedArrayBuffer = new SharedArrayBuffer(12);
1010
const local = Buffer.from(sharedArrayBuffer);
1111

1212
const w = new Worker(`
13-
const { parentPort } = require('worker');
13+
const { parentPort } = require('worker_threads');
1414
parentPort.on('message', ({ sharedArrayBuffer }) => {
1515
const local = Buffer.from(sharedArrayBuffer);
1616
local.write('world!', 6);

test/parallel/test-message-channel.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33
const common = require('../common');
44
const assert = require('assert');
5-
const { MessageChannel, MessagePort, Worker } = require('worker');
5+
const { MessageChannel, MessagePort, Worker } = require('worker_threads');
66

77
{
88
const channel = new MessageChannel();
@@ -29,9 +29,9 @@ const { MessageChannel, MessagePort, Worker } = require('worker');
2929
const channel = new MessageChannel();
3030

3131
const w = new Worker(`
32-
const { MessagePort } = require('worker');
32+
const { MessagePort } = require('worker_threads');
3333
const assert = require('assert');
34-
require('worker').parentPort.on('message', ({ port }) => {
34+
require('worker_threads').parentPort.on('message', ({ port }) => {
3535
assert(port instanceof MessagePort);
3636
port.postMessage('works');
3737
});

test/parallel/test-message-port-arraybuffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const common = require('../common');
44
const assert = require('assert');
55

6-
const { MessageChannel } = require('worker');
6+
const { MessageChannel } = require('worker_threads');
77

88
{
99
const { port1, port2 } = new MessageChannel();

test/parallel/test-message-port-message-port-transferring.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const common = require('../common');
44
const assert = require('assert');
55

6-
const { MessageChannel } = require('worker');
6+
const { MessageChannel } = require('worker_threads');
77

88
{
99
const { port1: basePort1, port2: basePort2 } = new MessageChannel();

test/parallel/test-message-port.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const common = require('../common');
44
const assert = require('assert');
55

6-
const { MessageChannel, MessagePort } = require('worker');
6+
const { MessageChannel, MessagePort } = require('worker_threads');
77

88
{
99
const { port1, port2 } = new MessageChannel();

test/parallel/test-worker-cleanup-handles.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33
const common = require('../common');
44
const assert = require('assert');
5-
const { Worker, isMainThread, parentPort } = require('worker');
5+
const { Worker, isMainThread, parentPort } = require('worker_threads');
66
const { Server } = require('net');
77
const fs = require('fs');
88

test/parallel/test-worker-dns-terminate.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Flags: --experimental-worker
22
'use strict';
33
const common = require('../common');
4-
const { Worker } = require('worker');
4+
const { Worker } = require('worker_threads');
55

66
const w = new Worker(`
77
const dns = require('dns');
88
dns.lookup('nonexistent.org', () => {});
9-
require('worker').parentPort.postMessage('0');
9+
require('worker_threads').parentPort.postMessage('0');
1010
`, { eval: true });
1111

1212
w.on('message', common.mustCall(() => {

test/parallel/test-worker-esmodule.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const common = require('../common');
44
const fixtures = require('../common/fixtures');
55
const assert = require('assert');
6-
const { Worker } = require('worker');
6+
const { Worker } = require('worker_threads');
77

88
const w = new Worker(fixtures.path('worker-script.mjs'));
99
w.on('message', common.mustCall((message) => {

test/parallel/test-worker-memory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const common = require('../common');
44
const assert = require('assert');
55
const util = require('util');
6-
const { Worker } = require('worker');
6+
const { Worker } = require('worker_threads');
77

88
const numWorkers = +process.env.JOBS || require('os').cpus().length;
99

@@ -13,7 +13,7 @@ function run(n, done) {
1313
if (n <= 0)
1414
return done();
1515
const worker = new Worker(
16-
'require(\'worker\').parentPort.postMessage(2 + 2)',
16+
'require(\'worker_threads\').parentPort.postMessage(2 + 2)',
1717
{ eval: true });
1818
worker.on('message', common.mustCall((value) => {
1919
assert.strictEqual(value, 4);

test/parallel/test-worker-nexttick-terminate.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Flags: --experimental-worker
22
'use strict';
33
const common = require('../common');
4-
const { Worker } = require('worker');
4+
const { Worker } = require('worker_threads');
55

66
// Checks that terminating in the middle of `process.nextTick()` does not
77
// Crash the process.
88

99
const w = new Worker(`
10-
require('worker').parentPort.postMessage('0');
10+
require('worker_threads').parentPort.postMessage('0');
1111
process.nextTick(() => {
1212
while(1);
1313
});

test/parallel/test-worker-stdio.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert');
55
const fs = require('fs');
66
const util = require('util');
77
const { Writable } = require('stream');
8-
const { Worker, isMainThread } = require('worker');
8+
const { Worker, isMainThread } = require('worker_threads');
99

1010
class BufferingWritable extends Writable {
1111
constructor() {

test/parallel/test-worker-syntax-error-file.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const common = require('../common');
44
const fixtures = require('../common/fixtures');
55
const assert = require('assert');
6-
const { Worker } = require('worker');
6+
const { Worker } = require('worker_threads');
77

88
// Do not use isMainThread so that this test itself can be run inside a Worker.
99
if (!process.env.HAS_STARTED_WORKER) {

0 commit comments

Comments
 (0)