This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
os.js
273 lines (219 loc) · 5.32 KB
/
os.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* @module OS
*
* This module provides normalized system information from all the major
* operating systems.
*/
import { toProperCase } from './util.js'
import process from './process.js'
import ipc from './ipc.js'
const UNKNOWN = 'unknown'
const cache = {
arch: UNKNOWN,
type: UNKNOWN,
platform: UNKNOWN
}
export function arch () {
let value = UNKNOWN
if (cache.arch !== UNKNOWN) {
return cache.arch
}
if (typeof window !== 'object') {
if (typeof process?.arch === 'string') {
return process.arch
}
}
if (typeof window === 'object') {
value = (
process.arch ||
ipc.sendSync('os.arch')?.data ||
UNKNOWN
)
}
if (value === 'arm64') {
return value
}
cache.arch = value
.replace('x86_64', 'x64')
.replace('x86', 'ia32')
.replace(/arm.*/, 'arm')
return cache.arch
}
export function networkInterfaces () {
const now = Date.now()
if (cache.networkInterfaces && cache.networkInterfacesTTL > now) {
return cache.networkInterfaces
}
const interfaces = {}
const result = ipc.sendSync('os.networkInterfaces')
if (!result.data) return interfaces
const { ipv4, ipv6 } = result.data
for (const type in ipv4) {
const info = typeof ipv4[type] === 'string'
? { address: ipv4[type] }
: ipv4[type]
const { address } = info
const family = 'IPv4'
let internal = info.internal || false
let netmask = info.netmask || '255.255.255.0'
let cidr = `${address}/24`
let mac = info.mac || null
if (address === '127.0.0.1' || address === '0.0.0.0') {
internal = true
mac = '00:00:00:00:00:00'
if (address === '127.0.0.1') {
cidr = '127.0.0.1/8'
netmask = '255.0.0.0'
} else {
cidr = '0.0.0.0/0'
netmask = '0.0.0.0'
}
}
interfaces[type] = interfaces[type] || []
interfaces[type].push({
address,
netmask,
internal,
family,
cidr,
mac
})
}
for (const type in ipv6) {
const info = typeof ipv6[type] === 'string'
? { address: ipv6[type] }
: ipv6[type]
const { address } = info
const family = 'IPv6'
let internal = info.internal || false
let netmask = internal.netmask || 'ffff:ffff:ffff:ffff::'
let cidr = `${address}/64`
let mac = info.mac || null
if (address === '::1') {
internal = true
netmask = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
cidr = '::1/128'
mac = '00:00:00:00:00:00'
}
interfaces[type] = interfaces[type] || []
interfaces[type].push({
address,
netmask,
internal,
family,
cidr,
mac
})
}
cache.networkInterfaces = interfaces
cache.networkInterfacesTTL = Date.now() + 512
return interfaces
}
export function platform () {
let value = UNKNOWN
if (cache.platform !== UNKNOWN) {
return cache.platform
}
if (typeof window !== 'object') {
if (typeof process?.platform === 'string') {
return process.platform.toLowerCase()
}
}
if (typeof window === 'object') {
value = (
process.os ||
ipc.sendSync('os.platform')?.data ||
platform?.platform ||
UNKNOWN
)
}
cache.platform = value
.replace(/^mac/i, 'darwin')
.toLowerCase()
return cache.platform
}
export function type () {
let value = 'unknown'
if (cache.type !== UNKNOWN) {
return cache.type
}
if (typeof window !== 'object') {
switch (platform()) {
case 'android': return 'Linux'
case 'cygwin': return 'CYGWIN_NT'
case 'freebsd': return 'FreeBSD'
case 'linux': return 'Linux'
case 'openbsd': return 'OpenBSD'
case 'win32': return 'Windows_NT'
case 'ios': case 'mac': case 'Mac': case 'darwin': return 'Darwin'
}
}
if (typeof window === 'object') {
value = (
platform?.platform ||
ipc.sendSync('os.type')?.data ||
UNKNOWN
)
}
value = value.replace(/android/i, 'Linux')
value = value.replace(/ios/i, 'Darwin')
if (value !== UNKNOWN) {
value = toProperCase(value)
}
cache.type = value
return cache.type
}
export function isWindows () {
if ('isWindows' in cache) {
return cache.isWindows
}
cache.isWindows = /^win.*/i.test(type())
return cache.isWindows
}
export function tmpdir () {
let path = ''
if (isWindows()) {
path = (
process?.env?.TEMPDIR ||
process?.env?.TMPDIR ||
process?.env?.TEMP ||
process?.env?.TMP ||
(process?.env?.SystemRoot || process?.env?.windir || '') + '\\temp'
)
if (path.length > 1 && path.endsWith('\\') && !path.endsWith(':\\')) {
path = path.slice(0, -1)
}
} else {
path = (
process?.env?.TEMPDIR ||
process?.env?.TMPDIR ||
process?.env?.TEMP ||
process?.env?.TMP ||
''
)
// derive default
if (!path) {
if (platform() === 'ios') {
// @TODO(jwerle): use a path module
path = [process.cwd(), 'tmp'].join('/')
} else if (platform() === 'android') {
path = '/data/local/tmp'
} else {
path = '/tmp'
}
}
if (path.length > 1 && path.endsWith('/')) {
path = path.slice(0, -1)
}
}
return path
}
export const EOL = (() => {
if (isWindows()) {
return '\r\n'
}
return '\n'
})()
// eslint-disable-next-line
import * as exports from './os.js'
export default exports