UNPKG

6.1 kBMarkdownView Raw
1# through2
2
3<!--
4
5soon ...
6
7[![Build Status](https://secure.travis-ci.org/rvagg/through2.png)](http://travis-ci.org/rvagg/through2)
8
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?downloads&downloadRank)](https://nodei.co/npm/through2/)
14
15<!--
16not happy with these, we need to peg to readable-stream@1.0.x so it'll always report out-of-date
17
18[![david-dm](https://david-dm.org/rvagg/through2.png)](https://david-dm.org/rvagg/through2/)
19[![david-dm](https://david-dm.org/rvagg/through2/dev-status.png)](https://david-dm.org/rvagg/through2#info=devDependencies/)
20-->
21
22**A tiny wrapper around Node streams.Transform (Streams2) to avoid explicit subclassing noise**
23
24Inspired 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
26Note: 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
28```js
29fs.createReadStream('ex.txt')
30 .pipe(through2(function (chunk, enc, callback) {
31 for (var i = 0; i < chunk.length; i++)
32 if (chunk[i] == 97)
33 chunk[i] = 122 // swap 'a' for 'z'
34
35 this.push(chunk)
36
37 callback()
38 }))
39 .pipe(fs.createWriteStream('out.txt'))
40```
41
42Or object streams:
43
44```js
45var all = []
46
47fs.createReadStream('data.csv')
48 .pipe(csv2())
49 .pipe(through2.obj(function (chunk, enc, callback) {
50 var data = {
51 name : chunk[0]
52 , address : chunk[3]
53 , phone : chunk[10]
54 }
55 this.push(data)
56
57 callback()
58 }))
59 .on('data', function (data) {
60 all.push(data)
61 })
62 .on('end', function () {
63 doSomethingSpecial(all)
64 })
65```
66
67Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.
68
69## API
70
71<b><code>through2([ options, ] [ transformFunction ] [, flushFunction ])</code></b>
72
73Consult the **[stream.Transform](http://nodejs.org/docs/latest/api/stream.html#stream_class_stream_transform)** documentation for the exact rules of the `transformFunction` (i.e. `this._transform`) and the optional `flushFunction` (i.e. `this._flush`).
74
75### options
76
77The 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()`).
78
79The `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:
80
81```js
82fs.createReadStream('/tmp/important.dat')
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 )
89 .pipe(fs.createWriteStream('/tmp/wut.txt'))
90```
91
92### transformFunction
93
94The `transformFunction` must have the following signature: `function (chunk, encoding, callback) {}`. A minimal implementation should call the `callback` function to indicate that the transformation is done, even if that transformation means discarding the chunk.
95
96To queue a new chunk, call `this.push(chunk)`&mdash;this can be called as many times as required before the `callback()` if you have multiple pieces to send on.
97
98Alternatively, you may use `callback(err, chunk)` as shorthand for emitting a single chunk or an error.
99
100If you **do not provide a `transformFunction`** then you will get a simple pass-through stream.
101
102### flushFunction
103
104The 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.
105
106```js
107fs.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
118<b><code>through2.ctor([ options, ] transformFunction[, flushFunction ])</code></b>
119
120Instead 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.
121
122```js
123var FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) {
124 if (record.temp != null && record.unit = "F") {
125 record.temp = ( ( record.temp - 32 ) * 5 ) / 9
126 record.unit = "C"
127 }
128 this.push(record)
129 callback()
130})
131
132// Create instances of FToC like so:
133var converter = new FToC()
134// Or:
135var converter = FToC()
136// Or specify/override options when you instantiate, if you prefer:
137var converter = FToC({objectMode: true})
138```
139
140## See Also
141
142 - [through2-map](https://github.com/brycebaril/through2-map) - Array.prototype.map analog for streams.
143 - [through2-filter](https://github.com/brycebaril/through2-filter) - Array.prototype.filter analog for streams.
144 - [through2-reduce](https://github.com/brycebaril/through2-reduce) - Array.prototype.reduce analog for streams.
145 - [through2-spy](https://github.com/brycebaril/through2-spy) - Wrapper for simple stream.PassThrough spies.
146
147## License
148
149**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.