From 3090def6354af329d16e36a9be0369a4d0fd2629 Mon Sep 17 00:00:00 2001 From: theanarkh Date: Tue, 5 Aug 2025 21:45:41 +0800 Subject: [PATCH] worker: add name for worker PR-URL: https://github.com/nodejs/node/pull/59213 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- doc/api/worker_threads.md | 24 ++++++++++++++++++++++++ lib/internal/worker.js | 8 ++++++++ lib/worker_threads.js | 2 ++ src/api/environment.cc | 22 +++++++++++++++++++++- src/env-inl.h | 4 ++++ src/env.cc | 6 ++++-- src/env.h | 5 ++++- src/env_properties.h | 1 + src/node.h | 10 ++++++++++ src/node_worker.cc | 23 ++++++++++++++++++++++- test/parallel/test-worker-thread-name.js | 18 ++++++++++++++++++ 11 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-worker-thread-name.js diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 9a6093e45f..5cae55796c 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -721,6 +721,17 @@ An integer identifier for the current thread. On the corresponding worker object (if there is any), it is available as [`worker.threadId`][]. This value is unique for each [`Worker`][] instance inside a single process. +## `worker.threadName` + + + +* {string|null} + +A string identifier for the current thread or null if the thread is not running. +On the corresponding worker object (if there is any), it is available as [`worker.threadName`][]. + ## `worker.workerData` + +* {string|null} + +A string identifier for the referenced thread or null if the thread is not running. +Inside the worker thread, it is available as [`require('node:worker_threads').threadName`][]. + ### `worker.unref()`