Skip to content

Migrate process things #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions src/Node/Library/Execa/SignalExit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ export function processCallFn(originalProcessReallyExit, exitCode) {
return originalProcessReallyExit.call(global.process, exitCode);
}

export function processKill(pid, sig) {
global.process.kill(pid, sig);
}

export function processListenersLength(sig) {
return global.process.listeners(sig).length;
}

export function processOn(sig, listener) {
return global.process.on(sig, listener);
}

export function processOff(sig, listener) {
return global.process.off(sig, listener);
}

export function customProcessEmit(cb) {
return function (ev, arg) {
const thisArg = this;
Expand All @@ -38,6 +22,3 @@ export function customProcessEmit(cb) {
};
}

export function processExitCode() {
return global.process.exitCode;
}
39 changes: 19 additions & 20 deletions src/Node/Library/Execa/SignalExit.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Data.Foldable (traverse_)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Monoid (guard)
import Data.Nullable (Nullable, notNull, null, toMaybe, toNullable)
import Data.Posix (Pid)
import Data.Set (Set)
import Data.Set as Set
import Data.Traversable (for)
Expand All @@ -23,10 +22,12 @@ import Effect.Exception (try)
import Effect.Ref (Ref)
import Effect.Ref as Ref
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, mkEffectFn1, mkEffectFn2, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn3)
import Node.EventEmitter (EventEmitter, EventHandle(..), on, unsafeEmitFn)
import Node.EventEmitter (EventEmitter, EventHandle(..), listenerCount, on, unsafeEmitFn)
import Node.EventEmitter as EventEmitter
import Node.Platform (Platform(..))
import Node.Process (Process, mkSignalH', process)
import Node.Process as Process
import Unsafe.Coerce (unsafeCoerce)

foreign import unsafeProcessHasProp :: EffectFn1 String Boolean
foreign import unsafeReadProcessProp :: forall a. EffectFn1 String a
Expand All @@ -36,12 +37,10 @@ foreign import data ProcessEmitFn :: Type
foreign import data ProcessReallyExitFn :: Type
foreign import processCallFn :: EffectFn2 ProcessReallyExitFn (Nullable Int) Unit

foreign import processOn :: forall cb. EffectFn2 String cb Unit
foreign import processOff :: forall cb. EffectFn2 String cb Unit
foreign import processKill :: EffectFn2 Pid String Unit
foreign import processListenersLength :: EffectFn1 String Int
foreign import customProcessEmit :: EffectFn3 (EffectFn1 ProcessEmitFn Boolean) String (Nullable Int) Boolean -> EffectFn2 String (Nullable Int) Boolean
foreign import processExitCode :: Effect (Nullable Int)

processToEventEmitter :: Process -> EventEmitter
processToEventEmitter = unsafeCoerce

isWin :: Boolean
isWin = Just Win32 == Process.platform
Expand Down Expand Up @@ -148,17 +147,17 @@ onExit' cb options = do
-- for that to occur.
signalListeners <- for signals \sig -> map hush $ try do
let listener = mkListener sig countRef
runEffectFn2 processOn sig listener
rm <- process # on (mkSignalH' sig) listener
pure $ void $ try do
runEffectFn2 processOff sig listener
rm
Ref.write signalListeners signalListenersRef
runEffectFn2 unsafeWriteProcessProp "emit" processEmitFn
runEffectFn2 unsafeWriteProcessProp "reallyExit" processReallyExitFn

-- Good
mkListener :: String -> Ref Int -> Effect Unit
mkListener sig countRef = do
listenersLen <- runEffectFn1 processListenersLength sig
listenersLen <- listenerCount (processToEventEmitter process) sig
count <- Ref.read countRef
when (listenersLen == count) do
unload
Expand All @@ -167,7 +166,7 @@ onExit' cb options = do
-- "SIGHUP" throws an `ENOSYS` error on Windows,
-- so use a supported signal instead
let sig' = if isWin && sig == "SIGHUP" then "SIGINT" else sig
runEffectFn2 processKill Process.pid sig'
Process.killStr Process.pid sig'

processReallyExitFn = mkEffectFn1 \(code :: Nullable Int) -> do
{ emitter
Expand All @@ -183,9 +182,10 @@ onExit' cb options = do
{ originalProcessEmit } <- getGlobalRecOnProcessObject
if ev == exitEvent then do
exitCode <- case toMaybe arg of
Nothing -> processExitCode
Nothing ->
map toNullable $ Process.getExitCode
Just exitCode' -> do
runEffectFn2 unsafeWriteProcessProp exitEvent exitCode'
Process.setExitCode exitCode'
pure $ notNull exitCode'

ret <- runEffectFn1 runOriginalProcessEmit originalProcessEmit
Expand Down Expand Up @@ -266,17 +266,17 @@ getGlobalRecOnProcessObject =
-- state from which it is not safe to try and enter JS
-- listeners.
signals :: Array String
signals = normal <> windows <> linux
signals = normal <> nonWindows <> linux
where
normal =
[ "SIGABRT"
, "SIGALRM"
, "SIGHUP"
[ "SIGHUP"
, "SIGINT"
, "SIGTERM"
]
windows = guard isWin
[ "SIGVTALRM"
nonWindows = guard (not isWin)
[ "SIGABRT"
, "SIGALRM"
, "SIGVTALRM"
, "SIGXCPU"
, "SIGXFSZ"
, "SIGUSR2"
Expand All @@ -297,5 +297,4 @@ signals = normal <> windows <> linux
, "SIGPOLL"
, "SIGPWR"
, "SIGSTKFLT"
, "SIGUNUSED"
]