-
Notifications
You must be signed in to change notification settings - Fork 70
/
process.js
130 lines (114 loc) · 3.06 KB
/
process.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
/**
* @module Process
*
* Example usage:
* ```js
* import process from 'socket:process'
* ```
*/
import { primordials, send } from './ipc.js'
import { EventEmitter } from './events.js'
import os from './os.js'
let didEmitExitEvent = false
class Process extends EventEmitter {
arch = primordials.arch
argv = globalThis.__args?.argv ?? []
argv0 = globalThis.__args?.argv?.[0] ?? ''
cwd = () => primordials.cwd
env = { ...(globalThis.__args?.env ?? {}) }
exit = exit
homedir = homedir
platform = primordials.platform
version = primordials.version
}
const isNode = Boolean(globalThis.process?.versions?.node)
const process = isNode
? globalThis.process
: new Process()
if (!isNode) {
EventEmitter.call(process)
}
export default process
/**
* Adds callback to the 'nextTick' queue.
* @param {Function} callback
*/
export function nextTick (callback) {
if (typeof process.nextTick === 'function' && process.nextTick !== nextTick) {
process.nextTick(callback)
} else if (typeof globalThis.setImmediate === 'function') {
globalThis.setImmediate(callback)
} else if (typeof globalThis.queueMicrotask === 'function') {
globalThis.queueMicrotask(() => {
try {
callback()
} catch (err) {
setTimeout(() => { throw err })
}
})
} else if (typeof globalThis.setTimeout === 'function') {
globalThis.setTimeout(callback)
} else if (typeof globalThis.requestAnimationFrame === 'function') {
globalThis.requestAnimationFrame(callback)
} else {
throw new TypeError('\'process.nextTick\' is not supported in environment.')
}
}
if (typeof process.nextTick !== 'function') {
process.nextTick = nextTick
}
/**
* @returns {string} The home directory of the current user.
*/
export function homedir () {
return globalThis.__args.env.HOME ?? ''
}
/**
* Computed high resolution time as a `BigInt`.
* @param {Array<number>?} [time]
* @return {bigint}
*/
export function hrtime (time = [0, 0]) {
if (!time) time = [0, 0]
if (time && (!Array.isArray(time) || time.length !== 2)) {
throw new TypeError('Expecting time to be an array of 2 numbers.')
}
const value = os.hrtime()
const seconds = BigInt(1e9)
const x = value / seconds
const y = value - (x * seconds)
return [Number(x) - time[0], Number(y) - time[1]]
}
hrtime.bigint = function bigint () {
return os.hrtime()
}
if (typeof process.hrtime !== 'function') {
process.hrtime = hrtime
}
/**
* @param {number=} [code=0] - The exit code. Default: 0.
*/
export async function exit (code) {
if (!didEmitExitEvent) {
didEmitExitEvent = true
queueMicrotask(() => process.emit('exit', code))
await send('application.exit', { value: code ?? 0 })
}
}
/**
* Returns an object describing the memory usage of the Node.js process measured in bytes.
* @returns {Object}
*/
export function memoryUsage () {
const rss = memoryUsage.rss()
return {
rss
}
}
if (typeof process.memoryUsage !== 'function') {
process.memoryUsage = memoryUsage
}
memoryUsage.rss = function rss () {
const rusage = os.rusage()
return rusage.ru_maxrss
}