Skip to content

Commit 2e30a68

Browse files
addaleaxtargos
authored andcommitted
worker: hide MessagePort init function behind symbol
This reduces unintended exposure of internals. PR-URL: #23037 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f3d09b6 commit 2e30a68

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

lib/internal/worker.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ util.inherits(MessagePort, EventEmitter);
2424
const {
2525
Worker: WorkerImpl,
2626
getEnvMessagePort,
27-
threadId
27+
threadId,
28+
oninit: oninit_symbol
2829
} = internalBinding('worker');
2930

3031
const isMainThread = threadId === 0;
@@ -93,7 +94,7 @@ function oninit() {
9394
setupPortReferencing(this, this, 'message');
9495
}
9596

96-
Object.defineProperty(MessagePort.prototype, 'oninit', {
97+
Object.defineProperty(MessagePort.prototype, oninit_symbol, {
9798
enumerable: true,
9899
writable: false,
99100
value: oninit

src/env.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ struct PackageConfig {
113113
#define PER_ISOLATE_SYMBOL_PROPERTIES(V) \
114114
V(handle_onclose_symbol, "handle_onclose") \
115115
V(owner_symbol, "owner") \
116+
V(oninit_symbol, "oninit") \
116117

117118
// Strings are per-isolate primitives but Environment proxies them
118119
// for the sake of convenience. Strings should be ASCII-only.
@@ -219,7 +220,6 @@ struct PackageConfig {
219220
V(onhandshakedone_string, "onhandshakedone") \
220221
V(onhandshakestart_string, "onhandshakestart") \
221222
V(onheaders_string, "onheaders") \
222-
V(oninit_string, "oninit") \
223223
V(onmessage_string, "onmessage") \
224224
V(onnewsession_string, "onnewsession") \
225225
V(onocspresponse_string, "onocspresponse") \

src/node_messaging.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ MessagePort::MessagePort(Environment* env,
421421
async()->data = static_cast<void*>(this);
422422

423423
Local<Value> fn;
424-
if (!wrap->Get(context, env->oninit_string()).ToLocal(&fn))
424+
if (!wrap->Get(context, env->oninit_symbol()).ToLocal(&fn))
425425
return;
426426

427427
if (fn->IsFunction()) {

src/node_worker.cc

+4
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ void InitWorker(Local<Object> target,
501501
thread_id_string,
502502
Number::New(env->isolate(),
503503
static_cast<double>(env->thread_id()))).FromJust();
504+
Local<String> oninit_string = FIXED_ONE_BYTE_STRING(env->isolate(), "oninit");
505+
target->Set(env->context(),
506+
oninit_string,
507+
env->oninit_symbol()).FromJust();
504508
}
505509

506510
} // anonymous namespace

0 commit comments

Comments
 (0)