Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: print versions by sort #46428

Merged
merged 5 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -45,7 +44,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