Skip to content

Commit 73f61d6

Browse files
committed
Merge pull request #7 from purescript-node/1.0-updates
Updates for 1.0 core libraries
2 parents f1b4688 + fd05928 commit 73f61d6

File tree

6 files changed

+196
-66
lines changed

6 files changed

+196
-66
lines changed

bower.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
"url": "git://github.com/purescript-node/purescript-node-streams.git"
1313
},
1414
"devDependencies": {
15-
"purescript-console": "^0.1.0",
16-
"purescript-assert": "~0.1.1"
15+
"purescript-console": "^1.0.0",
16+
"purescript-assert": "^1.0.0",
17+
"purescript-partial": "^1.1.2"
1718
},
1819
"dependencies": {
19-
"purescript-eff": "~0.1.1",
20-
"purescript-node-buffer": "~0.2.0",
21-
"purescript-prelude": "~0.1.2",
22-
"purescript-either": "~0.2.3",
23-
"purescript-exceptions": "~0.3.3"
20+
"purescript-eff": "^1.0.0",
21+
"purescript-node-buffer": "^1.0.0",
22+
"purescript-prelude": "^1.0.0",
23+
"purescript-either": "^1.0.0",
24+
"purescript-exceptions": "^1.0.0"
2425
}
2526
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"devDependencies": {
88
"jshint": "^2.8.0",
9-
"pulp": "^8.2.1",
9+
"pulp": "^9.0.0",
1010
"rimraf": "^2.4.1",
1111
"stream-buffers": "^3.0.0"
1212
}

src/Node/Stream.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
/* global Buffer */
33
"use strict";
44

5-
// module Node.Stream
6-
75
exports.undefined = undefined;
86

97
exports.setEncodingImpl = function(s) {
@@ -109,7 +107,7 @@ exports.readImpl = function(readChunk) {
109107
return function(r) {
110108
return function(s) {
111109
return function() {
112-
const v = r.read(s);
110+
var v = r.read(s);
113111
if (v === null) {
114112
return Nothing;
115113
} else {

src/Node/Stream.purs

Lines changed: 133 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ module Node.Stream
3333
import Prelude
3434

3535
import Control.Bind ((<=<))
36-
import Data.Maybe (Maybe(..), maybe, fromMaybe)
36+
import Control.Monad.Eff (Eff)
37+
import Control.Monad.Eff.Exception (throw, EXCEPTION(), Error())
38+
import Control.Monad.Eff.Unsafe (unsafeInterleaveEff)
3739
import Data.Either (Either(..))
38-
import Node.Encoding
40+
import Data.Maybe (Maybe(..), fromMaybe)
3941
import Node.Buffer (Buffer())
4042
import Node.Buffer as Buffer
41-
42-
import Control.Monad.Eff
43-
import Control.Monad.Eff.Exception (throw, EXCEPTION(), Error())
44-
import Control.Monad.Eff.Unsafe (unsafeInterleaveEff)
43+
import Node.Encoding (Encoding)
4544

4645
-- | A stream.
4746
-- |
@@ -69,13 +68,23 @@ type Duplex = Stream (read :: Read, write :: Write)
6968
foreign import undefined :: forall a. a
7069

7170
foreign import data Chunk :: *
71+
72+
foreign import readChunkImpl
73+
:: (forall l r. l -> Either l r)
74+
-> (forall l r. r -> Either l r)
75+
-> Chunk
76+
-> Either String Buffer
77+
7278
readChunk :: Chunk -> Either String Buffer
7379
readChunk = readChunkImpl Left Right
74-
foreign import readChunkImpl :: (forall l r. l -> Either l r) -> (forall l r. r -> Either l r) -> Chunk -> Either String Buffer
7580

7681
-- | Listen for `data` events, returning data in a Buffer. Note that this will fail
7782
-- | if `setEncoding` has been called on the stream.
78-
onData :: forall w eff. Readable w (err :: EXCEPTION | eff) -> (Buffer -> Eff (err :: EXCEPTION | eff) Unit) -> Eff (err :: EXCEPTION | eff) Unit
83+
onData
84+
:: forall w eff
85+
. Readable w (err :: EXCEPTION | eff)
86+
-> (Buffer -> Eff (err :: EXCEPTION | eff) Unit)
87+
-> Eff (err :: EXCEPTION | eff) Unit
7988
onData r cb =
8089
onDataEither r (cb <=< fromEither)
8190
where
@@ -86,63 +95,121 @@ onData r cb =
8695
Right buf ->
8796
pure buf
8897

89-
read :: forall w eff. Readable w (err :: EXCEPTION | eff) -> Maybe Int -> Eff (err :: EXCEPTION | eff) (Maybe Buffer)
98+
read
99+
:: forall w eff
100+
. Readable w (err :: EXCEPTION | eff)
101+
-> Maybe Int
102+
-> Eff (err :: EXCEPTION | eff) (Maybe Buffer)
90103
read r size = do
91104
v <- readEither r size
92105
case v of
93-
Nothing -> pure Nothing
94-
Just (Left _) -> throw "Stream encoding should not be set"
95-
Just (Right b) -> pure (Just b)
96-
97-
readString :: forall w eff. Readable w (err :: EXCEPTION | eff) -> Maybe Int -> Encoding -> Eff (err :: EXCEPTION | eff) (Maybe String)
106+
Nothing -> pure Nothing
107+
Just (Left _) -> throw "Stream encoding should not be set"
108+
Just (Right b) -> pure (Just b)
109+
110+
readString
111+
:: forall w eff
112+
. Readable w (err :: EXCEPTION | eff)
113+
-> Maybe Int
114+
-> Encoding
115+
-> Eff (err :: EXCEPTION | eff) (Maybe String)
98116
readString r size enc = do
99117
v <- readEither r size
100118
case v of
101119
Nothing -> pure Nothing
102120
Just (Left _) -> throw "Stream encoding should not be set"
103121
Just (Right buf) -> Just <$> (unsafeInterleaveEff $ Buffer.toString enc buf)
104122

105-
readEither :: forall w eff. Readable w eff -> Maybe Int -> Eff eff (Maybe (Either String Buffer))
123+
readEither
124+
:: forall w eff
125+
. Readable w eff
126+
-> Maybe Int
127+
-> Eff eff (Maybe (Either String Buffer))
106128
readEither r size = readImpl readChunk Nothing Just r (fromMaybe undefined size)
107129

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))
130+
foreign import readImpl
131+
:: forall r eff
132+
. (Chunk -> Either String Buffer)
133+
-> (forall a. Maybe a)
134+
-> (forall a. a -> Maybe a)
135+
-> Readable r eff
136+
-> Int
137+
-> Eff eff (Maybe (Either String Buffer))
109138

110139
-- | Listen for `data` events, returning data in a String, which will be
111140
-- | decoded using the given encoding. Note that this will fail if `setEncoding`
112141
-- | has been called on the stream.
113-
onDataString :: forall w eff. Readable w (err :: EXCEPTION | eff) -> Encoding -> (String -> Eff (err :: EXCEPTION | eff) Unit) -> Eff (err :: EXCEPTION | eff) Unit
142+
onDataString
143+
:: forall w eff
144+
. Readable w (err :: EXCEPTION | eff)
145+
-> Encoding
146+
-> (String -> Eff (err :: EXCEPTION | eff) Unit)
147+
-> Eff (err :: EXCEPTION | eff) Unit
114148
onDataString r enc cb = onData r (cb <=< unsafeInterleaveEff <<< Buffer.toString enc)
115149

116150
-- | Listen for `data` events, returning data in an `Either String Buffer`. This
117151
-- | function is provided for the (hopefully rare) case that `setEncoding` has
118152
-- | been called on the stream.
119-
onDataEither :: forall r eff. Readable r (err :: EXCEPTION | eff) -> (Either String Buffer -> Eff (err :: EXCEPTION | eff) Unit) -> Eff (err :: EXCEPTION | eff) Unit
153+
onDataEither
154+
:: forall r eff
155+
. Readable r (err :: EXCEPTION | eff)
156+
-> (Either String Buffer -> Eff (err :: EXCEPTION | eff) Unit)
157+
-> Eff (err :: EXCEPTION | eff) Unit
120158
onDataEither r cb = onDataEitherImpl readChunk r cb
121159

122-
foreign import onDataEitherImpl :: forall r eff. (Chunk -> Either String Buffer) -> Readable r eff -> (Either String Buffer -> Eff eff Unit) -> Eff eff Unit
160+
foreign import onDataEitherImpl
161+
:: forall r eff
162+
. (Chunk -> Either String Buffer)
163+
-> Readable r eff
164+
-> (Either String Buffer -> Eff eff Unit)
165+
-> Eff eff Unit
123166

124-
foreign import setEncodingImpl :: forall w eff. Readable w eff -> String -> Eff eff Unit
167+
foreign import setEncodingImpl
168+
:: forall w eff
169+
. Readable w eff
170+
-> String
171+
-> Eff eff Unit
125172

126173
-- | Set the encoding used to read chunks as strings from the stream. This
127174
-- | function may be useful when you are passing a readable stream to some other
128175
-- | JavaScript library, which already expects an encoding to be set.
129176
-- |
130177
-- | Where possible, you should try to use `onDataString` instead of this
131178
-- | function.
132-
setEncoding :: forall w eff. Readable w eff -> Encoding -> Eff eff Unit
179+
setEncoding
180+
:: forall w eff
181+
. Readable w eff
182+
-> Encoding
183+
-> Eff eff Unit
133184
setEncoding r enc = setEncodingImpl r (show enc)
134185

135186
-- | Listen for `readable` events.
136-
foreign import onReadable :: forall w eff. Readable w eff -> Eff eff Unit -> Eff eff Unit
187+
foreign import onReadable
188+
:: forall w eff
189+
. Readable w eff
190+
-> Eff eff Unit
191+
-> Eff eff Unit
137192

138193
-- | Listen for `end` events.
139-
foreign import onEnd :: forall w eff. Readable w eff -> Eff eff Unit -> Eff eff Unit
194+
foreign import onEnd
195+
:: forall w eff
196+
. Readable w eff
197+
-> Eff eff Unit
198+
-> Eff eff Unit
140199

141200
-- | Listen for `close` events.
142-
foreign import onClose :: forall w eff. Readable w eff -> Eff eff Unit -> Eff eff Unit
201+
foreign import onClose
202+
:: forall w eff
203+
. Readable w eff
204+
-> Eff eff Unit
205+
-> Eff eff Unit
143206

144207
-- | Listen for `error` events.
145-
foreign import onError :: forall w eff. Readable w eff -> (Error -> Eff eff Unit) -> Eff eff Unit
208+
foreign import onError
209+
:: forall w eff
210+
. Readable w eff
211+
-> (Error -> Eff eff Unit)
212+
-> Eff eff Unit
146213

147214
-- | Resume reading from the stream.
148215
foreign import resume :: forall w eff. Readable w eff -> Eff eff Unit
@@ -154,15 +221,36 @@ foreign import pause :: forall w eff. Readable w eff -> Eff eff Unit
154221
foreign import isPaused :: forall w eff. Readable w eff -> Eff eff Boolean
155222

156223
-- | Read chunks from a readable stream and write them to a writable stream.
157-
foreign import pipe :: forall r w eff. Readable w eff -> Writable r eff -> Eff eff (Writable r eff)
224+
foreign import pipe
225+
:: forall r w eff
226+
. Readable w eff
227+
-> Writable r eff
228+
-> Eff eff (Writable r eff)
158229

159230
-- | Write a Buffer to a writable stream.
160-
foreign import write :: forall r eff. Writable r eff -> Buffer -> Eff eff Unit -> Eff eff Boolean
161-
162-
foreign import writeStringImpl :: forall r eff. Writable r eff -> String -> String -> Eff eff Unit -> Eff eff Boolean
231+
foreign import write
232+
:: forall r eff
233+
. Writable r eff
234+
-> Buffer
235+
-> Eff eff Unit
236+
-> Eff eff Boolean
237+
238+
foreign import writeStringImpl
239+
:: forall r eff
240+
. Writable r eff
241+
-> String
242+
-> String
243+
-> Eff eff Unit
244+
-> Eff eff Boolean
163245

164246
-- | Write a string in the specified encoding to a writable stream.
165-
writeString :: forall r eff. Writable r eff -> Encoding -> String -> Eff eff Unit -> Eff eff Boolean
247+
writeString
248+
:: forall r eff
249+
. Writable r eff
250+
-> Encoding
251+
-> String
252+
-> Eff eff Unit
253+
-> Eff eff Boolean
166254
writeString w enc = writeStringImpl w (show enc)
167255

168256
-- | Force buffering of writes.
@@ -171,16 +259,27 @@ foreign import cork :: forall r eff. Writable r eff -> Eff eff Unit
171259
-- | Flush buffered data.
172260
foreign import uncork :: forall r eff. Writable r eff -> Eff eff Unit
173261

174-
foreign import setDefaultEncodingImpl :: forall r eff. Writable r eff -> String -> Eff eff Unit
262+
foreign import setDefaultEncodingImpl
263+
:: forall r eff
264+
. Writable r eff
265+
-> String
266+
-> Eff eff Unit
175267

176268
-- | Set the default encoding used to write strings to the stream. This function
177269
-- | is useful when you are passing a writable stream to some other JavaScript
178270
-- | library, which already expects a default encoding to be set. It has no
179271
-- | effect on the behaviour of the `writeString` function (because that
180272
-- | function ensures that the encoding is always supplied explicitly).
181-
setDefaultEncoding :: forall r eff. Writable r eff -> Encoding -> Eff eff Unit
273+
setDefaultEncoding
274+
:: forall r eff
275+
. Writable r eff
276+
-> Encoding
277+
-> Eff eff Unit
182278
setDefaultEncoding r enc = setDefaultEncodingImpl r (show enc)
183279

184280
-- | End writing data to the stream.
185-
foreign import end :: forall r eff. Writable r eff -> Eff eff Unit -> Eff eff Unit
186-
281+
foreign import end
282+
:: forall r eff
283+
. Writable r eff
284+
-> Eff eff Unit
285+
-> Eff eff Unit

test/Main.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
"use strict";
22

3-
// module Test.Main
4-
5-
63
exports.writableStreamBuffer = function() {
74
var W = require('stream-buffers').WritableStreamBuffer;
85
return new W;

0 commit comments

Comments
 (0)