Skip to content

Commit

Permalink
src: move version metadata into node_metadata{.h, .cc}
Browse files Browse the repository at this point in the history
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: nodejs#24774
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
joyeecheung authored and Trott committed Dec 4, 2018
1 parent 7761881 commit d17d7bd
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 90 deletions.
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
'src/node_http2.cc',
'src/node_i18n.cc',
'src/node_messaging.cc',
'src/node_metadata.cc',
'src/node_native_module.cc',
'src/node_options.cc',
'src/node_os.cc',
Expand Down Expand Up @@ -430,6 +431,7 @@
'src/node_i18n.h',
'src/node_internals.h',
'src/node_messaging.h',
'src/node_metadata.h',
'src/node_mutex.h',
'src/node_native_module.h',
'src/node_object_wrap.h',
Expand Down
99 changes: 9 additions & 90 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "node_context_data.h"
#include "node_errors.h"
#include "node_internals.h"
#include "node_metadata.h"
#include "node_native_module.h"
#include "node_perf.h"
#include "node_platform.h"
Expand All @@ -48,16 +49,9 @@
#include "node_dtrace.h"
#endif

#include "ares.h"
#include "async_wrap-inl.h"
#include "env-inl.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 "string_bytes.h"
#include "tracing/agent.h"
Expand All @@ -68,7 +62,6 @@
#include "libplatform/libplatform.h"
#endif // NODE_USE_V8_PLATFORM
#include "v8-profiler.h"
#include "zlib.h"

#ifdef NODE_ENABLE_VTUNE_PROFILING
#include "../deps/v8/src/third_party/vtune/v8-vtune.h"
Expand Down Expand Up @@ -156,22 +149,6 @@ using v8::Value;

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)
unsigned int reverted = 0;

Expand Down Expand Up @@ -210,27 +187,12 @@ class NodeTraceStateObserver :
auto trace_process = tracing::TracedValue::Create();
trace_process->BeginDictionary("versions");

#ifdef NODE_EXPERIMENTAL_HTTP
trace_process->SetString("llhttp", llhttp_version);
#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);
const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);
#define V(key) \
trace_process->SetString(#key, per_process::metadata.versions.key.c_str());

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);
NODE_VERSIONS_KEYS(V)
#undef V

#if HAVE_OPENSSL
trace_process->SetString("openssl", crypto::GetOpenSSLVersion());
#endif
trace_process->EndDictionary();

trace_process->SetString("arch", NODE_ARCH);
Expand Down Expand Up @@ -943,53 +905,10 @@ void SetupProcessObject(Environment* env,
Local<Object> versions = Object::New(env->isolate());
READONLY_PROPERTY(process, "versions", versions);

#ifdef NODE_EXPERIMENTAL_HTTP
READONLY_PROPERTY(versions,
"llhttp",
FIXED_ONE_BYTE_STRING(env->isolate(), llhttp_version));
#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
#define V(key) \
READONLY_STRING_PROPERTY(versions, #key, per_process::metadata.versions.key);
NODE_VERSIONS_KEYS(V)
#undef V

// process.arch
READONLY_PROPERTY(process, "arch", OneByteString(env->isolate(), NODE_ARCH));
Expand Down
49 changes: 49 additions & 0 deletions src/node_metadata.cc
Original file line number Diff line number Diff line change
@@ -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 changes: 57 additions & 0 deletions src/node_metadata.h
Original file line number Diff line number Diff line change
@@ -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_

0 comments on commit d17d7bd

Please sign in to comment.