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

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 import Prelude