through2 0.5.1 → 0.6.3
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/README.md +23 -14
- package/package.json +6 -7
- package/through2.js +22 -4
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ soon ...
|
|
|
10
10
|
|
|
11
11
|
-->
|
|
12
12
|
|
|
13
|
-
[](https://nodei.co/npm/through2/)
|
|
14
14
|
|
|
15
15
|
<!--
|
|
16
16
|
not happy with these, we need to peg to readable-stream@1.0.x so it'll always report out-of-date
|
|
@@ -23,12 +23,11 @@ not happy with these, we need to peg to readable-stream@1.0.x so it'll always re
|
|
|
23
23
|
|
|
24
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) { ... })`.
|
|
25
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 [
|
|
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 see the article **[Why I don't use Node's core 'stream' module](http://r.va.gg/2014/06/why-i-dont-use-nodes-core-stream-module.html)**.
|
|
27
27
|
|
|
28
28
|
```js
|
|
29
29
|
fs.createReadStream('ex.txt')
|
|
30
30
|
.pipe(through2(function (chunk, enc, callback) {
|
|
31
|
-
|
|
32
31
|
for (var i = 0; i < chunk.length; i++)
|
|
33
32
|
if (chunk[i] == 97)
|
|
34
33
|
chunk[i] = 122 // swap 'a' for 'z'
|
|
@@ -36,7 +35,6 @@ fs.createReadStream('ex.txt')
|
|
|
36
35
|
this.push(chunk)
|
|
37
36
|
|
|
38
37
|
callback()
|
|
39
|
-
|
|
40
38
|
}))
|
|
41
39
|
.pipe(fs.createWriteStream('out.txt'))
|
|
42
40
|
```
|
|
@@ -49,17 +47,14 @@ var all = []
|
|
|
49
47
|
fs.createReadStream('data.csv')
|
|
50
48
|
.pipe(csv2())
|
|
51
49
|
.pipe(through2.obj(function (chunk, enc, callback) {
|
|
52
|
-
|
|
53
50
|
var data = {
|
|
54
51
|
name : chunk[0]
|
|
55
52
|
, address : chunk[3]
|
|
56
53
|
, phone : chunk[10]
|
|
57
54
|
}
|
|
58
|
-
|
|
59
55
|
this.push(data)
|
|
60
56
|
|
|
61
57
|
callback()
|
|
62
|
-
|
|
63
58
|
}))
|
|
64
59
|
.on('data', function (data) {
|
|
65
60
|
all.push(data)
|
|
@@ -85,12 +80,12 @@ The `options` argument is first, unlike standard convention, because if I'm pass
|
|
|
85
80
|
|
|
86
81
|
```js
|
|
87
82
|
fs.createReadStream('/tmp/important.dat')
|
|
88
|
-
.pipe(through2({ objectMode: true, allowHalfOpen: false },
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
83
|
+
.pipe(through2({ objectMode: true, allowHalfOpen: false },
|
|
84
|
+
function (chunk, enc, cb) {
|
|
85
|
+
cb(null, 'wut?') // note we can use the second argument on the callback
|
|
86
|
+
// to provide data as an alternative to this.push('wut?')
|
|
87
|
+
}
|
|
88
|
+
)
|
|
94
89
|
.pipe(fs.createWriteStream('/tmp/wut.txt'))
|
|
95
90
|
```
|
|
96
91
|
|
|
@@ -100,12 +95,26 @@ The `transformFunction` must have the following signature: `function (chunk, enc
|
|
|
100
95
|
|
|
101
96
|
To queue a new chunk, call `this.push(chunk)`—this can be called as many times as required before the `callback()` if you have multiple pieces to send on.
|
|
102
97
|
|
|
103
|
-
|
|
98
|
+
Alternatively, you may use `callback(err, chunk)` as shorthand for emitting a single chunk or an error.
|
|
99
|
+
|
|
100
|
+
If you **do not provide a `transformFunction`** then you will get a simple pass-through stream.
|
|
104
101
|
|
|
105
102
|
### flushFunction
|
|
106
103
|
|
|
107
104
|
The optional `flushFunction` is provided as the last argument (2nd or 3rd, depending on whether you've supplied options) is called just prior to the stream ending. Can be used to finish up any processing that may be in progress.
|
|
108
105
|
|
|
106
|
+
```js
|
|
107
|
+
fs.createReadStream('/tmp/important.dat')
|
|
108
|
+
.pipe(through2(
|
|
109
|
+
function (chunk, enc, cb) { cb(null, chunk) }, // transform is a noop
|
|
110
|
+
function (cb) { // flush function
|
|
111
|
+
this.push('tacking on an extra buffer to the end');
|
|
112
|
+
cb();
|
|
113
|
+
}
|
|
114
|
+
))
|
|
115
|
+
.pipe(fs.createWriteStream('/tmp/wut.txt'));
|
|
116
|
+
```
|
|
117
|
+
|
|
109
118
|
<b><code>through2.ctor([ options, ] transformFunction[, flushFunction ])</code></b>
|
|
110
119
|
|
|
111
120
|
Instead of returning a `stream.Transform` instance, `through2.ctor()` returns a **constructor** for a custom Transform. This is useful when you want to use the same transform logic in multiple instances.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "through2",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"description": "A tiny wrapper around Node streams2 Transform to avoid explicit subclassing noise",
|
|
5
5
|
"main": "through2.js",
|
|
6
6
|
"scripts": {
|
|
@@ -20,13 +20,12 @@
|
|
|
20
20
|
"author": "Rod Vagg <r@va.gg> (https://github.com/rvagg)",
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"readable-stream": "
|
|
24
|
-
"xtend": "
|
|
23
|
+
"readable-stream": ">=1.0.33-1 <1.1.0-0",
|
|
24
|
+
"xtend": ">=4.0.0 <4.1.0-0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"brtapsauce": "~0.2.2"
|
|
27
|
+
"bl": ">=0.9.0 <0.10.0-0",
|
|
28
|
+
"stream-spigot": ">=3.0.4 <3.1.0-0",
|
|
29
|
+
"tape": ">=2.14.0 <2.15.0-0"
|
|
31
30
|
}
|
|
32
31
|
}
|
package/through2.js
CHANGED
|
@@ -2,6 +2,24 @@ var Transform = require('readable-stream/transform')
|
|
|
2
2
|
, inherits = require('util').inherits
|
|
3
3
|
, xtend = require('xtend')
|
|
4
4
|
|
|
5
|
+
function DestroyableTransform(opts) {
|
|
6
|
+
Transform.call(this, opts)
|
|
7
|
+
this._destroyed = false
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
inherits(DestroyableTransform, Transform)
|
|
11
|
+
|
|
12
|
+
DestroyableTransform.prototype.destroy = function(err) {
|
|
13
|
+
if (this._destroyed) return
|
|
14
|
+
this._destroyed = true
|
|
15
|
+
|
|
16
|
+
var self = this
|
|
17
|
+
process.nextTick(function() {
|
|
18
|
+
if (err)
|
|
19
|
+
self.emit('error', err)
|
|
20
|
+
self.emit('close')
|
|
21
|
+
})
|
|
22
|
+
}
|
|
5
23
|
|
|
6
24
|
// a noop _transform function
|
|
7
25
|
function noop (chunk, enc, callback) {
|
|
@@ -32,7 +50,7 @@ function through2 (construct) {
|
|
|
32
50
|
|
|
33
51
|
// main export, just make me a transform stream!
|
|
34
52
|
module.exports = through2(function (options, transform, flush) {
|
|
35
|
-
var t2 = new
|
|
53
|
+
var t2 = new DestroyableTransform(options)
|
|
36
54
|
|
|
37
55
|
t2._transform = transform
|
|
38
56
|
|
|
@@ -52,10 +70,10 @@ module.exports.ctor = through2(function (options, transform, flush) {
|
|
|
52
70
|
|
|
53
71
|
this.options = xtend(options, override)
|
|
54
72
|
|
|
55
|
-
|
|
73
|
+
DestroyableTransform.call(this, this.options)
|
|
56
74
|
}
|
|
57
75
|
|
|
58
|
-
inherits(Through2,
|
|
76
|
+
inherits(Through2, DestroyableTransform)
|
|
59
77
|
|
|
60
78
|
Through2.prototype._transform = transform
|
|
61
79
|
|
|
@@ -67,7 +85,7 @@ module.exports.ctor = through2(function (options, transform, flush) {
|
|
|
67
85
|
|
|
68
86
|
|
|
69
87
|
module.exports.obj = through2(function (options, transform, flush) {
|
|
70
|
-
var t2 = new
|
|
88
|
+
var t2 = new DestroyableTransform(xtend({ objectMode: true, highWaterMark: 16 }, options))
|
|
71
89
|
|
|
72
90
|
t2._transform = transform
|
|
73
91
|
|