Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit 183aeb4

Browse files
authored
Merge pull request #5 from jamesdbrock/features
Improvements
2 parents d84ec46 + c219d05 commit 183aeb4

File tree

5 files changed

+59
-38
lines changed

5 files changed

+59
-38
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
# Changelog
2+
3+
Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
4+
5+
## [Unreleased]
6+
7+
Breaking changes:
8+
9+
New features:
10+
11+
Bugfixes:
12+
13+
Other improvements:
14+
15+
# [Unreleased]
16+
17+
Breaking changes:
18+
19+
* Delete `writableClose` and add `end`.
20+
21+
New features:
22+
23+
* Add `toStringUTF8` and `fromStringUTF8`.
24+
25+
Bugfixes:
26+
27+
* Bugfix `onceError`.
128

229
# v2.0.0
330

src/Node/Stream/Aff.purs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
-- | Open __process streams__ with
77
-- | [__Node.Process__](https://pursuit.purescript.org/packages/purescript-node-process/docs/Node.Process).
88
-- |
9-
-- | Read and write __`String`__s with the `toString` and `fromString` functions in
10-
-- | [__Node.Buffer__](https://pursuit.purescript.org/packages/purescript-node-buffer/docs/Node.Buffer#t:MutableBuffer).
11-
-- |
129
-- | All __I/O errors__ will be thrown through the `Aff` `MonadError` class
1310
-- | instance.
1411
-- |
@@ -81,7 +78,9 @@ module Node.Stream.Aff
8178
, readAll
8279
, readN
8380
, write
84-
, writableClose
81+
, end
82+
, toStringUTF8
83+
, fromStringUTF8
8584
)
8685
where
8786

@@ -95,14 +94,16 @@ import Data.Either (Either(..))
9594
import Data.Maybe (Maybe(..))
9695
import Data.Tuple (Tuple(..))
9796
import Effect (Effect, untilE)
98-
import Effect.Aff (effectCanceler, makeAff)
97+
import Effect.Aff (effectCanceler, makeAff, nonCanceler)
9998
import Effect.Aff.Class (class MonadAff, liftAff)
99+
import Effect.Class (class MonadEffect, liftEffect)
100100
import Effect.Exception (catchException)
101101
import Node.Buffer (Buffer)
102102
import Node.Buffer as Buffer
103+
import Node.Encoding as Encoding
103104
import Node.Stream (Readable, Writable)
104105
import Node.Stream as Stream
105-
import Node.Stream.Aff.Internal (onceDrain, onceEnd, onceError, onceReadable, readable, writeStreamClose)
106+
import Node.Stream.Aff.Internal (onceDrain, onceEnd, onceError, onceReadable, readable)
106107

107108

108109
-- | Wait until there is some data available from the stream, then read it.
@@ -375,21 +376,29 @@ write w bs = liftAff <<< makeAff $ \res -> do
375376
removeError
376377
join $ liftST $ ST.Ref.read removeDrain
377378

378-
-- | Close a `Writable` file stream.
379+
-- | Signal that no more data will be written to the `Writable`. Will complete
380+
-- | after all data is written and flushed.
381+
-- |
382+
-- | When the `Writable` is an [__fs.WriteStream__](https://nodejs.org/api/fs.html#class-fswritestream)
383+
-- | then this will close the file descriptor because
379384
-- |
380-
-- | Will complete after the file stream is closed.
381-
writableClose
385+
-- | > “If `autoClose` is set to true (default behavior) on `'error'`
386+
-- | > or `'finish'` the file descriptor will be closed automatically.”
387+
end
382388
:: forall m w
383389
. MonadAff m
384390
=> Writable w
385391
-> m Unit
386-
writableClose w = liftAff <<< makeAff $ \res -> do
387-
388-
removeError <- onceError w $ res <<< Left
389-
390-
writeStreamClose w do
391-
removeError
392-
res (Right unit)
393-
394-
pure $ effectCanceler do
395-
removeError
392+
end w = liftAff <<< makeAff $ \res -> do
393+
Stream.end w $ case _ of
394+
Nothing -> res (Right unit)
395+
Just err -> res (Left err)
396+
pure $ nonCanceler
397+
398+
-- | Concatenate an `Array` of UTF-8 encoded `Buffer`s into a `String`.
399+
toStringUTF8 :: forall m. MonadEffect m => Array Buffer -> m String
400+
toStringUTF8 bs = liftEffect $ Buffer.toString Encoding.UTF8 =<< Buffer.concat bs
401+
402+
-- | Encode a `String` as an `Array` containing one UTF-8 encoded `Buffer`.
403+
fromStringUTF8 :: forall m. MonadEffect m => String -> m (Array Buffer)
404+
fromStringUTF8 s = liftEffect $ map pure $ Buffer.fromString s Encoding.UTF8

src/Node/Stream/Internal.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@ export const onceDrain = s => f => () => {
1414
}
1515

1616
export const onceError = s => f => () => {
17-
s.once('error', f);
17+
s.once('error', error => f(error)());
1818
return () => {s.removeListener('error', f);};
1919
}
2020

2121
export const readable = s => () => {
2222
return s.readable;
2323
}
24-
25-
export const writeStreamClose = s => cb => () => {
26-
return s.close(cb);
27-
}

src/Node/Stream/Internal.purs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ module Node.Stream.Aff.Internal
77
, onceError
88
, onceReadable
99
, readable
10-
, writeStreamClose
1110
)
1211
where
1312

@@ -65,18 +64,8 @@ foreign import onceError
6564
-- | property of a stream.
6665
-- |
6766
-- | > Is true if it is safe to call `readable.read()`, which means the stream
68-
-- | > has not been destroyed or emitted 'error' or 'end'.
67+
-- | > has not been destroyed or emitted `'error'` or `'end'`.
6968
foreign import readable
7069
:: forall r
7170
. Readable r
7271
-> Effect Boolean
73-
74-
-- | The [`writeStream.close([callback])`](https://nodejs.org/api/fs.html#writestreamclosecallback)
75-
-- | function.
76-
-- |
77-
-- | Accepts a callback that will be executed when the writeStream has closed.
78-
foreign import writeStreamClose
79-
:: forall w
80-
. Writable w
81-
-> Effect Unit
82-
-> Effect Unit

test/Main.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Node.Buffer (Buffer, concat)
2121
import Node.Buffer as Buffer
2222
import Node.Encoding (Encoding(..))
2323
import Node.FS.Stream (createReadStream, createWriteStream)
24-
import Node.Stream.Aff (readAll, readN, readSome, writableClose, write)
24+
import Node.Stream.Aff (end, readAll, readN, readSome, write)
2525
import Partial.Unsafe (unsafePartial)
2626
import Test.Spec (describe, it)
2727
import Test.Spec.Assertions (expectError, shouldEqual)
@@ -62,7 +62,7 @@ main = unsafePartial $ do
6262
outfile <- liftEffect $ createWriteStream outfilename
6363
b <- liftEffect $ Buffer.fromString "test" UTF8
6464
write outfile [b]
65-
writableClose outfile
65+
end outfile
6666
expectError $ write outfile [b]
6767

6868
pure (pure unit)

0 commit comments

Comments
 (0)