-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
child_process.ts
843 lines (737 loc) · 21.4 KB
/
child_process.ts
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module implements 'child_process' module of Node.JS API.
// ref: https://nodejs.org/api/child_process.html
// TODO(petamoriken): enable prefer-primordials for node polyfills
// deno-lint-ignore-file prefer-primordials
import { internals } from "ext:core/mod.js";
import {
op_bootstrap_unstable_args,
op_node_child_ipc_pipe,
op_npm_process_state,
} from "ext:core/ops";
import {
ChildProcess,
ChildProcessOptions,
normalizeSpawnArguments,
setupChannel,
type SpawnOptions,
spawnSync as _spawnSync,
type SpawnSyncOptions,
type SpawnSyncResult,
stdioStringToArray,
} from "ext:deno_node/internal/child_process.ts";
import {
validateAbortSignal,
validateFunction,
validateObject,
validateString,
} from "ext:deno_node/internal/validators.mjs";
import {
ERR_CHILD_PROCESS_IPC_REQUIRED,
ERR_CHILD_PROCESS_STDIO_MAXBUFFER,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_OUT_OF_RANGE,
genericNodeError,
} from "ext:deno_node/internal/errors.ts";
import {
ArrayIsArray,
ArrayPrototypeJoin,
ArrayPrototypePush,
ArrayPrototypeSlice,
ObjectAssign,
StringPrototypeSlice,
} from "ext:deno_node/internal/primordials.mjs";
import { getSystemErrorName, promisify } from "node:util";
import { createDeferredPromise } from "ext:deno_node/internal/util.mjs";
import process from "node:process";
import { Buffer } from "node:buffer";
import {
convertToValidSignal,
kEmptyObject,
} from "ext:deno_node/internal/util.mjs";
const MAX_BUFFER = 1024 * 1024;
type ForkOptions = ChildProcessOptions;
/**
* Spawns a new Node.js process + fork.
* @param modulePath
* @param args
* @param option
* @returns
*/
export function fork(
modulePath: string,
_args?: string[],
_options?: ForkOptions,
) {
validateString(modulePath, "modulePath");
// Get options and args arguments.
let execArgv;
let options: SpawnOptions & {
execArgv?: string;
execPath?: string;
silent?: boolean;
} = {};
let args: string[] = [];
let pos = 1;
if (pos < arguments.length && Array.isArray(arguments[pos])) {
args = arguments[pos++];
}
if (pos < arguments.length && arguments[pos] == null) {
pos++;
}
if (pos < arguments.length && arguments[pos] != null) {
if (typeof arguments[pos] !== "object") {
throw new ERR_INVALID_ARG_VALUE(`arguments[${pos}]`, arguments[pos]);
}
options = { ...arguments[pos++] };
}
// Prepare arguments for fork:
execArgv = options.execArgv || process.execArgv;
if (execArgv === process.execArgv && process._eval != null) {
const index = execArgv.lastIndexOf(process._eval);
if (index > 0) {
// Remove the -e switch to avoid fork bombing ourselves.
execArgv = execArgv.slice(0);
execArgv.splice(index - 1, 2);
}
}
// TODO(bartlomieju): this is incomplete, currently only handling a single
// V8 flag to get Prisma integration running, we should fill this out with
// more
const v8Flags: string[] = [];
if (Array.isArray(execArgv)) {
for (let index = 0; index < execArgv.length; index++) {
const flag = execArgv[index];
if (flag.startsWith("--max-old-space-size")) {
execArgv.splice(index, 1);
v8Flags.push(flag);
} else if (flag.startsWith("--enable-source-maps")) {
// https://github.com/denoland/deno/issues/21750
execArgv.splice(index, 1);
}
}
}
const stringifiedV8Flags: string[] = [];
if (v8Flags.length > 0) {
stringifiedV8Flags.push("--v8-flags=" + v8Flags.join(","));
}
args = [
"run",
...op_bootstrap_unstable_args(),
"--node-modules-dir",
"-A",
...stringifiedV8Flags,
...execArgv,
modulePath,
...args,
];
if (typeof options.stdio === "string") {
options.stdio = stdioStringToArray(options.stdio, "ipc");
} else if (!Array.isArray(options.stdio)) {
// Use a separate fd=3 for the IPC channel. Inherit stdin, stdout,
// and stderr from the parent if silent isn't set.
options.stdio = stdioStringToArray(
options.silent ? "pipe" : "inherit",
"ipc",
);
} else if (!options.stdio.includes("ipc")) {
throw new ERR_CHILD_PROCESS_IPC_REQUIRED("options.stdio");
}
options.execPath = options.execPath || Deno.execPath();
options.shell = false;
Object.assign(options.env ??= {}, {
DENO_DONT_USE_INTERNAL_NODE_COMPAT_STATE: op_npm_process_state(),
});
return spawn(options.execPath, args, options);
}
export function spawn(command: string): ChildProcess;
export function spawn(command: string, options: SpawnOptions): ChildProcess;
export function spawn(command: string, args: string[]): ChildProcess;
export function spawn(
command: string,
args: string[],
options: SpawnOptions,
): ChildProcess;
/**
* Spawns a child process using `command`.
*/
export function spawn(
command: string,
argsOrOptions?: string[] | SpawnOptions,
maybeOptions?: SpawnOptions,
): ChildProcess {
const args = Array.isArray(argsOrOptions) ? argsOrOptions : [];
let options = !Array.isArray(argsOrOptions) && argsOrOptions != null
? argsOrOptions
: maybeOptions as SpawnOptions;
options = normalizeSpawnArguments(command, args, options);
validateAbortSignal(options?.signal, "options.signal");
return new ChildProcess(command, args, options);
}
function validateTimeout(timeout?: number) {
if (timeout != null && !(Number.isInteger(timeout) && timeout >= 0)) {
throw new ERR_OUT_OF_RANGE("timeout", "an unsigned integer", timeout);
}
}
function validateMaxBuffer(maxBuffer?: number) {
if (
maxBuffer != null &&
!(typeof maxBuffer === "number" && maxBuffer >= 0)
) {
throw new ERR_OUT_OF_RANGE(
"options.maxBuffer",
"a positive number",
maxBuffer,
);
}
}
function sanitizeKillSignal(killSignal?: string | number) {
if (typeof killSignal === "string" || typeof killSignal === "number") {
return convertToValidSignal(killSignal);
} else if (killSignal != null) {
throw new ERR_INVALID_ARG_TYPE(
"options.killSignal",
["string", "number"],
killSignal,
);
}
}
export function spawnSync(
command: string,
argsOrOptions?: string[] | SpawnSyncOptions,
maybeOptions?: SpawnSyncOptions,
): SpawnSyncResult {
const args = Array.isArray(argsOrOptions) ? argsOrOptions : [];
let options = !Array.isArray(argsOrOptions) && argsOrOptions
? argsOrOptions
: maybeOptions as SpawnSyncOptions;
options = {
maxBuffer: MAX_BUFFER,
...normalizeSpawnArguments(command, args, options),
};
// Validate the timeout, if present.
validateTimeout(options.timeout);
// Validate maxBuffer, if present.
validateMaxBuffer(options.maxBuffer);
// Validate and translate the kill signal, if present.
sanitizeKillSignal(options.killSignal);
return _spawnSync(command, args, options);
}
interface ExecOptions extends
Pick<
ChildProcessOptions,
| "env"
| "signal"
| "uid"
| "gid"
| "windowsHide"
> {
cwd?: string | URL;
encoding?: string;
/**
* Shell to execute the command with.
*/
shell?: string;
timeout?: number;
/**
* Largest amount of data in bytes allowed on stdout or stderr. If exceeded, the child process is terminated and any output is truncated.
*/
maxBuffer?: number;
killSignal?: string | number;
}
type ExecException = ChildProcessError;
type ExecCallback = (
error: ExecException | null,
stdout?: string | Buffer,
stderr?: string | Buffer,
) => void;
type ExecSyncOptions = SpawnSyncOptions;
type ExecFileSyncOptions = SpawnSyncOptions;
function normalizeExecArgs(
command: string,
optionsOrCallback?: ExecOptions | ExecSyncOptions | ExecCallback,
maybeCallback?: ExecCallback,
) {
let callback: ExecFileCallback | undefined = maybeCallback;
if (typeof optionsOrCallback === "function") {
callback = optionsOrCallback;
optionsOrCallback = undefined;
}
// Make a shallow copy so we don't clobber the user's options object.
const options: ExecOptions | ExecSyncOptions = { ...optionsOrCallback };
options.shell = typeof options.shell === "string" ? options.shell : true;
return {
file: command,
options: options!,
callback: callback!,
};
}
/**
* Spawns a shell executing the given command.
*/
export function exec(command: string): ChildProcess;
export function exec(command: string, options: ExecOptions): ChildProcess;
export function exec(command: string, callback: ExecCallback): ChildProcess;
export function exec(
command: string,
options: ExecOptions,
callback: ExecCallback,
): ChildProcess;
export function exec(
command: string,
optionsOrCallback?: ExecOptions | ExecCallback,
maybeCallback?: ExecCallback,
): ChildProcess {
const opts = normalizeExecArgs(command, optionsOrCallback, maybeCallback);
return execFile(opts.file, opts.options as ExecFileOptions, opts.callback);
}
interface PromiseWithChild<T> extends Promise<T> {
child: ChildProcess;
}
type ExecOutputForPromisify = {
stdout?: string | Buffer;
stderr?: string | Buffer;
};
type ExecExceptionForPromisify = ExecException & ExecOutputForPromisify;
const customPromiseExecFunction = (orig: typeof exec) => {
return (...args: [command: string, options: ExecOptions]) => {
const { promise, resolve, reject } = createDeferredPromise() as unknown as {
promise: PromiseWithChild<ExecOutputForPromisify>;
resolve?: (value: ExecOutputForPromisify) => void;
reject?: (reason?: ExecExceptionForPromisify) => void;
};
promise.child = orig(...args, (err, stdout, stderr) => {
if (err !== null) {
const _err: ExecExceptionForPromisify = err;
_err.stdout = stdout;
_err.stderr = stderr;
reject && reject(_err);
} else {
resolve && resolve({ stdout, stderr });
}
});
return promise;
};
};
Object.defineProperty(exec, promisify.custom, {
enumerable: false,
value: customPromiseExecFunction(exec),
});
interface ExecFileOptions extends ChildProcessOptions {
encoding?: string;
timeout?: number;
maxBuffer?: number;
killSignal?: string | number;
}
interface ChildProcessError extends Error {
code?: string | number;
killed?: boolean;
signal?: AbortSignal;
cmd?: string;
}
class ExecFileError extends Error implements ChildProcessError {
code?: string | number;
constructor(message: string) {
super(message);
this.code = "UNKNOWN";
}
}
type ExecFileCallback = (
error: ChildProcessError | null,
stdout?: string | Buffer,
stderr?: string | Buffer,
) => void;
export function execFile(file: string): ChildProcess;
export function execFile(
file: string,
callback: ExecFileCallback,
): ChildProcess;
export function execFile(file: string, args: string[]): ChildProcess;
export function execFile(
file: string,
args: string[],
callback: ExecFileCallback,
): ChildProcess;
export function execFile(file: string, options: ExecFileOptions): ChildProcess;
export function execFile(
file: string,
options: ExecFileOptions,
callback: ExecFileCallback,
): ChildProcess;
export function execFile(
file: string,
args: string[],
options: ExecFileOptions,
callback: ExecFileCallback,
): ChildProcess;
export function execFile(
file: string,
argsOrOptionsOrCallback?: string[] | ExecFileOptions | ExecFileCallback,
optionsOrCallback?: ExecFileOptions | ExecFileCallback,
maybeCallback?: ExecFileCallback,
): ChildProcess {
let args: string[] = [];
let options: ExecFileOptions = {};
let callback: ExecFileCallback | undefined;
if (Array.isArray(argsOrOptionsOrCallback)) {
args = argsOrOptionsOrCallback;
} else if (argsOrOptionsOrCallback instanceof Function) {
callback = argsOrOptionsOrCallback;
} else if (argsOrOptionsOrCallback) {
options = argsOrOptionsOrCallback;
}
if (optionsOrCallback instanceof Function) {
callback = optionsOrCallback;
} else if (optionsOrCallback) {
options = optionsOrCallback;
callback = maybeCallback;
}
const execOptions = {
encoding: "utf8",
timeout: 0,
maxBuffer: MAX_BUFFER,
killSignal: "SIGTERM",
shell: false,
...options,
};
validateTimeout(execOptions.timeout);
if (execOptions.maxBuffer < 0) {
throw new ERR_OUT_OF_RANGE(
"options.maxBuffer",
"a positive number",
execOptions.maxBuffer,
);
}
const spawnOptions: SpawnOptions = {
cwd: execOptions.cwd,
env: execOptions.env,
gid: execOptions.gid,
shell: execOptions.shell,
signal: execOptions.signal,
uid: execOptions.uid,
windowsHide: !!execOptions.windowsHide,
windowsVerbatimArguments: !!execOptions.windowsVerbatimArguments,
};
const child = spawn(file, args, spawnOptions);
let encoding: string | null;
const _stdout: (string | Uint8Array)[] = [];
const _stderr: (string | Uint8Array)[] = [];
if (
execOptions.encoding !== "buffer" && Buffer.isEncoding(execOptions.encoding)
) {
encoding = execOptions.encoding;
} else {
encoding = null;
}
let stdoutLen = 0;
let stderrLen = 0;
let killed = false;
let exited = false;
let timeoutId: number | null;
let ex: ChildProcessError | null = null;
let cmd = file;
function exithandler(code = 0, signal?: AbortSignal) {
if (exited) return;
exited = true;
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
if (!callback) return;
// merge chunks
let stdout;
let stderr;
if (
encoding ||
(
child.stdout &&
child.stdout.readableEncoding
)
) {
stdout = _stdout.join("");
} else {
stdout = Buffer.concat(_stdout as Buffer[]);
}
if (
encoding ||
(
child.stderr &&
child.stderr.readableEncoding
)
) {
stderr = _stderr.join("");
} else {
stderr = Buffer.concat(_stderr as Buffer[]);
}
if (!ex && code === 0 && signal === null) {
callback(null, stdout, stderr);
return;
}
if (args?.length) {
cmd += ` ${args.join(" ")}`;
}
if (!ex) {
ex = new ExecFileError(
"Command failed: " + cmd + "\n" + stderr,
);
ex.code = code < 0 ? getSystemErrorName(code) : code;
ex.killed = child.killed || killed;
ex.signal = signal;
}
ex.cmd = cmd;
callback(ex, stdout, stderr);
}
function errorhandler(e: ExecFileError) {
ex = e;
if (child.stdout) {
child.stdout.destroy();
}
if (child.stderr) {
child.stderr.destroy();
}
exithandler();
}
function kill() {
if (child.stdout) {
child.stdout.destroy();
}
if (child.stderr) {
child.stderr.destroy();
}
killed = true;
try {
child.kill(execOptions.killSignal);
} catch (e) {
if (e) {
ex = e as ChildProcessError;
}
exithandler();
}
}
if (execOptions.timeout > 0) {
timeoutId = setTimeout(function delayedKill() {
kill();
timeoutId = null;
}, execOptions.timeout);
}
if (child.stdout) {
if (encoding) {
child.stdout.setEncoding(encoding);
}
child.stdout.on("data", function onChildStdout(chunk: string | Buffer) {
// Do not need to count the length
if (execOptions.maxBuffer === Infinity) {
ArrayPrototypePush(_stdout, chunk);
return;
}
const encoding = child.stdout?.readableEncoding;
const length = encoding
? Buffer.byteLength(chunk, encoding)
: chunk.length;
const slice = encoding
? StringPrototypeSlice
: (buf: string | Buffer, ...args: number[]) => buf.slice(...args);
stdoutLen += length;
if (stdoutLen > execOptions.maxBuffer) {
const truncatedLen = execOptions.maxBuffer - (stdoutLen - length);
ArrayPrototypePush(_stdout, slice(chunk, 0, truncatedLen));
ex = new ERR_CHILD_PROCESS_STDIO_MAXBUFFER("stdout");
kill();
} else {
ArrayPrototypePush(_stdout, chunk);
}
});
}
if (child.stderr) {
if (encoding) {
child.stderr.setEncoding(encoding);
}
child.stderr.on("data", function onChildStderr(chunk: string | Buffer) {
// Do not need to count the length
if (execOptions.maxBuffer === Infinity) {
ArrayPrototypePush(_stderr, chunk);
return;
}
const encoding = child.stderr?.readableEncoding;
const length = encoding
? Buffer.byteLength(chunk, encoding)
: chunk.length;
const slice = encoding
? StringPrototypeSlice
: (buf: string | Buffer, ...args: number[]) => buf.slice(...args);
stderrLen += length;
if (stderrLen > execOptions.maxBuffer) {
const truncatedLen = execOptions.maxBuffer - (stderrLen - length);
ArrayPrototypePush(_stderr, slice(chunk, 0, truncatedLen));
ex = new ERR_CHILD_PROCESS_STDIO_MAXBUFFER("stderr");
kill();
} else {
ArrayPrototypePush(_stderr, chunk);
}
});
}
child.addListener("close", exithandler);
child.addListener("error", errorhandler);
return child;
}
type ExecFileExceptionForPromisify = ExecFileError & ExecOutputForPromisify;
const customPromiseExecFileFunction = (
orig: (
file: string,
argsOrOptionsOrCallback?: string[] | ExecFileOptions | ExecFileCallback,
optionsOrCallback?: ExecFileOptions | ExecFileCallback,
maybeCallback?: ExecFileCallback,
) => ChildProcess,
) => {
return (
...args: [
file: string,
argsOrOptions?: string[] | ExecFileOptions,
options?: ExecFileOptions,
]
) => {
const { promise, resolve, reject } = createDeferredPromise() as unknown as {
promise: PromiseWithChild<ExecOutputForPromisify>;
resolve?: (value: ExecOutputForPromisify) => void;
reject?: (reason?: ExecFileExceptionForPromisify) => void;
};
promise.child = orig(...args, (err, stdout, stderr) => {
if (err !== null) {
const _err: ExecFileExceptionForPromisify = err;
_err.stdout = stdout;
_err.stderr = stderr;
reject && reject(_err);
} else {
resolve && resolve({ stdout, stderr });
}
});
return promise;
};
};
Object.defineProperty(execFile, promisify.custom, {
enumerable: false,
value: customPromiseExecFileFunction(execFile),
});
function checkExecSyncError(
ret: SpawnSyncResult,
args: string[],
cmd?: string,
) {
let err;
if (ret.error) {
err = ret.error;
ObjectAssign(err, ret);
} else if (ret.status !== 0) {
let msg = "Command failed: ";
msg += cmd || ArrayPrototypeJoin(args, " ");
if (ret.stderr && ret.stderr.length > 0) {
msg += `\n${ret.stderr.toString()}`;
}
err = genericNodeError(msg, ret);
}
return err;
}
export function execSync(command: string, options: ExecSyncOptions) {
const opts = normalizeExecArgs(command, options);
const inheritStderr = !(opts.options as ExecSyncOptions).stdio;
const ret = spawnSync(opts.file, opts.options as SpawnSyncOptions);
if (inheritStderr && ret.stderr) {
process.stderr.write(ret.stderr);
}
const err = checkExecSyncError(ret, [], command);
if (err) {
throw err;
}
return ret.stdout;
}
function normalizeExecFileArgs(
file: string,
args?: string[] | null | ExecFileSyncOptions | ExecFileCallback,
options?: ExecFileSyncOptions | null | ExecFileCallback,
callback?: ExecFileCallback,
): {
file: string;
args: string[];
options: ExecFileSyncOptions;
callback?: ExecFileCallback;
} {
if (ArrayIsArray(args)) {
args = ArrayPrototypeSlice(args);
} else if (args != null && typeof args === "object") {
callback = options as ExecFileCallback;
options = args as ExecFileSyncOptions;
args = null;
} else if (typeof args === "function") {
callback = args;
options = null;
args = null;
}
if (args == null) {
args = [];
}
if (typeof options === "function") {
callback = options as ExecFileCallback;
} else if (options != null) {
validateObject(options, "options");
}
if (options == null) {
options = kEmptyObject;
}
args = args as string[];
options = options as ExecFileSyncOptions;
if (callback != null) {
validateFunction(callback, "callback");
}
// Validate argv0, if present.
if (options.argv0 != null) {
validateString(options.argv0, "options.argv0");
}
return { file, args, options, callback };
}
export function execFileSync(file: string): string | Buffer;
export function execFileSync(file: string, args: string[]): string | Buffer;
export function execFileSync(
file: string,
options: ExecFileSyncOptions,
): string | Buffer;
export function execFileSync(
file: string,
args: string[],
options: ExecFileSyncOptions,
): string | Buffer;
export function execFileSync(
file: string,
args?: string[] | ExecFileSyncOptions,
options?: ExecFileSyncOptions,
): string | Buffer {
({ file, args, options } = normalizeExecFileArgs(file, args, options));
const inheritStderr = !options.stdio;
const ret = spawnSync(file, args, options);
if (inheritStderr && ret.stderr) {
process.stderr.write(ret.stderr);
}
const errArgs: string[] = [options.argv0 || file, ...(args as string[])];
const err = checkExecSyncError(ret, errArgs);
if (err) {
throw err;
}
return ret.stdout as string | Buffer;
}
function setupChildProcessIpcChannel() {
const fd = op_node_child_ipc_pipe();
if (typeof fd != "number" || fd < 0) return;
setupChannel(process, fd);
}
internals.__setupChildProcessIpcChannel = setupChildProcessIpcChannel;
export default {
fork,
spawn,
exec,
execFile,
execFileSync,
execSync,
ChildProcess,
spawnSync,
};
export { ChildProcess };