Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src,worker: add isInternalWorker #56469

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 24 additions & 7 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,42 @@ if (isMainThread) {
}
```

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

<!-- YAML
added: REPLACEME
-->

* {boolean}

Is `true` if this code is running inside of an internal [`Worker`][] thread (e.g a loader thread).
Is `true` if this code is running inside of an internal [`Worker`][] thread (e.g the loader thread).

```js
```bash
node --experimental-loader ./loader.js main.js
```

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

console.log(isInternalWorker); // Prints 'true'.
```mjs
// loader.js
import { isInternalThread } from 'node:worker_threads';
console.log(isInternalThread); // true
```

```bash
node --no-warnings --experimental-loader ./loader.js --eval ""
```cjs
// main.js
const { isInternalThread } = require('node:worker_threads');
console.log(isInternalThread); // false
```

```mjs
// main.js
import { isInternalThread } from 'node:worker_threads';
console.log(isInternalThread); // false
```

## `worker.isMainThread`
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);
}));
});
});
Loading