From 2e30a6897d7eba9d84f3d37b343c40bfa437573d Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 23 Sep 2018 19:10:54 +0200 Subject: [PATCH] worker: hide MessagePort init function behind symbol MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reduces unintended exposure of internals. PR-URL: https://github.com/nodejs/node/pull/23037 Reviewed-By: Gus Caplan Reviewed-By: Michaƫl Zasso Reviewed-By: Luigi Pinca Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- lib/internal/worker.js | 5 +++-- src/env.h | 2 +- src/node_messaging.cc | 2 +- src/node_worker.cc | 4 ++++ 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 4f797dd9e0094c..b5dab224211b13 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -24,7 +24,8 @@ util.inherits(MessagePort, EventEmitter); const { Worker: WorkerImpl, getEnvMessagePort, - threadId + threadId, + oninit: oninit_symbol } = internalBinding('worker'); const isMainThread = threadId === 0; @@ -93,7 +94,7 @@ function oninit() { setupPortReferencing(this, this, 'message'); } -Object.defineProperty(MessagePort.prototype, 'oninit', { +Object.defineProperty(MessagePort.prototype, oninit_symbol, { enumerable: true, writable: false, value: oninit diff --git a/src/env.h b/src/env.h index 9b9135afdccf90..8469f95614c404 100644 --- a/src/env.h +++ b/src/env.h @@ -113,6 +113,7 @@ struct PackageConfig { #define PER_ISOLATE_SYMBOL_PROPERTIES(V) \ V(handle_onclose_symbol, "handle_onclose") \ V(owner_symbol, "owner") \ + V(oninit_symbol, "oninit") \ // Strings are per-isolate primitives but Environment proxies them // for the sake of convenience. Strings should be ASCII-only. @@ -219,7 +220,6 @@ struct PackageConfig { V(onhandshakedone_string, "onhandshakedone") \ V(onhandshakestart_string, "onhandshakestart") \ V(onheaders_string, "onheaders") \ - V(oninit_string, "oninit") \ V(onmessage_string, "onmessage") \ V(onnewsession_string, "onnewsession") \ V(onocspresponse_string, "onocspresponse") \ diff --git a/src/node_messaging.cc b/src/node_messaging.cc index bb7344503e72c5..6dd66f243e8a20 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -421,7 +421,7 @@ MessagePort::MessagePort(Environment* env, async()->data = static_cast(this); Local fn; - if (!wrap->Get(context, env->oninit_string()).ToLocal(&fn)) + if (!wrap->Get(context, env->oninit_symbol()).ToLocal(&fn)) return; if (fn->IsFunction()) { diff --git a/src/node_worker.cc b/src/node_worker.cc index 7825c9b0f54793..6b9657e115562c 100644 --- a/src/node_worker.cc +++ b/src/node_worker.cc @@ -501,6 +501,10 @@ void InitWorker(Local target, thread_id_string, Number::New(env->isolate(), static_cast(env->thread_id()))).FromJust(); + Local oninit_string = FIXED_ONE_BYTE_STRING(env->isolate(), "oninit"); + target->Set(env->context(), + oninit_string, + env->oninit_symbol()).FromJust(); } } // anonymous namespace