summaryrefslogtreecommitdiff
path: root/lib/os.js
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2017-04-25 14:48:32 -0700
committerJames M Snell <jasnell@gmail.com>2017-05-01 11:44:56 -0700
commit473572ea2512cbe71e3423f8094657986352c32d (patch)
tree55e476006103b217a0148aa1fd2d2a106d8b72d8 /lib/os.js
parentea9eed56433c714b08930b906b292a21650b3928 (diff)
downloadandroid-node-v8-473572ea2512cbe71e3423f8094657986352c32d.tar.gz
android-node-v8-473572ea2512cbe71e3423f8094657986352c32d.tar.bz2
android-node-v8-473572ea2512cbe71e3423f8094657986352c32d.zip
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: https://github.com/nodejs/node/pull/12654 Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'lib/os.js')
-rw-r--r--lib/os.js123
1 files changed, 79 insertions, 44 deletions
diff --git a/lib/os.js b/lib/os.js
index 7de2e79c15..4a99cab81e 100644
--- a/lib/os.js
+++ b/lib/os.js
@@ -21,31 +21,50 @@
'use strict';
-const binding = process.binding('os');
-const getCPUs = binding.getCPUs;
-const getLoadAvg = binding.getLoadAvg;
const pushValToArrayMax = process.binding('util').pushValToArrayMax;
const constants = process.binding('constants').os;
-const internalUtil = require('internal/util');
+const deprecate = require('internal/util').deprecate;
const isWindows = process.platform === 'win32';
-exports.hostname = binding.getHostname;
-exports.uptime = binding.getUptime;
-exports.freemem = binding.getFreeMem;
-exports.totalmem = binding.getTotalMem;
-exports.type = binding.getOSType;
-exports.release = binding.getOSRelease;
-exports.networkInterfaces = binding.getInterfaceAddresses;
-exports.homedir = binding.getHomeDirectory;
-exports.userInfo = binding.getUserInfo;
+const {
+ getCPUs,
+ getFreeMem,
+ getHomeDirectory,
+ getHostname,
+ getInterfaceAddresses,
+ getLoadAvg,
+ getOSRelease,
+ getOSType,
+ getTotalMem,
+ getUserInfo,
+ getUptime,
+ isBigEndian
+} = process.binding('os');
+
+getFreeMem[Symbol.toPrimitive] = () => getFreeMem();
+getHostname[Symbol.toPrimitive] = () => getHostname();
+getHomeDirectory[Symbol.toPrimitive] = () => getHomeDirectory();
+getOSRelease[Symbol.toPrimitive] = () => getOSRelease();
+getOSType[Symbol.toPrimitive] = () => getOSType();
+getTotalMem[Symbol.toPrimitive] = () => getTotalMem();
+getUptime[Symbol.toPrimitive] = () => getUptime();
+
+const kEndianness = isBigEndian ? 'BE' : 'LE';
+
+const tmpDirDeprecationMsg =
+ 'os.tmpDir() is deprecated. Use os.tmpdir() instead.';
+
+const getNetworkInterfacesDepMsg =
+ 'os.getNetworkInterfaces is deprecated. Use os.networkInterfaces instead.';
const avgValues = new Float64Array(3);
-exports.loadavg = function loadavg() {
+const cpuValues = new Float64Array(6 * pushValToArrayMax);
+
+function loadavg() {
getLoadAvg(avgValues);
return [avgValues[0], avgValues[1], avgValues[2]];
-};
+}
-const cpuValues = new Float64Array(6 * pushValToArrayMax);
function addCPUInfo() {
for (var i = 0, c = 0; i < arguments.length; ++i, c += 6) {
this[this.length] = {
@@ -61,25 +80,22 @@ function addCPUInfo() {
};
}
}
-exports.cpus = function cpus() {
- return getCPUs(addCPUInfo, cpuValues, []);
-};
-Object.defineProperty(exports, 'constants', {
- configurable: false,
- enumerable: true,
- value: constants
-});
+function cpus() {
+ return getCPUs(addCPUInfo, cpuValues, []);
+}
-exports.arch = function() {
+function arch() {
return process.arch;
-};
+}
+arch[Symbol.toPrimitive] = () => process.arch;
-exports.platform = function() {
+function platform() {
return process.platform;
-};
+}
+platform[Symbol.toPrimitive] = () => process.platform;
-exports.tmpdir = function() {
+function tmpdir() {
var path;
if (isWindows) {
path = process.env.TEMP ||
@@ -97,22 +113,41 @@ exports.tmpdir = function() {
}
return path;
-};
+}
+tmpdir[Symbol.toPrimitive] = () => tmpdir();
-const tmpDirDeprecationMsg =
- 'os.tmpDir() is deprecated. Use os.tmpdir() instead.';
-exports.tmpDir = internalUtil.deprecate(exports.tmpdir,
- tmpDirDeprecationMsg,
- 'DEP0022');
+function endianness() {
+ return kEndianness;
+}
+endianness[Symbol.toPrimitive] = () => kEndianness;
-exports.getNetworkInterfaces = internalUtil.deprecate(function() {
- return exports.networkInterfaces();
-}, 'os.getNetworkInterfaces is deprecated. ' +
- 'Use os.networkInterfaces instead.', 'DEP0023');
+module.exports = exports = {
+ arch,
+ cpus,
+ EOL: isWindows ? '\r\n' : '\n',
+ endianness,
+ freemem: getFreeMem,
+ homedir: getHomeDirectory,
+ hostname: getHostname,
+ loadavg,
+ networkInterfaces: getInterfaceAddresses,
+ platform,
+ release: getOSRelease,
+ tmpdir,
+ totalmem: getTotalMem,
+ type: getOSType,
+ userInfo: getUserInfo,
+ uptime: getUptime,
-exports.EOL = isWindows ? '\r\n' : '\n';
+ // Deprecated APIs
+ getNetworkInterfaces: deprecate(getInterfaceAddresses,
+ getNetworkInterfacesDepMsg,
+ 'DEP0023'),
+ tmpDir: deprecate(tmpdir, tmpDirDeprecationMsg, 'DEP0022')
+};
-if (binding.isBigEndian)
- exports.endianness = function() { return 'BE'; };
-else
- exports.endianness = function() { return 'LE'; };
+Object.defineProperty(module.exports, 'constants', {
+ configurable: false,
+ enumerable: true,
+ value: constants
+});