9
9
-- | ```
10
10
-- |
11
11
-- | The [Node.js documentation](https://nodejs.org/api/child_process.html)
12
- -- | will probably also be useful to read if you want to use this module.
12
+ -- | forms the basis for this module and has in-depth documentation about
13
+ -- | runtime behaviour.
13
14
module Node.ChildProcess
14
15
( Handle
15
16
, ChildProcess
16
- , stderr
17
- , stdout
18
17
, stdin
18
+ , stdout
19
+ , stderr
19
20
, pid
20
21
, connected
21
22
, kill
@@ -70,6 +71,8 @@ import Unsafe.Coerce (unsafeCoerce)
70
71
-- | A handle for inter-process communication (IPC).
71
72
foreign import data Handle :: Type
72
73
74
+ -- | Opaque type returned by `spawn`, `fork` and `exec`.
75
+ -- | Needed as input for most methods in this module.
73
76
newtype ChildProcess = ChildProcess ChildProcessRec
74
77
75
78
runChildProcess :: ChildProcess -> ChildProcessRec
@@ -116,12 +119,24 @@ foreign import unsafeFromNullable :: forall a. String -> Nullable a -> a
116
119
pid :: ChildProcess -> Pid
117
120
pid = _.pid <<< runChildProcess
118
121
122
+ -- | Indicates whether it is still possible to send and receive
123
+ -- | messages from the child process.
119
124
connected :: ChildProcess -> Effect Boolean
120
125
connected (ChildProcess cp) = mkEffect \_ -> cp.connected
121
126
122
- send :: forall props . { | props } -> Handle -> ChildProcess -> Effect Boolean
127
+ -- | Send messages to the (`nodejs`) child process.
128
+ -- |
129
+ -- | See the [node documentation](https://nodejs.org/api/child_process.html#child_process_subprocess_send_message_sendhandle_options_callback)
130
+ -- | for in-depth documentation.
131
+ send
132
+ :: forall props
133
+ . { | props }
134
+ -> Handle
135
+ -> ChildProcess
136
+ -> Effect Boolean
123
137
send msg handle (ChildProcess cp) = mkEffect \_ -> runFn2 cp.send msg handle
124
138
139
+ -- | Closes the IPC channel between parent and child.
125
140
disconnect :: ChildProcess -> Effect Unit
126
141
disconnect = _.disconnect <<< runChildProcess
127
142
@@ -130,8 +145,8 @@ disconnect = _.disconnect <<< runChildProcess
130
145
-- | sending a signal to a child process won't necessarily kill it.
131
146
-- |
132
147
-- | The resulting effects of this function depend on the process
133
- -- | and the signal and can vary from system to system.
134
- -- | The child process might emit an "error" event if the signal
148
+ -- | and the signal. They can vary from system to system.
149
+ -- | The child process might emit an ` "error"` event if the signal
135
150
-- | could not be delivered.
136
151
kill :: Signal -> ChildProcess -> Effect Unit
137
152
kill sig (ChildProcess cp) = mkEffect \_ -> cp.kill (Signal .toString sig)
@@ -158,7 +173,11 @@ mkExit code signal =
158
173
fromCode = toMaybe >>> map Normally
159
174
fromSignal = toMaybe >=> Signal .fromString >>> map BySignal
160
175
161
- onExit :: ChildProcess -> (Exit -> Effect Unit ) -> Effect Unit
176
+ -- | Handle the `"exit"` signal.
177
+ onExit
178
+ :: ChildProcess
179
+ -> (Exit -> Effect Unit )
180
+ -> Effect Unit
162
181
onExit = mkOnExit mkExit
163
182
164
183
foreign import mkOnExit
@@ -167,7 +186,11 @@ foreign import mkOnExit
167
186
-> (Exit -> Effect Unit )
168
187
-> Effect Unit
169
188
170
- onClose :: ChildProcess -> (Exit -> Effect Unit ) -> Effect Unit
189
+ -- | Handle the `"close"` signal.
190
+ onClose
191
+ :: ChildProcess
192
+ -> (Exit -> Effect Unit )
193
+ -> Effect Unit
171
194
onClose = mkOnClose mkExit
172
195
173
196
foreign import mkOnClose
@@ -176,7 +199,11 @@ foreign import mkOnClose
176
199
-> (Exit -> Effect Unit )
177
200
-> Effect Unit
178
201
179
- onMessage :: ChildProcess -> (Foreign -> Maybe Handle -> Effect Unit ) -> Effect Unit
202
+ -- | Handle the `"message"` signal.
203
+ onMessage
204
+ :: ChildProcess
205
+ -> (Foreign -> Maybe Handle -> Effect Unit )
206
+ -> Effect Unit
180
207
onMessage = mkOnMessage Nothing Just
181
208
182
209
foreign import mkOnMessage
@@ -187,14 +214,27 @@ foreign import mkOnMessage
187
214
-> (Foreign -> Maybe Handle -> Effect Unit )
188
215
-> Effect Unit
189
216
190
- foreign import onDisconnect :: ChildProcess -> Effect Unit -> Effect Unit
191
- foreign import onError :: ChildProcess -> (Error -> Effect Unit ) -> Effect Unit
217
+ -- | Handle the `"disconnect"` signal.
218
+ foreign import onDisconnect
219
+ :: ChildProcess
220
+ -> Effect Unit
221
+ -> Effect Unit
222
+
223
+ -- | Handle the `"error"` signal.
224
+ foreign import onError
225
+ :: ChildProcess
226
+ -> (Error -> Effect Unit )
227
+ -> Effect Unit
192
228
193
229
-- | Spawn a child process. Note that, in the event that a child process could
194
230
-- | not be spawned (for example, if the executable was not found) this will
195
231
-- | not throw an error. Instead, the `ChildProcess` will be created anyway,
196
232
-- | but it will immediately emit an 'error' event.
197
- spawn :: String -> Array String -> SpawnOptions -> Effect ChildProcess
233
+ spawn
234
+ :: String
235
+ -> Array String
236
+ -> SpawnOptions
237
+ -> Effect ChildProcess
198
238
spawn cmd args = spawnImpl cmd args <<< convertOpts
199
239
where
200
240
convertOpts opts =
@@ -206,11 +246,18 @@ spawn cmd args = spawnImpl cmd args <<< convertOpts
206
246
, gid: fromMaybe undefined opts.gid
207
247
}
208
248
209
- foreign import spawnImpl :: forall opts . String -> Array String -> { | opts } -> Effect ChildProcess
249
+ foreign import spawnImpl
250
+ :: forall opts
251
+ . String
252
+ -> Array String
253
+ -> { | opts }
254
+ -> Effect ChildProcess
210
255
211
256
-- There's gotta be a better way.
212
257
foreign import undefined :: forall a . a
213
258
259
+ -- | Configuration of `spawn`. Fields set to `Nothing` will use
260
+ -- | the node defaults.
214
261
type SpawnOptions =
215
262
{ cwd :: Maybe String
216
263
, stdio :: Array (Maybe StdIOBehaviour )
@@ -220,6 +267,8 @@ type SpawnOptions =
220
267
, gid :: Maybe Gid
221
268
}
222
269
270
+ -- | A default set of `SpawnOptions`. Everything is set to `Nothing`,
271
+ -- | `detached` is `false` and `stdio` is `ChildProcess.pipe`.
223
272
defaultSpawnOptions :: SpawnOptions
224
273
defaultSpawnOptions =
225
274
{ cwd: Nothing
@@ -292,6 +341,8 @@ convertExecOptions opts = unsafeCoerce
292
341
, gid: fromMaybe undefined opts.gid
293
342
}
294
343
344
+ -- | Configuration of `exec`. Fields set to `Nothing`
345
+ -- | will use the node defaults.
295
346
type ExecOptions =
296
347
{ cwd :: Maybe String
297
348
, env :: Maybe (Object String )
@@ -302,6 +353,7 @@ type ExecOptions =
302
353
, gid :: Maybe Gid
303
354
}
304
355
356
+ -- | A default set of `ExecOptions`. Everything is set to `Nothing`.
305
357
defaultExecOptions :: ExecOptions
306
358
defaultExecOptions =
307
359
{ cwd: Nothing
@@ -313,6 +365,7 @@ defaultExecOptions =
313
365
, gid: Nothing
314
366
}
315
367
368
+ -- | The combined output of a process calld with `exec`.
316
369
type ExecResult =
317
370
{ stderr :: Buffer
318
371
, stdout :: Buffer
@@ -391,11 +444,13 @@ defaultExecSyncOptions =
391
444
, gid: Nothing
392
445
}
393
446
394
-
395
447
-- | A special case of `spawn` for creating Node.js child processes. The first
396
448
-- | argument is the module to be run, and the second is the argv (command line
397
449
-- | arguments).
398
- foreign import fork :: String -> Array String -> Effect ChildProcess
450
+ foreign import fork
451
+ :: String
452
+ -> Array String
453
+ -> Effect ChildProcess
399
454
400
455
-- | An error which occurred inside a child process.
401
456
type Error =
@@ -431,7 +486,8 @@ data StdIOBehaviour
431
486
pipe :: Array (Maybe StdIOBehaviour )
432
487
pipe = map Just [Pipe , Pipe , Pipe ]
433
488
434
- -- | Share stdin with stdin, stdout with stdout, and stderr with stderr.
489
+ -- | Share `stdin` with `stdin`, `stdout` with `stdout`,
490
+ -- | and `stderr` with `stderr`.
435
491
inherit :: Array (Maybe StdIOBehaviour )
436
492
inherit = map Just
437
493
[ ShareStream process.stdin
@@ -445,6 +501,9 @@ foreign import process :: forall props. { | props }
445
501
ignore :: Array (Maybe StdIOBehaviour )
446
502
ignore = map Just [Ignore , Ignore , Ignore ]
447
503
504
+
505
+ -- Helpers
506
+
448
507
foreign import data ActualStdIOBehaviour :: Type
449
508
450
509
toActualStdIOBehaviour :: StdIOBehaviour -> ActualStdIOBehaviour
0 commit comments