21
21
22
22
'use strict' ;
23
23
24
- const binding = process . binding ( 'os' ) ;
25
- const getCPUs = binding . getCPUs ;
26
- const getLoadAvg = binding . getLoadAvg ;
27
24
const pushValToArrayMax = process . binding ( 'util' ) . pushValToArrayMax ;
28
25
const constants = process . binding ( 'constants' ) . os ;
29
- const internalUtil = require ( 'internal/util' ) ;
26
+ const deprecate = require ( 'internal/util' ) . deprecate ;
30
27
const isWindows = process . platform === 'win32' ;
31
28
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.' ;
41
59
42
60
const avgValues = new Float64Array ( 3 ) ;
43
- exports . loadavg = function loadavg ( ) {
61
+ const cpuValues = new Float64Array ( 6 * pushValToArrayMax ) ;
62
+
63
+ function loadavg ( ) {
44
64
getLoadAvg ( avgValues ) ;
45
65
return [ avgValues [ 0 ] , avgValues [ 1 ] , avgValues [ 2 ] ] ;
46
- } ;
66
+ }
47
67
48
- const cpuValues = new Float64Array ( 6 * pushValToArrayMax ) ;
49
68
function addCPUInfo ( ) {
50
69
for ( var i = 0 , c = 0 ; i < arguments . length ; ++ i , c += 6 ) {
51
70
this [ this . length ] = {
@@ -61,25 +80,22 @@ function addCPUInfo() {
61
80
} ;
62
81
}
63
82
}
64
- exports . cpus = function cpus ( ) {
65
- return getCPUs ( addCPUInfo , cpuValues , [ ] ) ;
66
- } ;
67
83
68
- Object . defineProperty ( exports , 'constants' , {
69
- configurable : false ,
70
- enumerable : true ,
71
- value : constants
72
- } ) ;
84
+ function cpus ( ) {
85
+ return getCPUs ( addCPUInfo , cpuValues , [ ] ) ;
86
+ }
73
87
74
- exports . arch = function ( ) {
88
+ function arch ( ) {
75
89
return process . arch ;
76
- } ;
90
+ }
91
+ arch [ Symbol . toPrimitive ] = ( ) => process . arch ;
77
92
78
- exports . platform = function ( ) {
93
+ function platform ( ) {
79
94
return process . platform ;
80
- } ;
95
+ }
96
+ platform [ Symbol . toPrimitive ] = ( ) => process . platform ;
81
97
82
- exports . tmpdir = function ( ) {
98
+ function tmpdir ( ) {
83
99
var path ;
84
100
if ( isWindows ) {
85
101
path = process . env . TEMP ||
@@ -97,22 +113,41 @@ exports.tmpdir = function() {
97
113
}
98
114
99
115
return path ;
100
- } ;
116
+ }
117
+ tmpdir [ Symbol . toPrimitive ] = ( ) => tmpdir ( ) ;
101
118
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 ;
107
123
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 ,
112
141
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
+ } ;
114
148
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
+ } ) ;
0 commit comments