fix: share env

This commit is contained in:
bingus 2023-12-23 20:28:52 -06:00
parent 1f4995a2d1
commit 6648d349e9
Signed by: orion
GPG Key ID: 6D4165AE4C928719
2 changed files with 29 additions and 19 deletions

View File

@ -1,5 +1,6 @@
module Node.Worker.Parent
( Worker
, SpawnEnv
, stdout
, stderr
, stdin
@ -33,12 +34,11 @@ import Effect.Aff (Aff)
import Effect.Exception (Error)
import Effect.Uncurried (mkEffectFn1)
import Foreign (Foreign, unsafeToForeign)
import Foreign.Object (Object)
import Foreign.Object as Object
import Node.EventEmitter (EventHandle(..))
import Node.EventEmitter.UtilTypes (EventHandle1, EventHandle0)
import Node.Stream (Readable, Writable)
import Node.Worker (ResourceLimits)
import Node.Worker (ResourceLimits, shareEnv)
import Node.Worker.Port (class PortLike)
import Node.Worker.Serializable (class Serializable, deserialize, serialize)
import Prim.Row (class Nub, class Union)
@ -66,7 +66,7 @@ instance PortLike Worker where
type SpawnImplOptions =
( argv :: Nullable (Array Foreign)
, env :: Nullable (Object String)
, env :: Nullable Foreign
, eval :: Nullable (Boolean)
, execArgv :: Nullable (Array String)
, stdin :: Nullable (Boolean)
@ -79,9 +79,13 @@ type SpawnImplOptions =
, name :: Nullable (String)
)
data SpawnEnv
= InheritEnv
| FixedEnv (Map String String)
type SpawnOptions r =
( argv :: Maybe (Array String)
, env :: Maybe (Map String String)
, env :: Maybe SpawnEnv
, execArgv :: Maybe (Array String)
, stdin :: Maybe (Boolean)
, stdout :: Maybe (Boolean)
@ -108,20 +112,26 @@ emptySpawnOptions =
}
spawnOptionsToImpl :: forall a. Serializable a => Record (SpawnOptions (workerData :: Maybe a, eval :: Boolean)) -> Record SpawnImplOptions
spawnOptionsToImpl = applyRecord
{ argv: Nullable.toNullable <<< map (map unsafeToForeign)
, env: Nullable.toNullable <<< map Object.fromFoldableWithIndex
, eval: Nullable.notNull
, execArgv: Nullable.toNullable
, stdin: Nullable.toNullable
, stdout: Nullable.toNullable
, stderr: Nullable.toNullable
, trackUnmanagedFds: Nullable.toNullable
, transferList: Nullable.toNullable
, resourceLimits: Nullable.toNullable
, name: Nullable.toNullable
, workerData: Nullable.toNullable <<< map serialize
}
spawnOptionsToImpl =
let
mapEnv (Just InheritEnv) = Nullable.notNull $ unsafeToForeign shareEnv
mapEnv (Just (FixedEnv env)) = Nullable.notNull $ serialize $ Object.fromFoldableWithIndex env
mapEnv Nothing = Nullable.null
in
applyRecord
{ argv: Nullable.toNullable <<< map (map unsafeToForeign)
, env: mapEnv
, eval: Nullable.notNull
, execArgv: Nullable.toNullable
, stdin: Nullable.toNullable
, stdout: Nullable.toNullable
, stderr: Nullable.toNullable
, trackUnmanagedFds: Nullable.toNullable
, transferList: Nullable.toNullable
, resourceLimits: Nullable.toNullable
, name: Nullable.toNullable
, workerData: Nullable.toNullable <<< map serialize
}
spawnFile :: forall a r optsdup. Serializable a => Union r (SpawnOptions ()) optsdup => Nub optsdup (SpawnOptions ()) => Record r -> String -> a -> Effect Worker
spawnFile opts' file d =

View File

@ -1,4 +1,4 @@
module Node.Worker (ResourceLimits, resourceLimits, isMainThread, threadId, markUntransferable) where
module Node.Worker (ResourceLimits, shareEnv, resourceLimits, isMainThread, threadId, markUntransferable) where
import Prelude