Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix write/end FFI type signatures; export destroyWithError; prep v7.0.0 release #43

Merged
merged 10 commits into from
Apr 29, 2022
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
8 changes: 3 additions & 5 deletions src/Node/Stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function readImpl(readChunk) {
};
}

export function write(w) {
export function writeImpl(w) {
return chunk => done => () => w.write(chunk, null, done);
}

Expand All @@ -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);
};
}

Expand Down
36 changes: 27 additions & 9 deletions src/Node/Stream.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
--
Expand Down
49 changes: 46 additions & 3 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -39,6 +40,12 @@ main = do
log "test pipe"
_ <- testPipe

log "test write"
testWrite

log "test end"
testEnd

log "test manual reads"
testReads

Expand Down Expand Up @@ -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

Expand All @@ -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