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

lib / src : add undici version to process.versions #45599

Closed
Closed
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
47 changes: 47 additions & 0 deletions src/node_metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "uv.h"
#include "v8.h"
#include "zlib.h"
#include "fstream"
#include "sstream"
#include "filesystem"

#if HAVE_OPENSSL
#include <openssl/opensslv.h>
Expand All @@ -27,6 +30,7 @@
#include <unicode/uvernum.h>
#include <unicode/uversion.h>
#endif // NODE_HAVE_I18N_SUPPORT
#include <iostream>

namespace node {

Expand Down Expand Up @@ -68,6 +72,7 @@ void Metadata::Versions::InitializeIntlVersions() {
#endif // NODE_HAVE_I18N_SUPPORT

Metadata::Versions::Versions() {
undici = Metadata::Versions::GetDependencyVersionFromPackageJson("undici");
node = NODE_VERSION_STRING;
v8 = v8::V8::GetVersion();
uv = uv_version_string();
Expand Down Expand Up @@ -105,6 +110,48 @@ Metadata::Versions::Versions() {
#endif
}

bool hasEnding(std::string const& fullString, std::string const& ending) {
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare(fullString.length() - ending.length(),
ending.length(),
ending));
} else {
return false;
}
}

std::string Metadata::Versions::GetDependencyVersionFromPackageJson(
std::string packageName) {

// get current path and find parent which has ending 'node'
std::filesystem::path cwd = std::filesystem::current_path();
while (!hasEnding(cwd.parent_path().string(), "node")) {
cwd = cwd.parent_path();
}
std::ifstream myFile(cwd.parent_path().string()+"\\deps\\" + packageName + "\\src\\package.json");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this is not going to work. This file won't exist for the binaries available from nodejs.org.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richardlau How about updating the update script for undici to statically update a C++ variable with the latest version number?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this also be an option, read versions and write them in a text file in build step, then read that file to return missing versions? File and node.exe can be in the same directory.

Copy link
Member

@richardlau richardlau Nov 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node.js is compiled into a single binary so you cannot assume any other files are present at run time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a dep-versions.cc and replace the value on tools/deps_updater scripts either through bash or javascript.

std::ostringstream tmp;
tmp << myFile.rdbuf();
std::string fileString = tmp.str();

// 9 is character count of \"version\" and plus 1"
int ix10 = fileString.find("\"version\"") + 9;
std::string version = "";
bool startAdding = false;
for (int i = ix10; i < fileString.length(); i++) {

if (fileString[i] == '\"' && !startAdding) {
startAdding = true;
}else if (fileString[i] == '\"' && startAdding) {
break;
}
if (fileString[i] != '\"' && startAdding) {
version = version + fileString[i];
}
}

return version;
}

Metadata::Release::Release() : name(NODE_RELEASE) {
#if NODE_VERSION_IS_LTS
lts = NODE_VERSION_LTS_CODENAME;
Expand Down
3 changes: 2 additions & 1 deletion src/node_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace node {
V(nghttp2) \
V(napi) \
V(llhttp) \
V(undici) \

#if HAVE_OPENSSL
#define NODE_VERSIONS_KEY_CRYPTO(V) V(openssl)
Expand Down Expand Up @@ -79,7 +80,7 @@ class Metadata {

struct Versions {
Versions();

std::string GetDependencyVersionFromPackageJson(std::string packageName);
#ifdef NODE_HAVE_I18N_SUPPORT
// Must be called on the main thread after
// i18n::InitializeICUDirectory()
Expand Down
19 changes: 19 additions & 0 deletions test_node_metadata.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "node_metadata.h"
#include <string>
#include "gtest/gtest.h"

TEST(NodeMetadataTest, Versions) {
std::string ver = node::Metadata::Versions().undici;
std::stringstream versionstream(ver);
std::string segment;
std::vector<std::string> seglist;
while (std::getline(versionstream, segment, '.')) {
seglist.push_back(segment);
}

EXPECT_EQ(seglist.size(), 3);
EXPECT_GE(std::stoi(seglist[0]), 5);
EXPECT_GE(std::stoi(seglist[1]), 0);
EXPECT_GE(std::stoi(seglist[2]), 0);

}