through2 0.2.3 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.npmignore CHANGED
@@ -1 +1,3 @@
1
- node_modules
1
+ test
2
+ .jshintrc
3
+ .travis.yml
package/README.md CHANGED
@@ -1,25 +1,43 @@
1
1
  # through2
2
2
 
3
+ <!--
4
+
5
+ soon ...
6
+
3
7
  [![Build Status](https://secure.travis-ci.org/rvagg/through2.png)](http://travis-ci.org/rvagg/through2)
4
8
 
5
- [![NPM](https://nodei.co/npm/through2.png?compact=true)](https://nodei.co/npm/through2/)
9
+ [![Build Status](https://saucelabs.com/browser-matrix/through2-sauce.svg)](https://travis-ci.org/rvagg/through2)
10
+
11
+ -->
12
+
13
+ [![NPM](https://nodei.co/npm/through2.png?compact=true)](https://nodei.co/npm/through2/)
14
+
15
+ <!--
16
+ not happy with these, we need to peg to readable-stream@1.0.x so it'll always report out-of-date
6
17
 
7
18
  [![david-dm](https://david-dm.org/rvagg/through2.png)](https://david-dm.org/rvagg/through2/)
8
19
  [![david-dm](https://david-dm.org/rvagg/through2/dev-status.png)](https://david-dm.org/rvagg/through2#info=devDependencies/)
20
+ -->
9
21
 
10
22
  **A tiny wrapper around Node streams.Transform (Streams2) to avoid explicit subclassing noise**
11
23
 
12
24
  Inspired by [Dominic Tarr](https://github.com/dominictarr)'s [through](https://github.com/dominictarr/through) in that it's so much easier to make a stream out of a function than it is to set up the prototype chain properly: `through(function (chunk) { ... })`.
13
25
 
26
+ Note: A **Streams3** version of through2 is available in npm with the tag `"1.0"` rather than `"latest"` so an `npm install through2` will get you the current Streams2 version (version number is 0.x.x). To use a Streams3 version use `npm install through2@1` to fetch the latest version 1.x.x. More information about Streams2 vs Streams3 and recommendations [here](http://www.nearform.com/nodecrunch/dont-use-nodes-core-stream-module).
27
+
14
28
  ```js
15
29
  fs.createReadStream('ex.txt')
16
- .pipe(through2(function (chunk, enc, callback) {
30
+ .pipe(through2(function (chunk, enc, callback) {
31
+
17
32
  for (var i = 0; i < chunk.length; i++)
18
33
  if (chunk[i] == 97)
19
34
  chunk[i] = 122 // swap 'a' for 'z'
35
+
20
36
  this.push(chunk)
37
+
21
38
  callback()
22
- }))
39
+
40
+ }))
23
41
  .pipe(fs.createWriteStream('out.txt'))
24
42
  ```
25
43
 
@@ -27,16 +45,21 @@ Or object streams:
27
45
 
28
46
  ```js
29
47
  var all = []
48
+
30
49
  fs.createReadStream('data.csv')
31
50
  .pipe(csv2())
32
- .pipe(through2({ objectMode: true }, function (chunk, enc, callback) {
33
- var data = {
51
+ .pipe(through2.obj(function (chunk, enc, callback) {
52
+
53
+ var data = {
34
54
  name : chunk[0]
35
55
  , address : chunk[3]
36
56
  , phone : chunk[10]
37
57
  }
58
+
38
59
  this.push(data)
60
+
39
61
  callback()
62
+
40
63
  }))
41
64
  .on('data', function (data) {
42
65
  all.push(data)
@@ -46,6 +69,8 @@ fs.createReadStream('data.csv')
46
69
  })
47
70
  ```
48
71
 
72
+ Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.
73
+
49
74
  ## API
50
75
 
51
76
  <b><code>through2([ options, ] [ transformFunction ] [, flushFunction ])</code></b>
@@ -54,16 +79,18 @@ Consult the **[stream.Transform](http://nodejs.org/docs/latest/api/stream.html#s
54
79
 
55
80
  ### options
56
81
 
57
- The options argument is optional and is passed straight through to `stream.Transform`. So you can use `objectMode:true` if you are processing non-binary streams.
82
+ The options argument is optional and is passed straight through to `stream.Transform`. So you can use `objectMode:true` if you are processing non-binary streams (or just use `through2.obj()`).
58
83
 
59
84
  The `options` argument is first, unlike standard convention, because if I'm passing in an anonymous function then I'd prefer for the options argument to not get lost at the end of the call:
60
85
 
61
86
  ```js
62
87
  fs.createReadStream('/tmp/important.dat')
63
- .pipe(through2({ objectMode: true, allowHalfOpen: false }, function (chunk, enc, cb) {
64
- this.push(new Buffer('wut?'))
65
- cb()
66
- })
88
+ .pipe(through2({ objectMode: true, allowHalfOpen: false }, function (chunk, enc, cb) {
89
+
90
+ this.push(new Buffer('wut?'))
91
+ cb()
92
+
93
+ })
67
94
  .pipe(fs.createWriteStream('/tmp/wut.txt'))
68
95
  ```
69
96
 
@@ -99,9 +126,15 @@ var converter = new FToC()
99
126
  var converter = FToC()
100
127
  // Or specify/override options when you instantiate, if you prefer:
101
128
  var converter = FToC({objectMode: true})
102
-
103
129
  ```
104
130
 
131
+ ## See Also
132
+
133
+ - [through2-map](https://github.com/brycebaril/through2-map) - Array.prototype.map analog for streams.
134
+ - [through2-filter](https://github.com/brycebaril/through2-filter) - Array.prototype.filter analog for streams.
135
+ - [through2-reduce](https://github.com/brycebaril/through2-reduce) - Array.prototype.reduce analog for streams.
136
+ - [through2-spy](https://github.com/brycebaril/through2-spy) - Wrapper for simple stream.PassThrough spies.
137
+
105
138
  ## License
106
139
 
107
- **through2** is Copyright (c) 2013 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
140
+ **through2** is Copyright (c) 2013 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "through2",
3
- "version": "0.2.3",
3
+ "version": "0.4.2",
4
4
  "description": "A tiny wrapper around Node streams2 Transform to avoid explicit subclassing noise",
5
5
  "main": "through2.js",
6
6
  "scripts": {
7
- "test": "node test.js"
7
+ "test": "node test/test.js",
8
+ "test-local": "brtapsauce-local test/basic-test.js"
8
9
  },
9
10
  "repository": {
10
11
  "type": "git",
@@ -19,12 +20,13 @@
19
20
  "author": "Rod Vagg <r@va.gg> (https://github.com/rvagg)",
20
21
  "license": "MIT",
21
22
  "dependencies": {
22
- "readable-stream": "~1.1.9",
23
+ "readable-stream": "~1.0.17",
23
24
  "xtend": "~2.1.1"
24
25
  },
25
26
  "devDependencies": {
26
- "tape": "~1.1.1",
27
- "bl": "~0.4.1",
28
- "stream-spigot": "~2.1.2"
27
+ "tape": "~2.3.0",
28
+ "bl": "~0.6.0",
29
+ "stream-spigot": "~3.0.1",
30
+ "brtapsauce": "~0.2.2"
29
31
  }
30
32
  }
package/through2.js CHANGED
@@ -1,26 +1,57 @@
1
- const Transform = require('stream').Transform || require('readable-stream/transform')
2
- , inherits = require('util').inherits
3
- , xtend = require('xtend')
1
+ var Transform = require('readable-stream/transform')
2
+ , inherits = require('util').inherits
3
+ , xtend = require('xtend')
4
4
 
5
+
6
+ // a noop _transform function
5
7
  function noop (chunk, enc, callback) {
6
8
  callback(null, chunk)
7
9
  }
8
10
 
9
- function ctor (options, transform, flush) {
10
- if (typeof options == 'function') {
11
- flush = transform
12
- transform = options
13
- options = {}
11
+
12
+ // create a new export function, used by both the main export and
13
+ // the .ctor export, contains common logic for dealing with arguments
14
+ function through2 (construct) {
15
+ return function (options, transform, flush) {
16
+ if (typeof options == 'function') {
17
+ flush = transform
18
+ transform = options
19
+ options = {}
20
+ }
21
+
22
+ if (typeof transform != 'function')
23
+ transform = noop
24
+
25
+ if (typeof flush != 'function')
26
+ flush = null
27
+
28
+ return construct(options, transform, flush)
14
29
  }
30
+ }
31
+
32
+
33
+ // main export, just make me a transform stream!
34
+ module.exports = through2(function (options, transform, flush) {
35
+ var t2 = new Transform(options)
36
+
37
+ t2._transform = transform
15
38
 
16
- if (typeof transform != 'function')
17
- transform = noop
39
+ if (flush)
40
+ t2._flush = flush
18
41
 
42
+ return t2
43
+ })
44
+
45
+
46
+ // make me a reusable prototype that I can `new`, or implicitly `new`
47
+ // with a constructor call
48
+ module.exports.ctor = through2(function (options, transform, flush) {
19
49
  function Through2 (override) {
20
50
  if (!(this instanceof Through2))
21
51
  return new Through2(override)
22
52
 
23
53
  this.options = xtend(options, override)
54
+
24
55
  Transform.call(this, this.options)
25
56
  }
26
57
 
@@ -28,15 +59,20 @@ function ctor (options, transform, flush) {
28
59
 
29
60
  Through2.prototype._transform = transform
30
61
 
31
- if (typeof flush == 'function')
62
+ if (flush)
32
63
  Through2.prototype._flush = flush
33
64
 
34
65
  return Through2
35
- }
66
+ })
36
67
 
37
- function make (options, transform, flush) {
38
- return ctor(options, transform, flush)()
39
- }
40
68
 
41
- module.exports = make
42
- module.exports.ctor = ctor
69
+ module.exports.obj = through2(function (options, transform, flush) {
70
+ var t2 = new Transform(xtend({ objectMode: true }, options))
71
+
72
+ t2._transform = transform
73
+
74
+ if (flush)
75
+ t2._flush = flush
76
+
77
+ return t2
78
+ })
package/.jshintrc DELETED
@@ -1,59 +0,0 @@
1
- {
2
- "predef": [ ]
3
- , "bitwise": false
4
- , "camelcase": false
5
- , "curly": false
6
- , "eqeqeq": false
7
- , "forin": false
8
- , "immed": false
9
- , "latedef": false
10
- , "noarg": true
11
- , "noempty": true
12
- , "nonew": true
13
- , "plusplus": false
14
- , "quotmark": true
15
- , "regexp": false
16
- , "undef": true
17
- , "unused": true
18
- , "strict": false
19
- , "trailing": true
20
- , "maxlen": 120
21
- , "asi": true
22
- , "boss": true
23
- , "debug": true
24
- , "eqnull": true
25
- , "esnext": true
26
- , "evil": true
27
- , "expr": true
28
- , "funcscope": false
29
- , "globalstrict": false
30
- , "iterator": false
31
- , "lastsemic": true
32
- , "laxbreak": true
33
- , "laxcomma": true
34
- , "loopfunc": true
35
- , "multistr": false
36
- , "onecase": false
37
- , "proto": false
38
- , "regexdash": false
39
- , "scripturl": true
40
- , "smarttabs": false
41
- , "shadow": false
42
- , "sub": true
43
- , "supernew": false
44
- , "validthis": true
45
- , "browser": true
46
- , "couch": false
47
- , "devel": false
48
- , "dojo": false
49
- , "mootools": false
50
- , "node": true
51
- , "nonstandard": true
52
- , "prototypejs": false
53
- , "rhino": false
54
- , "worker": true
55
- , "wsh": false
56
- , "nomen": false
57
- , "onevar": false
58
- , "passfail": false
59
- }
package/.travis.yml DELETED
@@ -1,12 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - "0.8"
4
- - "0.10"
5
- - "0.11"
6
- branches:
7
- only:
8
- - master
9
- notifications:
10
- email:
11
- - rod@vagg.org
12
- script: npm test
package/test.js DELETED
@@ -1,314 +0,0 @@
1
- const test = require('tape')
2
- , through2 = require('./')
3
- , crypto = require('crypto')
4
- , bl = require('bl')
5
- , spigot = require('stream-spigot')
6
-
7
- test('plain through', function (t) {
8
- var th2 = through2(function (chunk, enc, callback) {
9
- if (!this._i)
10
- this._i = 97 // 'a'
11
- else
12
- this._i++
13
- var b = new Buffer(chunk.length)
14
- for (var i = 0; i < chunk.length; i++)
15
- b[i] = this._i
16
- this.push(b)
17
- callback()
18
- })
19
-
20
- th2.pipe(bl(function (err, b) {
21
- var s = b.toString('ascii')
22
- t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
23
- t.end()
24
- }))
25
-
26
- th2.write(crypto.randomBytes(10))
27
- th2.write(crypto.randomBytes(5))
28
- th2.write(crypto.randomBytes(10))
29
- th2.end()
30
- })
31
-
32
- test('pipeable through', function (t) {
33
- var th2 = through2(function (chunk, enc, callback) {
34
- if (!this._i)
35
- this._i = 97 // 'a'
36
- else
37
- this._i++
38
- var b = new Buffer(chunk.length)
39
- for (var i = 0; i < chunk.length; i++)
40
- b[i] = this._i
41
- this.push(b)
42
- callback()
43
- })
44
-
45
- th2.pipe(bl(function (err, b) {
46
- var s = b.toString('ascii')
47
- // bl() acts like a proper streams2 stream and passes as much as it's
48
- // asked for, so we really only get one write with such a small amount
49
- // of data
50
- t.equal(s, 'aaaaaaaaaaaaaaaaaaaaaaaaa', 'got transformed string')
51
- t.end()
52
- }))
53
-
54
- var bufs = bl()
55
- bufs.append(crypto.randomBytes(10))
56
- bufs.append(crypto.randomBytes(5))
57
- bufs.append(crypto.randomBytes(10))
58
- bufs.pipe(th2)
59
- })
60
-
61
- test('object through', function (t) {
62
- t.plan(3)
63
-
64
- var th2 = through2({ objectMode: true}, function (chunk, enc, callback) {
65
- this.push({ out: chunk.in + 1 })
66
- callback()
67
- })
68
-
69
- var e = 0
70
- th2.on('data', function (o) {
71
- t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
72
- e++
73
- })
74
-
75
- th2.write({ in: 101 })
76
- th2.write({ in: 202 })
77
- th2.write({ in: -100 })
78
- th2.end()
79
- })
80
-
81
- test('flushing through', function (t) {
82
- var th2 = through2(function (chunk, enc, callback) {
83
- if (!this._i)
84
- this._i = 97 // 'a'
85
- else
86
- this._i++
87
- var b = new Buffer(chunk.length)
88
- for (var i = 0; i < chunk.length; i++)
89
- b[i] = this._i
90
- this.push(b)
91
- callback()
92
- }, function (callback) {
93
- this.push(new Buffer([ 101, 110, 100 ]))
94
- callback()
95
- })
96
-
97
- th2.pipe(bl(function (err, b) {
98
- var s = b.toString('ascii')
99
- t.equal(s, 'aaaaaaaaaabbbbbccccccccccend', 'got transformed string')
100
- t.end()
101
- }))
102
-
103
- th2.write(crypto.randomBytes(10))
104
- th2.write(crypto.randomBytes(5))
105
- th2.write(crypto.randomBytes(10))
106
- th2.end()
107
- })
108
-
109
- test('plain through ctor', function (t) {
110
- var Th2 = through2.ctor(function (chunk, enc, callback) {
111
- if (!this._i)
112
- this._i = 97 // 'a'
113
- else
114
- this._i++
115
- var b = new Buffer(chunk.length)
116
- for (var i = 0; i < chunk.length; i++)
117
- b[i] = this._i
118
- this.push(b)
119
- callback()
120
- })
121
-
122
- var th2 = new Th2()
123
-
124
- th2.pipe(bl(function (err, b) {
125
- var s = b.toString('ascii')
126
- t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
127
- t.end()
128
- }))
129
-
130
- th2.write(crypto.randomBytes(10))
131
- th2.write(crypto.randomBytes(5))
132
- th2.write(crypto.randomBytes(10))
133
- th2.end()
134
- })
135
-
136
- test('reuse through ctor', function (t) {
137
- t.plan(4)
138
-
139
- var Th2 = through2.ctor(function (chunk, enc, callback) {
140
- if (!this._i) {
141
- t.ok(1, 'did not contain previous instance data (this._i)')
142
- this._i = 97 // 'a'
143
- } else
144
- this._i++
145
- var b = new Buffer(chunk.length)
146
- for (var i = 0; i < chunk.length; i++)
147
- b[i] = this._i
148
- this.push(b)
149
- callback()
150
- })
151
-
152
- var th2 = Th2()
153
-
154
- th2.pipe(bl(function (err, b) {
155
- var s = b.toString('ascii')
156
- t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
157
-
158
- var newInstance = Th2()
159
- newInstance.pipe(bl(function (err, b) {
160
- var s = b.toString('ascii')
161
- t.equal('aaaaaaabbbbccccccc', s, 'got transformed string')
162
- }))
163
-
164
- newInstance.write(crypto.randomBytes(7))
165
- newInstance.write(crypto.randomBytes(4))
166
- newInstance.write(crypto.randomBytes(7))
167
- newInstance.end()
168
- }))
169
-
170
- th2.write(crypto.randomBytes(10))
171
- th2.write(crypto.randomBytes(5))
172
- th2.write(crypto.randomBytes(10))
173
- th2.end()
174
- })
175
-
176
- test('object through ctor', function (t) {
177
- t.plan(3)
178
-
179
- var Th2 = through2.ctor({ objectMode: true}, function (chunk, enc, callback) {
180
- this.push({ out: chunk.in + 1 })
181
- callback()
182
- })
183
-
184
- var th2 = new Th2()
185
-
186
- var e = 0
187
- th2.on('data', function (o) {
188
- t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
189
- e++
190
- })
191
-
192
- th2.write({ in: 101 })
193
- th2.write({ in: 202 })
194
- th2.write({ in: -100 })
195
- th2.end()
196
- })
197
-
198
- test('pipeable object through ctor', function (t) {
199
- t.plan(4)
200
-
201
- var Th2 = through2.ctor({ objectMode: true}, function (record, enc, callback) {
202
- if (record.temp != null && record.unit == 'F') {
203
- record.temp = ( ( record.temp - 32 ) * 5 ) / 9
204
- record.unit = 'C'
205
- }
206
- this.push(record)
207
- callback()
208
- })
209
-
210
- var th2 = Th2()
211
-
212
- var expect = [-19, -40, 100, 22]
213
- th2.on('data', function (o) {
214
- t.deepEqual(o, { temp: expect.shift(), unit: 'C' }, 'got transformed object')
215
- })
216
-
217
- spigot({objectMode: true}, [
218
- {temp: -2.2, unit: 'F'},
219
- {temp: -40, unit: 'F'},
220
- {temp: 212, unit: 'F'},
221
- {temp: 22, unit: 'C'}
222
- ]).pipe(th2)
223
- })
224
-
225
- test('object through ctor override', function (t) {
226
- t.plan(3)
227
-
228
- var Th2 = through2.ctor(function (chunk, enc, callback) {
229
- this.push({ out: chunk.in + 1 })
230
- callback()
231
- })
232
-
233
- var th2 = Th2({objectMode: true})
234
-
235
- var e = 0
236
- th2.on('data', function (o) {
237
- t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
238
- e++
239
- })
240
-
241
- th2.write({ in: 101 })
242
- th2.write({ in: 202 })
243
- th2.write({ in: -100 })
244
- th2.end()
245
- })
246
-
247
- test('object settings available in transform', function (t) {
248
- t.plan(6)
249
-
250
- var Th2 = through2.ctor({objectMode: true, peek: true}, function (chunk, enc, callback) {
251
- t.ok(this.options.peek, "reading options from inside _transform")
252
- this.push({ out: chunk.in + 1 })
253
- callback()
254
- })
255
-
256
- var th2 = Th2()
257
-
258
- var e = 0
259
- th2.on('data', function (o) {
260
- t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
261
- e++
262
- })
263
-
264
- th2.write({ in: 101 })
265
- th2.write({ in: 202 })
266
- th2.write({ in: -100 })
267
- th2.end()
268
- })
269
-
270
- test('object settings available in transform override', function (t) {
271
- t.plan(6)
272
-
273
- var Th2 = through2.ctor(function (chunk, enc, callback) {
274
- t.ok(this.options.peek, "reading options from inside _transform")
275
- this.push({ out: chunk.in + 1 })
276
- callback()
277
- })
278
-
279
- var th2 = Th2({objectMode: true, peek: true})
280
-
281
- var e = 0
282
- th2.on('data', function (o) {
283
- t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
284
- e++
285
- })
286
-
287
- th2.write({ in: 101 })
288
- th2.write({ in: 202 })
289
- th2.write({ in: -100 })
290
- th2.end()
291
- })
292
-
293
- test('object override extends options', function (t) {
294
- t.plan(6)
295
-
296
- var Th2 = through2.ctor({objectMode: true}, function (chunk, enc, callback) {
297
- t.ok(this.options.peek, "reading options from inside _transform")
298
- this.push({ out: chunk.in + 1 })
299
- callback()
300
- })
301
-
302
- var th2 = Th2({peek: true})
303
-
304
- var e = 0
305
- th2.on('data', function (o) {
306
- t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
307
- e++
308
- })
309
-
310
- th2.write({ in: 101 })
311
- th2.write({ in: 202 })
312
- th2.write({ in: -100 })
313
- th2.end()
314
- })