Skip to content
File

Blob: src/node/child_process.ts

typescript175 lines
1// Copyright (c) 2017-2022 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import type {
6 SpawnSyncReturns,
7 SpawnSyncOptions,
8 ChildProcess as _ChildProcess,
9 ExecOptions,
10 ExecException,
11 ExecFileOptions,
12 ExecSyncOptions,
13 ForkOptions,
14} from 'node:child_process';
15import { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors';
16import { EventEmitter } from 'node-internal:events';
17import {
18 validateFunction,
19 validateObject,
20 validateString,
21 validateArray,
22} from 'node-internal:validators';
23import type { Readable, Writable } from 'node:stream';
24 
25export class ChildProcess extends EventEmitter implements _ChildProcess {
26 stdin: Writable | null = null;
27 stdout: Readable | null = null;
28 stderr: Readable | null = null;
29 killed: boolean = false;
30 stdio: [
31 Writable | null,
32 Readable | null,
33 Readable | null,
34 Writable | Readable | null | undefined,
35 Writable | Readable | null | undefined,
36 ] = [null, null, null, null, null];
37 connected: boolean = false;
38 exitCode: number | null = null;
39 signalCode: NodeJS.Signals | null = null;
40 spawnargs: string[] = [];
41 spawnfile: string;
42 
43 constructor() {
44 super();
45 throw new ERR_METHOD_NOT_IMPLEMENTED('child_process.ChildProcess');
46 }
47 
48 kill(_signal?: NodeJS.Signals | number): boolean {
49 return false;
50 }
51 
52 send(
53 _message: unknown,
54 _sendHandle?: unknown,
55 _options?: unknown,
56 _callback?: unknown
57 ): boolean {
58 return false;
59 }
60 
61 disconnect(): void {
62 // Do nothing.
63 }
64 
65 unref(): void {
66 // Do nothing
67 }
68 
69 ref(): void {
70 // Do nothing
71 }
72 
73 [Symbol.dispose](): void {
74 this.kill();
75 }
76}
77 
78export function _forkChild(_fd: number, _serializationMode: number): void {
79 throw new ERR_METHOD_NOT_IMPLEMENTED('child_process._forkChild');
80}
81 
82export function exec(
83 command: string,
84 options: ExecOptions | undefined | null,
85 callback?: (
86 error: ExecException | null,
87 stdout: string | Buffer,
88 stderr: string | Buffer
89 ) => void
90): ChildProcess {
91 validateString(command, 'command');
92 if (options != null) {
93 validateObject(options, 'options');
94 }
95 if (callback != null) {
96 validateFunction(callback, 'callback');
97 }
98 throw new ERR_METHOD_NOT_IMPLEMENTED('child_process.exec');
99}
100 
101export function execFile(
102 _file: string,
103 _args: string[],
104 _options?: ExecFileOptions | null,
105 _callback?: (
106 error?: Error,
107 stdout?: string | Buffer,
108 stderr?: string | Buffer
109 ) => unknown
110): void {
111 throw new ERR_METHOD_NOT_IMPLEMENTED('child_process.execFile');
112}
113 
114export function execFileSync(
115 _file: string,
116 _args: string[] | ExecFileOptions | null,
117 _options?: ExecFileOptions | null
118): void {
119 throw new ERR_METHOD_NOT_IMPLEMENTED('child_process.execFileSync');
120}
121 
122export function execSync(
123 _command: string,
124 _options?: ExecSyncOptions | null
125): Buffer | string {
126 throw new ERR_METHOD_NOT_IMPLEMENTED('child_process.execSync');
127}
128 
129export function fork(
130 _modulePath: string | URL,
131 args: readonly string[] | ForkOptions | null,
132 options?: ForkOptions | null
133): ChildProcess {
134 if (args == null) {
135 args = [];
136 } else if (typeof args === 'object' && !Array.isArray(args)) {
137 // @ts-expect-error TS2322 This is intentional.
138 options = args;
139 args = [];
140 } else {
141 validateArray(args, 'args');
142 }
143 
144 if (options != null) {
145 validateObject(options, 'options');
146 }
147 
148 throw new ERR_METHOD_NOT_IMPLEMENTED('child_process.fork');
149}
150 
151export function spawn(): void {
152 throw new ERR_METHOD_NOT_IMPLEMENTED('child_process.spawn');
153}
154 
155export function spawnSync(
156 command: string,
157 _args?: readonly string[] | SpawnSyncOptions,
158 _options?: SpawnSyncOptions
159): SpawnSyncReturns<string | Buffer> {
160 validateString(command, 'command');
161 throw new ERR_METHOD_NOT_IMPLEMENTED('child_process.spawnSync');
162}
163 
164export default {
165 ChildProcess,
166 _forkChild,
167 exec,
168 execFile,
169 execFileSync,
170 execSync,
171 fork,
172 spawn,
173 spawnSync,
174};