Skip to content

Commit 99ac74c

Browse files
committed
Merge pull request #4 from purescript-node/change-types
Remove the last parameter to Stream
2 parents f7c37c1 + ca16b93 commit 99ac74c

File tree

12 files changed

+347
-69
lines changed

12 files changed

+347
-69
lines changed

.jshintrc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"bitwise": true,
3+
"eqeqeq": true,
4+
"forin": true,
5+
"freeze": true,
6+
"funcscope": true,
7+
"futurehostile": true,
8+
"globalstrict": true,
9+
"latedef": true,
10+
"maxparams": 1,
11+
"noarg": true,
12+
"nocomma": true,
13+
"nonew": true,
14+
"notypeof": true,
15+
"singleGroups": true,
16+
"undef": true,
17+
"unused": true,
18+
"eqnull": true
19+
}
20+

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
sudo: false
3+
node_js:
4+
- 4.2
5+
- 5.2
6+
env:
7+
- PATH=$HOME/purescript:$PATH
8+
install:
9+
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
10+
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
11+
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
12+
- chmod a+x $HOME/purescript
13+
- npm install
14+
script:
15+
- npm run build

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
A wrapper for Node's Stream API
44

55
- [Module Documentation](docs/)
6-
- [Example](test/Main.purs)
6+
- [Example](example/Gzip.purs)

bower.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
"url": "git://github.com/purescript-node/purescript-node-streams.git"
1515
},
1616
"devDependencies": {
17-
"purescript-console": "^0.1.0"
17+
"purescript-console": "^0.1.0",
18+
"purescript-assert": "~0.1.1"
1819
},
1920
"dependencies": {
2021
"purescript-eff": "~0.1.1",
2122
"purescript-node-buffer": "~0.2.0",
22-
"purescript-prelude": "~0.1.2"
23+
"purescript-prelude": "~0.1.2",
24+
"purescript-either": "~0.2.3",
25+
"purescript-exceptions": "~0.3.3"
2326
}
2427
}

docs/Node/Stream.md

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This module provides a low-level wrapper for the Node Stream API.
55
#### `Stream`
66

77
``` purescript
8-
data Stream :: # * -> # ! -> * -> *
8+
data Stream :: # * -> # ! -> *
99
```
1010

1111
A stream.
@@ -14,7 +14,6 @@ The type arguments track, in order:
1414

1515
- Whether reading and/or writing from/to the stream are allowed.
1616
- Effects associated with reading/writing from/to this stream.
17-
- The type of chunks which will be read from/written to this stream (`String` or `Buffer`).
1817

1918
#### `Read`
2019

@@ -56,122 +55,152 @@ type Duplex = Stream (read :: Read, write :: Write)
5655

5756
A duplex (readable _and_ writable stream)
5857

59-
#### `setEncoding`
58+
#### `onData`
6059

6160
``` purescript
62-
setEncoding :: forall w eff. Readable w eff String -> Encoding -> Eff eff Unit
61+
onData :: forall w eff. Readable w (err :: EXCEPTION | eff) -> (Buffer -> Eff (err :: EXCEPTION | eff) Unit) -> Eff (err :: EXCEPTION | eff) Unit
6362
```
6463

65-
Set the encoding used to read chunks from the stream.
64+
Listen for `data` events, returning data in a Buffer. Note that this will fail
65+
if `setEncoding` has been called on the stream.
6666

67-
#### `onData`
67+
#### `onDataString`
68+
69+
``` purescript
70+
onDataString :: forall w eff. Readable w (err :: EXCEPTION | eff) -> Encoding -> (String -> Eff (err :: EXCEPTION | eff) Unit) -> Eff (err :: EXCEPTION | eff) Unit
71+
```
72+
73+
Listen for `data` events, returning data in a String, which will be
74+
decoded using the given encoding. Note that this will fail if `setEncoding`
75+
has been called on the stream.
76+
77+
#### `onDataEither`
6878

6979
``` purescript
70-
onData :: forall w eff a. Readable w eff a -> (a -> Eff eff Unit) -> Eff eff Unit
80+
onDataEither :: forall w eff. Readable w eff -> (Either String Buffer -> Eff eff Unit) -> Eff eff Unit
7181
```
7282

73-
Listen for `data` events.
83+
Listen for `data` events, returning data in an `Either String Buffer`. This
84+
function is provided for the (hopefully rare) case that `setEncoding` has
85+
been called on the stream.
86+
87+
#### `setEncoding`
88+
89+
``` purescript
90+
setEncoding :: forall w eff. Readable w eff -> Encoding -> Eff eff Unit
91+
```
92+
93+
Set the encoding used to read chunks as strings from the stream. This
94+
function may be useful when you are passing a readable stream to some other
95+
JavaScript library, which already expects an encoding to be set.
96+
97+
Where possible, you should try to use `onDataString` instead of this
98+
function.
7499

75100
#### `onEnd`
76101

77102
``` purescript
78-
onEnd :: forall w eff a. Readable w eff a -> Eff eff Unit -> Eff eff Unit
103+
onEnd :: forall w eff. Readable w eff -> Eff eff Unit -> Eff eff Unit
79104
```
80105

81106
Listen for `end` events.
82107

83108
#### `onClose`
84109

85110
``` purescript
86-
onClose :: forall w eff a. Readable w eff a -> Eff eff Unit -> Eff eff Unit
111+
onClose :: forall w eff. Readable w eff -> Eff eff Unit -> Eff eff Unit
87112
```
88113

89114
Listen for `close` events.
90115

91116
#### `onError`
92117

93118
``` purescript
94-
onError :: forall w eff a. Readable w eff a -> Eff eff Unit -> Eff eff Unit
119+
onError :: forall w eff. Readable w eff -> Eff eff Unit -> Eff eff Unit
95120
```
96121

97122
Listen for `error` events.
98123

99124
#### `resume`
100125

101126
``` purescript
102-
resume :: forall w eff a. Readable w eff a -> Eff eff Unit
127+
resume :: forall w eff. Readable w eff -> Eff eff Unit
103128
```
104129

105130
Resume reading from the stream.
106131

107132
#### `pause`
108133

109134
``` purescript
110-
pause :: forall w eff a. Readable w eff a -> Eff eff Unit
135+
pause :: forall w eff. Readable w eff -> Eff eff Unit
111136
```
112137

113138
Pause reading from the stream.
114139

115140
#### `isPaused`
116141

117142
``` purescript
118-
isPaused :: forall w eff a. Readable w eff a -> Eff eff Boolean
143+
isPaused :: forall w eff. Readable w eff -> Eff eff Boolean
119144
```
120145

121146
Check whether or not a stream is paused for reading.
122147

123148
#### `pipe`
124149

125150
``` purescript
126-
pipe :: forall r w eff a. Readable w eff a -> Writable r eff a -> Eff eff (Writable r eff a)
151+
pipe :: forall r w eff. Readable w eff -> Writable r eff -> Eff eff (Writable r eff)
127152
```
128153

129154
Read chunks from a readable stream and write them to a writable stream.
130155

131156
#### `write`
132157

133158
``` purescript
134-
write :: forall r eff a. Writable r eff String -> a -> Eff eff Unit -> Eff eff Boolean
159+
write :: forall r eff. Writable r eff -> Buffer -> Eff eff Unit -> Eff eff Boolean
135160
```
136161

137-
Write a chunk to a writable stream.
162+
Write a Buffer to a writable stream.
138163

139164
#### `writeString`
140165

141166
``` purescript
142-
writeString :: forall r eff. Writable r eff String -> Encoding -> String -> Eff eff Unit -> Eff eff Boolean
167+
writeString :: forall r eff. Writable r eff -> Encoding -> String -> Eff eff Unit -> Eff eff Boolean
143168
```
144169

145170
Write a string in the specified encoding to a writable stream.
146171

147172
#### `cork`
148173

149174
``` purescript
150-
cork :: forall r eff a. Writable r eff a -> Eff eff Unit
175+
cork :: forall r eff. Writable r eff -> Eff eff Unit
151176
```
152177

153178
Force buffering of writes.
154179

155180
#### `uncork`
156181

157182
``` purescript
158-
uncork :: forall r eff a. Writable r eff a -> Eff eff Unit
183+
uncork :: forall r eff. Writable r eff -> Eff eff Unit
159184
```
160185

161186
Flush buffered data.
162187

163188
#### `setDefaultEncoding`
164189

165190
``` purescript
166-
setDefaultEncoding :: forall r eff. Writable r eff String -> Encoding -> Eff eff Unit
191+
setDefaultEncoding :: forall r eff. Writable r eff -> Encoding -> Eff eff Unit
167192
```
168193

169-
Set the default encoding used to write chunks to the stream.
194+
Set the default encoding used to write strings to the stream. This function
195+
is useful when you are passing a writable stream to some other JavaScript
196+
library, which already expects a default encoding to be set. It has no
197+
effect on the behaviour of the `writeString` function (because that
198+
function ensures that the encoding is always supplied explicitly).
170199

171200
#### `end`
172201

173202
``` purescript
174-
end :: forall r eff a. Writable r eff a -> Eff eff Unit -> Eff eff Unit
203+
end :: forall r eff. Writable r eff -> Eff eff Unit -> Eff eff Unit
175204
```
176205

177206
End writing data to the stream.

example/Gzip.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* global exports */
2+
/* global require */
3+
"use strict";
4+
5+
// module Gzip
6+
7+
exports.gzip = require('zlib').createGzip;
8+
exports.stdout = process.stdout;
9+
exports.stdin = process.stdin;

example/Gzip.purs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Gzip where
2+
3+
import Prelude
4+
5+
import Node.Stream
6+
7+
import Control.Monad.Eff
8+
import Control.Monad.Eff.Console
9+
10+
foreign import data GZIP :: !
11+
12+
foreign import gzip :: forall eff. Eff (gzip :: GZIP | eff) (Duplex (gzip :: GZIP | eff))
13+
foreign import stdin :: forall eff. Readable () (console :: CONSOLE | eff)
14+
foreign import stdout :: forall eff. Writable () (console :: CONSOLE | eff)
15+
16+
main = do
17+
z <- gzip
18+
stdin `pipe` z
19+
z `pipe` stdout

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"postinstall": "pulp dep install",
5+
"build": "jshint src && pulp build && rimraf docs && pulp docs"
6+
},
7+
"devDependencies": {
8+
"jshint": "^2.8.0",
9+
"pulp": "^4.0.2",
10+
"rimraf": "^2.4.1",
11+
"stream-buffers": "^3.0.0"
12+
}
13+
}

src/Node/Stream.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* global exports */
2+
/* global Buffer */
13
"use strict";
24

35
// module Node.Stream
@@ -10,12 +12,27 @@ exports.setEncodingImpl = function(s) {
1012
};
1113
};
1214

13-
exports.onData = function(s) {
14-
return function(f) {
15-
return function() {
16-
s.on('data', function(chunk) {
17-
f(chunk)();
18-
});
15+
exports.onDataEitherImpl = function(left){
16+
return function(right){
17+
return function(s) {
18+
return function(f) {
19+
return function() {
20+
s.on('data', function(chunk) {
21+
if (chunk instanceof Buffer) {
22+
f(right(chunk))();
23+
}
24+
else if (typeof chunk === "string") {
25+
f(left(chunk))();
26+
}
27+
else {
28+
throw new Error(
29+
"Node.Stream.onDataEitherImpl: Unrecognised" +
30+
"chunk type; expected String or Buffer, got:" +
31+
chunk);
32+
}
33+
});
34+
};
35+
};
1936
};
2037
};
2138
};

0 commit comments

Comments
 (0)