From 1158f22a3596df46a5baaafc8a69e9d07f0457ea Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 06:02:25 -0500 Subject: [PATCH 01/10] Install nullable --- bower.json | 1 + 1 file changed, 1 insertion(+) 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" } } From 15409e19d9cb6db83bf5495987dfa2f4ccaf0d1f Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 06:19:15 -0500 Subject: [PATCH 02/10] Update write/writeString/end to take Maybe Error arg --- src/Node/Stream.js | 8 +++----- src/Node/Stream.purs | 35 ++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 14 deletions(-) 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..14dd30e 100644 --- a/src/Node/Stream.purs +++ b/src/Node/Stream.purs @@ -37,10 +37,12 @@ module Node.Stream 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 +251,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 +281,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 +309,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. -- From 03253667c8f6a31ffac5dfec206a00f77ff1b9c7 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 06:19:25 -0500 Subject: [PATCH 03/10] Add tests --- test/Main.purs | 51 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index c57c525..5a1941e 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -3,12 +3,12 @@ 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 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, destroy, end, onData, onDataEither, onDataString, onError, onReadable, pipe, read, readString, setDefaultEncoding, setEncoding, writeString) import Partial.Unsafe (unsafePartial) import Test.Assert (assert, assert') @@ -39,6 +39,12 @@ main = do log "test pipe" _ <- testPipe + log "test write" + testWrite + + log "test end" + testEnd + log "test manual reads" testReads @@ -129,7 +135,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 +146,42 @@ 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 + _ <- onError w1 (const $ pure unit) + 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 + _ <- destroy w1 + end w1 \err -> do + assert' "end - should have error" $ isJust err + + noError = do + w1 <- writableStreamBuffer + _ <- onError w1 (const $ pure unit) + void $ writeString w1 UTF8 "msg" \_ -> do + end w1 \err -> do + assert' "end - should have no error" $ isNothing err From d295bc3334efec44336e3aaec73f12522c8eea66 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 06:27:25 -0500 Subject: [PATCH 04/10] Update changelog --- CHANGELOG.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90d87a8..bf71611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,11 +12,11 @@ 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: @@ -25,6 +25,10 @@ Bugfixes: 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: From 066e280ea6147fbf08c9e3285d86c13fd6b97347 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 06:28:48 -0500 Subject: [PATCH 05/10] Tweak tests --- test/Main.purs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index 5a1941e..464c26e 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -161,7 +161,6 @@ testWrite = do noError = do w1 <- writableStreamBuffer - _ <- onError w1 (const $ pure unit) void $ writeString w1 UTF8 "msg1" \err -> do assert' "writeString - should have no error" $ isNothing err void $ end w1 (const $ pure unit) @@ -181,7 +180,5 @@ testEnd = do noError = do w1 <- writableStreamBuffer - _ <- onError w1 (const $ pure unit) - void $ writeString w1 UTF8 "msg" \_ -> do - end w1 \err -> do - assert' "end - should have no error" $ isNothing err + end w1 \err -> do + assert' "end - should have no error" $ isNothing err From b127c614cf3bd5f5f88f0537704ffa39c92b0f9f Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 07:42:35 -0500 Subject: [PATCH 06/10] Bump Node to 16 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 06ed895..53730da 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - uses: actions/setup-node@v2 with: - node-version: "14" + node-version: "16" - name: Install dependencies run: | From d80bd8e289e700c48f00f61febd187f25ada5fab Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 07:55:22 -0500 Subject: [PATCH 07/10] Revert "Bump Node to 16" This reverts commit b127c614cf3bd5f5f88f0537704ffa39c92b0f9f. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 53730da..06ed895 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - uses: actions/setup-node@v2 with: - node-version: "16" + node-version: "14" - name: Install dependencies run: | From 6d230a1941cbd5e1e86165237fc1dc3d5ce75148 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 08:01:07 -0500 Subject: [PATCH 08/10] Export destroyWithError --- src/Node/Stream.purs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Node/Stream.purs b/src/Node/Stream.purs index 14dd30e..1f0c470 100644 --- a/src/Node/Stream.purs +++ b/src/Node/Stream.purs @@ -32,6 +32,7 @@ module Node.Stream , setDefaultEncoding , end , destroy + , destroyWithError ) where import Prelude From d0648b8d1d4f6293cc86215bdb631c8bb58486ce Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 08:01:19 -0500 Subject: [PATCH 09/10] Update test to use destroyWithError --- test/Main.purs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index 464c26e..9d9a520 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -6,9 +6,10 @@ import Data.Either (Either(..)) 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, destroy, end, onData, onDataEither, onDataString, onError, onReadable, pipe, read, readString, setDefaultEncoding, setEncoding, writeString) +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') @@ -174,7 +175,7 @@ testEnd = do w1 <- writableStreamBuffer _ <- onError w1 (const $ pure unit) void $ writeString w1 UTF8 "msg" \_ -> do - _ <- destroy w1 + _ <- destroyWithError w1 $ error "Problem" end w1 \err -> do assert' "end - should have error" $ isJust err From ece11ff7dfc86f96824402a82b6784695bc38830 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 29 Apr 2022 08:01:55 -0500 Subject: [PATCH 10/10] Add entry for destroyWithError change --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf71611..fe2904a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Breaking changes: New features: Bugfixes: +- Exported `destroyWithError` (#43 by @JordanMartinez) Other improvements: - Fix `Gzip` example (#17, #36 by @matthewleon and @JordanMartinez)