Skip to content

Commit 7c70b61

Browse files
joyeecheungBridgeAR
authored andcommitted
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: #24774 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 53b59b4 commit 7c70b61

File tree

4 files changed

+117
-90
lines changed

4 files changed

+117
-90
lines changed

node.gyp

+2
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@
356356
'src/node_http2.cc',
357357
'src/node_i18n.cc',
358358
'src/node_messaging.cc',
359+
'src/node_metadata.cc',
359360
'src/node_native_module.cc',
360361
'src/node_options.cc',
361362
'src/node_os.cc',
@@ -427,6 +428,7 @@
427428
'src/node_i18n.h',
428429
'src/node_internals.h',
429430
'src/node_messaging.h',
431+
'src/node_metadata.h',
430432
'src/node_mutex.h',
431433
'src/node_native_module.h',
432434
'src/node_object_wrap.h',

src/node.cc

+9-90
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "node_context_data.h"
2626
#include "node_errors.h"
2727
#include "node_internals.h"
28+
#include "node_metadata.h"
2829
#include "node_native_module.h"
2930
#include "node_perf.h"
3031
#include "node_platform.h"
@@ -48,16 +49,9 @@
4849
#include "node_dtrace.h"
4950
#endif
5051

51-
#include "ares.h"
5252
#include "async_wrap-inl.h"
5353
#include "env-inl.h"
5454
#include "handle_wrap.h"
55-
#ifdef NODE_EXPERIMENTAL_HTTP
56-
# include "llhttp.h"
57-
#else /* !NODE_EXPERIMENTAL_HTTP */
58-
# include "http_parser.h"
59-
#endif /* NODE_EXPERIMENTAL_HTTP */
60-
#include "nghttp2/nghttp2ver.h"
6155
#include "req_wrap-inl.h"
6256
#include "string_bytes.h"
6357
#include "tracing/agent.h"
@@ -68,7 +62,6 @@
6862
#include "libplatform/libplatform.h"
6963
#endif // NODE_USE_V8_PLATFORM
7064
#include "v8-profiler.h"
71-
#include "zlib.h"
7265

7366
#ifdef NODE_ENABLE_VTUNE_PROFILING
7467
#include "../deps/v8/src/third_party/vtune/v8-vtune.h"
@@ -158,22 +151,6 @@ using v8::Value;
158151

159152
static bool v8_is_profiling = false;
160153

161-
#ifdef NODE_EXPERIMENTAL_HTTP
162-
static const char llhttp_version[] =
163-
NODE_STRINGIFY(LLHTTP_VERSION_MAJOR)
164-
"."
165-
NODE_STRINGIFY(LLHTTP_VERSION_MINOR)
166-
"."
167-
NODE_STRINGIFY(LLHTTP_VERSION_PATCH);
168-
#else /* !NODE_EXPERIMENTAL_HTTP */
169-
static const char http_parser_version[] =
170-
NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR)
171-
"."
172-
NODE_STRINGIFY(HTTP_PARSER_VERSION_MINOR)
173-
"."
174-
NODE_STRINGIFY(HTTP_PARSER_VERSION_PATCH);
175-
#endif /* NODE_EXPERIMENTAL_HTTP */
176-
177154
// Bit flag used to track security reverts (see node_revert.h)
178155
unsigned int reverted = 0;
179156

@@ -212,27 +189,12 @@ class NodeTraceStateObserver :
212189
auto trace_process = tracing::TracedValue::Create();
213190
trace_process->BeginDictionary("versions");
214191

215-
#ifdef NODE_EXPERIMENTAL_HTTP
216-
trace_process->SetString("llhttp", llhttp_version);
217-
#else /* !NODE_EXPERIMENTAL_HTTP */
218-
trace_process->SetString("http_parser", http_parser_version);
219-
#endif /* NODE_EXPERIMENTAL_HTTP */
220-
221-
const char node_napi_version[] = NODE_STRINGIFY(NAPI_VERSION);
222-
const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);
192+
#define V(key) \
193+
trace_process->SetString(#key, per_process::metadata.versions.key.c_str());
223194

224-
trace_process->SetString("node", NODE_VERSION_STRING);
225-
trace_process->SetString("v8", V8::GetVersion());
226-
trace_process->SetString("uv", uv_version_string());
227-
trace_process->SetString("zlib", ZLIB_VERSION);
228-
trace_process->SetString("ares", ARES_VERSION_STR);
229-
trace_process->SetString("modules", node_modules_version);
230-
trace_process->SetString("nghttp2", NGHTTP2_VERSION);
231-
trace_process->SetString("napi", node_napi_version);
195+
NODE_VERSIONS_KEYS(V)
196+
#undef V
232197

233-
#if HAVE_OPENSSL
234-
trace_process->SetString("openssl", crypto::GetOpenSSLVersion());
235-
#endif
236198
trace_process->EndDictionary();
237199

238200
trace_process->SetString("arch", NODE_ARCH);
@@ -980,53 +942,10 @@ void SetupProcessObject(Environment* env,
980942
Local<Object> versions = Object::New(env->isolate());
981943
READONLY_PROPERTY(process, "versions", versions);
982944

983-
#ifdef NODE_EXPERIMENTAL_HTTP
984-
READONLY_PROPERTY(versions,
985-
"llhttp",
986-
FIXED_ONE_BYTE_STRING(env->isolate(), llhttp_version));
987-
#else /* !NODE_EXPERIMENTAL_HTTP */
988-
READONLY_PROPERTY(versions,
989-
"http_parser",
990-
FIXED_ONE_BYTE_STRING(env->isolate(), http_parser_version));
991-
#endif /* NODE_EXPERIMENTAL_HTTP */
992-
993-
// +1 to get rid of the leading 'v'
994-
READONLY_PROPERTY(versions,
995-
"node",
996-
OneByteString(env->isolate(), NODE_VERSION + 1));
997-
READONLY_PROPERTY(versions,
998-
"v8",
999-
OneByteString(env->isolate(), V8::GetVersion()));
1000-
READONLY_PROPERTY(versions,
1001-
"uv",
1002-
OneByteString(env->isolate(), uv_version_string()));
1003-
READONLY_PROPERTY(versions,
1004-
"zlib",
1005-
FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION));
1006-
READONLY_PROPERTY(versions,
1007-
"ares",
1008-
FIXED_ONE_BYTE_STRING(env->isolate(), ARES_VERSION_STR));
1009-
1010-
const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION);
1011-
READONLY_PROPERTY(
1012-
versions,
1013-
"modules",
1014-
FIXED_ONE_BYTE_STRING(env->isolate(), node_modules_version));
1015-
READONLY_PROPERTY(versions,
1016-
"nghttp2",
1017-
FIXED_ONE_BYTE_STRING(env->isolate(), NGHTTP2_VERSION));
1018-
const char node_napi_version[] = NODE_STRINGIFY(NAPI_VERSION);
1019-
READONLY_PROPERTY(
1020-
versions,
1021-
"napi",
1022-
FIXED_ONE_BYTE_STRING(env->isolate(), node_napi_version));
1023-
1024-
#if HAVE_OPENSSL
1025-
READONLY_PROPERTY(
1026-
versions,
1027-
"openssl",
1028-
OneByteString(env->isolate(), crypto::GetOpenSSLVersion().c_str()));
1029-
#endif
945+
#define V(key) \
946+
READONLY_STRING_PROPERTY(versions, #key, per_process::metadata.versions.key);
947+
NODE_VERSIONS_KEYS(V)
948+
#undef V
1030949

1031950
// process.arch
1032951
READONLY_PROPERTY(process, "arch", OneByteString(env->isolate(), NODE_ARCH));

src/node_metadata.cc

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "node_metadata.h"
2+
#include "ares.h"
3+
#include "nghttp2/nghttp2ver.h"
4+
#include "node.h"
5+
#include "util.h"
6+
#include "uv.h"
7+
#include "v8.h"
8+
#include "zlib.h"
9+
10+
#if HAVE_OPENSSL
11+
#include "node_crypto.h"
12+
#endif
13+
14+
#ifdef NODE_EXPERIMENTAL_HTTP
15+
#include "llhttp.h"
16+
#else /* !NODE_EXPERIMENTAL_HTTP */
17+
#include "http_parser.h"
18+
#endif /* NODE_EXPERIMENTAL_HTTP */
19+
20+
namespace node {
21+
22+
namespace per_process {
23+
Metadata metadata;
24+
}
25+
26+
Metadata::Versions::Versions() {
27+
node = NODE_VERSION_STRING;
28+
v8 = v8::V8::GetVersion();
29+
uv = uv_version_string();
30+
zlib = ZLIB_VERSION;
31+
ares = ARES_VERSION_STR;
32+
modules = NODE_STRINGIFY(NODE_MODULE_VERSION);
33+
nghttp2 = NGHTTP2_VERSION;
34+
napi = NODE_STRINGIFY(NAPI_VERSION);
35+
36+
#ifdef NODE_EXPERIMENTAL_HTTP
37+
llhttp = NODE_STRINGIFY(LLHTTP_VERSION_MAJOR) "." NODE_STRINGIFY(
38+
LLHTTP_VERSION_MINOR) "." NODE_STRINGIFY(LLHTTP_VERSION_PATCH);
39+
#else /* !NODE_EXPERIMENTAL_HTTP */
40+
http_parser = NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR) "." NODE_STRINGIFY(
41+
HTTP_PARSER_VERSION_MINOR) "." NODE_STRINGIFY(HTTP_PARSER_VERSION_PATCH);
42+
#endif /* NODE_EXPERIMENTAL_HTTP */
43+
44+
#if HAVE_OPENSSL
45+
openssl = crypto::GetOpenSSLVersion();
46+
#endif
47+
}
48+
49+
} // namespace node

src/node_metadata.h

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef SRC_NODE_METADATA_H_
2+
#define SRC_NODE_METADATA_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include <string>
7+
8+
namespace node {
9+
10+
#define NODE_VERSIONS_KEYS_BASE(V) \
11+
V(node) \
12+
V(v8) \
13+
V(uv) \
14+
V(zlib) \
15+
V(ares) \
16+
V(modules) \
17+
V(nghttp2) \
18+
V(napi)
19+
20+
#ifdef NODE_EXPERIMENTAL_HTTP
21+
#define NODE_VERSIONS_KEY_HTTP(V) V(llhttp)
22+
#else /* !NODE_EXPERIMENTAL_HTTP */
23+
#define NODE_VERSIONS_KEY_HTTP(V) V(http_parser)
24+
#endif /* NODE_EXPERIMENTAL_HTTP */
25+
26+
#if HAVE_OPENSSL
27+
#define NODE_VERSIONS_KEY_CRYPTO(V) V(openssl)
28+
#else
29+
#define NODE_VERSIONS_KEY_CRYPTO(V)
30+
#endif
31+
32+
#define NODE_VERSIONS_KEYS(V) \
33+
NODE_VERSIONS_KEYS_BASE(V) \
34+
NODE_VERSIONS_KEY_HTTP(V) \
35+
NODE_VERSIONS_KEY_CRYPTO(V)
36+
37+
class Metadata {
38+
public:
39+
struct Versions {
40+
Versions();
41+
#define V(key) std::string key;
42+
NODE_VERSIONS_KEYS(V)
43+
#undef V
44+
};
45+
46+
Versions versions;
47+
};
48+
49+
// Per-process global
50+
namespace per_process {
51+
extern Metadata metadata;
52+
}
53+
54+
} // namespace node
55+
56+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
57+
#endif // SRC_NODE_METADATA_H_

0 commit comments

Comments
 (0)