UNPKG

5.23 kBMarkdownView Raw
1# through2
2
3![Build & Test](https://github.com/rvagg/through2/workflows/Build%20&%20Test/badge.svg)
4
5[![NPM](https://nodei.co/npm/through2.png?downloads&downloadRank)](https://nodei.co/npm/through2/)
6
7**A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise**
8
9Inspired 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
11```js
12fs.createReadStream('ex.txt')
13 .pipe(through2(function (chunk, enc, callback) {
14 for (let i = 0; i < chunk.length; i++)
15 if (chunk[i] == 97)
16 chunk[i] = 122 // swap 'a' for 'z'
17
18 this.push(chunk)
19
20 callback()
21 }))
22 .pipe(fs.createWriteStream('out.txt'))
23 .on('finish', () => doSomethingSpecial())
24```
25
26Or object streams:
27
28```js
29const all = []
30
31fs.createReadStream('data.csv')
32 .pipe(csv2())
33 .pipe(through2.obj(function (chunk, enc, callback) {
34 const data = {
35 name : chunk[0]
36 , address : chunk[3]
37 , phone : chunk[10]
38 }
39 this.push(data)
40
41 callback()
42 }))
43 .on('data', (data) => {
44 all.push(data)
45 })
46 .on('end', () => {
47 doSomethingSpecial(all)
48 })
49```
50
51Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.
52
53## Do you need this?
54
55Since Node.js introduced [Simplified Stream Construction](https://nodejs.org/api/stream.html#stream_simplified_construction), many uses of **through2** have become redundant. Consider whether you really need to use **through2** or just want to use the `'readable-stream'` package, or the core `'stream'` package (which is derived from `'readable-stream'`):
56
57```js
58const { Transform } = require('readable-stream')
59
60const transformer = new Transform({
61 transform(chunk, enc, callback) {
62 // ...
63 }
64})
65```
66
67## API
68
69<b><code>through2([ options, ] [ transformFunction ] [, flushFunction ])</code></b>
70
71Consult the **[stream.Transform](https://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`).
72
73### options
74
75The 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()`).
76
77The `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:
78
79```js
80fs.createReadStream('/tmp/important.dat')
81 .pipe(through2({ objectMode: true, allowHalfOpen: false },
82 (chunk, enc, cb) => {
83 cb(null, 'wut?') // note we can use the second argument on the callback
84 // to provide data as an alternative to this.push('wut?')
85 }
86 ))
87 .pipe(fs.createWriteStream('/tmp/wut.txt'))
88```
89
90### transformFunction
91
92The `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.
93
94To 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.
95
96Alternatively, you may use `callback(err, chunk)` as shorthand for emitting a single chunk or an error.
97
98If you **do not provide a `transformFunction`** then you will get a simple pass-through stream.
99
100### flushFunction
101
102The 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.
103
104```js
105fs.createReadStream('/tmp/important.dat')
106 .pipe(through2(
107 (chunk, enc, cb) => cb(null, chunk), // transform is a noop
108 function (cb) { // flush function
109 this.push('tacking on an extra buffer to the end');
110 cb();
111 }
112 ))
113 .pipe(fs.createWriteStream('/tmp/wut.txt'));
114```
115
116<b><code>through2.ctor([ options, ] transformFunction[, flushFunction ])</code></b>
117
118Instead 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.
119
120```js
121const FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) {
122 if (record.temp != null && record.unit == "F") {
123 record.temp = ( ( record.temp - 32 ) * 5 ) / 9
124 record.unit = "C"
125 }
126 this.push(record)
127 callback()
128})
129
130// Create instances of FToC like so:
131const converter = new FToC()
132// Or:
133const converter = FToC()
134// Or specify/override options when you instantiate, if you prefer:
135const converter = FToC({objectMode: true})
136```
137
138## License
139
140**through2** is Copyright &copy; Rod Vagg and additional contributors and licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.