{"name":"through2","version":"4.0.2","description":"A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise","main":"through2.js","scripts":{"test:node":"hundreds mocha test/test.js","test:browser":"node -e 'process.exit(process.version.startsWith(\"v8.\") ? 0 : 1)' || polendina --cleanup --runner=mocha test/test.js","test":"npm run lint && npm run test:node && npm run test:browser","lint":"standard","coverage":"c8 --reporter=text --reporter=html mocha test/test.js && npx st -d coverage -p 8888"},"repository":{"type":"git","url":"git+https://github.com/rvagg/through2.git"},"keywords":["stream","streams2","through","transform"],"author":{"name":"Rod Vagg","email":"r@va.gg","url":"https://github.com/rvagg"},"license":"MIT","dependencies":{"readable-stream":"3"},"devDependencies":{"bl":"^4.0.2","buffer":"^5.6.0","chai":"^4.2.0","hundreds":"~0.0.7","mocha":"^7.2.0","polendina":"^1.0.0","standard":"^14.3.4","stream-spigot":"^3.0.6"},"gitHead":"ebe3d0b736115ffca785336301119979df70a708","bugs":{"url":"https://github.com/rvagg/through2/issues"},"homepage":"https://github.com/rvagg/through2#readme","_id":"through2@4.0.2","_nodeVersion":"14.4.0","_npmVersion":"6.14.5","_npmUser":{"name":"rvagg","email":"r@va.gg"},"dist":{"shasum":"a7ce3ac2a7a8b0b966c80e7c49f0484c3b239764","size":4009,"noattachment":false,"tarball":"https://registry.npmmirror.com/through2/-/through2-4.0.2.tgz","integrity":"sha512-iOqSav00cVxEEICeD7TjLB1sueEL+81Wpzp2bY17uZjZN0pWZPuo4suZ/61VujxmqSGFfgOcNuTZ85QJwNZQpw=="},"maintainers":[{"name":"bryce","email":"bryce@ravenwall.com"},{"name":"rvagg","email":"rod@vagg.org"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/through2_4.0.2_1593480295626_0.011245994121877656"},"_hasShrinkwrap":false,"publish_time":1593480295820,"_cnpm_publish_time":1593480295820,"_cnpmcore_publish_time":"2021-12-13T08:28:01.418Z","readme":"# through2\n\n![Build & Test](https://github.com/rvagg/through2/workflows/Build%20&%20Test/badge.svg)\n\n[![NPM](https://nodei.co/npm/through2.png?downloads&downloadRank)](https://nodei.co/npm/through2/)\n\n**A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise**\n\nInspired 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) { ... })`.\n\n```js\nfs.createReadStream('ex.txt')\n .pipe(through2(function (chunk, enc, callback) {\n for (let i = 0; i < chunk.length; i++)\n if (chunk[i] == 97)\n chunk[i] = 122 // swap 'a' for 'z'\n\n this.push(chunk)\n\n callback()\n }))\n .pipe(fs.createWriteStream('out.txt'))\n .on('finish', () => doSomethingSpecial())\n```\n\nOr object streams:\n\n```js\nconst all = []\n\nfs.createReadStream('data.csv')\n .pipe(csv2())\n .pipe(through2.obj(function (chunk, enc, callback) {\n const data = {\n name : chunk[0]\n , address : chunk[3]\n , phone : chunk[10]\n }\n this.push(data)\n\n callback()\n }))\n .on('data', (data) => {\n all.push(data)\n })\n .on('end', () => {\n doSomethingSpecial(all)\n })\n```\n\nNote that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.\n\n## Do you need this?\n\nSince 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'`):\n\n```js\nconst { Transform } = require('readable-stream')\n\nconst transformer = new Transform({\n transform(chunk, enc, callback) {\n // ...\n }\n})\n```\n\n## API\n\nthrough2([ options, ] [ transformFunction ] [, flushFunction ])
\n\nConsult 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`).\n\n### options\n\nThe 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()`).\n\nThe `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:\n\n```js\nfs.createReadStream('/tmp/important.dat')\n .pipe(through2({ objectMode: true, allowHalfOpen: false },\n (chunk, enc, cb) => {\n cb(null, 'wut?') // note we can use the second argument on the callback\n // to provide data as an alternative to this.push('wut?')\n }\n ))\n .pipe(fs.createWriteStream('/tmp/wut.txt'))\n```\n\n### transformFunction\n\nThe `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.\n\nTo 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.\n\nAlternatively, you may use `callback(err, chunk)` as shorthand for emitting a single chunk or an error.\n\nIf you **do not provide a `transformFunction`** then you will get a simple pass-through stream.\n\n### flushFunction\n\nThe 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.\n\n```js\nfs.createReadStream('/tmp/important.dat')\n .pipe(through2(\n (chunk, enc, cb) => cb(null, chunk), // transform is a noop\n function (cb) { // flush function\n this.push('tacking on an extra buffer to the end');\n cb();\n }\n ))\n .pipe(fs.createWriteStream('/tmp/wut.txt'));\n```\n\nthrough2.ctor([ options, ] transformFunction[, flushFunction ])
\n\nInstead 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.\n\n```js\nconst FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) {\n if (record.temp != null && record.unit == \"F\") {\n record.temp = ( ( record.temp - 32 ) * 5 ) / 9\n record.unit = \"C\"\n }\n this.push(record)\n callback()\n})\n\n// Create instances of FToC like so:\nconst converter = new FToC()\n// Or:\nconst converter = FToC()\n// Or specify/override options when you instantiate, if you prefer:\nconst converter = FToC({objectMode: true})\n```\n\n## License\n\n**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.\n"}