From 5afffb4415b6c63525076202fb9490b40af64dcd Mon Sep 17 00:00:00 2001 From: Carlos Espa <43477095+Ceres6@users.noreply.github.com> Date: Tue, 14 Jan 2025 19:24:30 +0100 Subject: [PATCH] src,worker: add isInternalWorker PR-URL: https://github.com/nodejs/node/pull/56469 Reviewed-By: Jacob Smith Reviewed-By: James M Snell Reviewed-By: Bryan English --- doc/api/worker_threads.md | 38 ++++++++++++++++++++++ lib/internal/worker.js | 2 ++ lib/worker_threads.js | 2 ++ src/node_worker.cc | 19 +++++++++-- src/node_worker.h | 5 ++- test/fixtures/loader-is-internal-thread.js | 3 ++ test/fixtures/worker-is-internal-thread.js | 3 ++ test/parallel/test-is-internal-thread.mjs | 36 ++++++++++++++++++++ 8 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/loader-is-internal-thread.js create mode 100644 test/fixtures/worker-is-internal-thread.js create mode 100644 test/parallel/test-is-internal-thread.mjs diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 36a088027edc5f..d9f6e356fd7519 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -100,6 +100,44 @@ if (isMainThread) { } ``` +## `worker.isInternalThread` + + + +* {boolean} + +Is `true` if this code is running inside of an internal [`Worker`][] thread (e.g the loader thread). + +```bash +node --experimental-loader ./loader.js main.js +``` + +```cjs +// loader.js +const { isInternalThread } = require('node:worker_threads'); +console.log(isInternalThread); // true +``` + +```mjs +// loader.js +import { isInternalThread } from 'node:worker_threads'; +console.log(isInternalThread); // true +``` + +```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`