From 394487e3e882f7fcf3ef95beae646819683e4947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Mon, 9 Mar 2020 22:18:03 -0500 Subject: [PATCH] src: create a getter for kernel version Backport-PR-URL: https://github.com/nodejs/node/pull/32166 PR-URL: https://github.com/nodejs/node/pull/31732 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- doc/api/os.md | 14 ++++++++++++++ lib/os.js | 16 ++++++++++++---- src/node_os.cc | 38 +++++++++++++++----------------------- test/parallel/test-os.js | 4 ++++ 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/doc/api/os.md b/doc/api/os.md index bb5bd612382ce5..c17d249639b6af 100644 --- a/doc/api/os.md +++ b/doc/api/os.md @@ -389,6 +389,20 @@ operating system response. Throws a [`SystemError`][] if a user has no `username` or `homedir`. +## `os.version()` + + +* Returns {string} + +Returns a string identifying the kernel version. + +On POSIX systems, the operating system release is determined by calling +[uname(3)][]. On Windows, `RtlGetVersion()` is used, and if it is not available, +`GetVersionExW()` will be used. See +https://en.wikipedia.org/wiki/Uname#Examples for more information. + ## OS Constants The following constants are exported by `os.constants`. diff --git a/lib/os.js b/lib/os.js index 2cce09211c3fe8..f533c3f18f13fd 100644 --- a/lib/os.js +++ b/lib/os.js @@ -46,8 +46,7 @@ const { getHostname: _getHostname, getInterfaceAddresses: _getInterfaceAddresses, getLoadAvg, - getOSRelease: _getOSRelease, - getOSType: _getOSType, + getOSInformation: _getOSInformation, getPriority: _getPriority, getTotalMem, getUserInfo, @@ -67,17 +66,25 @@ function getCheckedFunction(fn) { }); } +const [ + type, + version, + release +] = _getOSInformation(); + const getHomeDirectory = getCheckedFunction(_getHomeDirectory); const getHostname = getCheckedFunction(_getHostname); const getInterfaceAddresses = getCheckedFunction(_getInterfaceAddresses); -const getOSRelease = getCheckedFunction(_getOSRelease); -const getOSType = getCheckedFunction(_getOSType); +const getOSRelease = () => release; +const getOSType = () => type; +const getOSVersion = () => version; getFreeMem[SymbolToPrimitive] = () => getFreeMem(); getHostname[SymbolToPrimitive] = () => getHostname(); getHomeDirectory[SymbolToPrimitive] = () => getHomeDirectory(); getOSRelease[SymbolToPrimitive] = () => getOSRelease(); getOSType[SymbolToPrimitive] = () => getOSType(); +getOSVersion[SymbolToPrimitive] = () => getOSVersion(); getTotalMem[SymbolToPrimitive] = () => getTotalMem(); getUptime[SymbolToPrimitive] = () => getUptime(); @@ -283,6 +290,7 @@ module.exports = { tmpdir, totalmem: getTotalMem, type: getOSType, + version: getOSVersion, userInfo, uptime: getUptime, diff --git a/src/node_os.cc b/src/node_os.cc index 131e38055685c2..ae6b9c060dc36a 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -76,7 +76,7 @@ static void GetHostname(const FunctionCallbackInfo& args) { } -static void GetOSType(const FunctionCallbackInfo& args) { +static void GetOSInformation(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); uv_utsname_t info; int err = uv_os_uname(&info); @@ -87,26 +87,19 @@ static void GetOSType(const FunctionCallbackInfo& args) { return args.GetReturnValue().SetUndefined(); } - args.GetReturnValue().Set( - String::NewFromUtf8(env->isolate(), info.sysname, NewStringType::kNormal) - .ToLocalChecked()); -} - - -static void GetOSRelease(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - uv_utsname_t info; - int err = uv_os_uname(&info); - - if (err != 0) { - CHECK_GE(args.Length(), 1); - env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_os_uname"); - return args.GetReturnValue().SetUndefined(); - } - - args.GetReturnValue().Set( - String::NewFromUtf8(env->isolate(), info.release, NewStringType::kNormal) - .ToLocalChecked()); + // [sysname, version, release] + Local osInformation[] = { + String::NewFromUtf8( + env->isolate(), info.sysname, NewStringType::kNormal).ToLocalChecked(), + String::NewFromUtf8( + env->isolate(), info.version, NewStringType::kNormal).ToLocalChecked(), + String::NewFromUtf8( + env->isolate(), info.release, NewStringType::kNormal).ToLocalChecked() + }; + + args.GetReturnValue().Set(Array::New(env->isolate(), + osInformation, + arraysize(osInformation))); } @@ -398,8 +391,7 @@ void Initialize(Local target, env->SetMethod(target, "getTotalMem", GetTotalMemory); env->SetMethod(target, "getFreeMem", GetFreeMemory); env->SetMethod(target, "getCPUs", GetCPUInfo); - env->SetMethod(target, "getOSType", GetOSType); - env->SetMethod(target, "getOSRelease", GetOSRelease); + env->SetMethod(target, "getOSInformation", GetOSInformation); env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses); env->SetMethod(target, "getHomeDirectory", GetHomeDirectory); env->SetMethod(target, "getUserInfo", GetUserInfo); diff --git a/test/parallel/test-os.js b/test/parallel/test-os.js index 57b68ff8dea9ac..5ee0fb9ca8dadb 100644 --- a/test/parallel/test-os.js +++ b/test/parallel/test-os.js @@ -194,6 +194,10 @@ const home = os.homedir(); is.string(home); assert.ok(home.includes(path.sep)); +const version = os.version(); +assert.strictEqual(typeof version, 'string'); +assert(version); + if (common.isWindows && process.env.USERPROFILE) { assert.strictEqual(home, process.env.USERPROFILE); delete process.env.USERPROFILE;