Skip to content

Commit

Permalink
process: print versions by sort
Browse files Browse the repository at this point in the history
Co-authored-by: James M Snell <[email protected]>
Co-authored-by: Anna Henningsen <[email protected]>
PR-URL: #46428
Fixes: #45630
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
  • Loading branch information
3 people authored and danielleadams committed Apr 5, 2023
1 parent 2b72a45 commit c231aae
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
37 changes: 31 additions & 6 deletions src/node_process_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,43 @@ MaybeLocal<Object> CreateProcessObject(Realm* realm) {
READONLY_PROPERTY(
process, "version", FIXED_ONE_BYTE_STRING(isolate, NODE_VERSION));

// process.versions
Local<Object> versions = Object::New(isolate);
READONLY_PROPERTY(process, "versions", versions);
// Node.js version is always on the top
READONLY_STRING_PROPERTY(
versions, "node", per_process::metadata.versions.node);

#define V(key) +1
std::pair<std::string_view, std::string_view>
versions_array[NODE_VERSIONS_KEYS(V)];
#undef V
auto* slot = &versions_array[0];

#define V(key) \
if (!per_process::metadata.versions.key.empty()) { \
READONLY_STRING_PROPERTY( \
versions, #key, per_process::metadata.versions.key); \
}
do { \
*slot++ = std::pair<std::string_view, std::string_view>( \
#key, per_process::metadata.versions.key); \
} while (0);
NODE_VERSIONS_KEYS(V)
#undef V

std::sort(&versions_array[0],
&versions_array[arraysize(versions_array)],
[](auto& a, auto& b) { return a.first < b.first; });

for (const auto& version : versions_array) {
versions
->DefineOwnProperty(
context,
OneByteString(isolate, version.first.data(), version.first.size()),
OneByteString(
isolate, version.second.data(), version.second.size()),
v8::ReadOnly)
.Check();
}

// process.versions
READONLY_PROPERTY(process, "versions", versions);

// process.arch
READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);

Expand Down
18 changes: 17 additions & 1 deletion src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,26 @@ static void PrintComponentVersions(JSONWriter* writer) {

writer->json_objectstart("componentVersions");

#define V(key) writer->json_keyvalue(#key, per_process::metadata.versions.key);
#define V(key) +1
std::pair<std::string, std::string> versions_array[NODE_VERSIONS_KEYS(V)];
#undef V
auto* slot = &versions_array[0];

#define V(key) \
do { \
*slot++ = std::make_pair(#key, per_process::metadata.versions.key); \
} while (0);
NODE_VERSIONS_KEYS(V)
#undef V

std::sort(&versions_array[0],
&versions_array[arraysize(versions_array)],
[](auto& a, auto& b) { return a.first < b.first; });

for (const auto& version : versions_array) {
writer->json_keyvalue(version.first, version.second);
}

writer->json_objectend();
}

Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-process-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const expected_keys = [
'ares',
'brotli',
'modules',
'node',
'uv',
'v8',
'zlib',
Expand Down Expand Up @@ -44,7 +43,9 @@ if (common.hasIntl) {
}

expected_keys.sort();
const actual_keys = Object.keys(process.versions).sort();
expected_keys.unshift('node');

const actual_keys = Object.keys(process.versions);

assert.deepStrictEqual(actual_keys, expected_keys);

Expand Down

0 comments on commit c231aae

Please sign in to comment.