-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChildProcessPolyfill.ts
More file actions
775 lines (672 loc) · 19.9 KB
/
ChildProcessPolyfill.ts
File metadata and controls
775 lines (672 loc) · 19.9 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/**
* @module ChildProcessPolyfill
*
* @description
* Polyfill for Node.js child_process module in the renderer sandbox.
* Maps child process operations to Mountain and Cocoon commands.
*
* @feature_set
* - spawn(command, args, options) → Mountain electron:spawn_child_process
* - exec(command, options) → Mountain electron:exec_command
* - fork(modulePath, args, options) → Mountain electron:fork_extension_host (for Cocoon)
*
* @return_types
* ChildProcess-like objects with:
* - pid, killed, exitCode, signalCode
* - stdin, stdout, stderr (mock with event handling)
* - on(event, listener), emit(event, ...args)
* - kill(signal)
*
* @phase 5 of Approach A3 implementation
*/
// ============================================================================
// Types
// ============================================================================
/**
* Spawn options
*/
interface SpawnOptions {
cwd?: string;
env?: Record<string, string>;
stdio?: Array<"pipe" | "ignore" | "inherit" | Stream | number | null> | "pipe" | "ignore" | "inherit";
detached?: boolean;
shell?: boolean | string;
windowsVerbatimArguments?: boolean;
windowsHide?: boolean;
uid?: number;
gid?: number;
serialization?: "json" | "advanced";
}
/**
* Exec options
*/
interface ExecOptions {
cwd?: string;
env?: Record<string, string>;
encoding?: BufferEncoding;
timeout?: number;
maxBuffer?: number;
killSignal?: string;
uid?: number;
gid?: number;
shell?: string | boolean;
windowsHide?: boolean;
}
/**
* Fork options
*/
interface ForkOptions {
cwd?: string;
env?: Record<string, string>;
execPath?: string;
execArgv?: string[];
silent?: boolean;
stdio?: Array<"pipe" | "ignore" | "inherit" | Stream | number | null> | "pipe" | "ignore" | "inherit";
detached?: boolean;
windowsVerbatimArguments?: boolean;
windowsHide?: boolean;
uid?: number;
gid?: number;
serialization?: "json" | "advanced";
}
/**
* Kill signals
*/
type SignalNumber =
| 1 // SIGHUP
| 2 // SIGINT
| 3 // SIGQUIT
| 9 // SIGKILL
| 10 // SIGUSR1
| 12 // SIGUSR2
| 15 // SIGTERM
| 17 // SIGSTOP
| 19; // SIGCONT
type SignalString =
| "SIGHUP"
| "SIGINT"
| "SIGQUIT"
| "SIGILL"
| "SIGTRAP"
| "SIGABRT"
| "SIGIOT"
| "SIGBUS"
| "SIGFPE"
| "SIGKILL"
| "SIGUSR1"
| "SIGSEGV"
| "SIGUSR2"
| "SIGPIPE"
| "SIGALRM"
| "SIGTERM"
| "SIGCHLD"
| "SIGCONT"
| "SIGSTOP"
| "SIGTSTP"
| "SIGTTIN"
| "SIGTTOU"
| "SIGURG"
| "SIGXCPU"
| "SIGXFSZ"
| "SIGVTALRM"
| "SIGPROF"
| "SIGWINCH"
| "SIGIO"
| "SIGPOLL"
| "SIGPWR"
| "SIGSYS";
type Signal = SignalNumber | SignalString;
/**
* Child process event types
*/
type ChildProcessEvent =
| "close"
| "disconnect"
| "error"
| "exit"
| "message"
| "spawn";
/**
* Child process event listener
*/
type ChildProcessEventListener = (...args: unknown[]) => void;
/**
* Mock Stream for stdin/stdout/stderr
*/
interface Stream {
write(data: string | Buffer): boolean;
end(data?: string | Buffer): void;
on(event: string, listener: (...args: unknown[]) => void): void;
removeAllListeners(event?: string): void;
stdio?: Stream;
fd?: number;
}
// ============================================================================
// Tauri Integration
// ============================================================================
/**
* Invoke Tauri command with proper error handling
*/
async function invokeTauri<T>(command: string, args: Record<string, unknown> = {}): Promise<T> {
try {
if (typeof (window as any).__TAURI__?.invoke !== "undefined") {
return await (window as any).__TAURI__.invoke<T>(command, args);
}
if (typeof (window as any).TAURI?.invoke !== "undefined") {
return await (window as any).TAURI.invoke<T>(command, args);
}
throw new Error(`Tauri invoke not available for command: ${command}`);
} catch (error: unknown) {
console.error(`[ChildProcessPolyfill] Tauri invoke failed for ${command}:`, error);
throw error;
}
}
/**
* Listen for Tauri events
*/
function listenToTauri(event: string, handler: (payload: unknown) => void): () => void {
if (typeof (window as any).__TAURI__?.event?.listen === "function") {
const unlistenPromise = (window as any).__TAURI__.event.listen(event, ({ payload }: { payload: unknown }) => {
handler(payload);
});
// Return cleanup function
return () => {
unlistenPromise.then((unlisten: () => void) => unlisten());
};
}
if (typeof (window as any).TAURI?.event?.listen === "function") {
const unlistenPromise = (window as any).TAURI.event.listen(event, ({ payload }: { payload: unknown }) => {
handler(payload);
});
return () => {
unlistenPromise.then((unlisten: () => void) => unlisten());
};
}
console.warn(`[ChildProcessPolyfill] Tauri event listener not available for: ${event}`);
return () => {};
}
// ============================================================================
// Mock Stream Implementation
// ============================================================================
/**
* Create a mock stream for stdin/stdout/stderr
*/
function createMockStream(direction: "read" | "write"): Stream {
const listeners: Map<string, Set<(...args: unknown[]) => void>> = new Map();
return {
write(data: string | Buffer): boolean {
console.log(`[ChildProcessPolyfill] Stream write (${direction}):`, data.toString().slice(0, 100));
return true;
},
end(data?: string | Buffer): void {
console.log(`[ChildProcessPolyfill] Stream end (${direction})`);
this.emit("end");
},
on(event: string, listener: (...args: unknown[]) => void): void {
if (!listeners.has(event)) {
listeners.set(event, new Set());
}
listeners.get(event)!.add(listener);
},
removeAllListeners(event?: string): void {
if (event) {
listeners.delete(event);
} else {
listeners.clear();
}
},
emit(event: string, ...args: unknown[]): void {
const eventListeners = listeners.get(event);
if (eventListeners) {
eventListeners.forEach((listener) => {
try {
listener(...args);
} catch (error) {
console.error(`[ChildProcessPolyfill] Stream event error (${event}):`, error);
}
});
}
},
};
}
// ============================================================================
// Child Process Implementation
// ============================================================================
/**
* ChildProcess class implementing mock child process behavior
*/
class ChildProcess {
// Process state
pid: number = 0;
killed: boolean = false;
exitCode: number | null = null;
signalCode: Signal | null = null;
// Streams
stdin: Stream;
stdout: Stream;
stderr: Stream;
stdio: Stream[];
// Event listeners
private listeners: Map<ChildProcessEvent, Set<ChildProcessEventListener>> = new Map();
// Process ID tracking
private _sPid: string;
constructor(spawnId: string) {
this._sPid = spawnId;
this.stdin = createMockStream("write");
this.stdout = createMockStream("read");
this.stderr = createMockStream("read");
this.stdio = [this.stdin, this.stdout, this.stderr];
this.setupEventListeners();
}
/**
* Set up Tauri event listeners for this process
*/
private setupEventListeners(): void {
// Listen for spawn events from Tauri
const unlistenSpawn = listenToTauri(`child_process:spawn:${this._sPid}`, (payload: unknown) => {
console.log(`[ChildProcessPolyfill] Spawn event for ${this._sPid}:`, payload);
this.emit("spawn");
});
// Listen for exit events from Tauri
const unlistenExit = listenToTauri(`child_process:exit:${this._sPid}`, (payload: unknown) => {
console.log(`[ChildProcessPolyfill] Exit event for ${this._sPid}:`, payload);
const data = payload as { exit_code: number; signal: Signal | null };
this.exitCode = data.exit_code;
this.signalCode = data.signal;
this.killed = true;
this.emit("exit", this.exitCode, this.signalCode);
this.emit("close", this.exitCode, this.signalCode);
});
// Listen for error events from Tauri
const unlistenError = listenToTauri(`child_process:error:${this._sPid}`, (payload: unknown) => {
console.error(`[ChildProcessPolyfill] Error event for ${this._sPid}:`, payload);
this.emit("error", payload);
});
// Listen for stdout data
const unlistenStdout = listenToTauri(`child_process:stdout:${this._sPid}`, (payload: unknown) => {
const data = payload as { data: string | Buffer };
this.stdout.emit("data", data.data instanceof Buffer ? data.data : Buffer.from(data.data));
});
// Listen for stderr data
const unlistenStderr = listenToTauri(`child_process:stderr:${this._sPid}`, (payload: unknown) => {
const data = payload as { data: string | Buffer };
this.stderr.emit("data", data.data instanceof Buffer ? data.data : Buffer.from(data.data));
});
// Store cleanup functions
this._unlistenFunctions = [
unlistenSpawn,
unlistenExit,
unlistenError,
unlistenStdout,
unlistenStderr,
];
}
private _unlistenFunctions: Array<() => void> = [];
// ============================================================================
// Event Methods
// ============================================================================
/**
* Add event listener
*/
on(event: ChildProcessEvent, listener: ChildProcessEventListener): this {
console.log(`[ChildProcessPolyfill] on(${event}) for ${this._sPid}`);
if (!this.listeners.has(event)) {
this.listeners.set(event, new Set());
}
this.listeners.get(event)!.add(listener);
return this;
}
/**
* Add one-time event listener
*/
once(event: ChildProcessEvent, listener: ChildProcessEventListener): this {
const wrappedListener: ChildProcessEventListener = (...args) => {
this.removeListener(event, wrappedListener);
listener(...args);
};
return this.on(event, wrappedListener);
}
/**
* Remove event listener
*/
removeListener(event: ChildProcessEvent, listener: ChildProcessEventListener): this {
const eventListeners = this.listeners.get(event);
if (eventListeners) {
eventListeners.delete(listener);
if (eventListeners.size === 0) {
this.listeners.delete(event);
}
}
return this;
}
/**
* Remove all listeners for an event
*/
removeAllListeners(event?: ChildProcessEvent): this {
if (event) {
this.listeners.delete(event);
} else {
this.listeners.clear();
}
return this;
}
/**
* Emit event to all listeners
*/
private emit(event: ChildProcessEvent, ...args: unknown[]): boolean {
const listeners = this.listeners.get(event);
if (!listeners || listeners.size === 0) {
return false;
}
listeners.forEach((listener) => {
try {
listener(...args);
} catch (error) {
console.error(`[ChildProcessPolyfill] Error in ${event} listener:`, error);
}
});
return true;
}
// ============================================================================
// Process Control
// ============================================================================
/**
* Kill the process
*/
kill(signal: Signal = "SIGTERM"): boolean {
console.log(`[ChildProcessPolyfill] Kill ${this._sPid} with signal: ${signal}`);
if (this.killed) {
return true;
}
this.signalCode = signal;
// Send kill command to Tauri
invokeTauri("child_process:kill", {
spawn_id: this._sPid,
signal,
}).catch((error) => {
console.error(`[ChildProcessPolyfill] Kill error for ${this._sPid}:`, error);
});
// Note: We don't immediately mark as killed; wait for exit event from Tauri
return true;
}
/**
* Send a message to the process (IPC)
*/
send(message: unknown, sendHandle?: unknown, options?: { swallowErrors?: boolean }): boolean {
console.log(`[ChildProcessPolyfill] Send message to ${this._sPid}:`, message);
invokeTauri("child_process:send", {
spawn_id: this._sPid,
message,
}).catch((error) => {
console.error(`[ChildProcessPolyfill] Send error for ${this._sPid}:`, error);
});
return true;
}
/**
* Disconnect from the process
*/
disconnect(): void {
console.log(`[ChildProcessPolyfill] Disconnect from ${this._sPid}`);
this.removeAllListeners();
this._unlistenFunctions.forEach((unlisten) => unlisten());
this.stdin.end();
}
/**
* Ref the process (keep it alive)
*/
ref(): this {
return this;
}
/**
* Unref the process (allow it to exit)
*/
unref(): this {
return this;
}
/**
* Cleanup resources
*/
private cleanup(): void {
this._unlistenFunctions.forEach((unlisten) => unlisten());
this._unlistenFunctions = [];
}
}
// ============================================================================
// Spawn Implementation
// ============================================================================
/**
* Spawn a child process
*/
function spawn(command: string, args?: string[], options?: SpawnOptions): ChildProcess {
console.log(`[ChildProcessPolyfill] spawn: ${command} ${args?.join(" ") ?? ""}`);
// Generate unique spawn ID
const spawnId = `spawn_${Date.now()}_${Math.random().toString(36).substring(7)}`;
const proc = new ChildProcess(spawnId);
// Call Mountain to spawn the process
invokeTauri<{
pid: number;
success: boolean;
error?: string;
}>("electron:spawn_child_process", {
command,
args: args ?? [],
cwd: options?.cwd,
env: options?.env,
shell: options?.shell,
// Note: stdio, detached, etc. are passed but may not be fully supported
})
.then((result) => {
if (result.success) {
proc.pid = result.pid;
console.log(`[ChildProcessPolyfill] Process spawned with PID: ${proc.pid}`);
proc.emit("spawn");
} else {
proc.emit("error", new Error(result.error ?? "Failed to spawn process"));
}
})
.catch((error) => {
console.error("[ChildProcessPolyfill] spawn error:", error);
proc.emit("error", error);
});
return proc;
}
// ============================================================================
// Exec Implementation
// ============================================================================
/**
* Execute a command and get output
*/
function exec(command: string, options?: ExecOptions, callback?: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess {
console.log(`[ChildProcessPolyfill] exec: ${command}`);
// Generate unique exec ID
const execId = `exec_${Date.now()}_${Math.random().toString(36).substring(7)}`;
const proc = new ChildProcess(execId);
let stdout = "";
let stderr = "";
let error: Error | null = null;
// Set up output capture
proc.stdout.on("data", (data: Buffer) => {
stdout += data.toString(options?.encoding ?? "utf8");
});
proc.stderr.on("data", (data: Buffer) => {
stderr += data.toString(options?.encoding ?? "utf8");
});
proc.on("exit", (code: number | null) => {
if (code !== 0) {
error = new Error(`Command failed: ${command}\n${stderr}`);
(error as Error & { code?: number; killed?: boolean }).code = code ?? undefined;
(error as Error & { killed?: boolean }).killed = proc.killed;
}
if (callback) {
callback(error, stdout, stderr);
}
});
// Call Mountain to execute the command
invokeTauri<{
pid: number;
success: boolean;
error?: string;
}>("electron:exec_command", {
command,
cwd: options?.cwd,
env: options?.env,
shell: options?.shell,
timeout: options?.timeout,
})
.then((result) => {
if (result.success) {
proc.pid = result.pid;
} else {
error = new Error(result.error ?? "Failed to execute command");
proc.emit("error", error);
}
})
.catch((err) => {
error = err;
proc.emit("error", err);
});
return proc;
}
/**
* Exec with Promise
*/
function execPromise(command: string, options?: ExecOptions): Promise<{ stdout: string; stderr: string }> {
return new Promise((resolve, reject) => {
const proc = exec(command, options, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve({ stdout, stderr });
}
});
});
}
// ============================================================================
// Fork Implementation
// ============================================================================
/**
* Fork a Node.js module as a child process
* This is primarily used for extension hosts in VSCode
*/
function fork(modulePath: string, args?: string[], options?: ForkOptions): ChildProcess {
console.log(`[ChildProcessPolyfill] fork: ${modulePath}`);
// Generate unique fork ID
const forkId = `fork_${Date.now()}_${Math.random().toString(36).substring(7)}`;
const proc = new ChildProcess(forkId);
// For extension hosting, we route to Cocoon
const isExtensionHost = modulePath.includes("extensionHost") || modulePath.includes("process");
if (isExtensionHost) {
// Treat as extension host fork - route to Cocoon
invokeTauri<{
pid: number;
success: boolean;
error?: string;
}>("electron:fork_extension_host", {
module_path: modulePath,
args: args ?? [],
cwd: options?.cwd,
env: options?.env,
exec_path: options?.execPath,
exec_argv: options?.execArgv,
silent: options?.silent,
})
.then((result) => {
if (result.success) {
proc.pid = result.pid;
console.log(`[ChildProcessPolyfill] Extension host forked with PID: ${proc.pid}`);
proc.emit("spawn");
} else {
proc.emit("error", new Error(result.error ?? "Failed to fork extension host"));
}
})
.catch((error) => {
console.error("[ChildProcessPolyfill] fork error:", error);
proc.emit("error", error);
});
} else {
// Regular module fork - use spawn
const forkedProc = spawn(
options?.execPath ?? process.execPath,
[modulePath, ...(args ?? [])],
{
cwd: options?.cwd,
env: options?.env,
silent: options?.silent ? "pipe" : "inherit",
},
);
// Copy properties to our process
proc.pid = forkedProc.pid;
}
return proc;
}
// ============================================================================
// Child Process Namespace
// ============================================================================
/**
* Child process exports (mimicking Node.js child_process module)
*/
const childProcess = {
spawn,
exec,
execSync: () => {
throw new Error("childProcess.execSync() is not supported in browser/Tauri environment. Use async exec() instead.");
},
fork,
execFile: exec, // execFile is similar to exec in this context
};
// ============================================================================
// Installation
// ============================================================================
/**
* Install the child process polyfill
*/
export function installChildProcessPolyfill(): void {
if (typeof window === "undefined") {
return;
}
// Prevent double installation
if ((window as any).__CHILD_PROCESS_POLYFILL_INSTALLED__) {
console.log("[ChildProcessPolyfill] Already installed, skipping");
return;
}
(window as any).__CHILD_PROCESS_POLYFILL_INSTALLED__ = true;
console.log("[ChildProcessPolyfill] Installing Node.js child_process module polyfill...");
// Attach module to global (for Node.js compatibility)
(window as any).childProcess = childProcess;
// Extend require shim
if (typeof (window as any).require === "function") {
// biome-ignore lint/complexity/noExplicitAny: Required for require shim
const existingRequire = (window as any).require;
(window as any).require = (id: string) => {
if (id === "child_process") {
return childProcess;
}
return existingRequire(id);
};
}
// Also attach to window.vscode if available
if (typeof (window as any).vscode !== "undefined") {
(window as any).vscode.childProcess = childProcess;
}
console.log("[ChildProcessPolyfill] ✓ Node.js child_process module polyfill installed");
}
// Get process from global (for exec path, etc.)
const process = typeof window !== "undefined" && (window as any).process ? (window as any).process : { execPath: "/usr/local/bin/node" };
// ============================================================================
// Exports
// ============================================================================
export default {
install: installChildProcessPolyfill,
module: childProcess,
// Individual exports for convenience
spawn,
exec,
execPromise,
fork,
// Types
ChildProcess,
};
// Auto-install on import
if (typeof window !== "undefined") {
installChildProcessPolyfill();
}