From aefe4794cc3d74daaf188f8fcc7f5d9f19ba776e Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Wed, 26 Dec 2018 19:43:15 +0800 Subject: [PATCH] process: move process.features initialization into node.js Use `internalBinding('config')` to shim the legacy `process.features`. PR-URL: https://github.com/nodejs/node/pull/25239 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Minwoo Jung --- lib/internal/bootstrap/node.js | 18 +++++++++++ src/node.cc | 44 -------------------------- src/node_config.cc | 12 +++++++ src/util.h | 5 ++- test/parallel/test-process-features.js | 20 ++++++++++++ 5 files changed, 54 insertions(+), 45 deletions(-) create mode 100644 test/parallel/test-process-features.js diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js index 3c14d4851af308..c709635a755512 100644 --- a/lib/internal/bootstrap/node.js +++ b/lib/internal/bootstrap/node.js @@ -270,6 +270,24 @@ function startup() { 'process.assert() is deprecated. Please use the `assert` module instead.', 'DEP0100'); + // TODO(joyeecheung): this property has not been well-maintained, should we + // deprecate it in favor of a better API? + const { isDebugBuild, hasOpenSSL } = internalBinding('config'); + Object.defineProperty(process, 'features', { + enumerable: true, + writable: false, + configurable: false, + value: { + debug: isDebugBuild, + uv: true, + ipv6: true, // TODO(bnoordhuis) ping libuv + tls_alpn: hasOpenSSL, + tls_sni: hasOpenSSL, + tls_ocsp: hasOpenSSL, + tls: hasOpenSSL + } + }); + const perf = internalBinding('performance'); const { NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE, diff --git a/src/node.cc b/src/node.cc index bc359903be1b7a..edd1ca3cd19d34 100644 --- a/src/node.cc +++ b/src/node.cc @@ -801,49 +801,6 @@ static void OnMessage(Local message, Local error) { } } -static Local GetFeatures(Environment* env) { - EscapableHandleScope scope(env->isolate()); - - Local obj = Object::New(env->isolate()); -#if defined(DEBUG) && DEBUG - Local debug = True(env->isolate()); -#else - Local debug = False(env->isolate()); -#endif // defined(DEBUG) && DEBUG - - obj->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "debug"), - debug).FromJust(); - obj->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "uv"), - True(env->isolate())).FromJust(); - // TODO(bnoordhuis) ping libuv - obj->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "ipv6"), - True(env->isolate())).FromJust(); - -#ifdef HAVE_OPENSSL - Local have_openssl = True(env->isolate()); -#else - Local have_openssl = False(env->isolate()); -#endif - - obj->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "tls_alpn"), - have_openssl).FromJust(); - obj->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "tls_sni"), - have_openssl).FromJust(); - obj->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "tls_ocsp"), - have_openssl).FromJust(); - obj->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "tls"), - have_openssl).FromJust(); - - return scope.Escape(obj); -} - void SetupProcessObject(Environment* env, const std::vector& args, const std::vector& exec_args) { @@ -964,7 +921,6 @@ void SetupProcessObject(Environment* env, READONLY_PROPERTY(process, "pid", Integer::New(env->isolate(), uv_os_getpid())); - READONLY_PROPERTY(process, "features", GetFeatures(env)); CHECK(process->SetAccessor(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "ppid"), diff --git a/src/node_config.cc b/src/node_config.cc index edf738b6337878..7204e246562115 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -26,6 +26,18 @@ static void Initialize(Local target, Environment* env = Environment::GetCurrent(context); Isolate* isolate = env->isolate(); +#if defined(DEBUG) && DEBUG + READONLY_TRUE_PROPERTY(target, "isDebugBuild"); +#else + READONLY_FALSE_PROPERTY(target, "isDebugBuild"); +#endif // defined(DEBUG) && DEBUG + +#if HAVE_OPENSSL + READONLY_TRUE_PROPERTY(target, "hasOpenSSL"); +#else + READONLY_FALSE_PROPERTY(target, "hasOpenSSL"); +#endif // HAVE_OPENSSL + #ifdef NODE_FIPS_MODE READONLY_TRUE_PROPERTY(target, "fipsMode"); // TODO(addaleax): Use options parser variable instead. diff --git a/src/util.h b/src/util.h index d3835c7e692b7d..2db87c18b0d3df 100644 --- a/src/util.h +++ b/src/util.h @@ -546,8 +546,11 @@ inline v8::MaybeLocal ToV8Value(v8::Local context, .FromJust(); \ } while (0) +#define READONLY_FALSE_PROPERTY(obj, name) \ + READONLY_PROPERTY(obj, name, v8::False(isolate)) + #define READONLY_TRUE_PROPERTY(obj, name) \ - READONLY_PROPERTY(obj, name, True(isolate)) + READONLY_PROPERTY(obj, name, v8::True(isolate)) #define READONLY_STRING_PROPERTY(obj, name, str) \ READONLY_PROPERTY(obj, name, ToV8Value(context, str).ToLocalChecked()) diff --git a/test/parallel/test-process-features.js b/test/parallel/test-process-features.js new file mode 100644 index 00000000000000..7752cc53b2a1ca --- /dev/null +++ b/test/parallel/test-process-features.js @@ -0,0 +1,20 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +const keys = new Set(Object.keys(process.features)); + +assert.deepStrictEqual(keys, new Set([ + 'debug', + 'uv', + 'ipv6', + 'tls_alpn', + 'tls_sni', + 'tls_ocsp', + 'tls' +])); + +for (const key of keys) { + assert.strictEqual(typeof process.features[key], 'boolean'); +}