2024-05-09 22:21:15 +00:00
|
|
|
module Test.Pipes.Node.Stream where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
import Control.Monad.Trans.Class (lift)
|
|
|
|
import Data.Array as Array
|
2024-05-10 20:04:09 +00:00
|
|
|
import Data.Foldable (fold)
|
2024-05-09 22:21:15 +00:00
|
|
|
import Data.List ((:))
|
|
|
|
import Data.List as List
|
2024-05-10 20:04:09 +00:00
|
|
|
import Data.Maybe (Maybe)
|
2024-05-09 22:21:15 +00:00
|
|
|
import Data.Newtype (wrap)
|
|
|
|
import Data.String.Gen (genAlphaString)
|
|
|
|
import Data.Tuple.Nested (type (/\), (/\))
|
|
|
|
import Effect (Effect)
|
2024-05-10 20:04:09 +00:00
|
|
|
import Effect.Aff (Aff, delay)
|
|
|
|
import Effect.Class (class MonadEffect, liftEffect)
|
2024-05-09 22:21:15 +00:00
|
|
|
import Node.Buffer (Buffer)
|
|
|
|
import Node.Buffer as Buffer
|
|
|
|
import Node.Encoding (Encoding(..))
|
|
|
|
import Node.FS.Stream as FS.Stream
|
|
|
|
import Node.FS.Sync as FS
|
|
|
|
import Node.Stream.Object as O
|
|
|
|
import Node.Zlib as Zlib
|
2024-05-10 20:04:09 +00:00
|
|
|
import Pipes (each) as Pipes
|
2024-05-09 22:21:15 +00:00
|
|
|
import Pipes (yield, (>->))
|
2024-05-10 20:04:09 +00:00
|
|
|
import Pipes.Core (Consumer, Producer, runEffect)
|
|
|
|
import Pipes.Node.Buffer as Pipes.Buffer
|
2024-05-09 22:21:15 +00:00
|
|
|
import Pipes.Node.Stream as S
|
2024-05-10 20:04:09 +00:00
|
|
|
import Pipes.Prelude (mapFoldable, toListM) as Pipes
|
|
|
|
import Simple.JSON (writeJSON)
|
|
|
|
import Test.Common (jsonParse, jsonStringify, tmpFile, tmpFiles)
|
2024-05-09 22:21:15 +00:00
|
|
|
import Test.QuickCheck.Arbitrary (arbitrary)
|
2024-05-10 20:04:09 +00:00
|
|
|
import Test.QuickCheck.Gen (randomSample')
|
2024-05-09 22:21:15 +00:00
|
|
|
import Test.Spec (Spec, around, describe, it)
|
|
|
|
import Test.Spec.Assertions (shouldEqual)
|
|
|
|
|
|
|
|
foreign import readableFromArray :: forall @a. Array a -> O.Readable a
|
2024-05-10 20:04:09 +00:00
|
|
|
foreign import discardTransform :: forall a b. Effect (O.Transform a b)
|
|
|
|
foreign import charsTransform :: Effect (O.Transform String String)
|
2024-05-09 22:21:15 +00:00
|
|
|
|
2024-05-10 20:04:09 +00:00
|
|
|
writer :: forall m. MonadEffect m => String -> m (O.Writable Buffer /\ Consumer (Maybe Buffer) Aff Unit)
|
|
|
|
writer a = do
|
|
|
|
stream <- liftEffect $ O.fromBufferWritable <$> FS.Stream.createWriteStream a
|
|
|
|
pure $ stream /\ S.fromWritable stream
|
2024-05-09 22:21:15 +00:00
|
|
|
|
2024-05-10 20:04:09 +00:00
|
|
|
reader :: forall m. MonadEffect m => String -> m (Producer (Maybe Buffer) Aff Unit)
|
|
|
|
reader a = liftEffect $ S.fromReadable <$> O.fromBufferReadable <$> FS.Stream.createReadStream a
|
2024-05-09 22:21:15 +00:00
|
|
|
|
|
|
|
spec :: Spec Unit
|
|
|
|
spec =
|
|
|
|
describe "Test.Pipes.Node.Stream" do
|
|
|
|
describe "Readable" do
|
|
|
|
describe "Readable.from(<Iterable>)" do
|
|
|
|
it "empty" do
|
2024-05-10 20:04:09 +00:00
|
|
|
vals <- Pipes.toListM $ (S.fromReadable $ readableFromArray @{ foo :: String } []) >-> S.unEOS
|
2024-05-09 22:21:15 +00:00
|
|
|
vals `shouldEqual` List.Nil
|
|
|
|
it "singleton" do
|
2024-05-10 20:04:09 +00:00
|
|
|
vals <- Pipes.toListM $ (S.fromReadable $ readableFromArray @{ foo :: String } [ { foo: "1" } ]) >-> S.unEOS
|
2024-05-09 22:21:15 +00:00
|
|
|
vals `shouldEqual` ({ foo: "1" } : List.Nil)
|
|
|
|
it "many elements" do
|
|
|
|
let exp = (\n -> { foo: show n }) <$> Array.range 0 100
|
2024-05-10 20:04:09 +00:00
|
|
|
vals <- Pipes.toListM $ (S.fromReadable $ readableFromArray exp) >-> S.unEOS
|
2024-05-09 22:21:15 +00:00
|
|
|
vals `shouldEqual` (List.fromFoldable exp)
|
|
|
|
describe "Writable" $ around tmpFile do
|
|
|
|
describe "fs.WriteStream" do
|
|
|
|
it "pipe to file" \p -> do
|
2024-05-10 20:04:09 +00:00
|
|
|
stream <- O.fromBufferWritable <$> liftEffect (FS.Stream.createWriteStream p)
|
2024-05-09 22:21:15 +00:00
|
|
|
let
|
2024-05-10 20:04:09 +00:00
|
|
|
w = S.fromWritable stream
|
2024-05-09 22:21:15 +00:00
|
|
|
source = do
|
|
|
|
buf <- liftEffect $ Buffer.fromString "hello" UTF8
|
2024-05-10 20:04:09 +00:00
|
|
|
yield buf
|
|
|
|
runEffect $ S.withEOS source >-> w
|
2024-05-09 22:21:15 +00:00
|
|
|
contents <- liftEffect $ FS.readTextFile UTF8 p
|
|
|
|
contents `shouldEqual` "hello"
|
2024-05-10 20:04:09 +00:00
|
|
|
shouldEqual true =<< liftEffect (O.isWritableEnded stream)
|
2024-05-09 22:21:15 +00:00
|
|
|
it "async pipe to file" \p -> do
|
|
|
|
w <- S.fromWritable <$> O.fromBufferWritable <$> liftEffect (FS.Stream.createWriteStream p)
|
|
|
|
let
|
|
|
|
source = do
|
2024-05-10 20:04:09 +00:00
|
|
|
yield "hello, "
|
2024-05-09 22:21:15 +00:00
|
|
|
lift $ delay $ wrap 5.0
|
2024-05-10 20:04:09 +00:00
|
|
|
yield "world!"
|
2024-05-09 22:21:15 +00:00
|
|
|
lift $ delay $ wrap 5.0
|
2024-05-10 20:04:09 +00:00
|
|
|
yield " "
|
2024-05-09 22:21:15 +00:00
|
|
|
lift $ delay $ wrap 5.0
|
2024-05-10 20:04:09 +00:00
|
|
|
yield "this is a "
|
2024-05-09 22:21:15 +00:00
|
|
|
lift $ delay $ wrap 5.0
|
2024-05-10 20:04:09 +00:00
|
|
|
yield "test."
|
|
|
|
runEffect $ S.withEOS (source >-> Pipes.Buffer.fromString UTF8) >-> w
|
2024-05-09 22:21:15 +00:00
|
|
|
contents <- liftEffect $ FS.readTextFile UTF8 p
|
|
|
|
contents `shouldEqual` "hello, world! this is a test."
|
|
|
|
it "chained pipes" \p -> do
|
|
|
|
let
|
|
|
|
obj = do
|
|
|
|
str :: String <- genAlphaString
|
|
|
|
num :: Int <- arbitrary
|
|
|
|
stuff :: Array String <- arbitrary
|
2024-05-11 23:01:34 +00:00
|
|
|
pure { str, num, stuff }
|
2024-05-10 20:04:09 +00:00
|
|
|
objs <- liftEffect (randomSample' 1 obj)
|
2024-05-09 22:21:15 +00:00
|
|
|
let
|
|
|
|
exp = fold (writeJSON <$> objs)
|
2024-05-10 20:04:09 +00:00
|
|
|
stream /\ w <- liftEffect $ writer p
|
|
|
|
runEffect $ S.withEOS (Pipes.each objs >-> jsonStringify >-> Pipes.Buffer.fromString UTF8) >-> w
|
2024-05-09 22:21:15 +00:00
|
|
|
contents <- liftEffect $ FS.readTextFile UTF8 p
|
|
|
|
contents `shouldEqual` exp
|
2024-05-10 20:04:09 +00:00
|
|
|
shouldEqual true =<< liftEffect (O.isWritableEnded stream)
|
2024-05-09 22:21:15 +00:00
|
|
|
describe "Transform" do
|
|
|
|
it "gzip" do
|
|
|
|
let
|
2024-05-11 23:01:34 +00:00
|
|
|
json = yield $ writeJSON { foo: "bar" }
|
2024-05-09 22:21:15 +00:00
|
|
|
exp = "1f8b0800000000000003ab564acbcf57b2524a4a2c52aa0500eff52bfe0d000000"
|
|
|
|
gzip <- S.fromTransform <$> O.fromBufferTransform <$> liftEffect (Zlib.toDuplex <$> Zlib.createGzip)
|
2024-05-10 20:04:09 +00:00
|
|
|
outs :: List.List String <- Pipes.toListM (S.withEOS (json >-> Pipes.Buffer.fromString UTF8) >-> gzip >-> S.unEOS >-> Pipes.Buffer.toString Hex)
|
2024-05-09 22:21:15 +00:00
|
|
|
fold outs `shouldEqual` exp
|
|
|
|
around tmpFiles
|
|
|
|
$ it "file >-> gzip >-> file >-> gunzip" \(a /\ b) -> do
|
2024-05-11 23:01:34 +00:00
|
|
|
liftEffect $ FS.writeTextFile UTF8 a $ writeJSON [ 1, 2, 3, 4 ]
|
2024-05-09 22:21:15 +00:00
|
|
|
areader <- liftEffect $ reader a
|
2024-05-10 20:04:09 +00:00
|
|
|
bwritestream /\ bwriter <- liftEffect $ writer b
|
2024-05-09 22:21:15 +00:00
|
|
|
gzip <- S.fromTransform <$> O.fromBufferTransform <$> liftEffect (Zlib.toDuplex <$> Zlib.createGzip)
|
|
|
|
runEffect $ areader >-> gzip >-> bwriter
|
2024-05-10 20:04:09 +00:00
|
|
|
shouldEqual true =<< liftEffect (O.isWritableEnded bwritestream)
|
2024-05-09 22:21:15 +00:00
|
|
|
|
|
|
|
gunzip <- S.fromTransform <$> O.fromBufferTransform <$> liftEffect (Zlib.toDuplex <$> Zlib.createGunzip)
|
|
|
|
breader <- liftEffect $ reader b
|
2024-05-10 20:04:09 +00:00
|
|
|
nums <- Pipes.toListM (breader >-> gunzip >-> S.unEOS >-> Pipes.Buffer.toString UTF8 >-> jsonParse @(Array Int) >-> Pipes.mapFoldable identity)
|
2024-05-11 23:01:34 +00:00
|
|
|
Array.fromFoldable nums `shouldEqual` [ 1, 2, 3, 4 ]
|
2024-05-10 20:04:09 +00:00
|
|
|
around tmpFile $ it "file >-> discardTransform" \(p :: String) -> do
|
|
|
|
liftEffect $ FS.writeTextFile UTF8 p "foo"
|
|
|
|
r <- reader p
|
|
|
|
discard' <- liftEffect discardTransform
|
|
|
|
out :: List.List Int <- Pipes.toListM $ r >-> S.fromTransform discard' >-> S.unEOS
|
|
|
|
out `shouldEqual` List.Nil
|
|
|
|
around tmpFile $ it "file >-> charsTransform" \(p :: String) -> do
|
|
|
|
liftEffect $ FS.writeTextFile UTF8 p "foo bar"
|
|
|
|
r <- reader p
|
|
|
|
chars' <- liftEffect charsTransform
|
|
|
|
out :: List.List String <- Pipes.toListM $ r >-> S.inEOS (Pipes.Buffer.toString UTF8) >-> S.fromTransform chars' >-> S.unEOS
|
2024-05-11 23:01:34 +00:00
|
|
|
out `shouldEqual` List.fromFoldable [ "f", "o", "o", " ", "b", "a", "r" ]
|