Skip to content

Commit 4e4caba

Browse files
committed
worker: add support for worker title prefix
Fixes: nodejs#41589
1 parent 42be7f6 commit 4e4caba

10 files changed

+49
-24
lines changed

lib/internal/worker.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const {
5757
const { deserializeError } = require('internal/error_serdes');
5858
const { fileURLToPath, isURLInstance, pathToFileURL } = require('internal/url');
5959
const { kEmptyObject } = require('internal/util');
60-
const { validateArray } = require('internal/validators');
60+
const { validateArray, validateString } = require('internal/validators');
6161

6262
const {
6363
ownsProcessState,
@@ -188,12 +188,19 @@ class Worker extends EventEmitter {
188188
options.env);
189189
}
190190

191+
let title_prefix = '';
192+
if (options.titlePrefix) {
193+
validateString(options.titlePrefix, 'options.titlePrefix');
194+
title_prefix = options.titlePrefix;
195+
}
196+
191197
// Set up the C++ handle for the worker, as well as some internal wiring.
192198
this[kHandle] = new WorkerImpl(url,
193199
env === process.env ? null : env,
194200
options.execArgv,
195201
parseResourceLimits(options.resourceLimits),
196-
!!(options.trackUnmanagedFds ?? true));
202+
!!(options.trackUnmanagedFds ?? true),
203+
title_prefix);
197204
if (this[kHandle].invalidExecArgv) {
198205
throw new ERR_WORKER_INVALID_EXEC_ARGV(this[kHandle].invalidExecArgv);
199206
}

src/api/environment.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,13 @@ void FreeEnvironment(Environment* env) {
508508
NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
509509
Environment* env,
510510
ThreadId thread_id,
511-
const char* url) {
511+
const char* url,
512+
const char* title_prefix) {
512513
CHECK_NOT_NULL(env);
513514
CHECK_NE(thread_id.id, static_cast<uint64_t>(-1));
514515
#if HAVE_INSPECTOR
515516
return std::make_unique<InspectorParentHandleImpl>(
516-
env->inspector_agent()->GetParentHandle(thread_id.id, url));
517+
env->inspector_agent()->GetParentHandle(thread_id.id, url, title_prefix));
517518
#else
518519
return {};
519520
#endif

src/inspector/worker_inspector.cc

+12-9
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ class WorkerStartedRequest : public Request {
1414
uint64_t id,
1515
const std::string& url,
1616
std::shared_ptr<node::inspector::MainThreadHandle> worker_thread,
17-
bool waiting)
17+
bool waiting,
18+
const std::string& title_prefix)
1819
: id_(id),
19-
info_(BuildWorkerTitle(id), url, worker_thread),
20+
info_(BuildWorkerTitle(id, title_prefix), url, worker_thread),
2021
waiting_(waiting) {}
2122
void Call(MainThreadInterface* thread) override {
2223
auto manager = thread->inspector_agent()->GetWorkerManager();
2324
manager->WorkerStarted(id_, info_, waiting_);
2425
}
2526

2627
private:
27-
static std::string BuildWorkerTitle(int id) {
28-
return "Worker " + std::to_string(id);
28+
static std::string BuildWorkerTitle(int id, const std::string& title_prefix) {
29+
return title_prefix + "Worker " + std::to_string(id);
2930
}
3031

3132
uint64_t id_;
@@ -57,11 +58,13 @@ ParentInspectorHandle::ParentInspectorHandle(
5758
uint64_t id,
5859
const std::string& url,
5960
std::shared_ptr<MainThreadHandle> parent_thread,
60-
bool wait_for_connect)
61+
bool wait_for_connect,
62+
const std::string& title_prefix)
6163
: id_(id),
6264
url_(url),
6365
parent_thread_(parent_thread),
64-
wait_(wait_for_connect) {}
66+
wait_(wait_for_connect),
67+
title_prefix_(title_prefix) {}
6568

6669
ParentInspectorHandle::~ParentInspectorHandle() {
6770
parent_thread_->Post(
@@ -71,7 +74,7 @@ ParentInspectorHandle::~ParentInspectorHandle() {
7174
void ParentInspectorHandle::WorkerStarted(
7275
std::shared_ptr<MainThreadHandle> worker_thread, bool waiting) {
7376
std::unique_ptr<Request> request(
74-
new WorkerStartedRequest(id_, url_, worker_thread, waiting));
77+
new WorkerStartedRequest(id_, url_, worker_thread, waiting, title_prefix_));
7578
parent_thread_->Post(std::move(request));
7679
}
7780

@@ -97,9 +100,9 @@ void WorkerManager::WorkerStarted(uint64_t session_id,
97100
}
98101

99102
std::unique_ptr<ParentInspectorHandle> WorkerManager::NewParentHandle(
100-
uint64_t thread_id, const std::string& url) {
103+
uint64_t thread_id, const std::string& url, const std::string& title_prefix) {
101104
bool wait = !delegates_waiting_on_start_.empty();
102-
return std::make_unique<ParentInspectorHandle>(thread_id, url, thread_, wait);
105+
return std::make_unique<ParentInspectorHandle>(thread_id, url, thread_, wait, title_prefix);
103106
}
104107

105108
void WorkerManager::RemoveAttachDelegate(int id) {

src/inspector/worker_inspector.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ class ParentInspectorHandle {
5656
ParentInspectorHandle(uint64_t id,
5757
const std::string& url,
5858
std::shared_ptr<MainThreadHandle> parent_thread,
59-
bool wait_for_connect);
59+
bool wait_for_connect,
60+
const std::string& title_prefix);
6061
~ParentInspectorHandle();
6162
std::unique_ptr<ParentInspectorHandle> NewParentInspectorHandle(
62-
uint64_t thread_id, const std::string& url) {
63+
uint64_t thread_id, const std::string& url, const std::string& title_prefix) {
6364
return std::make_unique<ParentInspectorHandle>(thread_id,
6465
url,
6566
parent_thread_,
66-
wait_);
67+
wait_,
68+
title_prefix);
6769
}
6870
void WorkerStarted(std::shared_ptr<MainThreadHandle> worker_thread,
6971
bool waiting);
@@ -80,6 +82,7 @@ class ParentInspectorHandle {
8082
std::string url_;
8183
std::shared_ptr<MainThreadHandle> parent_thread_;
8284
bool wait_;
85+
std::string title_prefix_;
8386
};
8487

8588
class WorkerManager : public std::enable_shared_from_this<WorkerManager> {
@@ -88,7 +91,7 @@ class WorkerManager : public std::enable_shared_from_this<WorkerManager> {
8891
: thread_(thread) {}
8992

9093
std::unique_ptr<ParentInspectorHandle> NewParentHandle(
91-
uint64_t thread_id, const std::string& url);
94+
uint64_t thread_id, const std::string& url, const std::string& title_prefix);
9295
void WorkerStarted(uint64_t session_id, const WorkerInfo& info, bool waiting);
9396
void WorkerFinished(uint64_t session_id);
9497
std::unique_ptr<WorkerManagerEventHandle> SetAutoAttach(

src/inspector_agent.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -952,17 +952,17 @@ void Agent::SetParentHandle(
952952
}
953953

954954
std::unique_ptr<ParentInspectorHandle> Agent::GetParentHandle(
955-
uint64_t thread_id, const std::string& url) {
955+
uint64_t thread_id, const std::string& url, const std::string& title_prefix) {
956956
if (!parent_env_->should_create_inspector() && !client_) {
957957
ThrowUninitializedInspectorError(parent_env_);
958958
return std::unique_ptr<ParentInspectorHandle>{};
959959
}
960960

961961
CHECK_NOT_NULL(client_);
962962
if (!parent_handle_) {
963-
return client_->getWorkerManager()->NewParentHandle(thread_id, url);
963+
return client_->getWorkerManager()->NewParentHandle(thread_id, url, title_prefix);
964964
} else {
965-
return parent_handle_->NewParentInspectorHandle(thread_id, url);
965+
return parent_handle_->NewParentInspectorHandle(thread_id, url, title_prefix);
966966
}
967967
}
968968

src/inspector_agent.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Agent {
8282

8383
void SetParentHandle(std::unique_ptr<ParentInspectorHandle> parent_handle);
8484
std::unique_ptr<ParentInspectorHandle> GetParentHandle(
85-
uint64_t thread_id, const std::string& url);
85+
uint64_t thread_id, const std::string& url, const std::string& title_prefix);
8686

8787
// Called to create inspector sessions that can be used from the same thread.
8888
// The inspector responds by using the delegate to send messages back.

src/node.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,8 @@ NODE_EXTERN Environment* CreateEnvironment(
677677
NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle(
678678
Environment* parent_env,
679679
ThreadId child_thread_id,
680-
const char* child_url);
680+
const char* child_url,
681+
const char* title_prefix);
681682

682683
struct StartExecutionCallbackInfo {
683684
v8::Local<v8::Object> process_object;

src/node_worker.cc

+10-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ constexpr double kMB = 1024 * 1024;
4949
Worker::Worker(Environment* env,
5050
Local<Object> wrap,
5151
const std::string& url,
52+
const std::string& title_prefix,
5253
std::shared_ptr<PerIsolateOptions> per_isolate_opts,
5354
std::vector<std::string>&& exec_argv,
5455
std::shared_ptr<KVStore> env_vars,
@@ -83,7 +84,7 @@ Worker::Worker(Environment* env,
8384
.Check();
8485

8586
inspector_parent_handle_ = GetInspectorParentHandle(
86-
env, thread_id_, url.c_str());
87+
env, thread_id_, url.c_str(), title_prefix.c_str());
8788

8889
argv_ = std::vector<std::string>{env->argv()[0]};
8990
// Mark this Worker object as weak until we actually start the thread.
@@ -467,6 +468,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
467468
}
468469

469470
std::string url;
471+
std::string title_prefix;
470472
std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr;
471473
std::shared_ptr<KVStore> env_vars = nullptr;
472474

@@ -479,6 +481,12 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
479481
url.append(value.out(), value.length());
480482
}
481483

484+
if (!args[5]->IsNullOrUndefined()) {
485+
Utf8Value value(
486+
isolate, args[5]->ToString(env->context()).FromMaybe(Local<String>()));
487+
title_prefix.append(value.out(), value.length());
488+
}
489+
482490
if (args[1]->IsNull()) {
483491
// Means worker.env = { ...process.env }.
484492
env_vars = env->env_vars()->Clone(isolate);
@@ -589,6 +597,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
589597
Worker* worker = new Worker(env,
590598
args.This(),
591599
url,
600+
title_prefix,
592601
per_isolate_opts,
593602
std::move(exec_argv_out),
594603
env_vars,

src/node_worker.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Worker : public AsyncWrap {
3030
Worker(Environment* env,
3131
v8::Local<v8::Object> wrap,
3232
const std::string& url,
33+
const std::string& title_prefix,
3334
std::shared_ptr<PerIsolateOptions> per_isolate_opts,
3435
std::vector<std::string>&& exec_argv,
3536
std::shared_ptr<KVStore> env_vars,

test/cctest/test_environment.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ TEST_F(EnvironmentTest, InspectorMultipleEmbeddedEnvironments) {
449449
ChildEnvironmentData data;
450450
data.thread_id = node::AllocateEnvironmentThreadId();
451451
data.inspector_parent_handle =
452-
GetInspectorParentHandle(*env, data.thread_id, "file:///embedded.js");
452+
GetInspectorParentHandle(*env, data.thread_id, "file:///embedded.js", "");
453453
CHECK(data.inspector_parent_handle);
454454
data.platform = GetMultiIsolatePlatform(*env);
455455
CHECK_NOT_NULL(data.platform);

0 commit comments

Comments
 (0)