Skip to content

Commit f580718

Browse files
committed
Prevent null propagation when a stream is missing
1 parent c4cd425 commit f580718

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/Node/ChildProcess.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
// module Node.ChildProcess
44
/* eslint-env node*/
55

6+
exports.unsafeFromNullable = function unsafeFromNullable(msg){
7+
return function(x) {
8+
if (x === null) {
9+
throw new Error(msg);
10+
} else {
11+
return x;
12+
};
13+
};
14+
};
615
exports.spawnImpl = function spawnImpl(command) {
716
return function(args) {
817
return function(opts) {

src/Node/ChildProcess.purs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,38 @@ runChildProcess (ChildProcess r) = r
5757
-- | Note: some of these types are lies, and so it is unsafe to access some of
5858
-- | these record fields directly.
5959
type ChildProcessRec =
60-
{ stderr :: forall eff. Readable () (cp :: CHILD_PROCESS | eff) Buffer
61-
, stdin :: forall eff. Writable () (cp :: CHILD_PROCESS | eff) Buffer
62-
, stdout :: forall eff. Readable () (cp :: CHILD_PROCESS | eff) Buffer
60+
{ stdin :: forall eff. Nullable (Writable () (cp :: CHILD_PROCESS | eff) Buffer)
61+
, stdout :: forall eff. Nullable (Readable () (cp :: CHILD_PROCESS | eff) Buffer)
62+
, stderr :: forall eff. Nullable (Readable () (cp :: CHILD_PROCESS | eff) Buffer)
6363
, pid :: Int
6464
, connected :: Boolean
6565
, kill :: Signal -> Boolean
6666
, send :: forall r. Fn2 { | r} Handle Boolean
6767
, disconnect :: forall eff. Eff eff Unit
6868
}
6969

70-
-- | The standard error stream of a child process. Note that this is only
71-
-- | available if the process was spawned with the stderr option set to "pipe".
72-
stderr :: forall eff. ChildProcess -> Readable () (cp :: CHILD_PROCESS | eff) Buffer
73-
stderr = _.stderr <<< runChildProcess
70+
-- | The standard input stream of a child process. Note that this is only
71+
-- | available if the process was spawned with the stdin option set to "pipe".
72+
stdin :: forall eff. ChildProcess -> Writable () (cp :: CHILD_PROCESS | eff) Buffer
73+
stdin = unsafeFromNullable (missingStream "stdin") <<< _.stdin <<< runChildProcess
7474

7575
-- | The standard output stream of a child process. Note that this is only
7676
-- | available if the process was spawned with the stdout option set to "pipe".
7777
stdout :: forall eff. ChildProcess -> Readable () (cp :: CHILD_PROCESS | eff) Buffer
78-
stdout = _.stdout <<< runChildProcess
78+
stdout = unsafeFromNullable (missingStream "stdout") <<< _.stdout <<< runChildProcess
7979

80-
-- | The standard input stream of a child process. Note that this is only
81-
-- | available if the process was spawned with the stdin option set to "pipe".
82-
stdin :: forall eff. ChildProcess -> Writable () (cp :: CHILD_PROCESS | eff) Buffer
83-
stdin = _.stdin <<< runChildProcess
80+
-- | The standard error stream of a child process. Note that this is only
81+
-- | available if the process was spawned with the stderr option set to "pipe".
82+
stderr :: forall eff. ChildProcess -> Readable () (cp :: CHILD_PROCESS| eff) Buffer
83+
stderr = unsafeFromNullable (missingStream "stderr") <<< _.stderr <<< runChildProcess
84+
85+
missingStream :: String -> String
86+
missingStream str =
87+
"Node.ChildProcess: stream not available: " <> str <> "\nThis is probably "
88+
<> "because you passed something other than Pipe to the stdio option when "
89+
<> "you spawned it."
90+
91+
foreign import unsafeFromNullable :: forall a. String -> Nullable a -> a
8492

8593
-- | The process ID of a child process. Note that if the process has already
8694
-- | exited, another process may have taken the same ID, so be careful!

0 commit comments

Comments
 (0)