Skip to content

Commit 1e1bcca

Browse files
committed
src: create a getter for kernel version
1 parent d63bcdd commit 1e1bcca

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

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, `pRtlGetVersion` 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: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ const {
4545
getHostname: _getHostname,
4646
getInterfaceAddresses: _getInterfaceAddresses,
4747
getLoadAvg,
48-
getOSRelease: _getOSRelease,
49-
getOSType: _getOSType,
5048
getPriority: _getPriority,
49+
getOSInformation: getInformation,
5150
getTotalMem,
5251
getUserInfo,
5352
getUptime,
@@ -66,17 +65,25 @@ function getCheckedFunction(fn) {
6665
});
6766
}
6867

68+
const [
69+
type,
70+
version,
71+
release
72+
] = getInformation();
73+
6974
const getHomeDirectory = getCheckedFunction(_getHomeDirectory);
7075
const getHostname = getCheckedFunction(_getHostname);
7176
const getInterfaceAddresses = getCheckedFunction(_getInterfaceAddresses);
72-
const getOSRelease = getCheckedFunction(_getOSRelease);
73-
const getOSType = getCheckedFunction(_getOSType);
77+
const getOSRelease = () => release;
78+
const getOSType = () => type;
79+
const getOSVersion = () => version;
7480

7581
getFreeMem[SymbolToPrimitive] = () => getFreeMem();
7682
getHostname[SymbolToPrimitive] = () => getHostname();
77-
getHomeDirectory[SymbolToPrimitive] = () => getHomeDirectory();
78-
getOSRelease[SymbolToPrimitive] = () => getOSRelease();
83+
getOSVersion[SymbolToPrimitive] = () => getOSVersion();
7984
getOSType[SymbolToPrimitive] = () => getOSType();
85+
getOSRelease[SymbolToPrimitive] = () => getOSRelease();
86+
getHomeDirectory[SymbolToPrimitive] = () => getHomeDirectory();
8087
getTotalMem[SymbolToPrimitive] = () => getTotalMem();
8188
getUptime[SymbolToPrimitive] = () => getUptime();
8289

@@ -281,6 +288,7 @@ module.exports = {
281288
type: getOSType,
282289
userInfo,
283290
uptime: getUptime,
291+
version: getOSVersion
284292
};
285293

286294
ObjectDefineProperties(module.exports, {

src/node_os.cc

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ static void GetHostname(const FunctionCallbackInfo<Value>& args) {
7575
.ToLocalChecked());
7676
}
7777

78-
79-
static void GetOSType(const FunctionCallbackInfo<Value>& args) {
78+
static void GetOSInformation(const FunctionCallbackInfo<Value>& args) {
8079
Environment* env = Environment::GetCurrent(args);
8180
uv_utsname_t info;
8281
int err = uv_os_uname(&info);
@@ -87,29 +86,18 @@ static void GetOSType(const FunctionCallbackInfo<Value>& args) {
8786
return args.GetReturnValue().SetUndefined();
8887
}
8988

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

107-
args.GetReturnValue().Set(
108-
String::NewFromUtf8(env->isolate(), info.release, NewStringType::kNormal)
109-
.ToLocalChecked());
96+
args.GetReturnValue().Set(Array::New(env->isolate(),
97+
osInformation,
98+
arraysize(osInformation)));
11099
}
111100

112-
113101
static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {
114102
Environment* env = Environment::GetCurrent(args);
115103
Isolate* isolate = env->isolate();
@@ -398,13 +386,12 @@ void Initialize(Local<Object> target,
398386
env->SetMethod(target, "getTotalMem", GetTotalMemory);
399387
env->SetMethod(target, "getFreeMem", GetFreeMemory);
400388
env->SetMethod(target, "getCPUs", GetCPUInfo);
401-
env->SetMethod(target, "getOSType", GetOSType);
402-
env->SetMethod(target, "getOSRelease", GetOSRelease);
403389
env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses);
404390
env->SetMethod(target, "getHomeDirectory", GetHomeDirectory);
405391
env->SetMethod(target, "getUserInfo", GetUserInfo);
406392
env->SetMethod(target, "setPriority", SetPriority);
407393
env->SetMethod(target, "getPriority", GetPriority);
394+
env->SetMethod(target, "getOSInformation", GetOSInformation);
408395
target->Set(env->context(),
409396
FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"),
410397
Boolean::New(env->isolate(), IsBigEndian())).Check();

test/parallel/test-os.js

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

197+
const version = os.version();
198+
assert.strictEqual(typeof version, 'string');
199+
197200
if (common.isWindows && process.env.USERPROFILE) {
198201
assert.strictEqual(home, process.env.USERPROFILE);
199202
delete process.env.USERPROFILE;

0 commit comments

Comments
 (0)