-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.ts
More file actions
134 lines (121 loc) · 3.74 KB
/
Copy pathprocess.ts
File metadata and controls
134 lines (121 loc) · 3.74 KB
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
/**
* @file Safe call-through accessors for the `process` global's methods and
* value reads. The `process` object reference is captured once at module load
* (immune to a later `globalThis.process = …` reassignment), but each method
* is CALLED at access time off that captured object — so `vi.spyOn(process,
* 'cwd')`, which mutates the same captured object, still intercepts. Binding
* the method reference instead (`process.cwd.bind(process)`) would freeze it
* and break that test seam, so we deliberately keep the late call. Consumers
* read cwd / platform / env / argv through these instead of touching
* `process` directly; enforced fleet-wide by
* `socket/prefer-process-primordial`. This is the `process` leaf of the
* node-module primordials: where `node/fs` / `node/path` lazy-load a `node:`
* module behind a function, this captures the always-present `process` global
* and routes its hot reads through one tamper-resistant surface.
*/
// The `process` object captured at module load. A later
// `globalThis.process = evil` cannot redirect reads that go through this
// reference; a `vi.spyOn(process, 'cwd')` (mutating this same object) still
// takes effect because the method is looked up at call time below.
const SafeProcess: NodeJS.Process = process
/**
* The CPU architecture token (`'x64'` / `'arm64'` / …).
*/
export function processArch(): string {
return SafeProcess.arch
}
/**
* The argv array (`[execPath, scriptPath, ...args]`).
*
* @example
* ;```typescript
* const entry = processArgv()[1]
* ```
*/
export function processArgv(): string[] {
return SafeProcess.argv
}
/**
* The current working directory. Call-through to the captured process's `cwd` —
* late-bound so test spies still intercept.
*
* @example
* ;```typescript
* const dir = processCwd()
* ```
*/
export function processCwd(): string {
return SafeProcess.cwd()
}
/**
* Emit a process warning. Call-through so a test spy on `process.emitWarning`
* still intercepts.
*/
export function processEmitWarning(
...args: Parameters<NodeJS.Process['emitWarning']>
): void {
SafeProcess.emitWarning(...args)
}
/**
* The process environment object. Returns the live `process.env` off the
* captured process (call-through, so a test that swaps `process.env` is seen).
*
* @example
* ;```typescript
* const token = processEnv()['SOCKET_API_TOKEN']
* ```
*/
export function processEnv(): NodeJS.ProcessEnv {
return SafeProcess.env
}
/**
* The absolute path to the Node executable (`process.execPath`).
*/
export function processExecPath(): string {
return SafeProcess.execPath
}
/**
* Schedule a callback on the next tick. Call-through (late-bound).
*/
export function processNextTick(
...args: Parameters<NodeJS.Process['nextTick']>
): void {
SafeProcess.nextTick(...args)
}
/**
* The process id.
*/
export function processPid(): number {
return SafeProcess.pid
}
/**
* The OS platform token (`'darwin'` / `'linux'` / `'win32'` / …).
*
* @example
* ;```typescript
* if (processPlatform() === 'win32') { … }
* ```
*/
export function processPlatform(): NodeJS.Platform {
return SafeProcess.platform
}
/**
* The standard error stream. Returned off the captured process so a test that
* spies on `process.stderr.write` still intercepts.
*/
export function processStderr(): NodeJS.WriteStream {
return SafeProcess.stderr
}
/**
* The standard output stream. Returned off the captured process so a test that
* spies on `process.stdout.write` still intercepts.
*/
export function processStdout(): NodeJS.WriteStream {
return SafeProcess.stdout
}
/**
* The Node version string (`process.version`, e.g. `'v26.2.0'`).
*/
export function processVersion(): string {
return SafeProcess.version
}