Skip to content

Commit 99fd379

Browse files
Add note about stdin, pipe, and inherit (#62)
* Add note about stdin, pipe, and inherit See purescript/spago#1048 * Drop unused functions * Add variant of waitSpawned
1 parent 12932f4 commit 99fd379

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/Node/ChildProcess/Aff.purs

+16-6
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import Data.Maybe (fromJust)
88
import Data.Posix (Pid)
99
import Effect.Aff (Aff, effectCanceler, makeAff)
1010
import Effect.Ref as Ref
11-
import Node.ChildProcess (ChildProcess, pid)
12-
import Node.ChildProcess as CP
11+
import Node.ChildProcess.Types (UnsafeChildProcess)
12+
import Node.UnsafeChildProcess.Safe as CPSafe
13+
import Node.ChildProcess (ChildProcess, toUnsafeChildProcess)
1314
import Node.Errors.SystemError (SystemError)
1415
import Node.EventEmitter (once)
1516
import Partial.Unsafe (unsafePartial)
@@ -19,21 +20,30 @@ import Partial.Unsafe (unsafePartial)
1920
-- | and the `pid` of the process can be obtained.
2021
-- | If an `error` event fires, child process was not started successfully.
2122
waitSpawned :: ChildProcess -> Aff (Either SystemError Pid)
22-
waitSpawned cp = parOneOf [ pidOnSpawn, errored ]
23+
waitSpawned = toUnsafeChildProcess >>> waitSpawned'
24+
25+
-- | Same as `waitSpawned` but works on `UnsafeChildProcess`
26+
-- |
27+
-- | Blocks until either a `spawn` or `error` event is fired.
28+
-- | If a `spawn` event fired, child process was successfully started
29+
-- | and the `pid` of the process can be obtained.
30+
-- | If an `error` event fires, child process was not started successfully.
31+
waitSpawned' :: UnsafeChildProcess -> Aff (Either SystemError Pid)
32+
waitSpawned' cp = parOneOf [ pidOnSpawn, errored ]
2333
where
2434
pidOnSpawn = makeAff \done -> do
2535
ref <- Ref.new mempty
26-
removeListener <- cp # once CP.spawnH do
36+
removeListener <- cp # once CPSafe.spawnH do
2737
join $ Ref.read ref
28-
pid' <- pid cp
38+
pid' <- CPSafe.pid cp
2939
done $ Right $ Right $ unsafePartial $ fromJust pid'
3040
Ref.write removeListener ref
3141
pure $ effectCanceler do
3242
removeListener
3343

3444
errored = makeAff \done -> do
3545
ref <- Ref.new mempty
36-
removeListener <- cp # once CP.errorH \sysErr -> do
46+
removeListener <- cp # once CPSafe.errorH \sysErr -> do
3747
join $ Ref.read ref
3848
done $ Right $ Left sysErr
3949
Ref.write removeListener ref

src/Node/ChildProcess/Types.purs

+20
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ foreign import data Handle :: Type
4444
-- | See https://nodejs.org/docs/latest-v18.x/api/child_process.html#optionsstdio
4545
foreign import data StdIO :: Type
4646

47+
-- | When used for X, then Y will exist on the child process where X and Y are
48+
-- | - `stdio[0]` - `stdin`
49+
-- | - `stdio[1]` - `stdout`
50+
-- | - `stdio[2]` - `stderr`
51+
-- |
52+
-- | Note: when used with `stdin`, piping the parent stdin to this stream
53+
-- | will not cause the child process to terminate when that parent stdin stream
54+
-- | ends via `Ctrl+D` user input. Rather, the child process will hang
55+
-- | until the parent process calls `Stream.end` on the child process'
56+
-- | `stdin` stream. Since it's impossible to know when the user
57+
-- | inputs `Ctrl+D`, `inherit` should be used instead.
4758
pipe :: StdIO
4859
pipe = unsafeCoerce "pipe"
4960

@@ -56,6 +67,15 @@ overlapped = unsafeCoerce "overlapped"
5667
ipc :: StdIO
5768
ipc = unsafeCoerce "ipc"
5869

70+
-- | Uses the parent's corresponding stream.
71+
-- |
72+
-- | Note: this value must be used for `stdin` if one
73+
-- | wants to pipe the parent's `stdin` into the child process' `stdin`
74+
-- | AND cause the child process to terminate when the user closes
75+
-- | the parent's `stdin` via `Ctrl+D`. Using `pipe` instead
76+
-- | will cause the child process to hang, even when `Ctrl+D` is pressed,
77+
-- | until the parent process calls `Stream.end`, which cannot be reliably
78+
-- | called the moment AFTER `Ctrl+D` is pressed.
5979
inherit :: StdIO
6080
inherit = unsafeCoerce "inherit"
6181

0 commit comments

Comments
 (0)