Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,11 @@ Inside a worker, \[`worker_threads.parentPort`]\[] is the port behind
`self.postMessage()` and the worker's `message` events, `isMainThread` is
`false`, and `workerData` is `undefined`.

Because that thread keeps the event loop alive, `Worker` instances also get
the non-standard `ref()` and `unref()` methods of [`node:worker_threads`][]
{Worker}: `worker.unref()` allows the program to exit even while the worker
is still running, and `worker.ref()` restores the default behavior.

As a rule of thumb, use [`node:worker_threads`][] directly when a program
needs `workerData`, a custom `env` or `execArgv`, resource limits, stdio
redirection, the `'online'` and `'exit'` events, or `worker.threadId`;
Expand Down
13 changes: 13 additions & 0 deletions lib/internal/webworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,11 +865,24 @@ class Worker extends EventTarget {
// arguments, and returned the same return value."
this[kWorker]?.postMessage(message, transfer);
}

// The following properties are non-standard, Node.js extensions
ref() {
validateThisInternalField(this, kWorker, 'Worker');
this[kWorker]?.ref();
}

unref() {
validateThisInternalField(this, kWorker, 'Worker');
this[kWorker]?.unref();
}
}

ObjectDefineProperties(Worker.prototype, {
terminate: kEnumerableProperty,
postMessage: kEnumerableProperty,
ref: kEnumerableProperty,
unref: kEnumerableProperty,
[SymbolToStringTag]: {
__proto__: null,
configurable: true,
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/web-worker/echo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

addEventListener('message', (event) => {
postMessage(event.data);
});
34 changes: 34 additions & 0 deletions test/parallel/test-webworker-ref-unref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Flags: --experimental-web-worker
'use strict';

const common = require('../common');
const fixtures = require('../common/fixtures');
const assert = require('assert');

for (const method of ['ref', 'unref']) {
assert.throws(() => Worker.prototype[method].call({}),
{ code: 'ERR_INVALID_THIS' });
}

{
// Both are no-ops on a worker whose script could not be loaded.
const worker = new Worker(fixtures.fileURL('web-worker', 'nonexistent.js').href);
worker.addEventListener('error', common.mustCall());
worker.unref();
worker.ref();
}

const worker = new Worker(fixtures.fileURL('web-worker', 'echo.js').href);

worker.addEventListener('error', common.mustNotCall('worker failed'));
worker.addEventListener('message', common.mustCall(({ data }) => {
assert.strictEqual(data, 'hello');
worker.terminate();
}));

process.once('beforeExit', common.mustCall(() => {
worker.ref();
worker.postMessage('hello');
}));

worker.unref();
Loading