/
ChildProcess.hx
418 lines (348 loc) · 14.4 KB
/
ChildProcess.hx
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
/*
* Copyright (C)2014-2020 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package js.node;
import haxe.DynamicAccess;
import haxe.extern.EitherType;
import js.node.child_process.ChildProcess as ChildProcessObject;
#if haxe4
import js.lib.Error;
#else
import js.Error;
#end
/**
Common options for all `ChildProcess` methods.
**/
private typedef ChildProcessCommonOptions = {
/**
Current working directory of the child process.
**/
@:optional var cwd:String;
/**
Environment key-value pairs
**/
@:optional var env:DynamicAccess<String>;
/**
Sets the user identity of the process. See setuid(2).
**/
@:optional var uid:Int;
/**
Sets the group identity of the process. See setgid(2).
**/
@:optional var gid:Int;
/**
Shell to execute the command with.
Default: '/bin/sh' on UNIX, 'cmd.exe' on Windows.
The shell should understand the -c switch on UNIX or /s /c on Windows.
On Windows, command line parsing should be compatible with cmd.exe.
**/
@:optional var shell:EitherType<Bool, String>;
}
/**
Common options for `spawn` and `spawnSync` methods.
**/
private typedef ChildProcessSpawnOptionsBase = {
> ChildProcessCommonOptions,
/**
Child's stdio configuration.
**/
@:optional var stdio:ChildProcessSpawnOptionsStdio;
}
/**
Options for the `spawn` method.
**/
typedef ChildProcessSpawnOptions = {
> ChildProcessSpawnOptionsBase,
/**
The child will be a process group leader.
**/
@:optional var detached:Bool;
}
/**
Options for the `spawnSync` method.
**/
typedef ChildProcessSpawnSyncOptions = {
> ChildProcessSpawnOptionsBase,
> ChildProcessExecOptionsBase,
@:optional var input:EitherType<String, Buffer>;
}
/**
The `stdio` option is an array where each index corresponds to a fd in the child.
The value is one of the following:
* 'pipe' - Create a pipe between the child process and the parent process.
The parent end of the pipe is exposed to the parent as a property on the child_process object as ChildProcess.stdio[fd].
Pipes created for fds 0 - 2 are also available as ChildProcess.stdin, ChildProcess.stdout and ChildProcess.stderr, respectively.
* 'ipc' - Create an IPC channel for passing messages/file descriptors between parent and child.
A ChildProcess may have at most one IPC stdio file descriptor. Setting this option enables the ChildProcess.send() method.
If the child writes JSON messages to this file descriptor, then this will trigger ChildProcess.on('message').
If the child is a Node.js program, then the presence of an IPC channel will enable process.send() and process.on('message').
* 'ignore' - Do not set this file descriptor in the child. Note that Node will always open fd 0 - 2 for the processes it spawns.
When any of these is ignored node will open /dev/null and attach it to the child's fd.
* Stream object - Share a readable or writable stream that refers to a tty, file, socket, or a pipe with the child process.
The stream's underlying file descriptor is duplicated in the child process to the fd that corresponds to the index
in the stdio array. Note that the stream must have an underlying descriptor (file streams do not until the 'open'
event has occurred).
* Positive integer - The integer value is interpreted as a file descriptor that is is currently open in the parent process.
It is shared with the child process, similar to how Stream objects can be shared.
* null - Use default value. For stdio fds 0, 1 and 2 (in other words, stdin, stdout, and stderr) a pipe is created.
For fd 3 and up, the default is 'ignore'.
As a shorthand, the stdio argument may also be one of the following strings, rather than an array:
ignore - ['ignore', 'ignore', 'ignore']
pipe - ['pipe', 'pipe', 'pipe']
inherit - [process.stdin, process.stdout, process.stderr] or [0,1,2]
**/
typedef ChildProcessSpawnOptionsStdio = EitherType<ChildProcessSpawnOptionsStdioSimple, ChildProcessSpawnOptionsStdioFull>;
/**
A shorthand for the `stdio` argument in `ChildProcessSpawnOptions`
**/
enum abstract ChildProcessSpawnOptionsStdioSimple(String) from String to String {
/**
Equivalent to ['ignore', 'ignore', 'ignore']
**/
var Ignore = "ignore";
/**
Equivalent to ['pipe', 'pipe', 'pipe']
**/
var Pipe = "pipe";
/**
Equivalent to [process.stdin, process.stdout, process.stderr] or [0,1,2]
**/
var Inherit = "inherit";
}
/**
Enumeration of possible `stdio` behaviours.
**/
enum abstract ChildProcessSpawnOptionsStdioBehaviour(String) from String to String {
/**
Create a pipe between the child process and the parent process.
The parent end of the pipe is exposed to the parent as a property on the child_process object as ChildProcess.stdio[fd].
Pipes created for fds 0 - 2 are also available as ChildProcess.stdin, ChildProcess.stdout and ChildProcess.stderr, respectively.
**/
var Pipe = "pipe";
/**
Create an IPC channel for passing messages/file descriptors between parent and child.
A ChildProcess may have at most one IPC stdio file descriptor.
Setting this option enables the ChildProcess.send() method.
If the child writes JSON messages to this file descriptor, then this will trigger
ChildProcess.on('message').
If the child is a Node.js program, then the presence of an IPC channel will
enable process.send() and process.on('message').
**/
var Ipc = "ipc";
/**
Do not set this file descriptor in the child.
Note that Node will always open fd 0 - 2 for the processes it spawns.
When any of these is ignored node will open /dev/null and attach it to the child's fd.
**/
var Ignore = "ignore";
}
// see https://github.com/HaxeFoundation/haxe/issues/3499
// typedef ChildProcessSpawnOptionsStdioFull = Array<EitherType<ChildProcessSpawnOptionsStdioBehaviour,EitherType<IStream,Int>>>;
typedef ChildProcessSpawnOptionsStdioFull = Array<Dynamic>;
/**
Common options for `exec` and `execFile` methods.
**/
private typedef ChildProcessExecOptionsBase = {
> ChildProcessCommonOptions,
/**
Default: 'utf8'
**/
@:optional var encoding:String;
/**
If greater than 0, then it will kill the child process if it runs longer than timeout milliseconds.
**/
@:optional var timeout:Int;
/**
The child process is killed with `killSignal` (default: 'SIGTERM').
**/
@:optional var killSignal:String;
/**
The largest amount of data allowed on stdout or stderr.
If this value is exceeded then the child process is killed.
Default: 200*1024
**/
@:optional var maxBuffer:Int;
}
/**
Options for the `exec` method.
**/
typedef ChildProcessExecOptions = {
> ChildProcessExecOptionsBase,
}
/**
Options for the `execFile` method.
**/
typedef ChildProcessExecFileOptions = {
> ChildProcessExecOptionsBase,
}
/**
Options for the `fork` method.
**/
typedef ChildProcessForkOptions = {
> ChildProcessCommonOptions,
/**
Executable used to create the child process
**/
@:optional var execPath:String;
/**
List of string arguments passed to the executable (Default: process.execArgv)
**/
@:optional var execArgv:Array<String>;
/**
If `true`, stdin, stdout, and stderr of the child will be piped to the parent,
otherwise they will be inherited from the parent, see the "pipe" and "inherit"
options for `ChildProcessSpawnOptions.stdio` for more details (default is `false`)
**/
@:optional var silent:Bool;
}
/**
An error passed to the `ChildProcess.exec` callback.
**/
@:native("Error")
extern class ChildProcessExecError extends Error {
/**
the exit code of the child proces.
**/
var code(default, null):Int;
/**
the signal that terminated the process.
**/
var signal(default, null):String;
}
/**
A callback type for `ChildProcess.exec`.
It receives three arguments: `error`, `stdout`, `stderr`.
On success, error will be `null`. On error, `error` will be an instance of `Error`
and `error.code` will be the exit code of the child process, and `error.signal` will be set
to the signal that terminated the process (see `ChildProcessExecError`).
**/
typedef ChildProcessExecCallback = #if (haxe_ver >= 4) (error:Null<ChildProcessExecError>, stdout:EitherType<Buffer, String>,
stderr:EitherType<Buffer, String>) -> Void; #else Null<ChildProcessExecError>->EitherType<Buffer, String>->EitherType<Buffer, String>->Void; #end
/**
Object returned from the `spawnSync` method.
**/
typedef ChildProcessSpawnSyncResult = {
/**
Pid of the child process
**/
var pid:Int;
/**
Array of results from stdio output
**/
var output:Array<EitherType<Buffer, String>>;
/**
The contents of output[1]
**/
var stdout:EitherType<Buffer, String>;
/**
The contents of output[2]
**/
var stderr:EitherType<Buffer, String>;
/**
The exit code of the child process
**/
var status:Int;
/**
The signal used to kill the child process
**/
var signal:String;
/**
The error object if the child process failed or timed out
**/
var error:Error;
}
@:jsRequire("child_process")
extern class ChildProcess {
/**
Launches a new process with the given `command`, with command line arguments in `args`.
If omitted, `args` defaults to an empty `Array`.
The third argument is used to specify additional options, which defaults to:
{ cwd: null,
env: process.env
}
Note that if spawn receives an empty options object, it will result in spawning the process with an empty
environment rather than using `process.env`. This due to backwards compatibility issues with a deprecated API.
**/
@:overload(function(command:String, ?options:ChildProcessSpawnOptions):ChildProcessObject {})
@:overload(function(command:String, args:Array<String>, ?options:ChildProcessSpawnOptions):ChildProcessObject {})
static function spawn(command:String, ?args:Array<String>):ChildProcessObject;
/**
Runs a command in a shell and buffers the output.
`command` is the command to run, with space-separated arguments.
The default `options` are:
{ encoding: 'utf8',
timeout: 0,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: null,
env: null }
**/
@:overload(function(command:String, options:ChildProcessExecOptions, callback:ChildProcessExecCallback):ChildProcessObject {})
static function exec(command:String, callback:ChildProcessExecCallback):ChildProcessObject;
/**
This is similar to `exec` except it does not execute a subshell but rather the specified file directly.
This makes it slightly leaner than `exec`
**/
@:overload(function(file:String, args:Array<String>, options:ChildProcessExecFileOptions, ?callback:ChildProcessExecCallback):ChildProcessObject {})
@:overload(function(file:String, options:ChildProcessExecFileOptions, ?callback:ChildProcessExecCallback):ChildProcessObject {})
@:overload(function(file:String, args:Array<String>, ?callback:ChildProcessExecCallback):ChildProcessObject {})
static function execFile(file:String, ?callback:ChildProcessExecCallback):ChildProcessObject;
/**
This is a special case of the `spawn` functionality for spawning Node processes.
In addition to having all the methods in a normal `ChildProcess` instance,
the returned object has a communication channel built-in.
See `send` for details.
**/
@:overload(function(modulePath:String, args:Array<String>, options:ChildProcessForkOptions):ChildProcessObject {})
@:overload(function(modulePath:String, options:ChildProcessForkOptions):ChildProcessObject {})
static function fork(modulePath:String, ?args:Array<String>):ChildProcessObject;
/**
Synchronous version of `spawn`.
`spawnSync` will not return until the child process has fully closed.
When a timeout has been encountered and `killSignal` is sent, the method won't return until the process
has completely exited. That is to say, if the process handles the SIGTERM signal and doesn't exit,
your process will wait until the child process has exited.
**/
@:overload(function(command:String, args:Array<String>, ?options:ChildProcessSpawnSyncOptions):ChildProcessSpawnSyncResult {})
static function spawnSync(command:String, ?options:ChildProcessSpawnSyncOptions):ChildProcessSpawnSyncResult;
/**
Synchronous version of `execFile`.
`execFileSync` will not return until the child process has fully closed.
When a timeout has been encountered and `killSignal` is sent, the method won't return until the process
has completely exited. That is to say, if the process handles the SIGTERM signal and doesn't exit,
your process will wait until the child process has exited.
If the process times out, or has a non-zero exit code, this method will throw.
The Error object will contain the entire result from `spawnSync`
**/
@:overload(function(command:String, ?options:ChildProcessSpawnSyncOptions):EitherType<String, Buffer> {})
@:overload(function(command:String, args:Array<String>, ?options:ChildProcessSpawnSyncOptions):EitherType<String, Buffer> {})
static function execFileSync(command:String, ?args:Array<String>):EitherType<String, Buffer>;
/**
Synchronous version of `exec`.
`execSync` will not return until the child process has fully closed.
When a timeout has been encountered and `killSignal` is sent, the method won't return until the process
has completely exited. That is to say, if the process handles the SIGTERM signal and doesn't exit,
your process will wait until the child process has exited.
If the process times out, or has a non-zero exit code, this method will throw.
The Error object will contain the entire result from `spawnSync`
**/
static function execSync(command:String, ?options:ChildProcessSpawnSyncOptions):EitherType<String, Buffer>;
}