Skip to content

Commit

Permalink
lib / src : add undici version to process.versions
Browse files Browse the repository at this point in the history
process.versions is not returning undici version,
this PR will read the version number from undici/src/package.json
and add it to result. If this approach is ok then other missing libraries
will be added

Refs:#45260
  • Loading branch information
kivancbilen committed Nov 23, 2022
1 parent 2a7635f commit ca57d50
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
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");
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);

}

0 comments on commit ca57d50

Please sign in to comment.