Skip to content

Commit 473572e

Browse files
committed
os: refactor os structure, add Symbol.toPrimitive
Refactor the structure of the os module to use more efficient module.exports = {} pattern. Add Symbol.toPrimitive support to os methods that return simple primitives. This is a minor tweak that makes using these slightly more friendly when doing things like: ```js var m = `${os.tmpdir}/foo` ``` PR-URL: #12654 Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Alexey Orlenko <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent ea9eed5 commit 473572e

File tree

2 files changed

+88
-44
lines changed

2 files changed

+88
-44
lines changed

lib/os.js

+79-44
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,50 @@
2121

2222
'use strict';
2323

24-
const binding = process.binding('os');
25-
const getCPUs = binding.getCPUs;
26-
const getLoadAvg = binding.getLoadAvg;
2724
const pushValToArrayMax = process.binding('util').pushValToArrayMax;
2825
const constants = process.binding('constants').os;
29-
const internalUtil = require('internal/util');
26+
const deprecate = require('internal/util').deprecate;
3027
const isWindows = process.platform === 'win32';
3128

32-
exports.hostname = binding.getHostname;
33-
exports.uptime = binding.getUptime;
34-
exports.freemem = binding.getFreeMem;
35-
exports.totalmem = binding.getTotalMem;
36-
exports.type = binding.getOSType;
37-
exports.release = binding.getOSRelease;
38-
exports.networkInterfaces = binding.getInterfaceAddresses;
39-
exports.homedir = binding.getHomeDirectory;
40-
exports.userInfo = binding.getUserInfo;
29+
const {
30+
getCPUs,
31+
getFreeMem,
32+
getHomeDirectory,
33+
getHostname,
34+
getInterfaceAddresses,
35+
getLoadAvg,
36+
getOSRelease,
37+
getOSType,
38+
getTotalMem,
39+
getUserInfo,
40+
getUptime,
41+
isBigEndian
42+
} = process.binding('os');
43+
44+
getFreeMem[Symbol.toPrimitive] = () => getFreeMem();
45+
getHostname[Symbol.toPrimitive] = () => getHostname();
46+
getHomeDirectory[Symbol.toPrimitive] = () => getHomeDirectory();
47+
getOSRelease[Symbol.toPrimitive] = () => getOSRelease();
48+
getOSType[Symbol.toPrimitive] = () => getOSType();
49+
getTotalMem[Symbol.toPrimitive] = () => getTotalMem();
50+
getUptime[Symbol.toPrimitive] = () => getUptime();
51+
52+
const kEndianness = isBigEndian ? 'BE' : 'LE';
53+
54+
const tmpDirDeprecationMsg =
55+
'os.tmpDir() is deprecated. Use os.tmpdir() instead.';
56+
57+
const getNetworkInterfacesDepMsg =
58+
'os.getNetworkInterfaces is deprecated. Use os.networkInterfaces instead.';
4159

4260
const avgValues = new Float64Array(3);
43-
exports.loadavg = function loadavg() {
61+
const cpuValues = new Float64Array(6 * pushValToArrayMax);
62+
63+
function loadavg() {
4464
getLoadAvg(avgValues);
4565
return [avgValues[0], avgValues[1], avgValues[2]];
46-
};
66+
}
4767

48-
const cpuValues = new Float64Array(6 * pushValToArrayMax);
4968
function addCPUInfo() {
5069
for (var i = 0, c = 0; i < arguments.length; ++i, c += 6) {
5170
this[this.length] = {
@@ -61,25 +80,22 @@ function addCPUInfo() {
6180
};
6281
}
6382
}
64-
exports.cpus = function cpus() {
65-
return getCPUs(addCPUInfo, cpuValues, []);
66-
};
6783

68-
Object.defineProperty(exports, 'constants', {
69-
configurable: false,
70-
enumerable: true,
71-
value: constants
72-
});
84+
function cpus() {
85+
return getCPUs(addCPUInfo, cpuValues, []);
86+
}
7387

74-
exports.arch = function() {
88+
function arch() {
7589
return process.arch;
76-
};
90+
}
91+
arch[Symbol.toPrimitive] = () => process.arch;
7792

78-
exports.platform = function() {
93+
function platform() {
7994
return process.platform;
80-
};
95+
}
96+
platform[Symbol.toPrimitive] = () => process.platform;
8197

82-
exports.tmpdir = function() {
98+
function tmpdir() {
8399
var path;
84100
if (isWindows) {
85101
path = process.env.TEMP ||
@@ -97,22 +113,41 @@ exports.tmpdir = function() {
97113
}
98114

99115
return path;
100-
};
116+
}
117+
tmpdir[Symbol.toPrimitive] = () => tmpdir();
101118

102-
const tmpDirDeprecationMsg =
103-
'os.tmpDir() is deprecated. Use os.tmpdir() instead.';
104-
exports.tmpDir = internalUtil.deprecate(exports.tmpdir,
105-
tmpDirDeprecationMsg,
106-
'DEP0022');
119+
function endianness() {
120+
return kEndianness;
121+
}
122+
endianness[Symbol.toPrimitive] = () => kEndianness;
107123

108-
exports.getNetworkInterfaces = internalUtil.deprecate(function() {
109-
return exports.networkInterfaces();
110-
}, 'os.getNetworkInterfaces is deprecated. ' +
111-
'Use os.networkInterfaces instead.', 'DEP0023');
124+
module.exports = exports = {
125+
arch,
126+
cpus,
127+
EOL: isWindows ? '\r\n' : '\n',
128+
endianness,
129+
freemem: getFreeMem,
130+
homedir: getHomeDirectory,
131+
hostname: getHostname,
132+
loadavg,
133+
networkInterfaces: getInterfaceAddresses,
134+
platform,
135+
release: getOSRelease,
136+
tmpdir,
137+
totalmem: getTotalMem,
138+
type: getOSType,
139+
userInfo: getUserInfo,
140+
uptime: getUptime,
112141

113-
exports.EOL = isWindows ? '\r\n' : '\n';
142+
// Deprecated APIs
143+
getNetworkInterfaces: deprecate(getInterfaceAddresses,
144+
getNetworkInterfacesDepMsg,
145+
'DEP0023'),
146+
tmpDir: deprecate(tmpdir, tmpDirDeprecationMsg, 'DEP0022')
147+
};
114148

115-
if (binding.isBigEndian)
116-
exports.endianness = function() { return 'BE'; };
117-
else
118-
exports.endianness = function() { return 'LE'; };
149+
Object.defineProperty(module.exports, 'constants', {
150+
configurable: false,
151+
enumerable: true,
152+
value: constants
153+
});

test/parallel/test-os.js

+9
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,12 @@ is.string(pwd.username);
191191
assert.ok(pwd.homedir.includes(path.sep));
192192
assert.strictEqual(pwd.username, pwdBuf.username.toString('utf8'));
193193
assert.strictEqual(pwd.homedir, pwdBuf.homedir.toString('utf8'));
194+
195+
// Test that the Symbol.toPrimitive functions work correctly
196+
[
197+
[`${os.hostname}`, os.hostname()],
198+
[`${os.homedir}`, os.homedir()],
199+
[`${os.release}`, os.release()],
200+
[`${os.type}`, os.type()],
201+
[`${os.endianness}`, os.endianness()]
202+
].forEach((set) => assert.strictEqual(set[0], set[1]));

0 commit comments

Comments
 (0)