Skip to content

Commit

Permalink
fixup! Update doc/api/worker_threads.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceres6 committed Jan 5, 2025
1 parent 110da20 commit c933aaa
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 33 deletions.
6 changes: 3 additions & 3 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ if (isMainThread) {
}
```

## `worker.isInternalWorker`
## `worker.isInternalThread`

<!-- YAML
added: REPLACEME
Expand All @@ -112,9 +112,9 @@ Is `true` if this code is running inside of an internal [`Worker`][] thread (e.g

```js
// loader.js
const { isInternalWorker } = require('node:worker_threads');
const { isInternalThread } = require('node:worker_threads');

console.log(isInternalWorker); // Prints 'true'.
console.log(isInternalThread); // Prints 'true'.
```

```bash
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const {
const {
ownsProcessState,
isMainThread,
isInternalWorker,
isInternalThread,
resourceLimits: resourceLimitsRaw,
threadId,
Worker: WorkerImpl,
Expand Down Expand Up @@ -539,7 +539,7 @@ module.exports = {
ownsProcessState,
kIsOnline,
isMainThread,
isInternalWorker,
isInternalThread,
SHARE_ENV,
resourceLimits:
!isMainThread ? makeResourceLimits(resourceLimitsRaw) : {},
Expand Down
4 changes: 2 additions & 2 deletions lib/worker_threads.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const {
isMainThread,
isInternalWorker,
isInternalThread,
SHARE_ENV,
resourceLimits,
setEnvironmentData,
Expand Down Expand Up @@ -31,7 +31,7 @@ const {

module.exports = {
isMainThread,
isInternalWorker,
isInternalThread,
MessagePort,
MessageChannel,
markAsUncloneable,
Expand Down
2 changes: 1 addition & 1 deletion src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ void CreateWorkerPerContextProperties(Local<Object> target,
// Set the is_internal property
target
->Set(env->context(),
FIXED_ONE_BYTE_STRING(isolate, "isInternalWorker"),
FIXED_ONE_BYTE_STRING(isolate, "isInternalThread"),
Boolean::New(isolate, is_internal))
.Check();

Expand Down
22 changes: 0 additions & 22 deletions test/es-module/test-loaders-is-internal-thread.mjs

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/loader-is-internal-thread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { isInternalThread } = require('node:worker_threads');

console.log(isInternalThread);
3 changes: 0 additions & 3 deletions test/fixtures/loader-is-internal-worker.js

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/worker-is-internal-thread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { isInternalThread, parentPort } = require('node:worker_threads');

parentPort.postMessage(isInternalThread);
36 changes: 36 additions & 0 deletions test/parallel/test-is-internal-thread.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { spawnPromisified } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import assert from 'node:assert';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';
import { isInternalThread, Worker } from 'node:worker_threads';
import * as common from '../common/index.mjs';

describe('isInternalThread is only true on InternalWorker', { concurrency: !process.env.TEST_PARALLEL }, () => {
it('should be true inside the loader thread', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-loader',
fixtures.fileURL('loader-is-internal-thread.js'),
'--eval',
'setTimeout(() => {},99)',
]);

assert.strictEqual(stderr, '');
assert.match(stdout, /^true\r?\n$/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should be false inside the main thread', async () => {
assert.ok(!isInternalThread);
});

it('should be false inside a regular worker thread', async () => {
const worker = new Worker(fixtures.path('worker-is-internal-thread.js'));

worker.on('message', common.mustCall((isInternalThread) => {
assert.ok(!isInternalThread);
}));
});
});

0 comments on commit c933aaa

Please sign in to comment.