From 333d9f8de326dcc47f705cd43d3de54da4e3ae43 Mon Sep 17 00:00:00 2001 From: Carlos Espa Date: Sat, 4 Jan 2025 13:45:10 +0100 Subject: [PATCH 1/7] src,worker: add isInternalWorker --- doc/api/worker_threads.md | 21 ++++++++++++++++++ lib/internal/worker.js | 2 ++ lib/worker_threads.js | 2 ++ src/node_worker.cc | 19 +++++++++++++--- src/node_worker.h | 5 ++++- .../test-loaders-is-internal-thread.mjs | 22 +++++++++++++++++++ test/fixtures/loader-is-internal-worker.js | 3 +++ 7 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 test/es-module/test-loaders-is-internal-thread.mjs create mode 100644 test/fixtures/loader-is-internal-worker.js diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index c1ac8fdfc7717c..c93a6a4e6987bd 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -100,6 +100,27 @@ if (isMainThread) { } ``` +## `worker.isInternalWorker` + + + +* {boolean} + +Is `true` if this code is running inside of an internal [`Worker`][] thread (e.g a loader thread). + +```js +// loader.js +const { isInternalWorker } = require('node:worker_threads'); + +console.log(isInternalWorker); // Prints 'true'. +``` + +```bash +node --no-warnings --experimental-loader ./loader.js --eval "" +``` + ## `worker.isMainThread`