Skip to content

Commit d879dbc

Browse files
committed
Merge pull request #6 from felixSchl/feature/read-size
Expose the `size` parameter to `Stream#read`
2 parents 92bff3f + bf02b06 commit d879dbc

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

src/Node/Stream.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
// module Node.Stream
66

7+
exports.undefined = undefined;
8+
79
exports.setEncodingImpl = function(s) {
810
return function(enc) {
911
return function() {
@@ -105,13 +107,15 @@ exports.readImpl = function(readChunk) {
105107
return function(Nothing) {
106108
return function(Just) {
107109
return function(r) {
108-
return function() {
109-
const v = r.read();
110-
if (v === null) {
111-
return Nothing;
112-
} else {
113-
return Just(readChunk(v));
114-
}
110+
return function(s) {
111+
return function() {
112+
const v = r.read(s);
113+
if (v === null) {
114+
return Nothing;
115+
} else {
116+
return Just(readChunk(v));
117+
}
118+
};
115119
};
116120
};
117121
};

src/Node/Stream.purs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module Node.Stream
3333
import Prelude
3434

3535
import Control.Bind ((<=<))
36-
import Data.Maybe (Maybe(..), maybe)
36+
import Data.Maybe (Maybe(..), maybe, fromMaybe)
3737
import Data.Either (Either(..))
3838
import Node.Encoding
3939
import Node.Buffer (Buffer())
@@ -66,6 +66,8 @@ type Writable r = Stream (write :: Write | r)
6666
-- | A duplex (readable _and_ writable stream)
6767
type Duplex = Stream (read :: Read, write :: Write)
6868

69+
foreign import undefined :: forall a. a
70+
6971
foreign import data Chunk :: *
7072
readChunk :: Chunk -> Either String Buffer
7173
readChunk = readChunkImpl Left Right
@@ -84,26 +86,26 @@ onData r cb =
8486
Right buf ->
8587
pure buf
8688

87-
read :: forall w eff. Readable w (err :: EXCEPTION | eff) -> Eff (err :: EXCEPTION | eff) (Maybe Buffer)
88-
read r = do
89-
v <- readEither r
89+
read :: forall w eff. Readable w (err :: EXCEPTION | eff) -> Maybe Int -> Eff (err :: EXCEPTION | eff) (Maybe Buffer)
90+
read r size = do
91+
v <- readEither r size
9092
case v of
9193
Nothing -> pure Nothing
9294
Just (Left _) -> throw "Stream encoding should not be set"
9395
Just (Right b) -> pure (Just b)
9496

95-
readString :: forall w eff. Readable w (err :: EXCEPTION | eff) -> Encoding -> Eff (err :: EXCEPTION | eff) (Maybe String)
96-
readString r enc = do
97-
v <- readEither r
97+
readString :: forall w eff. Readable w (err :: EXCEPTION | eff) -> Maybe Int -> Encoding -> Eff (err :: EXCEPTION | eff) (Maybe String)
98+
readString r size enc = do
99+
v <- readEither r size
98100
case v of
99101
Nothing -> pure Nothing
100102
Just (Left _) -> throw "Stream encoding should not be set"
101103
Just (Right buf) -> Just <$> (unsafeInterleaveEff $ Buffer.toString enc buf)
102104

103-
readEither :: forall w eff. Readable w eff -> Eff eff (Maybe (Either String Buffer))
104-
readEither = readImpl readChunk Nothing Just
105+
readEither :: forall w eff. Readable w eff -> Maybe Int -> Eff eff (Maybe (Either String Buffer))
106+
readEither r size = readImpl readChunk Nothing Just r (fromMaybe undefined size)
105107

106-
foreign import readImpl :: forall r eff. (Chunk -> Either String Buffer) -> (forall a. Maybe a) -> (forall a. a -> Maybe a) -> Readable r eff -> Eff eff (Maybe (Either String Buffer))
108+
foreign import readImpl :: forall r eff. (Chunk -> Either String Buffer) -> (forall a. Maybe a) -> (forall a. a -> Maybe a) -> Readable r eff -> Int -> Eff eff (Maybe (Either String Buffer))
107109

108110
-- | Listen for `data` events, returning data in a String, which will be
109111
-- | decoded using the given encoding. Note that this will fail if `setEncoding`

test/Main.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ testReads = do
5454
where
5555
testReadString = do
5656
sIn <- passThrough
57-
v <- readString sIn UTF8
57+
v <- readString sIn Nothing UTF8
5858
assert (isNothing v)
5959

6060
onReadable sIn do
61-
str <- readString sIn UTF8
61+
str <- readString sIn Nothing UTF8
6262
assert (isJust str)
6363
assertEqual (fromJust str) testString
6464
return unit
@@ -68,11 +68,11 @@ testReads = do
6868

6969
testReadBuf = do
7070
sIn <- passThrough
71-
v <- read sIn
71+
v <- read sIn Nothing
7272
assert (isNothing v)
7373

7474
onReadable sIn do
75-
buf <- read sIn
75+
buf <- read sIn Nothing
7676
assert (isJust buf)
7777
assertEqual <$> (Buffer.toString UTF8 (fromJust buf))
7878
<*> pure testString

0 commit comments

Comments
 (0)