UNPKG

4.63 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?compact=true)](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
26```js
27fs.createReadStream('ex.txt')
28 .pipe(through2(function (chunk, enc, callback) {
29
30 for (var i = 0; i < chunk.length; i++)
31 if (chunk[i] == 97)
32 chunk[i] = 122 // swap 'a' for 'z'
33
34 this.push(chunk)
35
36 callback()
37
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
51 var data = {
52 name : chunk[0]
53 , address : chunk[3]
54 , phone : chunk[10]
55 }
56
57 this.push(data)
58
59 callback()
60
61 }))
62 .on('data', function (data) {
63 all.push(data)
64 })
65 .on('end', function () {
66 doSomethingSpecial(all)
67 })
68```
69
70Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.
71
72## API
73
74<b><code>through2([ options, ] [ transformFunction ] [, flushFunction ])</code></b>
75
76Consult 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`).
77
78### options
79
80The 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()`).
81
82The `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:
83
84```js
85fs.createReadStream('/tmp/important.dat')
86 .pipe(through2({ objectMode: true, allowHalfOpen: false }, function (chunk, enc, cb) {
87
88 this.push(new Buffer('wut?'))
89 cb()
90
91 })
92 .pipe(fs.createWriteStream('/tmp/wut.txt'))
93```
94
95### transformFunction
96
97The `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.
98
99To 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.
100
101If you **do not provide a `transformFunction`** then you will get a simple simple pass-through stream.
102
103### flushFunction
104
105The 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.
106
107<b><code>through2.ctor([ options, ] transformFunction[, flushFunction ])</code></b>
108
109Instead 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.
110
111```js
112var FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) {
113 if (record.temp != null && record.unit = "F") {
114 record.temp = ( ( record.temp - 32 ) * 5 ) / 9
115 record.unit = "C"
116 }
117 this.push(record)
118 callback()
119})
120
121// Create instances of FToC like so:
122var converter = new FToC()
123// Or:
124var converter = FToC()
125// Or specify/override options when you instantiate, if you prefer:
126var converter = FToC({objectMode: true})
127```
128
129## License
130
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.