src: move version metadata into node_metadata{.h, .cc}

This patch moves the computation of version metadata from node.cc
into node_metadata{.h, .cc}, and creates a macro that can be
used to iterate over the available version keys (v8, uv, .etc).
This makes the code clearer as now we no longer need to add
all the headers in node.cc just to compute the versions, and
makes it easier to reuse the version definitions.

PR-URL: https://github.com/nodejs/node/pull/24774
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Joyee Cheung 2018-12-02 04:16:09 +08:00 committed by Rich Trott
parent 77618817d0
commit d17d7bdf4e
4 changed files with 117 additions and 90 deletions

View File

@ -359,6 +359,7 @@
'src/node_http2.cc', 'src/node_http2.cc',
'src/node_i18n.cc', 'src/node_i18n.cc',
'src/node_messaging.cc', 'src/node_messaging.cc',
'src/node_metadata.cc',
'src/node_native_module.cc', 'src/node_native_module.cc',
'src/node_options.cc', 'src/node_options.cc',
'src/node_os.cc', 'src/node_os.cc',
@ -430,6 +431,7 @@
'src/node_i18n.h', 'src/node_i18n.h',
'src/node_internals.h', 'src/node_internals.h',
'src/node_messaging.h', 'src/node_messaging.h',
'src/node_metadata.h',
'src/node_mutex.h', 'src/node_mutex.h',
'src/node_native_module.h', 'src/node_native_module.h',
'src/node_object_wrap.h', 'src/node_object_wrap.h',

View File

@ -25,6 +25,7 @@
#include "node_context_data.h" #include "node_context_data.h"
#include "node_errors.h" #include "node_errors.h"
#include "node_internals.h" #include "node_internals.h"
#include "node_metadata.h"
#include "node_native_module.h" #include "node_native_module.h"
#include "node_perf.h" #include "node_perf.h"
#include "node_platform.h" #include "node_platform.h"
@ -48,16 +49,9 @@
#include "node_dtrace.h" #include "node_dtrace.h"
#endif #endif
#include "ares.h"
#include "async_wrap-inl.h" #include "async_wrap-inl.h"
#include "env-inl.h" #include "env-inl.h"
#include "handle_wrap.h" #include "handle_wrap.h"
#ifdef NODE_EXPERIMENTAL_HTTP
# include "llhttp.h"
#else /* !NODE_EXPERIMENTAL_HTTP */
# include "http_parser.h"
#endif /* NODE_EXPERIMENTAL_HTTP */
#include "nghttp2/nghttp2ver.h"
#include "req_wrap-inl.h" #include "req_wrap-inl.h"
#include "string_bytes.h" #include "string_bytes.h"
#include "tracing/agent.h" #include "tracing/agent.h"
@ -68,7 +62,6 @@
#include "libplatform/libplatform.h" #include "libplatform/libplatform.h"
#endif // NODE_USE_V8_PLATFORM #endif // NODE_USE_V8_PLATFORM
#include "v8-profiler.h" #include "v8-profiler.h"
#include "zlib.h"
#ifdef NODE_ENABLE_VTUNE_PROFILING #ifdef NODE_ENABLE_VTUNE_PROFILING
#include "../deps/v8/src/third_party/vtune/v8-vtune.h" #include "../deps/v8/src/third_party/vtune/v8-vtune.h"
@ -156,22 +149,6 @@ using v8::Value;
static bool v8_is_profiling = false; static bool v8_is_profiling = false;
#ifdef NODE_EXPERIMENTAL_HTTP
static const char llhttp_version[] =
NODE_STRINGIFY(LLHTTP_VERSION_MAJOR)
"."
NODE_STRINGIFY(LLHTTP_VERSION_MINOR)
"."
NODE_STRINGIFY(LLHTTP_VERSION_PATCH);
#else /* !NODE_EXPERIMENTAL_HTTP */
static const char http_parser_version[] =
NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR)
"."
NODE_STRINGIFY(HTTP_PARSER_VERSION_MINOR)
"."
NODE_STRINGIFY(HTTP_PARSER_VERSION_PATCH);
#endif /* NODE_EXPERIMENTAL_HTTP */
// Bit flag used to track security reverts (see node_revert.h) // Bit flag used to track security reverts (see node_revert.h)
unsigned int reverted = 0; unsigned int reverted = 0;
@ -210,27 +187,12 @@ class NodeTraceStateObserver :
auto trace_process = tracing::TracedValue::Create(); auto trace_process = tracing::TracedValue::Create();
trace_process->BeginDictionary("versions"); trace_process->BeginDictionary("versions");
#ifdef NODE_EXPERIMENTAL_HTTP #define V(key) \
trace_process->SetString("llhttp", llhttp_version); trace_process->SetString(#key, per_process::metadata.versions.key.c_str());
#else /* !NODE_EXPERIMENTAL_HTTP */
trace_process->SetString("http_parser", http_parser_version);
#endif /* NODE_EXPERIMENTAL_HTTP */
const char node_napi_version[] = NODE_STRINGIFY(NAPI_VERSION); NODE_VERSIONS_KEYS(V)
const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION); #undef V
trace_process->SetString("node", NODE_VERSION_STRING);
trace_process->SetString("v8", V8::GetVersion());
trace_process->SetString("uv", uv_version_string());
trace_process->SetString("zlib", ZLIB_VERSION);
trace_process->SetString("ares", ARES_VERSION_STR);
trace_process->SetString("modules", node_modules_version);
trace_process->SetString("nghttp2", NGHTTP2_VERSION);
trace_process->SetString("napi", node_napi_version);
#if HAVE_OPENSSL
trace_process->SetString("openssl", crypto::GetOpenSSLVersion());
#endif
trace_process->EndDictionary(); trace_process->EndDictionary();
trace_process->SetString("arch", NODE_ARCH); trace_process->SetString("arch", NODE_ARCH);
@ -943,53 +905,10 @@ void SetupProcessObject(Environment* env,
Local<Object> versions = Object::New(env->isolate()); Local<Object> versions = Object::New(env->isolate());
READONLY_PROPERTY(process, "versions", versions); READONLY_PROPERTY(process, "versions", versions);
#ifdef NODE_EXPERIMENTAL_HTTP #define V(key) \
READONLY_PROPERTY(versions, READONLY_STRING_PROPERTY(versions, #key, per_process::metadata.versions.key);
"llhttp", NODE_VERSIONS_KEYS(V)
FIXED_ONE_BYTE_STRING(env->isolate(), llhttp_version)); #undef V
#else /* !NODE_EXPERIMENTAL_HTTP */
READONLY_PROPERTY(versions,
"http_parser",
FIXED_ONE_BYTE_STRING(env->isolate(), http_parser_version));
#endif /* NODE_EXPERIMENTAL_HTTP */
// +1 to get rid of the leading 'v'
READONLY_PROPERTY(versions,
"node",
OneByteString(env->isolate(), NODE_VERSION + 1));
READONLY_PROPERTY(versions,
"v8",
OneByteString(env->isolate(), V8::GetVersion()));
READONLY_PROPERTY(versions,
"uv",
OneByteString(env->isolate(), uv_version_string()));
READONLY_PROPERTY(versions,
"zlib",
FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION));
READONLY_PROPERTY(versions,
"ares",
FIXED_ONE_BYTE_STRING(env->isolate(), ARES_VERSION_STR));
const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);
READONLY_PROPERTY(
versions,
"modules",
FIXED_ONE_BYTE_STRING(env->isolate(), node_modules_version));
READONLY_PROPERTY(versions,
"nghttp2",
FIXED_ONE_BYTE_STRING(env->isolate(), NGHTTP2_VERSION));
const char node_napi_version[] = NODE_STRINGIFY(NAPI_VERSION);
READONLY_PROPERTY(
versions,
"napi",
FIXED_ONE_BYTE_STRING(env->isolate(), node_napi_version));
#if HAVE_OPENSSL
READONLY_PROPERTY(
versions,
"openssl",
OneByteString(env->isolate(), crypto::GetOpenSSLVersion().c_str()));
#endif
// process.arch // process.arch
READONLY_PROPERTY(process, "arch", OneByteString(env->isolate(), NODE_ARCH)); READONLY_PROPERTY(process, "arch", OneByteString(env->isolate(), NODE_ARCH));

49
src/node_metadata.cc Normal file
View File

@ -0,0 +1,49 @@
#include "node_metadata.h"
#include "ares.h"
#include "nghttp2/nghttp2ver.h"
#include "node.h"
#include "util.h"
#include "uv.h"
#include "v8.h"
#include "zlib.h"
#if HAVE_OPENSSL
#include "node_crypto.h"
#endif
#ifdef NODE_EXPERIMENTAL_HTTP
#include "llhttp.h"
#else /* !NODE_EXPERIMENTAL_HTTP */
#include "http_parser.h"
#endif /* NODE_EXPERIMENTAL_HTTP */
namespace node {
namespace per_process {
Metadata metadata;
}
Metadata::Versions::Versions() {
node = NODE_VERSION_STRING;
v8 = v8::V8::GetVersion();
uv = uv_version_string();
zlib = ZLIB_VERSION;
ares = ARES_VERSION_STR;
modules = NODE_STRINGIFY(NODE_MODULE_VERSION);
nghttp2 = NGHTTP2_VERSION;
napi = NODE_STRINGIFY(NAPI_VERSION);
#ifdef NODE_EXPERIMENTAL_HTTP
llhttp = NODE_STRINGIFY(LLHTTP_VERSION_MAJOR) "." NODE_STRINGIFY(
LLHTTP_VERSION_MINOR) "." NODE_STRINGIFY(LLHTTP_VERSION_PATCH);
#else /* !NODE_EXPERIMENTAL_HTTP */
http_parser = NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR) "." NODE_STRINGIFY(
HTTP_PARSER_VERSION_MINOR) "." NODE_STRINGIFY(HTTP_PARSER_VERSION_PATCH);
#endif /* NODE_EXPERIMENTAL_HTTP */
#if HAVE_OPENSSL
openssl = crypto::GetOpenSSLVersion();
#endif
}
} // namespace node

57
src/node_metadata.h Normal file
View File

@ -0,0 +1,57 @@
#ifndef SRC_NODE_METADATA_H_
#define SRC_NODE_METADATA_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include <string>
namespace node {
#define NODE_VERSIONS_KEYS_BASE(V) \
V(node) \
V(v8) \
V(uv) \
V(zlib) \
V(ares) \
V(modules) \
V(nghttp2) \
V(napi)
#ifdef NODE_EXPERIMENTAL_HTTP
#define NODE_VERSIONS_KEY_HTTP(V) V(llhttp)
#else /* !NODE_EXPERIMENTAL_HTTP */
#define NODE_VERSIONS_KEY_HTTP(V) V(http_parser)
#endif /* NODE_EXPERIMENTAL_HTTP */
#if HAVE_OPENSSL
#define NODE_VERSIONS_KEY_CRYPTO(V) V(openssl)
#else
#define NODE_VERSIONS_KEY_CRYPTO(V)
#endif
#define NODE_VERSIONS_KEYS(V) \
NODE_VERSIONS_KEYS_BASE(V) \
NODE_VERSIONS_KEY_HTTP(V) \
NODE_VERSIONS_KEY_CRYPTO(V)
class Metadata {
public:
struct Versions {
Versions();
#define V(key) std::string key;
NODE_VERSIONS_KEYS(V)
#undef V
};
Versions versions;
};
// Per-process global
namespace per_process {
extern Metadata metadata;
}
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_NODE_METADATA_H_