Skip to content

Commit 0bb57f0

Browse files
committed
Remove the last type parameter of Stream
It should be possible to read and write both String and Buffer at any time; the Node API offers Strings for convenience only, so there is no need for the last type parameter of Stream.
1 parent f7c37c1 commit 0bb57f0

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

src/Node/Stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exports.setEncodingImpl = function(s) {
1010
};
1111
};
1212

13-
exports.onData = function(s) {
13+
exports.onDataImpl = function(s) {
1414
return function(f) {
1515
return function() {
1616
s.on('data', function(chunk) {

src/Node/Stream.purs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,19 @@ module Node.Stream
2727
import Prelude
2828

2929
import Node.Encoding
30+
import Node.Buffer (Buffer())
31+
import Node.Buffer as Buffer
3032

3133
import Control.Monad.Eff
34+
import Control.Monad.Eff.Unsafe (unsafeInterleaveEff)
3235

3336
-- | A stream.
3437
-- |
3538
-- | The type arguments track, in order:
3639
-- |
3740
-- | - Whether reading and/or writing from/to the stream are allowed.
3841
-- | - Effects associated with reading/writing from/to this stream.
39-
-- | - The type of chunks which will be read from/written to this stream (`String` or `Buffer`).
40-
foreign import data Stream :: # * -> # ! -> * -> *
42+
foreign import data Stream :: # * -> # ! -> *
4143

4244
-- | A phantom type associated with _readable streams_.
4345
data Read
@@ -54,56 +56,66 @@ type Writable r = Stream (write :: Write | r)
5456
-- | A duplex (readable _and_ writable stream)
5557
type Duplex = Stream (read :: Read, write :: Write)
5658

57-
foreign import setEncodingImpl :: forall w eff. Readable w eff String -> String -> Eff eff Unit
59+
foreign import setEncodingImpl :: forall w eff. Readable w eff -> String -> Eff eff Unit
5860

59-
-- | Set the encoding used to read chunks from the stream.
60-
setEncoding :: forall w eff. Readable w eff String -> Encoding -> Eff eff Unit
61+
-- | Set the encoding used to read chunks as strings from the stream.
62+
setEncoding :: forall w eff. Readable w eff -> Encoding -> Eff eff Unit
6163
setEncoding r enc = setEncodingImpl r (show enc)
6264

63-
-- | Listen for `data` events.
64-
foreign import onData :: forall w eff a. Readable w eff a -> (a -> Eff eff Unit) -> Eff eff Unit
65+
foreign import onDataImpl :: forall w eff a. Readable w eff -> (a -> Eff eff Unit) -> Eff eff Unit
66+
67+
-- | Listen for `data` events, returning data in a Buffer.
68+
onData :: forall w eff. Readable w eff -> (Buffer -> Eff eff Unit) -> Eff eff Unit
69+
onData = onDataImpl
70+
71+
-- | Listen for `data` events, returning data in a String, decoded with the
72+
-- | given encoding.
73+
onDataString :: forall w eff. Readable w eff -> Encoding -> (String -> Eff eff Unit) -> Eff eff Unit
74+
onDataString r enc cb = onData r $ \buf -> do
75+
str <- unsafeInterleaveEff (Buffer.toString enc buf)
76+
cb str
6577

6678
-- | Listen for `end` events.
67-
foreign import onEnd :: forall w eff a. Readable w eff a -> Eff eff Unit -> Eff eff Unit
79+
foreign import onEnd :: forall w eff. Readable w eff -> Eff eff Unit -> Eff eff Unit
6880

6981
-- | Listen for `close` events.
70-
foreign import onClose :: forall w eff a. Readable w eff a -> Eff eff Unit -> Eff eff Unit
82+
foreign import onClose :: forall w eff. Readable w eff -> Eff eff Unit -> Eff eff Unit
7183

7284
-- | Listen for `error` events.
73-
foreign import onError :: forall w eff a. Readable w eff a -> Eff eff Unit -> Eff eff Unit
85+
foreign import onError :: forall w eff. Readable w eff -> Eff eff Unit -> Eff eff Unit
7486

7587
-- | Resume reading from the stream.
76-
foreign import resume :: forall w eff a. Readable w eff a -> Eff eff Unit
88+
foreign import resume :: forall w eff. Readable w eff -> Eff eff Unit
7789

7890
-- | Pause reading from the stream.
79-
foreign import pause :: forall w eff a. Readable w eff a -> Eff eff Unit
91+
foreign import pause :: forall w eff. Readable w eff -> Eff eff Unit
8092

8193
-- | Check whether or not a stream is paused for reading.
82-
foreign import isPaused :: forall w eff a. Readable w eff a -> Eff eff Boolean
94+
foreign import isPaused :: forall w eff. Readable w eff -> Eff eff Boolean
8395

8496
-- | Read chunks from a readable stream and write them to a writable stream.
85-
foreign import pipe :: forall r w eff a. Readable w eff a -> Writable r eff a -> Eff eff (Writable r eff a)
97+
foreign import pipe :: forall r w eff. Readable w eff -> Writable r eff -> Eff eff (Writable r eff)
8698

87-
-- | Write a chunk to a writable stream.
88-
foreign import write :: forall r eff a. Writable r eff String -> a -> Eff eff Unit -> Eff eff Boolean
99+
-- | Write a Buffer to a writable stream.
100+
foreign import write :: forall r eff. Writable r eff -> Buffer -> Eff eff Unit -> Eff eff Boolean
89101

90-
foreign import writeStringImpl :: forall r eff. Writable r eff String -> String -> String -> Eff eff Unit -> Eff eff Boolean
102+
foreign import writeStringImpl :: forall r eff. Writable r eff -> String -> String -> Eff eff Unit -> Eff eff Boolean
91103

92104
-- | Write a string in the specified encoding to a writable stream.
93-
writeString :: forall r eff. Writable r eff String -> Encoding -> String -> Eff eff Unit -> Eff eff Boolean
105+
writeString :: forall r eff. Writable r eff -> Encoding -> String -> Eff eff Unit -> Eff eff Boolean
94106
writeString w enc = writeStringImpl w (show enc)
95107

96108
-- | Force buffering of writes.
97-
foreign import cork :: forall r eff a. Writable r eff a -> Eff eff Unit
109+
foreign import cork :: forall r eff. Writable r eff -> Eff eff Unit
98110

99111
-- | Flush buffered data.
100-
foreign import uncork :: forall r eff a. Writable r eff a -> Eff eff Unit
112+
foreign import uncork :: forall r eff. Writable r eff -> Eff eff Unit
101113

102-
foreign import setDefaultEncodingImpl :: forall r eff. Writable r eff String -> String -> Eff eff Unit
114+
foreign import setDefaultEncodingImpl :: forall r eff. Writable r eff -> String -> Eff eff Unit
103115

104-
-- | Set the default encoding used to write chunks to the stream.
105-
setDefaultEncoding :: forall r eff. Writable r eff String -> Encoding -> Eff eff Unit
116+
-- | Set the default encoding used to write chunks to the stream. This
117+
setDefaultEncoding :: forall r eff. Writable r eff -> Encoding -> Eff eff Unit
106118
setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc)
107119

108120
-- | End writing data to the stream.
109-
foreign import end :: forall r eff a. Writable r eff a -> Eff eff Unit -> Eff eff Unit
121+
foreign import end :: forall r eff. Writable r eff -> Eff eff Unit -> Eff eff Unit

test/Main.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import Control.Monad.Eff.Console
99

1010
foreign import data GZIP :: !
1111

12-
foreign import stdin :: forall eff. Readable () (console :: CONSOLE | eff) String
12+
foreign import stdin :: forall eff. Readable () (console :: CONSOLE | eff)
1313

14-
foreign import stdout :: forall eff. Writable () (console :: CONSOLE | eff) String
14+
foreign import stdout :: forall eff. Writable () (console :: CONSOLE | eff)
1515

16-
foreign import gzip :: forall eff a. Eff (gzip :: GZIP | eff) (Duplex (gzip :: GZIP | eff) a)
16+
foreign import gzip :: forall eff. Eff (gzip :: GZIP | eff) (Duplex (gzip :: GZIP | eff))
1717

1818
main = do
1919
z <- gzip

0 commit comments

Comments
 (0)