Skip to content

Commit

Permalink
process: move process.features initialization into node.js
Browse files Browse the repository at this point in the history
Use `internalBinding('config')` to shim the legacy
`process.features`.

PR-URL: #25239
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
  • Loading branch information
joyeecheung authored and BridgeAR committed Jan 17, 2019
1 parent c07b12d commit 743056e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 45 deletions.
18 changes: 18 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,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,
Expand Down
44 changes: 0 additions & 44 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -809,49 +809,6 @@ static void OnMessage(Local<Message> message, Local<Value> error) {
}
}

static Local<Object> GetFeatures(Environment* env) {
EscapableHandleScope scope(env->isolate());

Local<Object> obj = Object::New(env->isolate());
#if defined(DEBUG) && DEBUG
Local<Value> debug = True(env->isolate());
#else
Local<Value> 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<Boolean> have_openssl = True(env->isolate());
#else
Local<Boolean> 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<std::string>& args,
const std::vector<std::string>& exec_args) {
Expand Down Expand Up @@ -934,7 +891,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"),
Expand Down
12 changes: 12 additions & 0 deletions src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ static void Initialize(Local<Object> 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.
Expand Down
5 changes: 4 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,11 @@ inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> 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())
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-process-features.js
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 743056e

Please sign in to comment.