diff --git a/src/node_metadata.cc b/src/node_metadata.cc index a221dcde050ad7..ed00cf60c4799c 100644 --- a/src/node_metadata.cc +++ b/src/node_metadata.cc @@ -8,6 +8,9 @@ #include "uv.h" #include "v8.h" #include "zlib.h" +#include "fstream" +#include "sstream" +#include "filesystem" #if HAVE_OPENSSL #include @@ -27,6 +30,7 @@ #include #include #endif // NODE_HAVE_I18N_SUPPORT +#include namespace node { @@ -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(); @@ -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; diff --git a/src/node_metadata.h b/src/node_metadata.h index b7cacae4c3d430..e41d69d9d2a5af 100644 --- a/src/node_metadata.h +++ b/src/node_metadata.h @@ -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) @@ -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() diff --git a/test_node_metadata.cc b/test_node_metadata.cc new file mode 100644 index 00000000000000..d5038beedc78c7 --- /dev/null +++ b/test_node_metadata.cc @@ -0,0 +1,19 @@ +#include "node_metadata.h" +#include +#include "gtest/gtest.h" + +TEST(NodeMetadataTest, Versions) { + std::string ver = node::Metadata::Versions().undici; + std::stringstream versionstream(ver); + std::string segment; + std::vector 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); + +}