through2 0.2.2 → 0.4.1
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/.travis.yml +11 -6
- package/README.md +38 -11
- package/package.json +8 -6
- package/{test.js → test/basic-test.js} +23 -3
- package/test/bench.js +42 -0
- package/test/sauce.js +38 -0
- package/test/test.js +9 -0
- package/through2.js +53 -17
package/.travis.yml
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
language: node_js
|
|
2
2
|
node_js:
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
- '0.8'
|
|
4
|
+
- '0.10'
|
|
5
|
+
- '0.11'
|
|
6
6
|
branches:
|
|
7
7
|
only:
|
|
8
|
-
|
|
8
|
+
- master
|
|
9
9
|
notifications:
|
|
10
10
|
email:
|
|
11
|
-
|
|
12
|
-
script: npm test
|
|
11
|
+
- rod@vagg.org
|
|
12
|
+
script: npm test
|
|
13
|
+
env:
|
|
14
|
+
global:
|
|
15
|
+
- SAUCE_USER=through2-sauce
|
|
16
|
+
- secure: A8KyygIlklZOcTwTGhZHpkOu7Cn4tI1QQasg75CTSjudczGzkuh1pG2RDIkWqGcrWv9o5CjI6fFYYx0JvQu8o5PO+ZAtV1Z0AlnM965vE8kAx0XEC7giAV323bibfnHwyQu74vIhbOfdes7E4/cYerV2kAjK0T375D4DYEpDuvw=
|
|
17
|
+
|
package/README.md
CHANGED
|
@@ -1,22 +1,41 @@
|
|
|
1
1
|
# through2
|
|
2
2
|
|
|
3
|
+
<!--
|
|
4
|
+
|
|
5
|
+
soon ...
|
|
6
|
+
|
|
3
7
|
[](http://travis-ci.org/rvagg/through2)
|
|
4
8
|
|
|
9
|
+
[](https://travis-ci.org/rvagg/through2)
|
|
10
|
+
|
|
11
|
+
-->
|
|
12
|
+
|
|
5
13
|
[](https://nodei.co/npm/through2/)
|
|
6
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
|
|
17
|
+
|
|
18
|
+
[](https://david-dm.org/rvagg/through2/)
|
|
19
|
+
[](https://david-dm.org/rvagg/through2#info=devDependencies/)
|
|
20
|
+
-->
|
|
21
|
+
|
|
7
22
|
**A tiny wrapper around Node streams.Transform (Streams2) to avoid explicit subclassing noise**
|
|
8
23
|
|
|
9
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) { ... })`.
|
|
10
25
|
|
|
11
26
|
```js
|
|
12
27
|
fs.createReadStream('ex.txt')
|
|
13
|
-
|
|
28
|
+
.pipe(through2(function (chunk, enc, callback) {
|
|
29
|
+
|
|
14
30
|
for (var i = 0; i < chunk.length; i++)
|
|
15
31
|
if (chunk[i] == 97)
|
|
16
32
|
chunk[i] = 122 // swap 'a' for 'z'
|
|
33
|
+
|
|
17
34
|
this.push(chunk)
|
|
35
|
+
|
|
18
36
|
callback()
|
|
19
|
-
|
|
37
|
+
|
|
38
|
+
}))
|
|
20
39
|
.pipe(fs.createWriteStream('out.txt'))
|
|
21
40
|
```
|
|
22
41
|
|
|
@@ -24,16 +43,21 @@ Or object streams:
|
|
|
24
43
|
|
|
25
44
|
```js
|
|
26
45
|
var all = []
|
|
46
|
+
|
|
27
47
|
fs.createReadStream('data.csv')
|
|
28
48
|
.pipe(csv2())
|
|
29
|
-
.pipe(through2(
|
|
30
|
-
|
|
49
|
+
.pipe(through2.obj(function (chunk, enc, callback) {
|
|
50
|
+
|
|
51
|
+
var data = {
|
|
31
52
|
name : chunk[0]
|
|
32
53
|
, address : chunk[3]
|
|
33
54
|
, phone : chunk[10]
|
|
34
55
|
}
|
|
56
|
+
|
|
35
57
|
this.push(data)
|
|
58
|
+
|
|
36
59
|
callback()
|
|
60
|
+
|
|
37
61
|
}))
|
|
38
62
|
.on('data', function (data) {
|
|
39
63
|
all.push(data)
|
|
@@ -43,6 +67,8 @@ fs.createReadStream('data.csv')
|
|
|
43
67
|
})
|
|
44
68
|
```
|
|
45
69
|
|
|
70
|
+
Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.
|
|
71
|
+
|
|
46
72
|
## API
|
|
47
73
|
|
|
48
74
|
<b><code>through2([ options, ] [ transformFunction ] [, flushFunction ])</code></b>
|
|
@@ -51,16 +77,18 @@ Consult the **[stream.Transform](http://nodejs.org/docs/latest/api/stream.html#s
|
|
|
51
77
|
|
|
52
78
|
### options
|
|
53
79
|
|
|
54
|
-
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.
|
|
80
|
+
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()`).
|
|
55
81
|
|
|
56
82
|
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:
|
|
57
83
|
|
|
58
84
|
```js
|
|
59
85
|
fs.createReadStream('/tmp/important.dat')
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
86
|
+
.pipe(through2({ objectMode: true, allowHalfOpen: false }, function (chunk, enc, cb) {
|
|
87
|
+
|
|
88
|
+
this.push(new Buffer('wut?'))
|
|
89
|
+
cb()
|
|
90
|
+
|
|
91
|
+
})
|
|
64
92
|
.pipe(fs.createWriteStream('/tmp/wut.txt'))
|
|
65
93
|
```
|
|
66
94
|
|
|
@@ -96,9 +124,8 @@ var converter = new FToC()
|
|
|
96
124
|
var converter = FToC()
|
|
97
125
|
// Or specify/override options when you instantiate, if you prefer:
|
|
98
126
|
var converter = FToC({objectMode: true})
|
|
99
|
-
|
|
100
127
|
```
|
|
101
128
|
|
|
102
129
|
## License
|
|
103
130
|
|
|
104
|
-
**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.
|
|
131
|
+
**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.
|
|
3
|
+
"version": "0.4.1",
|
|
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.0.
|
|
23
|
+
"readable-stream": "~1.0.17",
|
|
23
24
|
"xtend": "~2.1.1"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
|
-
"tape": "~
|
|
27
|
-
"bl": "~0.
|
|
28
|
-
"stream-spigot": "~
|
|
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
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const test = require('tape')
|
|
2
|
-
, through2 = require('
|
|
2
|
+
, through2 = require('../')
|
|
3
3
|
, crypto = require('crypto')
|
|
4
4
|
, bl = require('bl')
|
|
5
5
|
, spigot = require('stream-spigot')
|
|
@@ -78,6 +78,26 @@ test('object through', function (t) {
|
|
|
78
78
|
th2.end()
|
|
79
79
|
})
|
|
80
80
|
|
|
81
|
+
test('object through with through2.obj', function (t) {
|
|
82
|
+
t.plan(3)
|
|
83
|
+
|
|
84
|
+
var th2 = through2.obj(function (chunk, enc, callback) {
|
|
85
|
+
this.push({ out: chunk.in + 1 })
|
|
86
|
+
callback()
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
var e = 0
|
|
90
|
+
th2.on('data', function (o) {
|
|
91
|
+
t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
|
|
92
|
+
e++
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
th2.write({ in: 101 })
|
|
96
|
+
th2.write({ in: 202 })
|
|
97
|
+
th2.write({ in: -100 })
|
|
98
|
+
th2.end()
|
|
99
|
+
})
|
|
100
|
+
|
|
81
101
|
test('flushing through', function (t) {
|
|
82
102
|
var th2 = through2(function (chunk, enc, callback) {
|
|
83
103
|
if (!this._i)
|
|
@@ -214,12 +234,12 @@ test('pipeable object through ctor', function (t) {
|
|
|
214
234
|
t.deepEqual(o, { temp: expect.shift(), unit: 'C' }, 'got transformed object')
|
|
215
235
|
})
|
|
216
236
|
|
|
217
|
-
spigot([
|
|
237
|
+
spigot({objectMode: true}, [
|
|
218
238
|
{temp: -2.2, unit: 'F'},
|
|
219
239
|
{temp: -40, unit: 'F'},
|
|
220
240
|
{temp: 212, unit: 'F'},
|
|
221
241
|
{temp: 22, unit: 'C'}
|
|
222
|
-
]
|
|
242
|
+
]).pipe(th2)
|
|
223
243
|
})
|
|
224
244
|
|
|
225
245
|
test('object through ctor override', function (t) {
|
package/test/bench.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*
|
|
2
|
+
on rvagg@fletcher:
|
|
3
|
+
@0.2.3: Ran 36719 iterations in 10000 ms
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const through2 = require('../')
|
|
7
|
+
, bl = require('bl')
|
|
8
|
+
, crypto = require('crypto')
|
|
9
|
+
, assert = require('assert')
|
|
10
|
+
|
|
11
|
+
function run (callback) {
|
|
12
|
+
var bufs = Array.apply(null, Array(10)).map(function () { return crypto.randomBytes(32) })
|
|
13
|
+
, stream = through2(function (chunk, env, callback) {
|
|
14
|
+
callback(null, chunk.toString('hex'))
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
stream.pipe(bl(function (err, data) {
|
|
18
|
+
assert(!err)
|
|
19
|
+
assert.equal(data.toString(), Buffer.concat(bufs).toString('hex'))
|
|
20
|
+
callback()
|
|
21
|
+
}))
|
|
22
|
+
|
|
23
|
+
bufs.forEach(function (b) {
|
|
24
|
+
stream.write(b)
|
|
25
|
+
})
|
|
26
|
+
stream.end()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var count = 0
|
|
30
|
+
, start = Date.now()
|
|
31
|
+
|
|
32
|
+
;(function exec () {
|
|
33
|
+
count++
|
|
34
|
+
run(function () {
|
|
35
|
+
if (Date.now() - start < 1000 * 10) {
|
|
36
|
+
return setImmediate(exec)
|
|
37
|
+
}
|
|
38
|
+
console.log('Ran', count, 'iterations in', Date.now() - start, 'ms')
|
|
39
|
+
})
|
|
40
|
+
}())
|
|
41
|
+
|
|
42
|
+
console.log('Running for ~10s')
|
package/test/sauce.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const user = process.env.SAUCE_USER
|
|
4
|
+
, key = process.env.SAUCE_KEY
|
|
5
|
+
, path = require('path')
|
|
6
|
+
, brtapsauce = require('brtapsauce')
|
|
7
|
+
, testFile = path.join(__dirname, 'basic-test.js')
|
|
8
|
+
|
|
9
|
+
, capabilities = [
|
|
10
|
+
{ browserName: 'chrome' , platform: 'Windows XP', version: '' }
|
|
11
|
+
, { browserName: 'firefox' , platform: 'Windows 8' , version: '' }
|
|
12
|
+
, { browserName: 'firefox' , platform: 'Windows XP', version: '4' }
|
|
13
|
+
, { browserName: 'internet explorer' , platform: 'Windows 8' , version: '10' }
|
|
14
|
+
, { browserName: 'internet explorer' , platform: 'Windows 7' , version: '9' }
|
|
15
|
+
, { browserName: 'internet explorer' , platform: 'Windows 7' , version: '8' }
|
|
16
|
+
, { browserName: 'internet explorer' , platform: 'Windows XP', version: '7' }
|
|
17
|
+
, { browserName: 'internet explorer' , platform: 'Windows XP', version: '6' }
|
|
18
|
+
, { browserName: 'safari' , platform: 'Windows 7' , version: '5' }
|
|
19
|
+
, { browserName: 'safari' , platform: 'OS X 10.8' , version: '6' }
|
|
20
|
+
, { browserName: 'opera' , platform: 'Windows 7' , version: '' }
|
|
21
|
+
, { browserName: 'opera' , platform: 'Windows 7' , version: '11' }
|
|
22
|
+
, { browserName: 'ipad' , platform: 'OS X 10.8' , version: '6' }
|
|
23
|
+
, { browserName: 'android' , platform: 'Linux' , version: '4.0', 'device-type': 'tablet' }
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
if (!user)
|
|
27
|
+
throw new Error('Must set a SAUCE_USER env var')
|
|
28
|
+
if (!key)
|
|
29
|
+
throw new Error('Must set a SAUCE_KEY env var')
|
|
30
|
+
|
|
31
|
+
brtapsauce({
|
|
32
|
+
name : 'Traversty'
|
|
33
|
+
, user : user
|
|
34
|
+
, key : key
|
|
35
|
+
, brsrc : testFile
|
|
36
|
+
, capabilities : capabilities
|
|
37
|
+
, options : { timeout: 60 * 6 }
|
|
38
|
+
})
|
package/test/test.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require('./basic-test')
|
|
2
|
+
|
|
3
|
+
if (!process.env.SAUCE_KEY || !process.env.SAUCE_USER)
|
|
4
|
+
return console.log('SAUCE_KEY and/or SAUCE_USER not set, not running sauce tests')
|
|
5
|
+
|
|
6
|
+
if (!/v0\.10/.test(process.version))
|
|
7
|
+
return console.log('Not Node v0.10.x, not running sauce tests')
|
|
8
|
+
|
|
9
|
+
require('./sauce.js')
|
package/through2.js
CHANGED
|
@@ -1,26 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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 (
|
|
17
|
-
|
|
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 (
|
|
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
|
|
42
|
-
|
|
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
|
+
})
|