mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
src: create a getter for kernel version
PR-URL: https://github.com/nodejs/node/pull/31732 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
0a539ddc0a
commit
c1e672595f
4 files changed
+44
-31
No files matched your search
@@ -389,6 +389,20 @@ operating system response.
|
||||
|
||||
Throws a [`SystemError`][] if a user has no `username` or `homedir`.
|
||||
|
||||
## `os.version()`
|
||||
<!-- YAML
|
||||
added: REPLACEME
|
||||
-->
|
||||
|
||||
* Returns {string}
|
||||
|
||||
Returns a string identifying the kernel version.
|
||||
|
||||
On POSIX systems, the operating system release is determined by calling
|
||||
[uname(3)][]. On Windows, `pRtlGetVersion` 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`.
|
||||
|
||||
@@ -45,9 +45,8 @@ const {
|
||||
getHostname: _getHostname,
|
||||
getInterfaceAddresses: _getInterfaceAddresses,
|
||||
getLoadAvg,
|
||||
getOSRelease: _getOSRelease,
|
||||
getOSType: _getOSType,
|
||||
getPriority: _getPriority,
|
||||
getOSInformation: _getOSInformation,
|
||||
getTotalMem,
|
||||
getUserInfo,
|
||||
getUptime,
|
||||
@@ -66,17 +65,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();
|
||||
getOSVersion[SymbolToPrimitive] = () => getOSVersion();
|
||||
getOSType[SymbolToPrimitive] = () => getOSType();
|
||||
getOSRelease[SymbolToPrimitive] = () => getOSRelease();
|
||||
getHomeDirectory[SymbolToPrimitive] = () => getHomeDirectory();
|
||||
getTotalMem[SymbolToPrimitive] = () => getTotalMem();
|
||||
getUptime[SymbolToPrimitive] = () => getUptime();
|
||||
|
||||
@@ -281,6 +288,7 @@ module.exports = {
|
||||
type: getOSType,
|
||||
userInfo,
|
||||
uptime: getUptime,
|
||||
version: getOSVersion
|
||||
};
|
||||
|
||||
ObjectDefineProperties(module.exports, {
|
||||
|
||||
+12
-25
@@ -75,8 +75,7 @@ static void GetHostname(const FunctionCallbackInfo<Value>& args) {
|
||||
.ToLocalChecked());
|
||||
}
|
||||
|
||||
|
||||
static void GetOSType(const FunctionCallbackInfo<Value>& args) {
|
||||
static void GetOSInformation(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
uv_utsname_t info;
|
||||
int err = uv_os_uname(&info);
|
||||
@@ -87,29 +86,18 @@ static void GetOSType(const FunctionCallbackInfo<Value>& args) {
|
||||
return args.GetReturnValue().SetUndefined();
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(
|
||||
String::NewFromUtf8(env->isolate(), info.sysname, NewStringType::kNormal)
|
||||
.ToLocalChecked());
|
||||
// [sysname, version, release]
|
||||
Local<Value> osInformation[] = {
|
||||
String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(),
|
||||
String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(),
|
||||
String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked()
|
||||
};
|
||||
|
||||
args.GetReturnValue().Set(Array::New(env->isolate(),
|
||||
osInformation,
|
||||
arraysize(osInformation)));
|
||||
}
|
||||
|
||||
|
||||
static void GetOSRelease(const FunctionCallbackInfo<Value>& 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());
|
||||
}
|
||||
|
||||
|
||||
static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
Isolate* isolate = env->isolate();
|
||||
@@ -398,13 +386,12 @@ void Initialize(Local<Object> 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, "getInterfaceAddresses", GetInterfaceAddresses);
|
||||
env->SetMethod(target, "getHomeDirectory", GetHomeDirectory);
|
||||
env->SetMethod(target, "getUserInfo", GetUserInfo);
|
||||
env->SetMethod(target, "setPriority", SetPriority);
|
||||
env->SetMethod(target, "getPriority", GetPriority);
|
||||
env->SetMethod(target, "getOSInformation", GetOSInformation);
|
||||
target->Set(env->context(),
|
||||
FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"),
|
||||
Boolean::New(env->isolate(), IsBigEndian())).Check();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user