From 7f9ce6535c8c5ff50c2923665840656ac53cd6fc Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 25 Jul 2023 10:11:58 -0700 Subject: [PATCH 1/4] Add fromKillSignal --- CHANGELOG.md | 1 + src/Node/ChildProcess/Types.js | 6 ++++++ src/Node/ChildProcess/Types.purs | 31 ++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/Node/ChildProcess/Types.js diff --git a/CHANGELOG.md b/CHANGELOG.md index eece8db..6a69dd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Other improvements: Other improvements: - Fix regression: add `ref`/`unref` APIs that were dropped in `v10.0.0` (#50 by @JordanMartinez) +- Added `fromKillSignal` (#51 by @JordanMartinez) ## [v10.0.0](https://github.com/purescript-node/purescript-node-child-process/releases/tag/v10.0.0) - 2023-07-20 diff --git a/src/Node/ChildProcess/Types.js b/src/Node/ChildProcess/Types.js new file mode 100644 index 0000000..920ef5e --- /dev/null +++ b/src/Node/ChildProcess/Types.js @@ -0,0 +1,6 @@ +export const fromKillSignalImpl = (left, right, sig) => { + const ty = typeof sig; + if (ty === "number") return right(sig | 0); + if (ty === "string") return left(sig); + throw new Error("Impossible. Got kill signal that was neither int nor string: " + sig); +} diff --git a/src/Node/ChildProcess/Types.purs b/src/Node/ChildProcess/Types.purs index 505638d..d99ccc0 100644 --- a/src/Node/ChildProcess/Types.purs +++ b/src/Node/ChildProcess/Types.purs @@ -1,7 +1,31 @@ -module Node.ChildProcess.Types where +module Node.ChildProcess.Types + ( UnsafeChildProcess + , Handle + , StdIO + , pipe + , ignore + , overlapped + , ipc + , inherit + , shareStream + , fileDescriptor + , fileDescriptor' + , defaultStdIO + , KillSignal + , intSignal + , stringSignal + , fromKillSignal + , Shell + , enableShell + , customShell + , StringOrBuffer + , Exit(..) + ) where import Prelude +import Data.Either (Either(..)) +import Data.Function.Uncurried (Fn3, runFn3) import Data.Nullable (Nullable, null) import Node.FS (FileDescriptor) import Node.Stream (Stream) @@ -54,6 +78,11 @@ intSignal = unsafeCoerce stringSignal :: String -> KillSignal stringSignal = unsafeCoerce +fromKillSignal :: KillSignal -> Either Int String +fromKillSignal sig = runFn3 fromKillSignalImpl Left Right sig + +foreign import fromKillSignalImpl :: Fn3 (forall l r. l -> Either l r) (forall l r. r -> Either l r) (KillSignal) (Either Int String) + foreign import data Shell :: Type enableShell :: Shell From 0f741957b9b52561b55f860a02571685067fa94e Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 25 Jul 2023 10:18:15 -0700 Subject: [PATCH 2/4] Fix kill signal arg; add fromKillSignal --- CHANGELOG.md | 13 +++++++++++-- src/Node/ChildProcess/Types.purs | 6 +++--- src/Node/UnsafeChildProcess/Safe.purs | 10 +++++----- src/Node/UnsafeChildProcess/Unsafe.purs | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a69dd3..2874114 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,11 +12,20 @@ Bugfixes: Other improvements: -## [v10.1.0](https://github.com/purescript-node/purescript-node-child-process/releases/tag/v10.1.0) - 2023-07-25 +## [v11.0.0](https://github.com/purescript-node/purescript-node-child-process/releases/tag/v11.0.0) - 2023-07-25 + +Breaking changes: +- Update the signal arg from `String` to `KillSignal` (#51 by @JordanMartinez) + + - `Exit`'s `BySignal` constructor's arg + - `exitH`/`closeH`'s signal arg + - `spawnSync`'s `SpawnResult`'s `signal` field + +New features: +- Added `fromKillSignal` (#51 by @JordanMartinez) Other improvements: - Fix regression: add `ref`/`unref` APIs that were dropped in `v10.0.0` (#50 by @JordanMartinez) -- Added `fromKillSignal` (#51 by @JordanMartinez) ## [v10.0.0](https://github.com/purescript-node/purescript-node-child-process/releases/tag/v10.0.0) - 2023-07-20 diff --git a/src/Node/ChildProcess/Types.purs b/src/Node/ChildProcess/Types.purs index d99ccc0..90c176c 100644 --- a/src/Node/ChildProcess/Types.purs +++ b/src/Node/ChildProcess/Types.purs @@ -24,7 +24,7 @@ module Node.ChildProcess.Types import Prelude -import Data.Either (Either(..)) +import Data.Either (Either(..), either) import Data.Function.Uncurried (Fn3, runFn3) import Data.Nullable (Nullable, null) import Node.FS (FileDescriptor) @@ -99,8 +99,8 @@ foreign import data StringOrBuffer :: Type -- | due to a signal. data Exit = Normally Int - | BySignal String + | BySignal KillSignal instance showExit :: Show Exit where show (Normally x) = "Normally " <> show x - show (BySignal sig) = "BySignal " <> show sig + show (BySignal sig) = "BySignal " <> (either show show $ fromKillSignal sig) diff --git a/src/Node/UnsafeChildProcess/Safe.purs b/src/Node/UnsafeChildProcess/Safe.purs index ce61bd5..09dbf0b 100644 --- a/src/Node/UnsafeChildProcess/Safe.purs +++ b/src/Node/UnsafeChildProcess/Safe.purs @@ -36,7 +36,7 @@ import Data.Posix.Signal as Signal import Effect (Effect) import Effect.Uncurried (EffectFn1, EffectFn2, mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2) import Foreign (Foreign) -import Node.ChildProcess.Types (Exit(..), Handle, StdIO, UnsafeChildProcess, ipc, pipe) +import Node.ChildProcess.Types (Exit(..), Handle, KillSignal, StdIO, UnsafeChildProcess, ipc, pipe) import Node.Errors.SystemError (SystemError) import Node.EventEmitter (EventEmitter, EventHandle(..)) import Node.EventEmitter.UtilTypes (EventHandle0, EventHandle1) @@ -46,12 +46,12 @@ import Unsafe.Coerce (unsafeCoerce) toEventEmitter :: UnsafeChildProcess -> EventEmitter toEventEmitter = unsafeCoerce -closeH :: EventHandle UnsafeChildProcess (Exit -> Effect Unit) (EffectFn2 (Nullable Int) (Nullable String) Unit) +closeH :: EventHandle UnsafeChildProcess (Exit -> Effect Unit) (EffectFn2 (Nullable Int) (Nullable KillSignal) Unit) closeH = EventHandle "close" \cb -> mkEffectFn2 \code signal -> case toMaybe code, toMaybe signal of Just c, _ -> cb $ Normally c _, Just s -> cb $ BySignal s - _, _ -> unsafeCrashWith $ "Impossible. 'close' event did not get an exit code or kill signal: " <> show code <> "; " <> show signal + _, _ -> unsafeCrashWith $ "Impossible. 'close' event did not get an exit code or kill signal: " <> show code <> "; " <> (unsafeCoerce signal) disconnectH :: EventHandle0 UnsafeChildProcess disconnectH = EventHandle "disconnect" identity @@ -59,12 +59,12 @@ disconnectH = EventHandle "disconnect" identity errorH :: EventHandle1 UnsafeChildProcess SystemError errorH = EventHandle "error" mkEffectFn1 -exitH :: EventHandle UnsafeChildProcess (Exit -> Effect Unit) (EffectFn2 (Nullable Int) (Nullable String) Unit) +exitH :: EventHandle UnsafeChildProcess (Exit -> Effect Unit) (EffectFn2 (Nullable Int) (Nullable KillSignal) Unit) exitH = EventHandle "exitH" \cb -> mkEffectFn2 \code signal -> case toMaybe code, toMaybe signal of Just c, _ -> cb $ Normally c _, Just s -> cb $ BySignal s - _, _ -> unsafeCrashWith $ "Impossible. 'exit' event did not get an exit code or kill signal: " <> show code <> "; " <> show signal + _, _ -> unsafeCrashWith $ "Impossible. 'exit' event did not get an exit code or kill signal: " <> show code <> "; " <> (unsafeCoerce signal) messageH :: EventHandle UnsafeChildProcess (Foreign -> Maybe Handle -> Effect Unit) (EffectFn2 Foreign (Nullable Handle) Unit) messageH = EventHandle "message" \cb -> mkEffectFn2 \a b -> cb a $ toMaybe b diff --git a/src/Node/UnsafeChildProcess/Unsafe.purs b/src/Node/UnsafeChildProcess/Unsafe.purs index 2101ac8..b7cbb04 100644 --- a/src/Node/UnsafeChildProcess/Unsafe.purs +++ b/src/Node/UnsafeChildProcess/Unsafe.purs @@ -290,7 +290,7 @@ type JsSpawnSyncResult = , stdout :: StringOrBuffer , stderr :: StringOrBuffer , status :: Nullable Int - , signal :: Nullable String + , signal :: Nullable KillSignal , error :: Nullable SystemError } From dc5e07fff81be661c4554e0bf052e5f9a50caa91 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 25 Jul 2023 10:29:45 -0700 Subject: [PATCH 3/4] Update tests --- test/Main.purs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index 5b71f49..1215ea7 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -2,6 +2,7 @@ module Test.Main where import Prelude +import Data.Either (hush) import Data.Maybe (Maybe(..)) import Data.Posix.Signal (Signal(..)) import Data.Posix.Signal as Signal @@ -9,7 +10,7 @@ import Effect (Effect) import Effect.Console (log) import Node.Buffer as Buffer import Node.ChildProcess (errorH, exec', execSync', exitH, kill, spawn, stdout) -import Node.ChildProcess.Types (Exit(..)) +import Node.ChildProcess.Types (Exit(..), fromKillSignal) import Node.Encoding (Encoding(..)) import Node.Encoding as NE import Node.Errors.SystemError (code) @@ -40,7 +41,7 @@ main = do _ <- kill ls ls # on_ exitH \exit -> case exit of - BySignal s | Just SIGTERM <- Signal.fromString s -> + BySignal s | Just SIGTERM <- Signal.fromString =<< (hush $ fromKillSignal s) -> log "All good!" _ -> do log ("Bad exit: expected `BySignal SIGTERM`, got: " <> show exit) From 028fd2a22e6de0664ee84e92eb129e64d272f0f6 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 25 Jul 2023 10:31:12 -0700 Subject: [PATCH 4/4] Fix linting issue --- src/Node/ChildProcess/Types.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Node/ChildProcess/Types.js b/src/Node/ChildProcess/Types.js index 920ef5e..cad6dce 100644 --- a/src/Node/ChildProcess/Types.js +++ b/src/Node/ChildProcess/Types.js @@ -3,4 +3,4 @@ export const fromKillSignalImpl = (left, right, sig) => { if (ty === "number") return right(sig | 0); if (ty === "string") return left(sig); throw new Error("Impossible. Got kill signal that was neither int nor string: " + sig); -} +};