@secure-exec/nodejs 0.2.0-rc.1
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.
- package/LICENSE +191 -0
- package/README.md +7 -0
- package/dist/bindings.d.ts +31 -0
- package/dist/bindings.js +67 -0
- package/dist/bridge/active-handles.d.ts +22 -0
- package/dist/bridge/active-handles.js +112 -0
- package/dist/bridge/child-process.d.ts +99 -0
- package/dist/bridge/child-process.js +672 -0
- package/dist/bridge/dispatch.d.ts +2 -0
- package/dist/bridge/dispatch.js +40 -0
- package/dist/bridge/fs.d.ts +502 -0
- package/dist/bridge/fs.js +3307 -0
- package/dist/bridge/index.d.ts +10 -0
- package/dist/bridge/index.js +41 -0
- package/dist/bridge/module.d.ts +75 -0
- package/dist/bridge/module.js +325 -0
- package/dist/bridge/network.d.ts +1093 -0
- package/dist/bridge/network.js +8651 -0
- package/dist/bridge/os.d.ts +13 -0
- package/dist/bridge/os.js +256 -0
- package/dist/bridge/polyfills.d.ts +9 -0
- package/dist/bridge/polyfills.js +67 -0
- package/dist/bridge/process.d.ts +121 -0
- package/dist/bridge/process.js +1382 -0
- package/dist/bridge/whatwg-url.d.ts +67 -0
- package/dist/bridge/whatwg-url.js +712 -0
- package/dist/bridge-contract.d.ts +774 -0
- package/dist/bridge-contract.js +172 -0
- package/dist/bridge-handlers.d.ts +199 -0
- package/dist/bridge-handlers.js +4263 -0
- package/dist/bridge-loader.d.ts +9 -0
- package/dist/bridge-loader.js +87 -0
- package/dist/bridge-setup.d.ts +1 -0
- package/dist/bridge-setup.js +3 -0
- package/dist/bridge.js +21652 -0
- package/dist/builtin-modules.d.ts +25 -0
- package/dist/builtin-modules.js +312 -0
- package/dist/default-network-adapter.d.ts +13 -0
- package/dist/default-network-adapter.js +351 -0
- package/dist/driver.d.ts +87 -0
- package/dist/driver.js +191 -0
- package/dist/esm-compiler.d.ts +14 -0
- package/dist/esm-compiler.js +68 -0
- package/dist/execution-driver.d.ts +37 -0
- package/dist/execution-driver.js +977 -0
- package/dist/host-network-adapter.d.ts +7 -0
- package/dist/host-network-adapter.js +279 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +23 -0
- package/dist/isolate-bootstrap.d.ts +86 -0
- package/dist/isolate-bootstrap.js +125 -0
- package/dist/ivm-compat.d.ts +7 -0
- package/dist/ivm-compat.js +31 -0
- package/dist/kernel-runtime.d.ts +58 -0
- package/dist/kernel-runtime.js +535 -0
- package/dist/module-access.d.ts +75 -0
- package/dist/module-access.js +606 -0
- package/dist/module-resolver.d.ts +8 -0
- package/dist/module-resolver.js +150 -0
- package/dist/os-filesystem.d.ts +42 -0
- package/dist/os-filesystem.js +161 -0
- package/dist/package-bundler.d.ts +36 -0
- package/dist/package-bundler.js +497 -0
- package/dist/polyfills.d.ts +17 -0
- package/dist/polyfills.js +97 -0
- package/dist/worker-adapter.d.ts +21 -0
- package/dist/worker-adapter.js +34 -0
- package/package.json +123 -0
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
// child_process module polyfill for the sandbox
|
|
2
|
+
// Provides Node.js child_process module emulation that bridges to host
|
|
3
|
+
//
|
|
4
|
+
// Uses the active handles mechanism to keep the sandbox alive while child
|
|
5
|
+
// processes are running. See: docs-internal/node/ACTIVE_HANDLES.md
|
|
6
|
+
import { exposeCustomGlobal } from "@secure-exec/core/internal/shared/global-exposure";
|
|
7
|
+
// Child process instances — routes stream events from host to ChildProcess objects.
|
|
8
|
+
// Process state (running/exited) is tracked by the kernel process table; this Map
|
|
9
|
+
// is only for dispatching stdout/stderr/exit events to the sandbox-side objects.
|
|
10
|
+
const childProcessInstances = new Map();
|
|
11
|
+
/**
|
|
12
|
+
* Global dispatcher invoked by the host when child process data arrives.
|
|
13
|
+
* Routes stdout/stderr chunks and exit codes to the corresponding ChildProcess
|
|
14
|
+
* instance by session ID, and unregisters the active handle on exit.
|
|
15
|
+
*/
|
|
16
|
+
const childProcessDispatch = (sessionId, type, data) => {
|
|
17
|
+
const child = childProcessInstances.get(sessionId);
|
|
18
|
+
if (!child)
|
|
19
|
+
return;
|
|
20
|
+
if (type === "stdout") {
|
|
21
|
+
const buf = typeof Buffer !== "undefined" ? Buffer.from(data) : data;
|
|
22
|
+
child.stdout.emit("data", buf);
|
|
23
|
+
}
|
|
24
|
+
else if (type === "stderr") {
|
|
25
|
+
const buf = typeof Buffer !== "undefined" ? Buffer.from(data) : data;
|
|
26
|
+
child.stderr.emit("data", buf);
|
|
27
|
+
}
|
|
28
|
+
else if (type === "exit") {
|
|
29
|
+
child.exitCode = data;
|
|
30
|
+
child.stdout.emit("end");
|
|
31
|
+
child.stderr.emit("end");
|
|
32
|
+
child.emit("close", data, null);
|
|
33
|
+
child.emit("exit", data, null);
|
|
34
|
+
childProcessInstances.delete(sessionId);
|
|
35
|
+
// Unregister handle - allows sandbox to exit if no other handles remain
|
|
36
|
+
// See: docs-internal/node/ACTIVE_HANDLES.md
|
|
37
|
+
if (typeof _unregisterHandle === "function") {
|
|
38
|
+
_unregisterHandle(`child:${sessionId}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
exposeCustomGlobal("_childProcessDispatch", childProcessDispatch);
|
|
43
|
+
/** Warn when listener count exceeds max (Node.js: warn, don't crash) */
|
|
44
|
+
function checkStreamMaxListeners(stream, event) {
|
|
45
|
+
if (stream._maxListeners > 0 && !stream._maxListenersWarned.has(event)) {
|
|
46
|
+
const total = (stream._listeners[event]?.length ?? 0) + (stream._onceListeners[event]?.length ?? 0);
|
|
47
|
+
if (total > stream._maxListeners) {
|
|
48
|
+
stream._maxListenersWarned.add(event);
|
|
49
|
+
const warning = `MaxListenersExceededWarning: Possible EventEmitter memory leak detected. ${total} ${event} listeners added. MaxListeners is ${stream._maxListeners}. Use emitter.setMaxListeners() to increase limit`;
|
|
50
|
+
if (typeof console !== "undefined" && console.error) {
|
|
51
|
+
console.error(warning);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Monotonic counter for unique ChildProcess PIDs
|
|
57
|
+
let _nextChildPid = 1000;
|
|
58
|
+
/**
|
|
59
|
+
* Polyfill of Node.js `ChildProcess`. Provides event-emitting stdin/stdout/stderr
|
|
60
|
+
* streams. In streaming mode, data arrives via the `_childProcessDispatch` global
|
|
61
|
+
* that the host calls with stdout/stderr/exit events keyed by session ID.
|
|
62
|
+
*/
|
|
63
|
+
class ChildProcess {
|
|
64
|
+
_listeners = {};
|
|
65
|
+
_onceListeners = {};
|
|
66
|
+
_maxListeners = 10;
|
|
67
|
+
_maxListenersWarned = new Set();
|
|
68
|
+
pid = _nextChildPid++;
|
|
69
|
+
killed = false;
|
|
70
|
+
exitCode = null;
|
|
71
|
+
signalCode = null;
|
|
72
|
+
connected = false;
|
|
73
|
+
spawnfile = "";
|
|
74
|
+
spawnargs = [];
|
|
75
|
+
stdin;
|
|
76
|
+
stdout;
|
|
77
|
+
stderr;
|
|
78
|
+
stdio;
|
|
79
|
+
constructor() {
|
|
80
|
+
// Create stdin stream stub
|
|
81
|
+
this.stdin = {
|
|
82
|
+
writable: true,
|
|
83
|
+
write(_data) {
|
|
84
|
+
return true;
|
|
85
|
+
},
|
|
86
|
+
end() {
|
|
87
|
+
this.writable = false;
|
|
88
|
+
},
|
|
89
|
+
on() {
|
|
90
|
+
return this;
|
|
91
|
+
},
|
|
92
|
+
once() {
|
|
93
|
+
return this;
|
|
94
|
+
},
|
|
95
|
+
emit() {
|
|
96
|
+
return false;
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
// Create stdout stream stub
|
|
100
|
+
this.stdout = {
|
|
101
|
+
readable: true,
|
|
102
|
+
_listeners: {},
|
|
103
|
+
_onceListeners: {},
|
|
104
|
+
_maxListeners: 10,
|
|
105
|
+
_maxListenersWarned: new Set(),
|
|
106
|
+
on(event, listener) {
|
|
107
|
+
if (!this._listeners[event])
|
|
108
|
+
this._listeners[event] = [];
|
|
109
|
+
this._listeners[event].push(listener);
|
|
110
|
+
checkStreamMaxListeners(this, event);
|
|
111
|
+
return this;
|
|
112
|
+
},
|
|
113
|
+
once(event, listener) {
|
|
114
|
+
if (!this._onceListeners[event])
|
|
115
|
+
this._onceListeners[event] = [];
|
|
116
|
+
this._onceListeners[event].push(listener);
|
|
117
|
+
checkStreamMaxListeners(this, event);
|
|
118
|
+
return this;
|
|
119
|
+
},
|
|
120
|
+
emit(event, ...args) {
|
|
121
|
+
if (this._listeners[event]) {
|
|
122
|
+
this._listeners[event].forEach((fn) => fn(...args));
|
|
123
|
+
}
|
|
124
|
+
if (this._onceListeners[event]) {
|
|
125
|
+
this._onceListeners[event].forEach((fn) => fn(...args));
|
|
126
|
+
this._onceListeners[event] = [];
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
},
|
|
130
|
+
read() {
|
|
131
|
+
return null;
|
|
132
|
+
},
|
|
133
|
+
setEncoding() {
|
|
134
|
+
return this;
|
|
135
|
+
},
|
|
136
|
+
setMaxListeners(n) {
|
|
137
|
+
this._maxListeners = n;
|
|
138
|
+
return this;
|
|
139
|
+
},
|
|
140
|
+
getMaxListeners() {
|
|
141
|
+
return this._maxListeners;
|
|
142
|
+
},
|
|
143
|
+
pipe(dest) {
|
|
144
|
+
return dest;
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
// Create stderr stream stub
|
|
148
|
+
this.stderr = {
|
|
149
|
+
readable: true,
|
|
150
|
+
_listeners: {},
|
|
151
|
+
_onceListeners: {},
|
|
152
|
+
_maxListeners: 10,
|
|
153
|
+
_maxListenersWarned: new Set(),
|
|
154
|
+
on(event, listener) {
|
|
155
|
+
if (!this._listeners[event])
|
|
156
|
+
this._listeners[event] = [];
|
|
157
|
+
this._listeners[event].push(listener);
|
|
158
|
+
checkStreamMaxListeners(this, event);
|
|
159
|
+
return this;
|
|
160
|
+
},
|
|
161
|
+
once(event, listener) {
|
|
162
|
+
if (!this._onceListeners[event])
|
|
163
|
+
this._onceListeners[event] = [];
|
|
164
|
+
this._onceListeners[event].push(listener);
|
|
165
|
+
checkStreamMaxListeners(this, event);
|
|
166
|
+
return this;
|
|
167
|
+
},
|
|
168
|
+
emit(event, ...args) {
|
|
169
|
+
if (this._listeners[event]) {
|
|
170
|
+
this._listeners[event].forEach((fn) => fn(...args));
|
|
171
|
+
}
|
|
172
|
+
if (this._onceListeners[event]) {
|
|
173
|
+
this._onceListeners[event].forEach((fn) => fn(...args));
|
|
174
|
+
this._onceListeners[event] = [];
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
},
|
|
178
|
+
read() {
|
|
179
|
+
return null;
|
|
180
|
+
},
|
|
181
|
+
setEncoding() {
|
|
182
|
+
return this;
|
|
183
|
+
},
|
|
184
|
+
setMaxListeners(n) {
|
|
185
|
+
this._maxListeners = n;
|
|
186
|
+
return this;
|
|
187
|
+
},
|
|
188
|
+
getMaxListeners() {
|
|
189
|
+
return this._maxListeners;
|
|
190
|
+
},
|
|
191
|
+
pipe(dest) {
|
|
192
|
+
return dest;
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
this.stdio = [this.stdin, this.stdout, this.stderr];
|
|
196
|
+
}
|
|
197
|
+
on(event, listener) {
|
|
198
|
+
if (!this._listeners[event])
|
|
199
|
+
this._listeners[event] = [];
|
|
200
|
+
this._listeners[event].push(listener);
|
|
201
|
+
this._checkMaxListeners(event);
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
once(event, listener) {
|
|
205
|
+
if (!this._onceListeners[event])
|
|
206
|
+
this._onceListeners[event] = [];
|
|
207
|
+
this._onceListeners[event].push(listener);
|
|
208
|
+
this._checkMaxListeners(event);
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
off(event, listener) {
|
|
212
|
+
if (this._listeners[event]) {
|
|
213
|
+
const idx = this._listeners[event].indexOf(listener);
|
|
214
|
+
if (idx !== -1)
|
|
215
|
+
this._listeners[event].splice(idx, 1);
|
|
216
|
+
}
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
removeListener(event, listener) {
|
|
220
|
+
return this.off(event, listener);
|
|
221
|
+
}
|
|
222
|
+
setMaxListeners(n) {
|
|
223
|
+
this._maxListeners = n;
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
getMaxListeners() {
|
|
227
|
+
return this._maxListeners;
|
|
228
|
+
}
|
|
229
|
+
_checkMaxListeners(event) {
|
|
230
|
+
if (this._maxListeners > 0 && !this._maxListenersWarned.has(event)) {
|
|
231
|
+
const total = (this._listeners[event]?.length ?? 0) + (this._onceListeners[event]?.length ?? 0);
|
|
232
|
+
if (total > this._maxListeners) {
|
|
233
|
+
this._maxListenersWarned.add(event);
|
|
234
|
+
const warning = `MaxListenersExceededWarning: Possible EventEmitter memory leak detected. ${total} ${event} listeners added to [ChildProcess]. MaxListeners is ${this._maxListeners}. Use emitter.setMaxListeners() to increase limit`;
|
|
235
|
+
if (typeof console !== "undefined" && console.error) {
|
|
236
|
+
console.error(warning);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
emit(event, ...args) {
|
|
242
|
+
let handled = false;
|
|
243
|
+
if (this._listeners[event]) {
|
|
244
|
+
this._listeners[event].forEach((fn) => {
|
|
245
|
+
fn(...args);
|
|
246
|
+
handled = true;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
if (this._onceListeners[event]) {
|
|
250
|
+
this._onceListeners[event].forEach((fn) => {
|
|
251
|
+
fn(...args);
|
|
252
|
+
handled = true;
|
|
253
|
+
});
|
|
254
|
+
this._onceListeners[event] = [];
|
|
255
|
+
}
|
|
256
|
+
return handled;
|
|
257
|
+
}
|
|
258
|
+
kill(_signal) {
|
|
259
|
+
this.killed = true;
|
|
260
|
+
this.signalCode = (typeof _signal === "string" ? _signal : "SIGTERM");
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
ref() {
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
unref() {
|
|
267
|
+
return this;
|
|
268
|
+
}
|
|
269
|
+
disconnect() {
|
|
270
|
+
this.connected = false;
|
|
271
|
+
}
|
|
272
|
+
_complete(stdout, stderr, code) {
|
|
273
|
+
this.exitCode = code;
|
|
274
|
+
// Emit data events for stdout/stderr as single chunks
|
|
275
|
+
if (stdout) {
|
|
276
|
+
const buf = typeof Buffer !== "undefined" ? Buffer.from(stdout) : stdout;
|
|
277
|
+
this.stdout.emit("data", buf);
|
|
278
|
+
}
|
|
279
|
+
if (stderr) {
|
|
280
|
+
const buf = typeof Buffer !== "undefined" ? Buffer.from(stderr) : stderr;
|
|
281
|
+
this.stderr.emit("data", buf);
|
|
282
|
+
}
|
|
283
|
+
// Emit end events
|
|
284
|
+
this.stdout.emit("end");
|
|
285
|
+
this.stderr.emit("end");
|
|
286
|
+
// Emit close event (code, signal)
|
|
287
|
+
this.emit("close", code, this.signalCode);
|
|
288
|
+
// Emit exit event
|
|
289
|
+
this.emit("exit", code, this.signalCode);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// exec - execute shell command, callback when done
|
|
293
|
+
// Uses spawn("bash", ["-c", command]) internally
|
|
294
|
+
// NOTE: WASIX bash returns incorrect exit codes (45 instead of 0) for -c flag,
|
|
295
|
+
// so error will be set even on successful commands. The stdout/stderr are correct.
|
|
296
|
+
function exec(command, options, callback) {
|
|
297
|
+
if (typeof options === "function") {
|
|
298
|
+
callback = options;
|
|
299
|
+
options = {};
|
|
300
|
+
}
|
|
301
|
+
// Use spawn with shell to execute the command
|
|
302
|
+
const child = spawn("bash", ["-c", command], { shell: false });
|
|
303
|
+
child.spawnargs = ["bash", "-c", command];
|
|
304
|
+
child.spawnfile = "bash";
|
|
305
|
+
// Collect output and invoke callback with maxBuffer enforcement
|
|
306
|
+
const maxBuffer = options?.maxBuffer ?? 1024 * 1024;
|
|
307
|
+
let stdout = "";
|
|
308
|
+
let stderr = "";
|
|
309
|
+
let stdoutBytes = 0;
|
|
310
|
+
let stderrBytes = 0;
|
|
311
|
+
let maxBufferExceeded = false;
|
|
312
|
+
child.stdout.on("data", (data) => {
|
|
313
|
+
if (maxBufferExceeded)
|
|
314
|
+
return;
|
|
315
|
+
const chunk = String(data);
|
|
316
|
+
stdout += chunk;
|
|
317
|
+
stdoutBytes += chunk.length;
|
|
318
|
+
if (stdoutBytes > maxBuffer) {
|
|
319
|
+
maxBufferExceeded = true;
|
|
320
|
+
child.kill("SIGTERM");
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
child.stderr.on("data", (data) => {
|
|
324
|
+
if (maxBufferExceeded)
|
|
325
|
+
return;
|
|
326
|
+
const chunk = String(data);
|
|
327
|
+
stderr += chunk;
|
|
328
|
+
stderrBytes += chunk.length;
|
|
329
|
+
if (stderrBytes > maxBuffer) {
|
|
330
|
+
maxBufferExceeded = true;
|
|
331
|
+
child.kill("SIGTERM");
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
child.on("close", (...args) => {
|
|
335
|
+
const code = args[0];
|
|
336
|
+
if (callback) {
|
|
337
|
+
if (maxBufferExceeded) {
|
|
338
|
+
const err = new Error("stdout maxBuffer length exceeded");
|
|
339
|
+
err.code = "ERR_CHILD_PROCESS_STDIO_MAXBUFFER";
|
|
340
|
+
err.killed = true;
|
|
341
|
+
err.cmd = command;
|
|
342
|
+
err.stdout = stdout;
|
|
343
|
+
err.stderr = stderr;
|
|
344
|
+
callback(err, stdout, stderr);
|
|
345
|
+
}
|
|
346
|
+
else if (code !== 0) {
|
|
347
|
+
const err = new Error("Command failed: " + command);
|
|
348
|
+
err.code = code;
|
|
349
|
+
err.killed = false;
|
|
350
|
+
err.signal = null;
|
|
351
|
+
err.cmd = command;
|
|
352
|
+
err.stdout = stdout;
|
|
353
|
+
err.stderr = stderr;
|
|
354
|
+
callback(err, stdout, stderr);
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
callback(null, stdout, stderr);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
child.on("error", (err) => {
|
|
362
|
+
if (callback) {
|
|
363
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
364
|
+
error.code = 1;
|
|
365
|
+
error.stdout = stdout;
|
|
366
|
+
error.stderr = stderr;
|
|
367
|
+
callback(error, stdout, stderr);
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
return child;
|
|
371
|
+
}
|
|
372
|
+
// execSync - synchronous shell execution
|
|
373
|
+
// Uses spawnSync("bash", ["-c", command]) internally
|
|
374
|
+
function execSync(command, options) {
|
|
375
|
+
const opts = options || {};
|
|
376
|
+
if (typeof _childProcessSpawnSync === "undefined") {
|
|
377
|
+
throw new Error("child_process.execSync requires CommandExecutor to be configured");
|
|
378
|
+
}
|
|
379
|
+
// Default maxBuffer 1MB (Node.js convention)
|
|
380
|
+
const maxBuffer = opts.maxBuffer ?? 1024 * 1024;
|
|
381
|
+
// Use synchronous bridge call - result is JSON string
|
|
382
|
+
const jsonResult = _childProcessSpawnSync.applySyncPromise(undefined, [
|
|
383
|
+
"bash",
|
|
384
|
+
JSON.stringify(["-c", command]),
|
|
385
|
+
JSON.stringify({ cwd: opts.cwd, env: opts.env, maxBuffer }),
|
|
386
|
+
]);
|
|
387
|
+
const result = JSON.parse(jsonResult);
|
|
388
|
+
if (result.maxBufferExceeded) {
|
|
389
|
+
const err = new Error("stdout maxBuffer length exceeded");
|
|
390
|
+
err.code = "ERR_CHILD_PROCESS_STDIO_MAXBUFFER";
|
|
391
|
+
err.stdout = result.stdout;
|
|
392
|
+
err.stderr = result.stderr;
|
|
393
|
+
throw err;
|
|
394
|
+
}
|
|
395
|
+
if (result.code !== 0) {
|
|
396
|
+
const err = new Error("Command failed: " + command);
|
|
397
|
+
err.status = result.code;
|
|
398
|
+
err.stdout = result.stdout;
|
|
399
|
+
err.stderr = result.stderr;
|
|
400
|
+
err.output = [null, result.stdout, result.stderr];
|
|
401
|
+
throw err;
|
|
402
|
+
}
|
|
403
|
+
if (opts.encoding === "buffer" || !opts.encoding) {
|
|
404
|
+
return typeof Buffer !== "undefined" ? Buffer.from(result.stdout) : result.stdout;
|
|
405
|
+
}
|
|
406
|
+
return result.stdout;
|
|
407
|
+
}
|
|
408
|
+
// spawn - spawn a command with streaming
|
|
409
|
+
function spawn(command, args, options) {
|
|
410
|
+
let argsArray = [];
|
|
411
|
+
let opts = {};
|
|
412
|
+
if (!Array.isArray(args)) {
|
|
413
|
+
opts = args || {};
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
argsArray = args;
|
|
417
|
+
opts = options || {};
|
|
418
|
+
}
|
|
419
|
+
const child = new ChildProcess();
|
|
420
|
+
child.spawnfile = command;
|
|
421
|
+
child.spawnargs = [command, ...argsArray];
|
|
422
|
+
// Check if streaming mode is available
|
|
423
|
+
if (typeof _childProcessSpawnStart !== "undefined") {
|
|
424
|
+
// Use process.cwd() as default if no cwd specified
|
|
425
|
+
// This ensures process.chdir() changes are reflected in child processes
|
|
426
|
+
const effectiveCwd = opts.cwd ?? (typeof process !== "undefined" ? process.cwd() : "/");
|
|
427
|
+
// Streaming mode - spawn immediately
|
|
428
|
+
const sessionId = _childProcessSpawnStart.applySync(undefined, [
|
|
429
|
+
command,
|
|
430
|
+
JSON.stringify(argsArray),
|
|
431
|
+
JSON.stringify({ cwd: effectiveCwd, env: opts.env }),
|
|
432
|
+
]);
|
|
433
|
+
childProcessInstances.set(sessionId, child);
|
|
434
|
+
// Register handle to keep sandbox alive until child exits
|
|
435
|
+
// See: docs-internal/node/ACTIVE_HANDLES.md
|
|
436
|
+
if (typeof _registerHandle === "function") {
|
|
437
|
+
_registerHandle(`child:${sessionId}`, `child_process: ${command} ${argsArray.join(" ")}`);
|
|
438
|
+
}
|
|
439
|
+
// Override stdin methods for streaming
|
|
440
|
+
child.stdin.write = (data) => {
|
|
441
|
+
if (typeof _childProcessStdinWrite === "undefined")
|
|
442
|
+
return false;
|
|
443
|
+
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
444
|
+
_childProcessStdinWrite.applySync(undefined, [sessionId, bytes]);
|
|
445
|
+
return true;
|
|
446
|
+
};
|
|
447
|
+
child.stdin.end = () => {
|
|
448
|
+
if (typeof _childProcessStdinClose !== "undefined") {
|
|
449
|
+
_childProcessStdinClose.applySync(undefined, [sessionId]);
|
|
450
|
+
}
|
|
451
|
+
child.stdin.writable = false;
|
|
452
|
+
};
|
|
453
|
+
// Override kill method
|
|
454
|
+
child.kill = (signal) => {
|
|
455
|
+
if (typeof _childProcessKill === "undefined")
|
|
456
|
+
return false;
|
|
457
|
+
const sig = signal === "SIGKILL" || signal === 9
|
|
458
|
+
? 9
|
|
459
|
+
: signal === "SIGINT" || signal === 2
|
|
460
|
+
? 2
|
|
461
|
+
: 15;
|
|
462
|
+
_childProcessKill.applySync(undefined, [sessionId, sig]);
|
|
463
|
+
child.killed = true;
|
|
464
|
+
child.signalCode = (typeof signal === "string" ? signal : "SIGTERM");
|
|
465
|
+
return true;
|
|
466
|
+
};
|
|
467
|
+
return child;
|
|
468
|
+
}
|
|
469
|
+
// Fallback: no CommandExecutor available
|
|
470
|
+
const err = new Error("child_process.spawn requires CommandExecutor to be configured");
|
|
471
|
+
// Emit error asynchronously to match Node.js behavior
|
|
472
|
+
setTimeout(() => {
|
|
473
|
+
child.emit("error", err);
|
|
474
|
+
child._complete("", err.message, 1);
|
|
475
|
+
}, 0);
|
|
476
|
+
return child;
|
|
477
|
+
}
|
|
478
|
+
// spawnSync - synchronous spawn
|
|
479
|
+
function spawnSync(command, args, options) {
|
|
480
|
+
let argsArray = [];
|
|
481
|
+
let opts = {};
|
|
482
|
+
if (!Array.isArray(args)) {
|
|
483
|
+
opts = args || {};
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
argsArray = args;
|
|
487
|
+
opts = options || {};
|
|
488
|
+
}
|
|
489
|
+
if (typeof _childProcessSpawnSync === "undefined") {
|
|
490
|
+
return {
|
|
491
|
+
pid: _nextChildPid++,
|
|
492
|
+
output: [null, "", "child_process.spawnSync requires CommandExecutor to be configured"],
|
|
493
|
+
stdout: "",
|
|
494
|
+
stderr: "child_process.spawnSync requires CommandExecutor to be configured",
|
|
495
|
+
status: 1,
|
|
496
|
+
signal: null,
|
|
497
|
+
error: new Error("child_process.spawnSync requires CommandExecutor to be configured"),
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
try {
|
|
501
|
+
// Use process.cwd() as default if no cwd specified
|
|
502
|
+
// This ensures process.chdir() changes are reflected in child processes
|
|
503
|
+
const effectiveCwd = opts.cwd ?? (typeof process !== "undefined" ? process.cwd() : "/");
|
|
504
|
+
// Pass maxBuffer through to host for enforcement
|
|
505
|
+
const maxBuffer = opts.maxBuffer;
|
|
506
|
+
// Args passed as JSON string for transferability
|
|
507
|
+
const jsonResult = _childProcessSpawnSync.applySyncPromise(undefined, [
|
|
508
|
+
command,
|
|
509
|
+
JSON.stringify(argsArray),
|
|
510
|
+
JSON.stringify({ cwd: effectiveCwd, env: opts.env, maxBuffer }),
|
|
511
|
+
]);
|
|
512
|
+
const result = JSON.parse(jsonResult);
|
|
513
|
+
const stdoutBuf = typeof Buffer !== "undefined" ? Buffer.from(result.stdout) : result.stdout;
|
|
514
|
+
const stderrBuf = typeof Buffer !== "undefined" ? Buffer.from(result.stderr) : result.stderr;
|
|
515
|
+
if (result.maxBufferExceeded) {
|
|
516
|
+
const err = new Error("stdout maxBuffer length exceeded");
|
|
517
|
+
err.code = "ERR_CHILD_PROCESS_STDIO_MAXBUFFER";
|
|
518
|
+
return {
|
|
519
|
+
pid: _nextChildPid++,
|
|
520
|
+
output: [null, stdoutBuf, stderrBuf],
|
|
521
|
+
stdout: stdoutBuf,
|
|
522
|
+
stderr: stderrBuf,
|
|
523
|
+
status: result.code,
|
|
524
|
+
signal: null,
|
|
525
|
+
error: err,
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
return {
|
|
529
|
+
pid: _nextChildPid++,
|
|
530
|
+
output: [null, stdoutBuf, stderrBuf],
|
|
531
|
+
stdout: stdoutBuf,
|
|
532
|
+
stderr: stderrBuf,
|
|
533
|
+
status: result.code,
|
|
534
|
+
signal: null,
|
|
535
|
+
error: undefined,
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
catch (err) {
|
|
539
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
540
|
+
const stderrBuf = typeof Buffer !== "undefined" ? Buffer.from(errMsg) : errMsg;
|
|
541
|
+
return {
|
|
542
|
+
pid: _nextChildPid++,
|
|
543
|
+
output: [null, "", stderrBuf],
|
|
544
|
+
stdout: typeof Buffer !== "undefined" ? Buffer.from("") : "",
|
|
545
|
+
stderr: stderrBuf,
|
|
546
|
+
status: 1,
|
|
547
|
+
signal: null,
|
|
548
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
// execFile - execute a file directly
|
|
553
|
+
function execFile(file, args, options, callback) {
|
|
554
|
+
let argsArray = [];
|
|
555
|
+
let opts = {};
|
|
556
|
+
let cb;
|
|
557
|
+
if (typeof args === "function") {
|
|
558
|
+
cb = args;
|
|
559
|
+
}
|
|
560
|
+
else if (typeof options === "function") {
|
|
561
|
+
argsArray = args.slice();
|
|
562
|
+
cb = options;
|
|
563
|
+
}
|
|
564
|
+
else {
|
|
565
|
+
argsArray = Array.isArray(args) ? args : [];
|
|
566
|
+
opts = options || {};
|
|
567
|
+
cb = callback;
|
|
568
|
+
}
|
|
569
|
+
// execFile is like spawn but with callback, with maxBuffer enforcement
|
|
570
|
+
const maxBuffer = opts.maxBuffer ?? 1024 * 1024;
|
|
571
|
+
const child = spawn(file, argsArray, opts);
|
|
572
|
+
let stdout = "";
|
|
573
|
+
let stderr = "";
|
|
574
|
+
let stdoutBytes = 0;
|
|
575
|
+
let stderrBytes = 0;
|
|
576
|
+
let maxBufferExceeded = false;
|
|
577
|
+
child.stdout.on("data", (data) => {
|
|
578
|
+
const chunk = String(data);
|
|
579
|
+
stdout += chunk;
|
|
580
|
+
stdoutBytes += chunk.length;
|
|
581
|
+
if (stdoutBytes > maxBuffer && !maxBufferExceeded) {
|
|
582
|
+
maxBufferExceeded = true;
|
|
583
|
+
child.kill("SIGTERM");
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
child.stderr.on("data", (data) => {
|
|
587
|
+
const chunk = String(data);
|
|
588
|
+
stderr += chunk;
|
|
589
|
+
stderrBytes += chunk.length;
|
|
590
|
+
if (stderrBytes > maxBuffer && !maxBufferExceeded) {
|
|
591
|
+
maxBufferExceeded = true;
|
|
592
|
+
child.kill("SIGTERM");
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
child.on("close", (...args) => {
|
|
596
|
+
const code = args[0];
|
|
597
|
+
if (cb) {
|
|
598
|
+
if (maxBufferExceeded) {
|
|
599
|
+
const err = new Error("stdout maxBuffer length exceeded");
|
|
600
|
+
err.code = "ERR_CHILD_PROCESS_STDIO_MAXBUFFER";
|
|
601
|
+
err.killed = true;
|
|
602
|
+
err.stdout = stdout;
|
|
603
|
+
err.stderr = stderr;
|
|
604
|
+
cb(err, stdout, stderr);
|
|
605
|
+
}
|
|
606
|
+
else if (code !== 0) {
|
|
607
|
+
const err = new Error("Command failed: " + file);
|
|
608
|
+
err.code = code;
|
|
609
|
+
err.stdout = stdout;
|
|
610
|
+
err.stderr = stderr;
|
|
611
|
+
cb(err, stdout, stderr);
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
cb(null, stdout, stderr);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
child.on("error", (err) => {
|
|
619
|
+
if (cb) {
|
|
620
|
+
cb(err, stdout, stderr);
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
return child;
|
|
624
|
+
}
|
|
625
|
+
// execFileSync
|
|
626
|
+
function execFileSync(file, args, options) {
|
|
627
|
+
let argsArray = [];
|
|
628
|
+
let opts = {};
|
|
629
|
+
if (!Array.isArray(args)) {
|
|
630
|
+
opts = args || {};
|
|
631
|
+
}
|
|
632
|
+
else {
|
|
633
|
+
argsArray = args;
|
|
634
|
+
opts = options || {};
|
|
635
|
+
}
|
|
636
|
+
// Default maxBuffer 1MB for execFileSync (Node.js convention)
|
|
637
|
+
const maxBuffer = opts.maxBuffer ?? 1024 * 1024;
|
|
638
|
+
const result = spawnSync(file, argsArray, { ...opts, maxBuffer });
|
|
639
|
+
if (result.error && String(result.error.code) === "ERR_CHILD_PROCESS_STDIO_MAXBUFFER") {
|
|
640
|
+
throw result.error;
|
|
641
|
+
}
|
|
642
|
+
if (result.status !== 0) {
|
|
643
|
+
const err = new Error("Command failed: " + file);
|
|
644
|
+
err.status = result.status ?? undefined;
|
|
645
|
+
err.stdout = String(result.stdout);
|
|
646
|
+
err.stderr = String(result.stderr);
|
|
647
|
+
throw err;
|
|
648
|
+
}
|
|
649
|
+
if (opts.encoding === "buffer" || !opts.encoding) {
|
|
650
|
+
return result.stdout;
|
|
651
|
+
}
|
|
652
|
+
return typeof result.stdout === "string" ? result.stdout : result.stdout.toString(opts.encoding);
|
|
653
|
+
}
|
|
654
|
+
// fork - intentionally not implemented (IPC between processes not supported in sandbox)
|
|
655
|
+
function fork(_modulePath, _args, _options) {
|
|
656
|
+
throw new Error("child_process.fork is not supported in sandbox");
|
|
657
|
+
}
|
|
658
|
+
// Create the child_process module
|
|
659
|
+
const childProcess = {
|
|
660
|
+
ChildProcess,
|
|
661
|
+
exec,
|
|
662
|
+
execSync,
|
|
663
|
+
spawn,
|
|
664
|
+
spawnSync,
|
|
665
|
+
execFile,
|
|
666
|
+
execFileSync,
|
|
667
|
+
fork,
|
|
668
|
+
};
|
|
669
|
+
// Expose to global for require() to use
|
|
670
|
+
exposeCustomGlobal("_childProcessModule", childProcess);
|
|
671
|
+
export { ChildProcess, exec, execSync, spawn, spawnSync, execFile, execFileSync, fork };
|
|
672
|
+
export default childProcess;
|