From fcf3b78b8f8bb1c8fbc287f2c60625bb2293f847 Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Sun, 24 Dec 2023 15:09:25 -0600 Subject: [PATCH] fix: bug --- src/Node.Worker.Parent.purs | 4 ++-- src/Node.Worker.Serializable.purs | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/Node.Worker.Parent.purs b/src/Node.Worker.Parent.purs index bf1378b..9c46288 100644 --- a/src/Node.Worker.Parent.purs +++ b/src/Node.Worker.Parent.purs @@ -114,12 +114,12 @@ emptySpawnOptions = spawnOptionsToImpl :: forall a. Serializable a => Record (SpawnOptions (workerData :: Maybe a, eval :: Boolean)) -> Record SpawnImplOptions spawnOptionsToImpl = let - mapEnv (Just InheritEnv) = Nullable.notNull $ unsafeToForeign shareEnv + mapEnv (Just InheritEnv) = Nullable.notNull $ (unsafeToForeign :: Symbol -> Foreign) 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 serialize) , env: mapEnv , eval: Nullable.notNull , execArgv: Nullable.toNullable diff --git a/src/Node.Worker.Serializable.purs b/src/Node.Worker.Serializable.purs index 7cf8afc..ffc06d3 100644 --- a/src/Node.Worker.Serializable.purs +++ b/src/Node.Worker.Serializable.purs @@ -25,6 +25,9 @@ import Type.Prelude (Proxy(..)) foreign import undef :: Foreign +convert :: forall @a. a -> Foreign +convert = unsafeToForeign + class Serializable a where serialize :: a -> Foreign deserialize :: Foreign -> Either String a @@ -34,44 +37,44 @@ instance Serializable Foreign where deserialize = pure instance Serializable String where - serialize = unsafeToForeign + serialize = convert @String deserialize = lmap show <<< runExcept <<< readString instance Serializable URL where - serialize = unsafeToForeign + serialize = convert @URL deserialize = lmap show <<< runExcept <<< unsafeReadTagged "URL" instance Serializable Int where - serialize = unsafeToForeign + serialize = convert @Int deserialize = lmap show <<< runExcept <<< readInt instance Serializable Number where - serialize = unsafeToForeign + serialize = convert @Number deserialize = lmap show <<< runExcept <<< readNumber instance Serializable BigInt where - serialize = unsafeToForeign + serialize = convert @BigInt deserialize = lmap show <<< runExcept <<< unsafeReadTagged "BigInt" instance Serializable Boolean where - serialize = unsafeToForeign + serialize = convert @Boolean deserialize = lmap show <<< runExcept <<< readBoolean instance Serializable a => Serializable (Maybe a) where - serialize (Just a) = unsafeToForeign a + serialize (Just a) = convert @a a serialize Nothing = undef deserialize = flip bind (traverse deserialize) <<< lmap show <<< runExcept <<< readNullOrUndefined instance Serializable a => Serializable (Array a) where - serialize a = unsafeToForeign a + serialize a = convert @(Array a) a deserialize = flip bind (traverse deserialize) <<< lmap show <<< runExcept <<< readArray instance Serializable a => Serializable (NonEmptyArray a) where - serialize a = unsafeToForeign $ Array.NonEmpty.toArray a + serialize a = convert @(Array a) $ Array.NonEmpty.toArray a deserialize = flip bind (liftMaybe "expected non-empty array" <<< Array.NonEmpty.fromArray) <<< deserialize instance Serializable a => Serializable (Object a) where - serialize a = unsafeToForeign a + serialize a = convert @(Object a) a deserialize a = if typeOf a == "object" then pure $ unsafeFromForeign a @@ -79,7 +82,7 @@ instance Serializable a => Serializable (Object a) where throwError $ "expected object, found " <> typeOf a instance (RowToList r rl, SerializeRecord rl r () to, DeserializeRecord rl () r) => Serializable (Record r) where - serialize a = unsafeToForeign $ serializeFields (Proxy @rl) a + serialize a = convert @(Record to) $ Record.Builder.buildFromScratch $ serializeFields (Proxy @rl) a deserialize a = map (flip Record.Builder.build {}) (deserializeFields (Proxy @rl) a) class DeserializeRecord (rl :: RowList Type) (from :: Row Type) (to :: Row Type) | rl -> from to where