Skip to content

Commit c231aae

Browse files
himself65jasnelladdaleax
authored andcommitted
process: print versions by sort
Co-authored-by: James M Snell <jasnell@gmail.com> Co-authored-by: Anna Henningsen <github@addaleax.net> PR-URL: #46428 Fixes: #45630 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent 2b72a45 commit c231aae

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

src/node_process_object.cc

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,43 @@ MaybeLocal<Object> CreateProcessObject(Realm* realm) {
105105
READONLY_PROPERTY(
106106
process, "version", FIXED_ONE_BYTE_STRING(isolate, NODE_VERSION));
107107

108-
// process.versions
109108
Local<Object> versions = Object::New(isolate);
110-
READONLY_PROPERTY(process, "versions", versions);
109+
// Node.js version is always on the top
110+
READONLY_STRING_PROPERTY(
111+
versions, "node", per_process::metadata.versions.node);
112+
113+
#define V(key) +1
114+
std::pair<std::string_view, std::string_view>
115+
versions_array[NODE_VERSIONS_KEYS(V)];
116+
#undef V
117+
auto* slot = &versions_array[0];
111118

112119
#define V(key) \
113-
if (!per_process::metadata.versions.key.empty()) { \
114-
READONLY_STRING_PROPERTY( \
115-
versions, #key, per_process::metadata.versions.key); \
116-
}
120+
do { \
121+
*slot++ = std::pair<std::string_view, std::string_view>( \
122+
#key, per_process::metadata.versions.key); \
123+
} while (0);
117124
NODE_VERSIONS_KEYS(V)
118125
#undef V
119126

127+
std::sort(&versions_array[0],
128+
&versions_array[arraysize(versions_array)],
129+
[](auto& a, auto& b) { return a.first < b.first; });
130+
131+
for (const auto& version : versions_array) {
132+
versions
133+
->DefineOwnProperty(
134+
context,
135+
OneByteString(isolate, version.first.data(), version.first.size()),
136+
OneByteString(
137+
isolate, version.second.data(), version.second.size()),
138+
v8::ReadOnly)
139+
.Check();
140+
}
141+
142+
// process.versions
143+
READONLY_PROPERTY(process, "versions", versions);
144+
120145
// process.arch
121146
READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);
122147

src/node_report.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,10 +794,26 @@ static void PrintComponentVersions(JSONWriter* writer) {
794794

795795
writer->json_objectstart("componentVersions");
796796

797-
#define V(key) writer->json_keyvalue(#key, per_process::metadata.versions.key);
797+
#define V(key) +1
798+
std::pair<std::string, std::string> versions_array[NODE_VERSIONS_KEYS(V)];
799+
#undef V
800+
auto* slot = &versions_array[0];
801+
802+
#define V(key) \
803+
do { \
804+
*slot++ = std::make_pair(#key, per_process::metadata.versions.key); \
805+
} while (0);
798806
NODE_VERSIONS_KEYS(V)
799807
#undef V
800808

809+
std::sort(&versions_array[0],
810+
&versions_array[arraysize(versions_array)],
811+
[](auto& a, auto& b) { return a.first < b.first; });
812+
813+
for (const auto& version : versions_array) {
814+
writer->json_keyvalue(version.first, version.second);
815+
}
816+
801817
writer->json_objectend();
802818
}
803819

test/parallel/test-process-versions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const expected_keys = [
99
'ares',
1010
'brotli',
1111
'modules',
12-
'node',
1312
'uv',
1413
'v8',
1514
'zlib',
@@ -44,7 +43,9 @@ if (common.hasIntl) {
4443
}
4544

4645
expected_keys.sort();
47-
const actual_keys = Object.keys(process.versions).sort();
46+
expected_keys.unshift('node');
47+
48+
const actual_keys = Object.keys(process.versions);
4849

4950
assert.deepStrictEqual(actual_keys, expected_keys);
5051

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy