Skip to content

Commit 258a80d

Browse files
juanarbolMylesBorins
authored andcommitted
src: create a getter for kernel version
Backport-PR-URL: #32166 PR-URL: #31732 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f263659 commit 258a80d

4 files changed

Lines changed: 41 additions & 26 deletions

File tree

‎doc/api/os.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,20 @@ operating system response.
389389

390390
Throws a [`SystemError`][] if a user has no `username` or `homedir`.
391391

392+
## `os.version()`
393+
<!-- YAML
394+
added: REPLACEME
395+
-->
396+
397+
* Returns {string}
398+
399+
Returns a string identifying the kernel version.
400+
401+
On POSIX systems, the operating system release is determined by calling
402+
[uname(3)][]. On Windows, `RtlGetVersion()` is used, and if it is not available,
403+
`GetVersionExW()` will be used. See
404+
https://en.wikipedia.org/wiki/Uname#Examples for more information.
405+
392406
## OS Constants
393407

394408
The following constants are exported by `os.constants`.

‎lib/os.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ const {
4646
getHostname: _getHostname,
4747
getInterfaceAddresses: _getInterfaceAddresses,
4848
getLoadAvg,
49-
getOSRelease: _getOSRelease,
50-
getOSType: _getOSType,
49+
getOSInformation: _getOSInformation,
5150
getPriority: _getPriority,
5251
getTotalMem,
5352
getUserInfo,
@@ -67,17 +66,25 @@ function getCheckedFunction(fn) {
6766
});
6867
}
6968

69+
const[
70+
type,
71+
version,
72+
release
73+
]=_getOSInformation();
74+
7075
constgetHomeDirectory=getCheckedFunction(_getHomeDirectory);
7176
constgetHostname=getCheckedFunction(_getHostname);
7277
constgetInterfaceAddresses=getCheckedFunction(_getInterfaceAddresses);
73-
constgetOSRelease=getCheckedFunction(_getOSRelease);
74-
constgetOSType=getCheckedFunction(_getOSType);
78+
constgetOSRelease=()=>release;
79+
constgetOSType=()=>type;
80+
constgetOSVersion=()=>version;
7581

7682
getFreeMem[SymbolToPrimitive]=()=>getFreeMem();
7783
getHostname[SymbolToPrimitive]=()=>getHostname();
7884
getHomeDirectory[SymbolToPrimitive]=()=>getHomeDirectory();
7985
getOSRelease[SymbolToPrimitive]=()=>getOSRelease();
8086
getOSType[SymbolToPrimitive]=()=>getOSType();
87+
getOSVersion[SymbolToPrimitive]=()=>getOSVersion();
8188
getTotalMem[SymbolToPrimitive]=()=>getTotalMem();
8289
getUptime[SymbolToPrimitive]=()=>getUptime();
8390

@@ -283,6 +290,7 @@ module.exports = {
283290
tmpdir,
284291
totalmem: getTotalMem,
285292
type: getOSType,
293+
version: getOSVersion,
286294
userInfo,
287295
uptime: getUptime,
288296

‎src/node_os.cc‎

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static void GetHostname(const FunctionCallbackInfo<Value>& args) {
7676
}
7777

7878

79-
staticvoidGetOSType(const FunctionCallbackInfo<Value>& args) {
79+
staticvoidGetOSInformation(const FunctionCallbackInfo<Value>& args) {
8080
Environment* env = Environment::GetCurrent(args);
8181
uv_utsname_t info;
8282
int err = uv_os_uname(&info);
@@ -87,26 +87,16 @@ static void GetOSType(const FunctionCallbackInfo<Value>& args) {
8787
return args.GetReturnValue().SetUndefined();
8888
}
8989

90-
args.GetReturnValue().Set(
91-
String::NewFromUtf8(env->isolate(), info.sysname, NewStringType::kNormal)
92-
.ToLocalChecked());
93-
}
94-
95-
96-
staticvoidGetOSRelease(const FunctionCallbackInfo<Value>& args) {
97-
Environment* env = Environment::GetCurrent(args);
98-
uv_utsname_t info;
99-
int err = uv_os_uname(&info);
90+
// [sysname, version, release]
91+
Local<Value> osInformation[] = {
92+
String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(),
93+
String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(),
94+
String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked()
95+
};
10096

101-
if (err != 0) {
102-
CHECK_GE(args.Length(), 1);
103-
env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_os_uname");
104-
return args.GetReturnValue().SetUndefined();
105-
}
106-
107-
args.GetReturnValue().Set(
108-
String::NewFromUtf8(env->isolate(), info.release, NewStringType::kNormal)
109-
.ToLocalChecked());
97+
args.GetReturnValue().Set(Array::New(env->isolate(),
98+
osInformation,
99+
arraysize(osInformation)));
110100
}
111101

112102

@@ -398,8 +388,7 @@ void Initialize(Local<Object> target,
398388
env->SetMethod(target, "getTotalMem", GetTotalMemory);
399389
env->SetMethod(target, "getFreeMem", GetFreeMemory);
400390
env->SetMethod(target, "getCPUs", GetCPUInfo);
401-
env->SetMethod(target, "getOSType", GetOSType);
402-
env->SetMethod(target, "getOSRelease", GetOSRelease);
391+
env->SetMethod(target, "getOSInformation", GetOSInformation);
403392
env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses);
404393
env->SetMethod(target, "getHomeDirectory", GetHomeDirectory);
405394
env->SetMethod(target, "getUserInfo", GetUserInfo);

‎test/parallel/test-os.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ const home = os.homedir();
194194
is.string(home);
195195
assert.ok(home.includes(path.sep));
196196

197+
constversion=os.version();
198+
assert.strictEqual(typeofversion,'string');
199+
assert(version);
200+
197201
if(common.isWindows&&process.env.USERPROFILE){
198202
assert.strictEqual(home,process.env.USERPROFILE);
199203
deleteprocess.env.USERPROFILE;

0 commit comments

Comments
 (0)