diff --git a/CHANGELOG.md b/CHANGELOG.md index 90d87a8..fe2904a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,19 +12,24 @@ Bugfixes: Other improvements: -## [v6.0.0](https://github.com/purescript-node/purescript-node-streams/releases/tag/v6.0.0) - 2022-04-27 +## [v7.0.0](https://github.com/purescript-node/purescript-node-streams/releases/tag/v7.0.0) - 2022-04-29 Breaking changes: - Update project and deps to PureScript v0.15.0 (#39 by @nwolverson, @JordanMartinez, @sigma-andex) -- Update `write` callback to include `Error` arg (#40 by @JordanMartinez) +- Update `write`/`writeString`/`end` callbacks to include `Maybe Error` arg (#40 and #43 by @JordanMartinez) New features: Bugfixes: +- Exported `destroyWithError` (#43 by @JordanMartinez) Other improvements: - Fix `Gzip` example (#17, #36 by @matthewleon and @JordanMartinez) +## [v6.0.0](https://github.com/purescript-node/purescript-node-streams/releases/tag/v6.0.0) - 2022-04-27 + +Due to an incorrectly-made breaking change, please use `v7.0.0` instead. + ## [v5.0.0](https://github.com/purescript-node/purescript-posix-types/releases/tag/v5.0.0) - 2021-02-26 Breaking changes: diff --git a/bower.json b/bower.json index dde2e71..be709a7 100644 --- a/bower.json +++ b/bower.json @@ -21,6 +21,7 @@ "purescript-either": "^6.0.0", "purescript-exceptions": "^6.0.0", "purescript-node-buffer": "^8.0.0", + "purescript-nullable": "^6.0.0", "purescript-prelude": "^6.0.0" } } diff --git a/src/Node/Stream.js b/src/Node/Stream.js index 4900bf6..cdd6748 100644 --- a/src/Node/Stream.js +++ b/src/Node/Stream.js @@ -102,7 +102,7 @@ export function readImpl(readChunk) { }; } -export function write(w) { +export function writeImpl(w) { return chunk => done => () => w.write(chunk, null, done); } @@ -124,11 +124,9 @@ export function setDefaultEncodingImpl(w) { }; } -export function end(w) { +export function endImpl(w) { return done => () => { - w.end(null, null, () => { - done(); - }); + w.end(null, null, done); }; } diff --git a/src/Node/Stream.purs b/src/Node/Stream.purs index ea3d7f4..1f0c470 100644 --- a/src/Node/Stream.purs +++ b/src/Node/Stream.purs @@ -32,15 +32,18 @@ module Node.Stream , setDefaultEncoding , end , destroy + , destroyWithError ) where import Prelude import Effect (Effect) -import Effect.Exception (throw, Error()) +import Effect.Exception (throw, Error) import Data.Either (Either(..)) import Data.Maybe (Maybe(..), fromMaybe) -import Node.Buffer (Buffer()) +import Node.Buffer (Buffer) +import Data.Nullable as N +import Effect.Uncurried (EffectFn1, mkEffectFn1) import Node.Buffer as Buffer import Node.Encoding (Encoding) @@ -249,20 +252,28 @@ foreign import unpipeAll . Readable w -> Effect Unit +foreign import writeImpl + :: forall r + . Writable r + -> Buffer + -> EffectFn1 (N.Nullable Error) Unit + -> Effect Boolean + -- | Write a Buffer to a writable stream. -foreign import write +write :: forall r . Writable r -> Buffer - -> (Error -> Effect Unit) + -> (Maybe Error -> Effect Unit) -> Effect Boolean +write w b cb = writeImpl w b $ mkEffectFn1 (cb <<< N.toMaybe) foreign import writeStringImpl :: forall r . Writable r -> String -> String - -> (Error -> Effect Unit) + -> EffectFn1 (N.Nullable Error) Unit -> Effect Boolean -- | Write a string in the specified encoding to a writable stream. @@ -271,9 +282,9 @@ writeString . Writable r -> Encoding -> String - -> (Error -> Effect Unit) + -> (Maybe Error -> Effect Unit) -> Effect Boolean -writeString w enc = writeStringImpl w (show enc) +writeString w enc s cb = writeStringImpl w (show enc) s $ mkEffectFn1 (cb <<< N.toMaybe) -- | Force buffering of writes. foreign import cork :: forall r. Writable r -> Effect Unit @@ -299,12 +310,19 @@ setDefaultEncoding -> Effect Unit setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc) --- | End writing data to the stream. -foreign import end +foreign import endImpl :: forall r . Writable r + -> EffectFn1 (N.Nullable Error) Unit -> Effect Unit + +-- | End writing data to the stream. +end + :: forall r + . Writable r + -> (Maybe Error -> Effect Unit) -> Effect Unit +end w cb = endImpl w $ mkEffectFn1 (cb <<< N.toMaybe) -- | Destroy the stream. It will release any internal resources. -- diff --git a/test/Main.purs b/test/Main.purs index c57c525..9d9a520 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -3,12 +3,13 @@ module Test.Main where import Prelude import Data.Either (Either(..)) -import Data.Maybe (Maybe(..), fromJust, isNothing, isJust) +import Data.Maybe (Maybe(..), fromJust, isJust, isNothing) import Effect (Effect) import Effect.Console (log) +import Effect.Exception (error) import Node.Buffer as Buffer import Node.Encoding (Encoding(..)) -import Node.Stream (Duplex, Readable, Writable, onDataString, end, writeString, pipe, onDataEither, onData, setEncoding, setDefaultEncoding, read, onReadable, readString) +import Node.Stream (Duplex, Readable, Writable, destroyWithError, end, onData, onDataEither, onDataString, onError, onReadable, pipe, read, readString, setDefaultEncoding, setEncoding, writeString) import Partial.Unsafe (unsafePartial) import Test.Assert (assert, assert') @@ -39,6 +40,12 @@ main = do log "test pipe" _ <- testPipe + log "test write" + testWrite + + log "test end" + testEnd + log "test manual reads" testReads @@ -129,7 +136,7 @@ testPipe = do _ <- unzip `pipe` sOut writeString sIn UTF8 testString \_ -> do - end sIn do + end sIn \_ -> do onDataString sOut UTF8 \str -> do assertEqual str testString @@ -140,3 +147,39 @@ foreign import createGunzip :: Effect Duplex -- | Create a PassThrough stream, which simply writes its input to its output. foreign import passThrough :: Effect Duplex + +testWrite :: Effect Unit +testWrite = do + hasError + noError + where + hasError = do + w1 <- writableStreamBuffer + _ <- onError w1 (const $ pure unit) + void $ end w1 $ const $ pure unit + void $ writeString w1 UTF8 "msg" \err -> do + assert' "writeString - should have error" $ isJust err + + noError = do + w1 <- writableStreamBuffer + void $ writeString w1 UTF8 "msg1" \err -> do + assert' "writeString - should have no error" $ isNothing err + void $ end w1 (const $ pure unit) + +testEnd :: Effect Unit +testEnd = do + hasError + noError + where + hasError = do + w1 <- writableStreamBuffer + _ <- onError w1 (const $ pure unit) + void $ writeString w1 UTF8 "msg" \_ -> do + _ <- destroyWithError w1 $ error "Problem" + end w1 \err -> do + assert' "end - should have error" $ isJust err + + noError = do + w1 <- writableStreamBuffer + end w1 \err -> do + assert' "end - should have no error" $ isNothing err