Skip to content

Commit 8395652

Browse files
Fix write/end FFI type signatures; export destroyWithError; prep v7.0.0 release (#43)
* Install nullable * Update write/writeString/end to take Maybe Error arg * Export destroyWithError * Add entry for destroyWithError change * Add tests * Update changelog
1 parent 224cf90 commit 8395652

File tree

5 files changed

+84
-19
lines changed

5 files changed

+84
-19
lines changed

CHANGELOG.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,24 @@ Bugfixes:
1212

1313
Other improvements:
1414

15-
## [v6.0.0](https://github.com/purescript-node/purescript-node-streams/releases/tag/v6.0.0) - 2022-04-27
15+
## [v7.0.0](https://github.com/purescript-node/purescript-node-streams/releases/tag/v7.0.0) - 2022-04-29
1616

1717
Breaking changes:
1818
- Update project and deps to PureScript v0.15.0 (#39 by @nwolverson, @JordanMartinez, @sigma-andex)
19-
- Update `write` callback to include `Error` arg (#40 by @JordanMartinez)
19+
- Update `write`/`writeString`/`end` callbacks to include `Maybe Error` arg (#40 and #43 by @JordanMartinez)
2020

2121
New features:
2222

2323
Bugfixes:
24+
- Exported `destroyWithError` (#43 by @JordanMartinez)
2425

2526
Other improvements:
2627
- Fix `Gzip` example (#17, #36 by @matthewleon and @JordanMartinez)
2728

29+
## [v6.0.0](https://github.com/purescript-node/purescript-node-streams/releases/tag/v6.0.0) - 2022-04-27
30+
31+
Due to an incorrectly-made breaking change, please use `v7.0.0` instead.
32+
2833
## [v5.0.0](https://github.com/purescript-node/purescript-posix-types/releases/tag/v5.0.0) - 2021-02-26
2934

3035
Breaking changes:

bower.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"purescript-either": "^6.0.0",
2222
"purescript-exceptions": "^6.0.0",
2323
"purescript-node-buffer": "^8.0.0",
24+
"purescript-nullable": "^6.0.0",
2425
"purescript-prelude": "^6.0.0"
2526
}
2627
}

src/Node/Stream.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function readImpl(readChunk) {
102102
};
103103
}
104104

105-
export function write(w) {
105+
export function writeImpl(w) {
106106
return chunk => done => () => w.write(chunk, null, done);
107107
}
108108

@@ -124,11 +124,9 @@ export function setDefaultEncodingImpl(w) {
124124
};
125125
}
126126

127-
export function end(w) {
127+
export function endImpl(w) {
128128
return done => () => {
129-
w.end(null, null, () => {
130-
done();
131-
});
129+
w.end(null, null, done);
132130
};
133131
}
134132

src/Node/Stream.purs

+27-9
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ module Node.Stream
3232
, setDefaultEncoding
3333
, end
3434
, destroy
35+
, destroyWithError
3536
) where
3637

3738
import Prelude
3839

3940
import Effect (Effect)
40-
import Effect.Exception (throw, Error())
41+
import Effect.Exception (throw, Error)
4142
import Data.Either (Either(..))
4243
import Data.Maybe (Maybe(..), fromMaybe)
43-
import Node.Buffer (Buffer())
44+
import Node.Buffer (Buffer)
45+
import Data.Nullable as N
46+
import Effect.Uncurried (EffectFn1, mkEffectFn1)
4447
import Node.Buffer as Buffer
4548
import Node.Encoding (Encoding)
4649

@@ -249,20 +252,28 @@ foreign import unpipeAll
249252
. Readable w
250253
-> Effect Unit
251254

255+
foreign import writeImpl
256+
:: forall r
257+
. Writable r
258+
-> Buffer
259+
-> EffectFn1 (N.Nullable Error) Unit
260+
-> Effect Boolean
261+
252262
-- | Write a Buffer to a writable stream.
253-
foreign import write
263+
write
254264
:: forall r
255265
. Writable r
256266
-> Buffer
257-
-> (Error -> Effect Unit)
267+
-> (Maybe Error -> Effect Unit)
258268
-> Effect Boolean
269+
write w b cb = writeImpl w b $ mkEffectFn1 (cb <<< N.toMaybe)
259270

260271
foreign import writeStringImpl
261272
:: forall r
262273
. Writable r
263274
-> String
264275
-> String
265-
-> (Error -> Effect Unit)
276+
-> EffectFn1 (N.Nullable Error) Unit
266277
-> Effect Boolean
267278

268279
-- | Write a string in the specified encoding to a writable stream.
@@ -271,9 +282,9 @@ writeString
271282
. Writable r
272283
-> Encoding
273284
-> String
274-
-> (Error -> Effect Unit)
285+
-> (Maybe Error -> Effect Unit)
275286
-> Effect Boolean
276-
writeString w enc = writeStringImpl w (show enc)
287+
writeString w enc s cb = writeStringImpl w (show enc) s $ mkEffectFn1 (cb <<< N.toMaybe)
277288

278289
-- | Force buffering of writes.
279290
foreign import cork :: forall r. Writable r -> Effect Unit
@@ -299,12 +310,19 @@ setDefaultEncoding
299310
-> Effect Unit
300311
setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc)
301312

302-
-- | End writing data to the stream.
303-
foreign import end
313+
foreign import endImpl
304314
:: forall r
305315
. Writable r
316+
-> EffectFn1 (N.Nullable Error) Unit
306317
-> Effect Unit
318+
319+
-- | End writing data to the stream.
320+
end
321+
:: forall r
322+
. Writable r
323+
-> (Maybe Error -> Effect Unit)
307324
-> Effect Unit
325+
end w cb = endImpl w $ mkEffectFn1 (cb <<< N.toMaybe)
308326

309327
-- | Destroy the stream. It will release any internal resources.
310328
--

test/Main.purs

+46-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module Test.Main where
33
import Prelude
44

55
import Data.Either (Either(..))
6-
import Data.Maybe (Maybe(..), fromJust, isNothing, isJust)
6+
import Data.Maybe (Maybe(..), fromJust, isJust, isNothing)
77
import Effect (Effect)
88
import Effect.Console (log)
9+
import Effect.Exception (error)
910
import Node.Buffer as Buffer
1011
import Node.Encoding (Encoding(..))
11-
import Node.Stream (Duplex, Readable, Writable, onDataString, end, writeString, pipe, onDataEither, onData, setEncoding, setDefaultEncoding, read, onReadable, readString)
12+
import Node.Stream (Duplex, Readable, Writable, destroyWithError, end, onData, onDataEither, onDataString, onError, onReadable, pipe, read, readString, setDefaultEncoding, setEncoding, writeString)
1213
import Partial.Unsafe (unsafePartial)
1314
import Test.Assert (assert, assert')
1415

@@ -39,6 +40,12 @@ main = do
3940
log "test pipe"
4041
_ <- testPipe
4142

43+
log "test write"
44+
testWrite
45+
46+
log "test end"
47+
testEnd
48+
4249
log "test manual reads"
4350
testReads
4451

@@ -129,7 +136,7 @@ testPipe = do
129136
_ <- unzip `pipe` sOut
130137

131138
writeString sIn UTF8 testString \_ -> do
132-
end sIn do
139+
end sIn \_ -> do
133140
onDataString sOut UTF8 \str -> do
134141
assertEqual str testString
135142

@@ -140,3 +147,39 @@ foreign import createGunzip :: Effect Duplex
140147

141148
-- | Create a PassThrough stream, which simply writes its input to its output.
142149
foreign import passThrough :: Effect Duplex
150+
151+
testWrite :: Effect Unit
152+
testWrite = do
153+
hasError
154+
noError
155+
where
156+
hasError = do
157+
w1 <- writableStreamBuffer
158+
_ <- onError w1 (const $ pure unit)
159+
void $ end w1 $ const $ pure unit
160+
void $ writeString w1 UTF8 "msg" \err -> do
161+
assert' "writeString - should have error" $ isJust err
162+
163+
noError = do
164+
w1 <- writableStreamBuffer
165+
void $ writeString w1 UTF8 "msg1" \err -> do
166+
assert' "writeString - should have no error" $ isNothing err
167+
void $ end w1 (const $ pure unit)
168+
169+
testEnd :: Effect Unit
170+
testEnd = do
171+
hasError
172+
noError
173+
where
174+
hasError = do
175+
w1 <- writableStreamBuffer
176+
_ <- onError w1 (const $ pure unit)
177+
void $ writeString w1 UTF8 "msg" \_ -> do
178+
_ <- destroyWithError w1 $ error "Problem"
179+
end w1 \err -> do
180+
assert' "end - should have error" $ isJust err
181+
182+
noError = do
183+
w1 <- writableStreamBuffer
184+
end w1 \err -> do
185+
assert' "end - should have no error" $ isNothing err

0 commit comments

Comments
 (0)