Skip to content

Commit 5e867de

Browse files
Fix KillSignal arg; add fromKillSignal (#51)
* Add fromKillSignal * Fix kill signal arg
1 parent c3f8b99 commit 5e867de

File tree

6 files changed

+58
-12
lines changed

6 files changed

+58
-12
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ Bugfixes:
1212

1313
Other improvements:
1414

15-
## [v10.1.0](https://github.com/purescript-node/purescript-node-child-process/releases/tag/v10.1.0) - 2023-07-25
15+
## [v11.0.0](https://github.com/purescript-node/purescript-node-child-process/releases/tag/v11.0.0) - 2023-07-25
16+
17+
Breaking changes:
18+
- Update the signal arg from `String` to `KillSignal` (#51 by @JordanMartinez)
19+
20+
- `Exit`'s `BySignal` constructor's arg
21+
- `exitH`/`closeH`'s signal arg
22+
- `spawnSync`'s `SpawnResult`'s `signal` field
23+
24+
New features:
25+
- Added `fromKillSignal` (#51 by @JordanMartinez)
1626

1727
Other improvements:
1828
- Fix regression: add `ref`/`unref` APIs that were dropped in `v10.0.0` (#50 by @JordanMartinez)

src/Node/ChildProcess/Types.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const fromKillSignalImpl = (left, right, sig) => {
2+
const ty = typeof sig;
3+
if (ty === "number") return right(sig | 0);
4+
if (ty === "string") return left(sig);
5+
throw new Error("Impossible. Got kill signal that was neither int nor string: " + sig);
6+
};

src/Node/ChildProcess/Types.purs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
module Node.ChildProcess.Types where
1+
module Node.ChildProcess.Types
2+
( UnsafeChildProcess
3+
, Handle
4+
, StdIO
5+
, pipe
6+
, ignore
7+
, overlapped
8+
, ipc
9+
, inherit
10+
, shareStream
11+
, fileDescriptor
12+
, fileDescriptor'
13+
, defaultStdIO
14+
, KillSignal
15+
, intSignal
16+
, stringSignal
17+
, fromKillSignal
18+
, Shell
19+
, enableShell
20+
, customShell
21+
, StringOrBuffer
22+
, Exit(..)
23+
) where
224

325
import Prelude
426

27+
import Data.Either (Either(..), either)
28+
import Data.Function.Uncurried (Fn3, runFn3)
529
import Data.Nullable (Nullable, null)
630
import Node.FS (FileDescriptor)
731
import Node.Stream (Stream)
@@ -54,6 +78,11 @@ intSignal = unsafeCoerce
5478
stringSignal :: String -> KillSignal
5579
stringSignal = unsafeCoerce
5680

81+
fromKillSignal :: KillSignal -> Either Int String
82+
fromKillSignal sig = runFn3 fromKillSignalImpl Left Right sig
83+
84+
foreign import fromKillSignalImpl :: Fn3 (forall l r. l -> Either l r) (forall l r. r -> Either l r) (KillSignal) (Either Int String)
85+
5786
foreign import data Shell :: Type
5887

5988
enableShell :: Shell
@@ -70,8 +99,8 @@ foreign import data StringOrBuffer :: Type
7099
-- | due to a signal.
71100
data Exit
72101
= Normally Int
73-
| BySignal String
102+
| BySignal KillSignal
74103

75104
instance showExit :: Show Exit where
76105
show (Normally x) = "Normally " <> show x
77-
show (BySignal sig) = "BySignal " <> show sig
106+
show (BySignal sig) = "BySignal " <> (either show show $ fromKillSignal sig)

src/Node/UnsafeChildProcess/Safe.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import Data.Posix.Signal as Signal
3636
import Effect (Effect)
3737
import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2)
3838
import Foreign (Foreign)
39-
import Node.ChildProcess.Types (Exit(..), Handle, StdIO, UnsafeChildProcess, ipc, pipe)
39+
import Node.ChildProcess.Types (Exit(..), Handle, KillSignal, StdIO, UnsafeChildProcess, ipc, pipe)
4040
import Node.Errors.SystemError (SystemError)
4141
import Node.EventEmitter (EventEmitter, EventHandle(..))
4242
import Node.EventEmitter.UtilTypes (EventHandle0, EventHandle1)
@@ -46,25 +46,25 @@ import Unsafe.Coerce (unsafeCoerce)
4646
toEventEmitter :: UnsafeChildProcess -> EventEmitter
4747
toEventEmitter = unsafeCoerce
4848

49-
closeH :: EventHandle UnsafeChildProcess (Exit -> Effect Unit) (EffectFn2 (Nullable Int) (Nullable String) Unit)
49+
closeH :: EventHandle UnsafeChildProcess (Exit -> Effect Unit) (EffectFn2 (Nullable Int) (Nullable KillSignal) Unit)
5050
closeH = EventHandle "close" \cb -> mkEffectFn2 \code signal ->
5151
case toMaybe code, toMaybe signal of
5252
Just c, _ -> cb $ Normally c
5353
_, Just s -> cb $ BySignal s
54-
_, _ -> unsafeCrashWith $ "Impossible. 'close' event did not get an exit code or kill signal: " <> show code <> "; " <> show signal
54+
_, _ -> unsafeCrashWith $ "Impossible. 'close' event did not get an exit code or kill signal: " <> show code <> "; " <> (unsafeCoerce signal)
5555

5656
disconnectH :: EventHandle0 UnsafeChildProcess
5757
disconnectH = EventHandle "disconnect" identity
5858

5959
errorH :: EventHandle1 UnsafeChildProcess SystemError
6060
errorH = EventHandle "error" mkEffectFn1
6161

62-
exitH :: EventHandle UnsafeChildProcess (Exit -> Effect Unit) (EffectFn2 (Nullable Int) (Nullable String) Unit)
62+
exitH :: EventHandle UnsafeChildProcess (Exit -> Effect Unit) (EffectFn2 (Nullable Int) (Nullable KillSignal) Unit)
6363
exitH = EventHandle "exitH" \cb -> mkEffectFn2 \code signal ->
6464
case toMaybe code, toMaybe signal of
6565
Just c, _ -> cb $ Normally c
6666
_, Just s -> cb $ BySignal s
67-
_, _ -> unsafeCrashWith $ "Impossible. 'exit' event did not get an exit code or kill signal: " <> show code <> "; " <> show signal
67+
_, _ -> unsafeCrashWith $ "Impossible. 'exit' event did not get an exit code or kill signal: " <> show code <> "; " <> (unsafeCoerce signal)
6868

6969
messageH :: EventHandle UnsafeChildProcess (Foreign -> Maybe Handle -> Effect Unit) (EffectFn2 Foreign (Nullable Handle) Unit)
7070
messageH = EventHandle "message" \cb -> mkEffectFn2 \a b -> cb a $ toMaybe b

src/Node/UnsafeChildProcess/Unsafe.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ type JsSpawnSyncResult =
290290
, stdout :: StringOrBuffer
291291
, stderr :: StringOrBuffer
292292
, status :: Nullable Int
293-
, signal :: Nullable String
293+
, signal :: Nullable KillSignal
294294
, error :: Nullable SystemError
295295
}
296296

test/Main.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ module Test.Main where
22

33
import Prelude
44

5+
import Data.Either (hush)
56
import Data.Maybe (Maybe(..))
67
import Data.Posix.Signal (Signal(..))
78
import Data.Posix.Signal as Signal
89
import Effect (Effect)
910
import Effect.Console (log)
1011
import Node.Buffer as Buffer
1112
import Node.ChildProcess (errorH, exec', execSync', exitH, kill, spawn, stdout)
12-
import Node.ChildProcess.Types (Exit(..))
13+
import Node.ChildProcess.Types (Exit(..), fromKillSignal)
1314
import Node.Encoding (Encoding(..))
1415
import Node.Encoding as NE
1516
import Node.Errors.SystemError (code)
@@ -40,7 +41,7 @@ main = do
4041
_ <- kill ls
4142
ls # on_ exitH \exit ->
4243
case exit of
43-
BySignal s | Just SIGTERM <- Signal.fromString s ->
44+
BySignal s | Just SIGTERM <- Signal.fromString =<< (hush $ fromKillSignal s) ->
4445
log "All good!"
4546
_ -> do
4647
log ("Bad exit: expected `BySignal SIGTERM`, got: " <> show exit)

0 commit comments

Comments
 (0)