From 6648d349e95a8c3c81a53425098a664332ea818c Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Sat, 23 Dec 2023 20:28:52 -0600 Subject: [PATCH] fix: share env --- src/Node.Worker.Parent.purs | 46 ++++++++++++++++++++++--------------- src/Node.Worker.purs | 2 +- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/Node.Worker.Parent.purs b/src/Node.Worker.Parent.purs index ba88613..9620994 100644 --- a/src/Node.Worker.Parent.purs +++ b/src/Node.Worker.Parent.purs @@ -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 = diff --git a/src/Node.Worker.purs b/src/Node.Worker.purs index 9567b89..1b0971c 100644 --- a/src/Node.Worker.purs +++ b/src/Node.Worker.purs @@ -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