deno.land / std@0.166.0 / node / os.ts

نووسراو ببینە
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Copyright Joyent, Inc. and other Node contributors.//// Permission is hereby granted, free of charge, to any person obtaining a// copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit// persons to whom the Software is furnished to do so, subject to the// following conditions://// The above copyright notice and this permission notice shall be included// in all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.
import { notImplemented } from "./_utils.ts";import { validateIntegerRange } from "./_utils.ts";import { EOL as fsEOL } from "../fs/eol.ts";import process from "./process.ts";import { isWindows, osType } from "../_util/os.ts";import { os } from "./internal_binding/constants.ts";
export const constants = os;
const SEE_GITHUB_ISSUE = "See https://github.com/denoland/deno_std/issues/1436";
interface CPUTimes { /** The number of milliseconds the CPU has spent in user mode */ user: number;
/** The number of milliseconds the CPU has spent in nice mode */ nice: number;
/** The number of milliseconds the CPU has spent in sys mode */ sys: number;
/** The number of milliseconds the CPU has spent in idle mode */ idle: number;
/** The number of milliseconds the CPU has spent in irq mode */ irq: number;}
interface CPUCoreInfo { model: string;
/** in MHz */ speed: number;
times: CPUTimes;}
interface NetworkAddress { /** The assigned IPv4 or IPv6 address */ address: string;
/** The IPv4 or IPv6 network mask */ netmask: string;
family: "IPv4" | "IPv6";
/** The MAC address of the network interface */ mac: string;
/** true if the network interface is a loopback or similar interface that is not remotely accessible; otherwise false */ internal: boolean;
/** The numeric IPv6 scope ID (only specified when family is IPv6) */ scopeid?: number;
/** The assigned IPv4 or IPv6 address with the routing prefix in CIDR notation. If the netmask is invalid, this property is set to null. */ cidr: string;}
interface NetworkInterfaces { [key: string]: NetworkAddress[];}
export interface UserInfoOptions { encoding: string;}
interface UserInfo { username: string; uid: number; gid: number; shell: string; homedir: string;}
export function arch(): string { return process.arch;}
// deno-lint-ignore no-explicit-any(arch as any)[Symbol.toPrimitive] = (): string => process.arch;// deno-lint-ignore no-explicit-any(endianness as any)[Symbol.toPrimitive] = (): string => endianness();// deno-lint-ignore no-explicit-any(freemem as any)[Symbol.toPrimitive] = (): number => freemem();// deno-lint-ignore no-explicit-any(homedir as any)[Symbol.toPrimitive] = (): string | null => homedir();// deno-lint-ignore no-explicit-any(hostname as any)[Symbol.toPrimitive] = (): string | null => hostname();// deno-lint-ignore no-explicit-any(platform as any)[Symbol.toPrimitive] = (): string => platform();// deno-lint-ignore no-explicit-any(release as any)[Symbol.toPrimitive] = (): string => release();// deno-lint-ignore no-explicit-any(totalmem as any)[Symbol.toPrimitive] = (): number => totalmem();// deno-lint-ignore no-explicit-any(type as any)[Symbol.toPrimitive] = (): string => type();// deno-lint-ignore no-explicit-any(uptime as any)[Symbol.toPrimitive] = (): number => uptime();
export function cpus(): CPUCoreInfo[] { return Array.from(Array(navigator.hardwareConcurrency)).map(() => { return { model: "", speed: 0, times: { user: 0, nice: 0, sys: 0, idle: 0, irq: 0, }, }; });}
/** * Returns a string identifying the endianness of the CPU for which the Deno * binary was compiled. Possible values are 'BE' for big endian and 'LE' for * little endian. */export function endianness(): "BE" | "LE" { // Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView#Endianness const buffer = new ArrayBuffer(2); new DataView(buffer).setInt16(0, 256, true /* littleEndian */); // Int16Array uses the platform's endianness. return new Int16Array(buffer)[0] === 256 ? "LE" : "BE";}
/** Return free memory amount */export function freemem(): number { return Deno.systemMemoryInfo().free;}
/** Not yet implemented */export function getPriority(pid = 0): number { validateIntegerRange(pid, "pid"); notImplemented(SEE_GITHUB_ISSUE);}
/** Returns the string path of the current user's home directory. */export function homedir(): string | null { // Note: Node/libuv calls getpwuid() / GetUserProfileDirectory() when the // environment variable isn't set but that's the (very uncommon) fallback // path. IMO, it's okay to punt on that for now. switch (osType) { case "windows": return Deno.env.get("USERPROFILE") || null; case "linux": case "darwin": case "freebsd": return Deno.env.get("HOME") || null; default: throw Error("unreachable"); }}
/** Returns the host name of the operating system as a string. */export function hostname(): string { return Deno.hostname();}
/** Returns an array containing the 1, 5, and 15 minute load averages */export function loadavg(): number[] { if (isWindows) { return [0, 0, 0]; } return Deno.loadavg();}
/** Returns an object containing network interfaces that have been assigned a network address. * Each key on the returned object identifies a network interface. The associated value is an array of objects that each describe an assigned network address. */export function networkInterfaces(): NetworkInterfaces { const interfaces: NetworkInterfaces = {}; for ( const { name, address, netmask, family, mac, scopeid, cidr } of Deno .networkInterfaces() ) { const addresses = interfaces[name] ||= []; const networkAddress: NetworkAddress = { address, netmask, family, mac, internal: (family === "IPv4" && isIPv4LoopbackAddr(address)) || (family === "IPv6" && isIPv6LoopbackAddr(address)), cidr, }; if (family === "IPv6") { networkAddress.scopeid = scopeid!; } addresses.push(networkAddress); } return interfaces;}
function isIPv4LoopbackAddr(addr: string) { return addr.startsWith("127");}
function isIPv6LoopbackAddr(addr: string) { return addr === "::1" || addr === "fe80::1";}
/** Returns the a string identifying the operating system platform. The value is set at compile time. Possible values are 'darwin', 'linux', and 'win32'. */export function platform(): string { return process.platform;}
/** Returns the operating system as a string */export function release(): string { return Deno.osRelease();}
/** Not yet implemented */export function setPriority(pid: number, priority?: number) { /* The node API has the 'pid' as the first parameter and as optional. This makes for a problematic implementation in Typescript. */ if (priority === undefined) { priority = pid; pid = 0; } validateIntegerRange(pid, "pid"); validateIntegerRange(priority, "priority", -20, 19);
notImplemented(SEE_GITHUB_ISSUE);}
/** Returns the operating system's default directory for temporary files as a string. */export function tmpdir(): string | null { /* This follows the node js implementation, but has a few differences: * On windows, if none of the environment variables are defined, we return null. * On unix we use a plain Deno.env.get, instead of safeGetenv, which special cases setuid binaries. * Node removes a single trailing / or \, we remove all. */ if (isWindows) { const temp = Deno.env.get("TEMP") || Deno.env.get("TMP"); if (temp) { return temp.replace(/(?<!:)[/\\]*$/, ""); } const base = Deno.env.get("SYSTEMROOT") || Deno.env.get("WINDIR"); if (base) { return base + "\\temp"; } return null; } else { // !isWindows const temp = Deno.env.get("TMPDIR") || Deno.env.get("TMP") || Deno.env.get("TEMP") || "/tmp"; return temp.replace(/(?<!^)\/*$/, ""); }}
/** Return total physical memory amount */export function totalmem(): number { return Deno.systemMemoryInfo().total;}
/** Returns operating system type (i.e. 'Windows_NT', 'Linux', 'Darwin') */export function type(): string { switch (Deno.build.os as string) { case "windows": return "Windows_NT"; case "linux": return "Linux"; case "darwin": return "Darwin"; case "freebsd": return "FreeBSD"; default: throw Error("unreachable"); }}
/** Not yet implemented */export function uptime(): number { notImplemented(SEE_GITHUB_ISSUE);}
/** Not yet implemented */export function userInfo( // deno-lint-ignore no-unused-vars options: UserInfoOptions = { encoding: "utf-8" },): UserInfo { notImplemented(SEE_GITHUB_ISSUE);}
export const EOL = isWindows ? fsEOL.CRLF : fsEOL.LF;export const devNull = isWindows ? "\\\\.\\nul" : "/dev/null";
export default { arch, cpus, endianness, freemem, getPriority, homedir, hostname, loadavg, networkInterfaces, platform, release, setPriority, tmpdir, totalmem, type, uptime, userInfo, constants, EOL, devNull,};
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago