@scelar/nodepod 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/LICENSE +43 -0
  2. package/README.md +240 -0
  3. package/dist/child_process-BJOMsZje.js +8233 -0
  4. package/dist/child_process-BJOMsZje.js.map +1 -0
  5. package/dist/child_process-Cj8vOcuc.cjs +7434 -0
  6. package/dist/child_process-Cj8vOcuc.cjs.map +1 -0
  7. package/dist/index-Cb1Cgdnd.js +35308 -0
  8. package/dist/index-Cb1Cgdnd.js.map +1 -0
  9. package/dist/index-DsMGS-xc.cjs +37195 -0
  10. package/dist/index-DsMGS-xc.cjs.map +1 -0
  11. package/dist/index.cjs +65 -0
  12. package/dist/index.cjs.map +1 -0
  13. package/dist/index.mjs +59 -0
  14. package/dist/index.mjs.map +1 -0
  15. package/package.json +95 -0
  16. package/src/__tests__/smoke.test.ts +11 -0
  17. package/src/constants/cdn-urls.ts +18 -0
  18. package/src/constants/config.ts +236 -0
  19. package/src/cross-origin.ts +26 -0
  20. package/src/engine-factory.ts +176 -0
  21. package/src/engine-types.ts +56 -0
  22. package/src/helpers/byte-encoding.ts +39 -0
  23. package/src/helpers/digest.ts +9 -0
  24. package/src/helpers/event-loop.ts +96 -0
  25. package/src/helpers/wasm-cache.ts +133 -0
  26. package/src/iframe-sandbox.ts +141 -0
  27. package/src/index.ts +192 -0
  28. package/src/isolation-helpers.ts +148 -0
  29. package/src/memory-volume.ts +941 -0
  30. package/src/module-transformer.ts +368 -0
  31. package/src/packages/archive-extractor.ts +248 -0
  32. package/src/packages/browser-bundler.ts +284 -0
  33. package/src/packages/installer.ts +396 -0
  34. package/src/packages/registry-client.ts +131 -0
  35. package/src/packages/version-resolver.ts +411 -0
  36. package/src/polyfills/assert.ts +384 -0
  37. package/src/polyfills/async_hooks.ts +144 -0
  38. package/src/polyfills/buffer.ts +628 -0
  39. package/src/polyfills/child_process.ts +2288 -0
  40. package/src/polyfills/chokidar.ts +336 -0
  41. package/src/polyfills/cluster.ts +106 -0
  42. package/src/polyfills/console.ts +136 -0
  43. package/src/polyfills/constants.ts +123 -0
  44. package/src/polyfills/crypto.ts +885 -0
  45. package/src/polyfills/dgram.ts +87 -0
  46. package/src/polyfills/diagnostics_channel.ts +76 -0
  47. package/src/polyfills/dns.ts +134 -0
  48. package/src/polyfills/domain.ts +68 -0
  49. package/src/polyfills/esbuild.ts +854 -0
  50. package/src/polyfills/events.ts +276 -0
  51. package/src/polyfills/fs.ts +2888 -0
  52. package/src/polyfills/fsevents.ts +79 -0
  53. package/src/polyfills/http.ts +1449 -0
  54. package/src/polyfills/http2.ts +199 -0
  55. package/src/polyfills/https.ts +76 -0
  56. package/src/polyfills/inspector.ts +62 -0
  57. package/src/polyfills/lightningcss.ts +105 -0
  58. package/src/polyfills/module.ts +191 -0
  59. package/src/polyfills/net.ts +353 -0
  60. package/src/polyfills/os.ts +238 -0
  61. package/src/polyfills/path.ts +206 -0
  62. package/src/polyfills/perf_hooks.ts +102 -0
  63. package/src/polyfills/process.ts +690 -0
  64. package/src/polyfills/punycode.ts +159 -0
  65. package/src/polyfills/querystring.ts +93 -0
  66. package/src/polyfills/quic.ts +118 -0
  67. package/src/polyfills/readdirp.ts +229 -0
  68. package/src/polyfills/readline.ts +692 -0
  69. package/src/polyfills/repl.ts +134 -0
  70. package/src/polyfills/rollup.ts +119 -0
  71. package/src/polyfills/sea.ts +33 -0
  72. package/src/polyfills/sqlite.ts +78 -0
  73. package/src/polyfills/stream.ts +1620 -0
  74. package/src/polyfills/string_decoder.ts +25 -0
  75. package/src/polyfills/tailwindcss-oxide.ts +309 -0
  76. package/src/polyfills/test.ts +197 -0
  77. package/src/polyfills/timers.ts +32 -0
  78. package/src/polyfills/tls.ts +105 -0
  79. package/src/polyfills/trace_events.ts +50 -0
  80. package/src/polyfills/tty.ts +71 -0
  81. package/src/polyfills/url.ts +174 -0
  82. package/src/polyfills/util.ts +559 -0
  83. package/src/polyfills/v8.ts +126 -0
  84. package/src/polyfills/vm.ts +132 -0
  85. package/src/polyfills/volume-registry.ts +15 -0
  86. package/src/polyfills/wasi.ts +44 -0
  87. package/src/polyfills/worker_threads.ts +326 -0
  88. package/src/polyfills/ws.ts +595 -0
  89. package/src/polyfills/zlib.ts +881 -0
  90. package/src/request-proxy.ts +716 -0
  91. package/src/script-engine.ts +3375 -0
  92. package/src/sdk/nodepod-fs.ts +93 -0
  93. package/src/sdk/nodepod-process.ts +86 -0
  94. package/src/sdk/nodepod-terminal.ts +350 -0
  95. package/src/sdk/nodepod.ts +509 -0
  96. package/src/sdk/types.ts +70 -0
  97. package/src/shell/commands/bun.ts +121 -0
  98. package/src/shell/commands/directory.ts +297 -0
  99. package/src/shell/commands/file-ops.ts +525 -0
  100. package/src/shell/commands/git.ts +2142 -0
  101. package/src/shell/commands/node.ts +80 -0
  102. package/src/shell/commands/npm.ts +198 -0
  103. package/src/shell/commands/pm-types.ts +45 -0
  104. package/src/shell/commands/pnpm.ts +82 -0
  105. package/src/shell/commands/search.ts +264 -0
  106. package/src/shell/commands/shell-env.ts +352 -0
  107. package/src/shell/commands/text-processing.ts +1152 -0
  108. package/src/shell/commands/yarn.ts +84 -0
  109. package/src/shell/shell-builtins.ts +19 -0
  110. package/src/shell/shell-helpers.ts +250 -0
  111. package/src/shell/shell-interpreter.ts +514 -0
  112. package/src/shell/shell-parser.ts +429 -0
  113. package/src/shell/shell-types.ts +85 -0
  114. package/src/syntax-transforms.ts +561 -0
  115. package/src/threading/engine-worker.ts +64 -0
  116. package/src/threading/inline-worker.ts +372 -0
  117. package/src/threading/offload-types.ts +112 -0
  118. package/src/threading/offload-worker.ts +383 -0
  119. package/src/threading/offload.ts +271 -0
  120. package/src/threading/process-context.ts +92 -0
  121. package/src/threading/process-handle.ts +275 -0
  122. package/src/threading/process-manager.ts +956 -0
  123. package/src/threading/process-worker-entry.ts +854 -0
  124. package/src/threading/shared-vfs.ts +352 -0
  125. package/src/threading/sync-channel.ts +135 -0
  126. package/src/threading/task-queue.ts +177 -0
  127. package/src/threading/vfs-bridge.ts +231 -0
  128. package/src/threading/worker-pool.ts +233 -0
  129. package/src/threading/worker-protocol.ts +358 -0
  130. package/src/threading/worker-vfs.ts +218 -0
  131. package/src/types/externals.d.ts +38 -0
  132. package/src/types/fs-streams.ts +142 -0
  133. package/src/types/manifest.ts +17 -0
  134. package/src/worker-sandbox.ts +90 -0
@@ -0,0 +1,690 @@
1
+ // Process polyfill -- buildProcessEnv() constructs a full process object
2
+
3
+ import { EventEmitter, EventHandler } from "./events";
4
+ import {
5
+ VERSIONS,
6
+ NODE_SUB_VERSIONS,
7
+ MOCK_OS,
8
+ MOCK_PROCESS,
9
+ MOCK_MEMORY,
10
+ DEFAULT_ENV,
11
+ DEFAULT_TERMINAL,
12
+ } from "../constants/config";
13
+
14
+ // capture before engine wrapper overrides globalThis.console
15
+ const _nativeConsole = console;
16
+
17
+ export interface ProcessEnvVars {
18
+ [key: string]: string | undefined;
19
+ }
20
+
21
+ interface OutputStreamBridge {
22
+ isTTY: boolean;
23
+ columns: number;
24
+ rows: number;
25
+ write: (data: string | Buffer, encoding?: string, cb?: () => void) => boolean;
26
+ end?: (data?: string, cb?: () => void) => void;
27
+ on: (evt: string, fn: EventHandler) => OutputStreamBridge;
28
+ once: (evt: string, fn: EventHandler) => OutputStreamBridge;
29
+ off: (evt: string, fn: EventHandler) => OutputStreamBridge;
30
+ emit: (evt: string, ...args: unknown[]) => boolean;
31
+ addListener: (evt: string, fn: EventHandler) => OutputStreamBridge;
32
+ removeListener: (evt: string, fn: EventHandler) => OutputStreamBridge;
33
+ removeAllListeners: (evt?: string) => OutputStreamBridge;
34
+ setMaxListeners: (n: number) => OutputStreamBridge;
35
+ getMaxListeners: () => number;
36
+ listenerCount: (evt: string) => number;
37
+ listeners: (evt: string) => EventHandler[];
38
+ rawListeners: (evt: string) => EventHandler[];
39
+ prependListener: (evt: string, fn: EventHandler) => OutputStreamBridge;
40
+ prependOnceListener: (evt: string, fn: EventHandler) => OutputStreamBridge;
41
+ eventNames: () => string[];
42
+ pause?: () => OutputStreamBridge;
43
+ resume?: () => OutputStreamBridge;
44
+ setEncoding?: (enc: string) => OutputStreamBridge;
45
+ clearLine?: (dir: number, cb?: () => void) => boolean;
46
+ cursorTo?: (x: number, y?: number, cb?: () => void) => boolean;
47
+ moveCursor?: (dx: number, dy: number, cb?: () => void) => boolean;
48
+ getWindowSize?: () => [number, number];
49
+ getColorDepth?: (env?: Record<string, string>) => number;
50
+ hasColors?: (
51
+ countOrEnv?: number | Record<string, string>,
52
+ env?: Record<string, string>,
53
+ ) => boolean;
54
+ }
55
+
56
+ interface InputStreamBridge extends OutputStreamBridge {
57
+ read?: (size?: number) => string | Buffer | null;
58
+ setRawMode?: (flag: boolean) => InputStreamBridge;
59
+ isRaw?: boolean;
60
+ destroy?: () => InputStreamBridge;
61
+ pipe?: (dest: any) => any;
62
+ unpipe?: () => InputStreamBridge;
63
+ unshift?: (...args: unknown[]) => void;
64
+ wrap?: (stream: any) => InputStreamBridge;
65
+ readable?: boolean;
66
+ writable?: boolean;
67
+ destroyed?: boolean;
68
+ [Symbol.asyncIterator]?: () => AsyncIterator<any>;
69
+ }
70
+
71
+ export interface ProcessObject {
72
+ env: ProcessEnvVars;
73
+ cwd: () => string;
74
+ chdir: (dir: string) => void;
75
+ _chdirHook?: (dir: string) => void;
76
+ platform: string;
77
+ version: string;
78
+ versions: {
79
+ node: string;
80
+ v8: string;
81
+ uv: string;
82
+ modules: string;
83
+ openssl: string;
84
+ napi: string;
85
+ webcontainer: string;
86
+ };
87
+ argv: string[];
88
+ argv0: string;
89
+ execPath: string;
90
+ execArgv: string[];
91
+ pid: number;
92
+ ppid: number;
93
+ exit: (code?: number) => never;
94
+ nextTick: (fn: (...args: unknown[]) => void, ...args: unknown[]) => void;
95
+ stdout: OutputStreamBridge;
96
+ stderr: OutputStreamBridge;
97
+ stdin: InputStreamBridge;
98
+ arch: string;
99
+ title: string;
100
+ hrtime: {
101
+ (prev?: [number, number]): [number, number];
102
+ bigint: () => bigint;
103
+ };
104
+ memoryUsage: () => {
105
+ rss: number;
106
+ heapTotal: number;
107
+ heapUsed: number;
108
+ external: number;
109
+ arrayBuffers: number;
110
+ };
111
+ uptime: () => number;
112
+ cpuUsage: () => { user: number; system: number };
113
+ resourceUsage: () => {
114
+ userCPUTime: number; systemCPUTime: number;
115
+ maxRSS: number; sharedMemorySize: number;
116
+ unsharedDataSize: number; unsharedStackSize: number;
117
+ minorPageFault: number; majorPageFault: number;
118
+ swappedOut: number; fsRead: number; fsWrite: number;
119
+ ipcSent: number; ipcReceived: number;
120
+ signalsCount: number; voluntaryContextSwitches: number;
121
+ involuntaryContextSwitches: number;
122
+ };
123
+ abort: () => never;
124
+ kill: (pid: number, signal?: string | number) => boolean;
125
+ umask: (mask?: number) => number;
126
+ config: {
127
+ variables: Record<string, unknown>;
128
+ target_defaults: Record<string, unknown>;
129
+ };
130
+ release: { name: string; sourceUrl: string; headersUrl: string };
131
+ features: {
132
+ inspector: boolean;
133
+ debug: boolean;
134
+ uv: boolean;
135
+ ipv6: boolean;
136
+ tls_alpn: boolean;
137
+ tls_sni: boolean;
138
+ tls_ocsp: boolean;
139
+ tls: boolean;
140
+ };
141
+ debugPort: number;
142
+ allowedNodeEnvironmentFlags: Set<string>;
143
+ on: (evt: string, fn: EventHandler) => ProcessObject;
144
+ once: (evt: string, fn: EventHandler) => ProcessObject;
145
+ off: (evt: string, fn: EventHandler) => ProcessObject;
146
+ emit: (evt: string, ...args: unknown[]) => boolean;
147
+ addListener: (evt: string, fn: EventHandler) => ProcessObject;
148
+ removeListener: (evt: string, fn: EventHandler) => ProcessObject;
149
+ removeAllListeners: (evt?: string) => ProcessObject;
150
+ listeners: (evt: string) => EventHandler[];
151
+ listenerCount: (evt: string) => number;
152
+ prependListener: (evt: string, fn: EventHandler) => ProcessObject;
153
+ prependOnceListener: (evt: string, fn: EventHandler) => ProcessObject;
154
+ eventNames: () => string[];
155
+ setMaxListeners: (n: number) => ProcessObject;
156
+ getMaxListeners: () => number;
157
+ send?: (msg: unknown, cb?: (err: Error | null) => void) => boolean;
158
+ disconnect?: () => void;
159
+ connected?: boolean;
160
+ _debugCwdCalls?: number;
161
+ mainModule?: unknown;
162
+ channel?: unknown;
163
+ noDeprecation?: boolean;
164
+ throwDeprecation?: boolean;
165
+ traceDeprecation?: boolean;
166
+ traceProcessWarnings?: boolean;
167
+ report?: Record<string, unknown>;
168
+ binding?: (name: string) => Record<string, unknown>;
169
+ _linkedBinding?: (name: string) => Record<string, unknown>;
170
+ dlopen?: (module: unknown, filename: string, flags?: number) => void;
171
+ reallyExit?: (code?: number) => void;
172
+ _getActiveRequests?: () => unknown[];
173
+ _getActiveHandles?: () => unknown[];
174
+ emitWarning?: (
175
+ warning: string | Error,
176
+ typeOrOptions?: string | { type?: string; code?: string; detail?: string },
177
+ code?: string,
178
+ ) => void;
179
+ hasUncaughtExceptionCaptureCallback?: () => boolean;
180
+ setUncaughtExceptionCaptureCallback?: (
181
+ fn: ((err: Error) => void) | null,
182
+ ) => void;
183
+ sourceMapsEnabled?: boolean;
184
+ setSourceMapsEnabled?: (val: boolean) => void;
185
+ constrainedMemory?: () => number;
186
+ availableMemory?: () => number;
187
+ }
188
+
189
+ function fabricateStream(
190
+ isOutput: boolean,
191
+ writeFn?: (text: string) => boolean,
192
+ ): OutputStreamBridge & InputStreamBridge {
193
+ const bus = new EventEmitter();
194
+
195
+ // raw ANSI writes bypass the stream's write() method
196
+ const rawWrite = (text: string) => {
197
+ if (writeFn) writeFn(text);
198
+ };
199
+
200
+ const stream: OutputStreamBridge & InputStreamBridge = {
201
+ isTTY: false,
202
+ columns: DEFAULT_TERMINAL.COLUMNS,
203
+ rows: DEFAULT_TERMINAL.ROWS,
204
+ isRaw: false,
205
+ on(evt, fn) {
206
+ bus.on(evt, fn);
207
+ return stream;
208
+ },
209
+ once(evt, fn) {
210
+ bus.once(evt, fn);
211
+ return stream;
212
+ },
213
+ off(evt, fn) {
214
+ bus.off(evt, fn);
215
+ return stream;
216
+ },
217
+ emit(evt, ...args) {
218
+ return bus.emit(evt, ...args);
219
+ },
220
+ addListener(evt, fn) {
221
+ bus.addListener(evt, fn);
222
+ return stream;
223
+ },
224
+ removeListener(evt, fn) {
225
+ bus.removeListener(evt, fn);
226
+ return stream;
227
+ },
228
+ removeAllListeners(evt) {
229
+ bus.removeAllListeners(evt);
230
+ return stream;
231
+ },
232
+ setMaxListeners(n) {
233
+ bus.setMaxListeners(n);
234
+ return stream;
235
+ },
236
+ getMaxListeners() {
237
+ return bus.getMaxListeners();
238
+ },
239
+ listenerCount(evt) {
240
+ return bus.listenerCount(evt);
241
+ },
242
+ listeners(evt) {
243
+ return bus.listeners(evt);
244
+ },
245
+ rawListeners(evt) {
246
+ return bus.rawListeners(evt);
247
+ },
248
+ prependListener(evt, fn) {
249
+ bus.prependListener(evt, fn);
250
+ return stream;
251
+ },
252
+ prependOnceListener(evt, fn) {
253
+ bus.prependOnceListener(evt, fn);
254
+ return stream;
255
+ },
256
+ eventNames() {
257
+ return bus.eventNames();
258
+ },
259
+ pause() {
260
+ return stream;
261
+ },
262
+ resume() {
263
+ return stream;
264
+ },
265
+ setEncoding(_enc) {
266
+ return stream;
267
+ },
268
+ write(data, _enc?, cb?) {
269
+ if (isOutput && writeFn && data != null) {
270
+ const text = typeof data === "string" ? data : String(data);
271
+ writeFn(text);
272
+ }
273
+ if (cb) queueMicrotask(cb);
274
+ return true;
275
+ },
276
+ end(data?, cb?) {
277
+ if (isOutput && writeFn && data != null) {
278
+ const text = typeof data === "string" ? data : String(data);
279
+ writeFn(text);
280
+ }
281
+ if (cb) queueMicrotask(cb);
282
+ },
283
+ read() {
284
+ return null;
285
+ },
286
+ destroy() {
287
+ return stream;
288
+ },
289
+ pipe(dest: any) {
290
+ const onData = (chunk: any) => {
291
+ if (dest.write) dest.write(chunk);
292
+ };
293
+ bus.on("data", onData);
294
+ if (!(stream as any)._pipeDests) (stream as any)._pipeDests = [];
295
+ (stream as any)._pipeDests.push({ dest, onData });
296
+ return dest;
297
+ },
298
+ unpipe(dest?: any) {
299
+ const dests: Array<{ dest: any; onData: Function }> =
300
+ (stream as any)._pipeDests || [];
301
+ if (dest) {
302
+ const idx = dests.findIndex((d) => d.dest === dest);
303
+ if (idx >= 0) {
304
+ bus.off("data", dests[idx].onData as any);
305
+ dests.splice(idx, 1);
306
+ }
307
+ } else {
308
+ for (const d of dests) bus.off("data", d.onData as any);
309
+ dests.length = 0;
310
+ }
311
+ return stream;
312
+ },
313
+ unshift() {},
314
+ wrap() {
315
+ return stream;
316
+ },
317
+ [Symbol.asyncIterator]() {
318
+ return { next: async () => ({ done: true, value: undefined }) };
319
+ },
320
+ readable: true,
321
+ writable: true,
322
+ destroyed: false,
323
+ setRawMode(flag) {
324
+ stream.isRaw = flag;
325
+ return stream;
326
+ },
327
+ clearLine(dir: number, cb?) {
328
+ if (isOutput) {
329
+ if (dir === -1)
330
+ rawWrite("\x1b[1K"); // clear left of cursor
331
+ else if (dir === 1)
332
+ rawWrite("\x1b[0K"); // clear right of cursor
333
+ else rawWrite("\x1b[2K"); // clear entire line
334
+ }
335
+ if (cb) cb();
336
+ return true;
337
+ },
338
+ cursorTo(x: number, y?: number | (() => void), cb?: () => void) {
339
+ if (typeof y === "function") {
340
+ cb = y;
341
+ y = undefined;
342
+ }
343
+ if (isOutput) {
344
+ if (typeof y === "number") {
345
+ rawWrite(`\x1b[${y + 1};${x + 1}H`); // move to row;col
346
+ } else {
347
+ rawWrite(`\x1b[${x + 1}G`); // move to column
348
+ }
349
+ }
350
+ if (typeof cb === "function") cb();
351
+ return true;
352
+ },
353
+ moveCursor(dx: number, dy: number, cb?) {
354
+ if (isOutput) {
355
+ if (dx > 0)
356
+ rawWrite(`\x1b[${dx}C`); // move right
357
+ else if (dx < 0) rawWrite(`\x1b[${-dx}D`); // move left
358
+ if (dy > 0)
359
+ rawWrite(`\x1b[${dy}B`); // move down
360
+ else if (dy < 0) rawWrite(`\x1b[${-dy}A`); // move up
361
+ }
362
+ if (cb) cb();
363
+ return true;
364
+ },
365
+ getWindowSize() {
366
+ return [stream.columns, stream.rows];
367
+ },
368
+ getColorDepth(_env?: Record<string, string>): number {
369
+ return 8;
370
+ },
371
+ hasColors(
372
+ countOrEnv?: number | Record<string, string>,
373
+ _env?: Record<string, string>,
374
+ ): boolean {
375
+ const count = typeof countOrEnv === "number" ? countOrEnv : 256;
376
+ return 256 >= count;
377
+ },
378
+ };
379
+
380
+ if (isOutput && writeFn) {
381
+ stream.write = (data: string | Buffer, _enc?: string, cb?: () => void) => {
382
+ const text = typeof data === "string" ? data : data.toString();
383
+ const ok = writeFn(text);
384
+ if (cb) queueMicrotask(cb);
385
+ return ok;
386
+ };
387
+ }
388
+
389
+ return stream;
390
+ }
391
+
392
+ export function buildProcessEnv(config?: {
393
+ cwd?: string;
394
+ env?: ProcessEnvVars;
395
+ onExit?: (code: number) => void;
396
+ onStdout?: (text: string) => void;
397
+ onStderr?: (text: string) => void;
398
+ pid?: number;
399
+ ppid?: number;
400
+ }): ProcessObject {
401
+ let workingDir = config?.cwd || "/";
402
+ let currentUmask = 0o022;
403
+ const bootTime = Date.now();
404
+ const bus = new EventEmitter();
405
+
406
+ const envVars: ProcessEnvVars = {
407
+ NODE_ENV: DEFAULT_ENV.NODE_ENV,
408
+ PATH: DEFAULT_ENV.PATH,
409
+ HOME: DEFAULT_ENV.HOME,
410
+ SHELL: DEFAULT_ENV.SHELL,
411
+ TERM: DEFAULT_ENV.TERM,
412
+ COLORTERM: DEFAULT_ENV.COLORTERM,
413
+ REQUIRES_WASM: DEFAULT_ENV.REQUIRES_WASM,
414
+ npm_config_user_agent: DEFAULT_ENV.npm_config_user_agent,
415
+ npm_execpath: DEFAULT_ENV.npm_execpath,
416
+ npm_node_execpath: DEFAULT_ENV.npm_node_execpath,
417
+ NEXT_TELEMETRY_DISABLED: "1",
418
+ DO_NOT_TRACK: "1",
419
+ ...config?.env,
420
+ };
421
+
422
+ const stdoutStream = fabricateStream(true, (text) => {
423
+ if (config?.onStdout) {
424
+ config.onStdout(text);
425
+ } else {
426
+ _nativeConsole.log(text);
427
+ }
428
+ return true;
429
+ });
430
+
431
+ const stderrStream = fabricateStream(true, (text) => {
432
+ if (config?.onStderr) {
433
+ config.onStderr(text);
434
+ } else {
435
+ _nativeConsole.error(text);
436
+ }
437
+ return true;
438
+ });
439
+
440
+ const stdinStream = fabricateStream(false);
441
+
442
+ const hrtimeFn = function hrtime(prev?: [number, number]): [number, number] {
443
+ const now = performance.now();
444
+ const secs = Math.floor(now / 1000);
445
+ const nanos = Math.floor((now % 1000) * 1e6);
446
+ if (prev) {
447
+ return [secs - prev[0], nanos - prev[1]];
448
+ }
449
+ return [secs, nanos];
450
+ };
451
+ hrtimeFn.bigint = (): bigint => BigInt(Math.floor(performance.now() * 1e6));
452
+
453
+ const proc: ProcessObject = {
454
+ env: envVars,
455
+
456
+ cwd() {
457
+ if (!proc._debugCwdCalls) proc._debugCwdCalls = 0;
458
+ proc._debugCwdCalls++;
459
+ return workingDir;
460
+ },
461
+
462
+ chdir(dir: string) {
463
+ if (!dir.startsWith("/")) {
464
+ dir = workingDir + "/" + dir;
465
+ }
466
+ workingDir = dir;
467
+ if (proc._chdirHook) proc._chdirHook(dir);
468
+ },
469
+
470
+ platform: MOCK_OS.PLATFORM,
471
+ arch: MOCK_OS.ARCH,
472
+ title: MOCK_PROCESS.TITLE,
473
+ version: VERSIONS.NODE,
474
+ versions: { ...NODE_SUB_VERSIONS },
475
+
476
+ argv: ["node", "/index.js"],
477
+ argv0: "node",
478
+ execPath: MOCK_PROCESS.EXEC_PATH,
479
+ execArgv: [],
480
+
481
+ pid: config?.pid ?? MOCK_PROCESS.PID,
482
+ ppid: config?.ppid ?? MOCK_PROCESS.PPID,
483
+
484
+ exit(code = 0) {
485
+ bus.emit("exit", code);
486
+ if (config?.onExit) config.onExit(code);
487
+ throw new Error(`Process exited with code ${code}`);
488
+ },
489
+
490
+ nextTick(fn, ...args) {
491
+ queueMicrotask(() => fn(...args));
492
+ },
493
+
494
+ stdout: stdoutStream,
495
+ stderr: stderrStream,
496
+ stdin: stdinStream,
497
+
498
+ hrtime: hrtimeFn as ProcessObject["hrtime"],
499
+
500
+ memoryUsage: Object.assign(
501
+ function memoryUsage() {
502
+ return {
503
+ rss: MOCK_MEMORY.RSS,
504
+ heapTotal: MOCK_MEMORY.HEAP_TOTAL,
505
+ heapUsed: MOCK_MEMORY.HEAP_USED,
506
+ external: MOCK_MEMORY.EXTERNAL,
507
+ arrayBuffers: 0,
508
+ };
509
+ },
510
+ { rss: () => MOCK_MEMORY.RSS },
511
+ ),
512
+
513
+ uptime() {
514
+ return (Date.now() - bootTime) / 1000;
515
+ },
516
+
517
+ cpuUsage() {
518
+ return { user: 0, system: 0 };
519
+ },
520
+
521
+ resourceUsage() {
522
+ return {
523
+ userCPUTime: 0, systemCPUTime: 0,
524
+ maxRSS: MOCK_MEMORY.RSS / 1024,
525
+ sharedMemorySize: 0, unsharedDataSize: 0, unsharedStackSize: 0,
526
+ minorPageFault: 0, majorPageFault: 0,
527
+ swappedOut: 0, fsRead: 0, fsWrite: 0,
528
+ ipcSent: 0, ipcReceived: 0,
529
+ signalsCount: 0, voluntaryContextSwitches: 0, involuntaryContextSwitches: 0,
530
+ };
531
+ },
532
+
533
+ abort() {
534
+ throw new Error("process.abort() called");
535
+ },
536
+
537
+ kill(_pid: number, _signal?: string | number) {
538
+ return true;
539
+ },
540
+
541
+ umask(mask?: number) {
542
+ const old = currentUmask;
543
+ if (mask !== undefined) currentUmask = mask;
544
+ return old;
545
+ },
546
+
547
+ config: { variables: {}, target_defaults: {} },
548
+ release: { name: "node", sourceUrl: "", headersUrl: "" },
549
+ features: {
550
+ inspector: false,
551
+ debug: false,
552
+ uv: true,
553
+ ipv6: true,
554
+ tls_alpn: true,
555
+ tls_sni: true,
556
+ tls_ocsp: true,
557
+ tls: true,
558
+ },
559
+ debugPort: 9229,
560
+ allowedNodeEnvironmentFlags: new Set<string>(),
561
+ mainModule: undefined as any,
562
+ channel: undefined,
563
+ noDeprecation: false,
564
+ throwDeprecation: false,
565
+ traceDeprecation: false,
566
+ traceProcessWarnings: false,
567
+ report: {
568
+ directory: "",
569
+ filename: "",
570
+ getReport: () => ({}),
571
+ reportOnFatalError: false,
572
+ reportOnSignal: false,
573
+ reportOnUncaughtException: false,
574
+ signal: "SIGUSR2",
575
+ writeReport: () => "",
576
+ },
577
+ binding(name: string): Record<string, unknown> {
578
+ if (name === "natives") return {};
579
+ if (name === "config") return { exposeInternals: false };
580
+ if (name === "constants") return {};
581
+ if (name === "util") return {};
582
+ if (name === "fs") return {};
583
+ if (name === "buffer") return {};
584
+ if (name === "stream_wrap") return {};
585
+ if (name === "tcp_wrap") return {};
586
+ if (name === "pipe_wrap") return {};
587
+ // throw for unknown bindings so callers fall back gracefully
588
+ throw new Error(`No such module: ${name}`);
589
+ },
590
+ _linkedBinding(_name: string): Record<string, unknown> {
591
+ return {};
592
+ },
593
+ dlopen(_module: unknown, _filename: string, _flags?: number): void {
594
+ throw new Error("process.dlopen is not supported in browser environment");
595
+ },
596
+ reallyExit(_code?: number): void {},
597
+ _getActiveRequests(): unknown[] {
598
+ return [];
599
+ },
600
+ _getActiveHandles(): unknown[] {
601
+ return [];
602
+ },
603
+ emitWarning(
604
+ warning: string | Error,
605
+ typeOrOptions?:
606
+ | string
607
+ | { type?: string; code?: string; detail?: string },
608
+ code?: string,
609
+ ): void {
610
+ const msg = typeof warning === "string" ? warning : warning.message;
611
+ const type =
612
+ typeof typeOrOptions === "string"
613
+ ? typeOrOptions
614
+ : (typeOrOptions?.type ?? "Warning");
615
+ bus.emit("warning", { name: type, message: msg, code });
616
+ },
617
+ hasUncaughtExceptionCaptureCallback(): boolean {
618
+ return false;
619
+ },
620
+ setUncaughtExceptionCaptureCallback(
621
+ _fn: ((err: Error) => void) | null,
622
+ ): void {},
623
+ sourceMapsEnabled: false,
624
+ setSourceMapsEnabled(_val: boolean): void {},
625
+ constrainedMemory(): number {
626
+ return 0;
627
+ },
628
+ availableMemory(): number {
629
+ return 512 * 1024 * 1024; // 512MB
630
+ },
631
+
632
+ on(evt, fn) {
633
+ bus.on(evt, fn);
634
+ return proc;
635
+ },
636
+ once(evt, fn) {
637
+ bus.once(evt, fn);
638
+ return proc;
639
+ },
640
+ off(evt, fn) {
641
+ bus.off(evt, fn);
642
+ return proc;
643
+ },
644
+ emit(evt, ...args) {
645
+ return bus.emit(evt, ...args);
646
+ },
647
+ addListener(evt, fn) {
648
+ bus.addListener(evt, fn);
649
+ return proc;
650
+ },
651
+ removeListener(evt, fn) {
652
+ bus.removeListener(evt, fn);
653
+ return proc;
654
+ },
655
+ removeAllListeners(evt) {
656
+ bus.removeAllListeners(evt);
657
+ return proc;
658
+ },
659
+ listeners(evt) {
660
+ return bus.listeners(evt);
661
+ },
662
+ listenerCount(evt) {
663
+ return bus.listenerCount(evt);
664
+ },
665
+ prependListener(evt, fn) {
666
+ bus.prependListener(evt, fn);
667
+ return proc;
668
+ },
669
+ prependOnceListener(evt, fn) {
670
+ bus.prependOnceListener(evt, fn);
671
+ return proc;
672
+ },
673
+ eventNames() {
674
+ return bus.eventNames();
675
+ },
676
+ setMaxListeners(n) {
677
+ bus.setMaxListeners(n);
678
+ return proc;
679
+ },
680
+ getMaxListeners() {
681
+ return bus.getMaxListeners();
682
+ },
683
+ };
684
+
685
+ return proc;
686
+ }
687
+
688
+ export const process = buildProcessEnv();
689
+
690
+ export default process;