Skip to content

Commit f06a2ec

Browse files
committed
lib / src : add undici version to process.versions
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 Fixes:nodejs#45260
1 parent 2a7635f commit f06a2ec

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/node_metadata.cc

+47
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "uv.h"
99
#include "v8.h"
1010
#include "zlib.h"
11+
#include "fstream"
12+
#include "sstream"
13+
#include "filesystem"
1114

1215
#if HAVE_OPENSSL
1316
#include <openssl/opensslv.h>
@@ -27,6 +30,7 @@
2730
#include <unicode/uvernum.h>
2831
#include <unicode/uversion.h>
2932
#endif // NODE_HAVE_I18N_SUPPORT
33+
#include <iostream>
3034

3135
namespace node {
3236

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

7074
Metadata::Versions::Versions() {
75+
undici = Metadata::Versions::GetDependencyVersionFromPackageJson("undici");
7176
node = NODE_VERSION_STRING;
7277
v8 = v8::V8::GetVersion();
7378
uv = uv_version_string();
@@ -105,6 +110,48 @@ Metadata::Versions::Versions() {
105110
#endif
106111
}
107112

113+
bool hasEnding(std::string const& fullString, std::string const& ending) {
114+
if (fullString.length() >= ending.length()) {
115+
return (0 == fullString.compare(fullString.length() - ending.length(),
116+
ending.length(),
117+
ending));
118+
} else {
119+
return false;
120+
}
121+
}
122+
123+
std::string Metadata::Versions::GetDependencyVersionFromPackageJson(
124+
std::string packageName) {
125+
126+
// get current path and find parent which has ending 'node'
127+
std::filesystem::path cwd = std::filesystem::current_path();
128+
while (!hasEnding(cwd.parent_path().string(), "node")) {
129+
cwd = cwd.parent_path();
130+
}
131+
std::ifstream myFile(cwd.parent_path().string()+"\\deps\\" + packageName + "\\src\\package.json");
132+
std::ostringstream tmp;
133+
tmp << myFile.rdbuf();
134+
std::string fileString = tmp.str();
135+
136+
// 9 is character count of \"version\" and plus 1"
137+
int ix10 = fileString.find("\"version\"") + 9;
138+
std::string version = "";
139+
bool startAdding = false;
140+
for (int i = ix10; i < fileString.length(); i++) {
141+
142+
if (fileString[i] == '\"' && !startAdding) {
143+
startAdding = true;
144+
}else if (fileString[i] == '\"' && startAdding) {
145+
break;
146+
}
147+
if (fileString[i] != '\"' && startAdding) {
148+
version = version + fileString[i];
149+
}
150+
}
151+
152+
return version;
153+
}
154+
108155
Metadata::Release::Release() : name(NODE_RELEASE) {
109156
#if NODE_VERSION_IS_LTS
110157
lts = NODE_VERSION_LTS_CODENAME;

src/node_metadata.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace node {
3838
V(nghttp2) \
3939
V(napi) \
4040
V(llhttp) \
41+
V(undici) \
4142

4243
#if HAVE_OPENSSL
4344
#define NODE_VERSIONS_KEY_CRYPTO(V) V(openssl)
@@ -79,7 +80,7 @@ class Metadata {
7980

8081
struct Versions {
8182
Versions();
82-
83+
std::string GetDependencyVersionFromPackageJson(std::string packageName);
8384
#ifdef NODE_HAVE_I18N_SUPPORT
8485
// Must be called on the main thread after
8586
// i18n::InitializeICUDirectory()

test_node_metadata.cc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "node_metadata.h"
2+
#include <string>
3+
#include "gtest/gtest.h"
4+
5+
TEST(NodeMetadataTest, Versions) {
6+
std::string ver = node::Metadata::Versions().undici;
7+
std::stringstream versionstream(ver);
8+
std::string segment;
9+
std::vector<std::string> seglist;
10+
while (std::getline(versionstream, segment, '.')) {
11+
seglist.push_back(segment);
12+
}
13+
14+
EXPECT_EQ(seglist.size(), 3);
15+
EXPECT_GE(std::stoi(seglist[0]), 5);
16+
EXPECT_GE(std::stoi(seglist[1]), 0);
17+
EXPECT_GE(std::stoi(seglist[2]), 0);
18+
19+
}

0 commit comments

Comments
 (0)