├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── package.json ├── test ├── bench.js └── test.js └── through2.js /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | name: Build & Test 3 | jobs: 4 | test: 5 | runs-on: ubuntu-latest 6 | strategy: 7 | fail-fast: false 8 | matrix: 9 | node-version: [8.x, 10.x, 12.x, 14.x] 10 | steps: 11 | - uses: actions/checkout@v2 12 | - name: Use Node.js ${{ matrix.node-version }} 13 | uses: actions/setup-node@v1 14 | with: 15 | node-version: ${{ matrix.node-version }} 16 | - name: Build 17 | run: npm install 18 | - name: Test 19 | run: npm test 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .nyc_output 3 | coverage/ 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | test 2 | coverage/ 3 | .github/ 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | **Copyright (c) Rod Vagg (the "Original Author") and additional contributors** 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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 | 9 | 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 | 11 | ```js 12 | fs.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 | 26 | Or object streams: 27 | 28 | ```js 29 | const all = [] 30 | 31 | fs.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 | 51 | Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`. 52 | 53 | ## Do you need this? 54 | 55 | Since 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 58 | const { Transform } = require('readable-stream') 59 | 60 | const transformer = new Transform({ 61 | transform(chunk, enc, callback) { 62 | // ... 63 | } 64 | }) 65 | ``` 66 | 67 | ## API 68 | 69 | through2([ options, ] [ transformFunction ] [, flushFunction ]) 70 | 71 | Consult 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 | 75 | 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()`). 76 | 77 | 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: 78 | 79 | ```js 80 | fs.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 | 92 | The `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 | 94 | 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. 95 | 96 | Alternatively, you may use `callback(err, chunk)` as shorthand for emitting a single chunk or an error. 97 | 98 | If you **do not provide a `transformFunction`** then you will get a simple pass-through stream. 99 | 100 | ### flushFunction 101 | 102 | 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. 103 | 104 | ```js 105 | fs.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 | through2.ctor([ options, ] transformFunction[, flushFunction ]) 117 | 118 | 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. 119 | 120 | ```js 121 | const 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: 131 | const converter = new FToC() 132 | // Or: 133 | const converter = FToC() 134 | // Or specify/override options when you instantiate, if you prefer: 135 | const converter = FToC({objectMode: true}) 136 | ``` 137 | 138 | ## License 139 | 140 | **through2** is Copyright © 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. 141 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "through2", 3 | "version": "4.0.2", 4 | "description": "A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise", 5 | "main": "through2.js", 6 | "scripts": { 7 | "test:node": "hundreds mocha test/test.js", 8 | "test:browser": "node -e 'process.exit(process.version.startsWith(\"v8.\") ? 0 : 1)' || polendina --cleanup --runner=mocha test/test.js", 9 | "test": "npm run lint && npm run test:node && npm run test:browser", 10 | "lint": "standard", 11 | "coverage": "c8 --reporter=text --reporter=html mocha test/test.js && npx st -d coverage -p 8888" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/rvagg/through2.git" 16 | }, 17 | "keywords": [ 18 | "stream", 19 | "streams2", 20 | "through", 21 | "transform" 22 | ], 23 | "author": "Rod Vagg (https://github.com/rvagg)", 24 | "license": "MIT", 25 | "dependencies": { 26 | "readable-stream": "3" 27 | }, 28 | "devDependencies": { 29 | "bl": "^4.0.2", 30 | "buffer": "^5.6.0", 31 | "chai": "^4.2.0", 32 | "hundreds": "~0.0.7", 33 | "mocha": "^7.2.0", 34 | "polendina": "^1.0.0", 35 | "standard": "^14.3.4", 36 | "stream-spigot": "^3.0.6" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test/bench.js: -------------------------------------------------------------------------------- 1 | const through2 = require('../') 2 | const { Buffer } = require('buffer') 3 | const bl = require('bl') 4 | const crypto = require('crypto') 5 | const assert = require('assert') 6 | 7 | function run (callback) { 8 | const bufs = Array.apply(null, Array(10)).map(() => crypto.randomBytes(32)) 9 | const stream = through2((chunk, env, callback) => { 10 | callback(null, chunk.toString('hex')) 11 | }) 12 | 13 | stream.pipe(bl((err, data) => { 14 | assert(!err) 15 | assert.strictEqual(data.toString(), Buffer.concat(bufs).toString('hex')) 16 | callback() 17 | })) 18 | 19 | bufs.forEach((b) => stream.write(b)) 20 | stream.end() 21 | } 22 | 23 | let count = 0 24 | const start = Date.now() 25 | 26 | ;(function exec () { 27 | count++ 28 | run(() => { 29 | if (Date.now() - start < 1000 * 10) { 30 | return setImmediate(exec) 31 | } 32 | console.log('Ran', count, 'iterations in', Date.now() - start, 'ms') 33 | }) 34 | }()) 35 | 36 | console.log('Running for ~10s') 37 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /* eslint-env mocha */ 2 | 3 | const test = it 4 | const { assert: t } = require('chai') 5 | const through2 = require('../') 6 | const { Buffer } = require('buffer') 7 | const bl = require('bl') 8 | const spigot = require('stream-spigot') 9 | 10 | function randomBytes (len) { 11 | const bytes = new Uint8Array(len) 12 | for (let i = 0; i < len; i++) { 13 | bytes[i] = Math.floor(Math.random() * 0xff) 14 | } 15 | return bytes 16 | } 17 | 18 | test('plain through', (done) => { 19 | const th2 = through2(function (chunk, enc, callback) { 20 | if (!this._i) { 21 | this._i = 97 22 | } else { // 'a' 23 | this._i++ 24 | } 25 | const b = Buffer.alloc(chunk.length) 26 | for (let i = 0; i < chunk.length; i++) { 27 | b[i] = this._i 28 | } 29 | this.push(b) 30 | callback() 31 | }) 32 | 33 | th2.pipe(bl((err, b) => { 34 | t.ifError(err) 35 | const s = b.toString('ascii') 36 | t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string') 37 | done() 38 | })) 39 | 40 | th2.write(randomBytes(10)) 41 | th2.write(randomBytes(5)) 42 | th2.write(randomBytes(10)) 43 | th2.end() 44 | }) 45 | 46 | test('pipeable through', (done) => { 47 | const th2 = through2(function (chunk, enc, callback) { 48 | if (!this._i) { 49 | this._i = 97 50 | } else { // 'a' 51 | this._i++ 52 | } 53 | const b = Buffer.alloc(chunk.length) 54 | for (let i = 0; i < chunk.length; i++) { 55 | b[i] = this._i 56 | } 57 | this.push(b) 58 | callback() 59 | }) 60 | 61 | th2.pipe(bl((err, b) => { 62 | t.ifError(err) 63 | const s = b.toString('ascii') 64 | // bl() acts like a proper streams2 stream and passes as much as it's 65 | // asked for, so we really only get one write with such a small amount 66 | // of data 67 | t.equal(s, 'aaaaaaaaaaaaaaaaaaaaaaaaa', 'got transformed string') 68 | done() 69 | })) 70 | 71 | const bufs = bl() 72 | bufs.append(randomBytes(10)) 73 | bufs.append(randomBytes(5)) 74 | bufs.append(randomBytes(10)) 75 | bufs.pipe(th2) 76 | }) 77 | 78 | test('object through', (done) => { 79 | const th2 = through2({ objectMode: true }, function (chunk, enc, callback) { 80 | this.push({ out: chunk.in + 1 }) 81 | callback() 82 | }) 83 | 84 | let e = 0 85 | th2.on('data', (o) => { 86 | t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object') 87 | if (++e === 3) { 88 | done() 89 | } 90 | }) 91 | 92 | th2.write({ in: 101 }) 93 | th2.write({ in: 202 }) 94 | th2.write({ in: -100 }) 95 | th2.end() 96 | }) 97 | 98 | test('object through with through2.obj', (done) => { 99 | const th2 = through2.obj(function (chunk, enc, callback) { 100 | this.push({ out: chunk.in + 1 }) 101 | callback() 102 | }) 103 | 104 | let e = 0 105 | th2.on('data', (o) => { 106 | t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object') 107 | if (++e === 3) { 108 | done() 109 | } 110 | }) 111 | 112 | th2.write({ in: 101 }) 113 | th2.write({ in: 202 }) 114 | th2.write({ in: -100 }) 115 | th2.end() 116 | }) 117 | 118 | test('flushing through', (done) => { 119 | const th2 = through2(function (chunk, enc, callback) { 120 | if (!this._i) { 121 | this._i = 97 122 | } else { // 'a' 123 | this._i++ 124 | } 125 | const b = Buffer.alloc(chunk.length) 126 | for (let i = 0; i < chunk.length; i++) { 127 | b[i] = this._i 128 | } 129 | this.push(b) 130 | callback() 131 | }, function (callback) { 132 | this.push(Buffer.from([101, 110, 100])) 133 | callback() 134 | }) 135 | 136 | th2.pipe(bl((err, b) => { 137 | t.ifError(err) 138 | const s = b.toString('ascii') 139 | t.equal(s, 'aaaaaaaaaabbbbbccccccccccend', 'got transformed string') 140 | done() 141 | })) 142 | 143 | th2.write(randomBytes(10)) 144 | th2.write(randomBytes(5)) 145 | th2.write(randomBytes(10)) 146 | th2.end() 147 | }) 148 | 149 | test('plain through ctor', (done) => { 150 | const Th2 = through2.ctor(function (chunk, enc, callback) { 151 | if (!this._i) { 152 | this._i = 97 // 'a' 153 | } else { 154 | this._i++ 155 | } 156 | const b = Buffer.alloc(chunk.length) 157 | for (let i = 0; i < chunk.length; i++) { 158 | b[i] = this._i 159 | } 160 | this.push(b) 161 | callback() 162 | }) 163 | 164 | const th2 = new Th2() 165 | 166 | th2.pipe(bl((err, b) => { 167 | t.ifError(err) 168 | const s = b.toString('ascii') 169 | t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string') 170 | done() 171 | })) 172 | 173 | th2.write(randomBytes(10)) 174 | th2.write(randomBytes(5)) 175 | th2.write(randomBytes(10)) 176 | th2.end() 177 | }) 178 | 179 | test('reuse through ctor', (done) => { 180 | let uses = 0 181 | const Th2 = through2.ctor(function (chunk, enc, callback) { 182 | if (!this._i) { 183 | uses++ 184 | this._i = 97 // 'a' 185 | } else { 186 | this._i++ 187 | } 188 | const b = Buffer.alloc(chunk.length) 189 | for (let i = 0; i < chunk.length; i++) { 190 | b[i] = this._i 191 | } 192 | this.push(b) 193 | callback() 194 | }) 195 | 196 | const th2 = Th2() 197 | 198 | th2.pipe(bl((err, b) => { 199 | t.ifError(err) 200 | const s = b.toString('ascii') 201 | t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string') 202 | 203 | const newInstance = Th2() 204 | newInstance.pipe(bl((err, b) => { 205 | t.ifError(err) 206 | const s = b.toString('ascii') 207 | t.equal('aaaaaaabbbbccccccc', s, 'got transformed string') 208 | t.equal(uses, 2) 209 | done() 210 | })) 211 | 212 | newInstance.write(randomBytes(7)) 213 | newInstance.write(randomBytes(4)) 214 | newInstance.write(randomBytes(7)) 215 | newInstance.end() 216 | })) 217 | 218 | th2.write(randomBytes(10)) 219 | th2.write(randomBytes(5)) 220 | th2.write(randomBytes(10)) 221 | th2.end() 222 | }) 223 | 224 | test('object through ctor', (done) => { 225 | const Th2 = through2.ctor({ objectMode: true }, function (chunk, enc, callback) { 226 | this.push({ out: chunk.in + 1 }) 227 | callback() 228 | }) 229 | 230 | const th2 = new Th2() 231 | 232 | let e = 0 233 | th2.on('data', (o) => { 234 | t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object') 235 | if (++e === 3) { 236 | done() 237 | } 238 | }) 239 | 240 | th2.write({ in: 101 }) 241 | th2.write({ in: 202 }) 242 | th2.write({ in: -100 }) 243 | th2.end() 244 | }) 245 | 246 | test('pipeable object through ctor', (done) => { 247 | const Th2 = through2.ctor({ objectMode: true }, function (record, enc, callback) { 248 | if (record.temp != null && record.unit === 'F') { 249 | record.temp = ((record.temp - 32) * 5) / 9 250 | record.unit = 'C' 251 | } 252 | this.push(record) 253 | callback() 254 | }) 255 | 256 | const th2 = Th2() 257 | 258 | const expect = [-19, -40, 100, 22] 259 | th2.on('data', (o) => { 260 | t.deepEqual(o, { temp: expect.shift(), unit: 'C' }, 'got transformed object') 261 | if (!expect.length) { 262 | done() 263 | } 264 | }) 265 | 266 | spigot({ objectMode: true }, [ 267 | { temp: -2.2, unit: 'F' }, 268 | { temp: -40, unit: 'F' }, 269 | { temp: 212, unit: 'F' }, 270 | { temp: 22, unit: 'C' } 271 | ]).pipe(th2) 272 | }) 273 | 274 | test('object through ctor override', (done) => { 275 | const Th2 = through2.ctor(function (chunk, enc, callback) { 276 | this.push({ out: chunk.in + 1 }) 277 | callback() 278 | }) 279 | 280 | const th2 = Th2({ objectMode: true }) 281 | 282 | let e = 0 283 | th2.on('data', (o) => { 284 | t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object') 285 | if (++e === 3) { 286 | done() 287 | } 288 | }) 289 | 290 | th2.write({ in: 101 }) 291 | th2.write({ in: 202 }) 292 | th2.write({ in: -100 }) 293 | th2.end() 294 | }) 295 | 296 | test('object settings available in transform', (done) => { 297 | const Th2 = through2.ctor({ objectMode: true, peek: true }, function (chunk, enc, callback) { 298 | t.ok(this.options.peek, 'reading options from inside _transform') 299 | this.push({ out: chunk.in + 1 }) 300 | callback() 301 | }) 302 | 303 | const th2 = Th2() 304 | 305 | let e = 0 306 | th2.on('data', (o) => { 307 | t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object') 308 | if (++e === 3) { 309 | done() 310 | } 311 | }) 312 | 313 | th2.write({ in: 101 }) 314 | th2.write({ in: 202 }) 315 | th2.write({ in: -100 }) 316 | th2.end() 317 | }) 318 | 319 | test('object settings available in transform override', (done) => { 320 | const Th2 = through2.ctor(function (chunk, enc, callback) { 321 | t.ok(this.options.peek, 'reading options from inside _transform') 322 | this.push({ out: chunk.in + 1 }) 323 | callback() 324 | }) 325 | 326 | const th2 = Th2({ objectMode: true, peek: true }) 327 | 328 | let e = 0 329 | th2.on('data', (o) => { 330 | t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object') 331 | if (++e === 3) { 332 | done() 333 | } 334 | }) 335 | 336 | th2.write({ in: 101 }) 337 | th2.write({ in: 202 }) 338 | th2.write({ in: -100 }) 339 | th2.end() 340 | }) 341 | 342 | test('object override extends options', (done) => { 343 | const Th2 = through2.ctor({ objectMode: true }, function (chunk, enc, callback) { 344 | t.ok(this.options.peek, 'reading options from inside _transform') 345 | this.push({ out: chunk.in + 1 }) 346 | callback() 347 | }) 348 | 349 | const th2 = Th2({ peek: true }) 350 | 351 | let e = 0 352 | th2.on('data', (o) => { 353 | t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object') 354 | if (++e === 3) { 355 | done() 356 | } 357 | }) 358 | 359 | th2.write({ in: 101 }) 360 | th2.write({ in: 202 }) 361 | th2.write({ in: -100 }) 362 | th2.end() 363 | }) 364 | 365 | test('ctor flush()', (done) => { 366 | let chunkCalled = false 367 | const th2 = through2.ctor( 368 | (chunk, enc, callback) => { 369 | t.equal(chunk.toString(), 'aa') 370 | chunkCalled = true 371 | callback() 372 | }, function fl () { 373 | t(chunkCalled) 374 | done() 375 | } 376 | )() 377 | 378 | th2.end('aa') 379 | }) 380 | 381 | test('obj flush()', (done) => { 382 | let chunkCalled = false 383 | const th2 = through2.obj( 384 | (chunk, enc, callback) => { 385 | t.deepEqual(chunk, { a: 'a' }) 386 | chunkCalled = true 387 | callback() 388 | }, function fl () { 389 | t(chunkCalled) 390 | done() 391 | } 392 | ) 393 | 394 | th2.end({ a: 'a' }) 395 | }) 396 | 397 | test('can be destroyed', (done) => { 398 | const th = through2() 399 | 400 | th.on('close', () => { 401 | t.ok(true, 'shoud emit close') 402 | done() 403 | }) 404 | 405 | th.destroy() 406 | }) 407 | 408 | test('can be destroyed twice', (done) => { 409 | const th = through2() 410 | 411 | th.on('close', () => { 412 | t.ok(true, 'shoud emit close') 413 | done() 414 | }) 415 | 416 | th.destroy() 417 | th.destroy() 418 | }) 419 | 420 | test('noop through', (done) => { 421 | const th = through2() 422 | th.pipe(bl((err, data) => { 423 | t.ifError(err) 424 | t.equal(data.toString(), 'eeee') 425 | done() 426 | })) 427 | th.end('eeee') 428 | }) 429 | -------------------------------------------------------------------------------- /through2.js: -------------------------------------------------------------------------------- 1 | const { Transform } = require('readable-stream') 2 | 3 | function inherits (fn, sup) { 4 | fn.super_ = sup 5 | fn.prototype = Object.create(sup.prototype, { 6 | constructor: { value: fn, enumerable: false, writable: true, configurable: true } 7 | }) 8 | } 9 | 10 | // create a new export function, used by both the main export and 11 | // the .ctor export, contains common logic for dealing with arguments 12 | function through2 (construct) { 13 | return (options, transform, flush) => { 14 | if (typeof options === 'function') { 15 | flush = transform 16 | transform = options 17 | options = {} 18 | } 19 | 20 | if (typeof transform !== 'function') { 21 | // noop 22 | transform = (chunk, enc, cb) => cb(null, chunk) 23 | } 24 | 25 | if (typeof flush !== 'function') { 26 | flush = null 27 | } 28 | 29 | return construct(options, transform, flush) 30 | } 31 | } 32 | 33 | // main export, just make me a transform stream! 34 | const make = through2((options, transform, flush) => { 35 | const t2 = new Transform(options) 36 | 37 | t2._transform = transform 38 | 39 | if (flush) { 40 | t2._flush = flush 41 | } 42 | 43 | return t2 44 | }) 45 | 46 | // make me a reusable prototype that I can `new`, or implicitly `new` 47 | // with a constructor call 48 | const ctor = through2((options, transform, flush) => { 49 | function Through2 (override) { 50 | if (!(this instanceof Through2)) { 51 | return new Through2(override) 52 | } 53 | 54 | this.options = Object.assign({}, options, override) 55 | 56 | Transform.call(this, this.options) 57 | 58 | this._transform = transform 59 | if (flush) { 60 | this._flush = flush 61 | } 62 | } 63 | 64 | inherits(Through2, Transform) 65 | 66 | return Through2 67 | }) 68 | 69 | const obj = through2(function (options, transform, flush) { 70 | const t2 = new Transform(Object.assign({ objectMode: true, highWaterMark: 16 }, options)) 71 | 72 | t2._transform = transform 73 | 74 | if (flush) { 75 | t2._flush = flush 76 | } 77 | 78 | return t2 79 | }) 80 | 81 | module.exports = make 82 | module.exports.ctor = ctor 83 | module.exports.obj = obj 84 | --------------------------------------------------------------------------------