pax_global_header 0000666 0000000 0000000 00000000064 13676512115 0014521 g ustar 00root root 0000000 0000000 52 comment=ebe3d0b736115ffca785336301119979df70a708
through2-4.0.2/ 0000775 0000000 0000000 00000000000 13676512115 0013266 5 ustar 00root root 0000000 0000000 through2-4.0.2/.github/ 0000775 0000000 0000000 00000000000 13676512115 0014626 5 ustar 00root root 0000000 0000000 through2-4.0.2/.github/workflows/ 0000775 0000000 0000000 00000000000 13676512115 0016663 5 ustar 00root root 0000000 0000000 through2-4.0.2/.github/workflows/ci.yml 0000664 0000000 0000000 00000000701 13676512115 0017777 0 ustar 00root root 0000000 0000000 on: [push, pull_request]
name: Build & Test
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: [8.x, 10.x, 12.x, 14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Build
run: npm install
- name: Test
run: npm test
through2-4.0.2/.gitignore 0000664 0000000 0000000 00000000043 13676512115 0015253 0 ustar 00root root 0000000 0000000 node_modules
.nyc_output
coverage/
through2-4.0.2/.npmignore 0000664 0000000 0000000 00000000030 13676512115 0015256 0 ustar 00root root 0000000 0000000 test
coverage/
.github/
through2-4.0.2/LICENSE.md 0000664 0000000 0000000 00000002150 13676512115 0014670 0 ustar 00root root 0000000 0000000 # The MIT License (MIT)
**Copyright (c) Rod Vagg (the "Original Author") and additional contributors**
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
through2-4.0.2/README.md 0000664 0000000 0000000 00000012161 13676512115 0014546 0 ustar 00root root 0000000 0000000 # through2

[](https://nodei.co/npm/through2/)
**A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise**
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) { ... })`.
```js
fs.createReadStream('ex.txt')
.pipe(through2(function (chunk, enc, callback) {
for (let i = 0; i < chunk.length; i++)
if (chunk[i] == 97)
chunk[i] = 122 // swap 'a' for 'z'
this.push(chunk)
callback()
}))
.pipe(fs.createWriteStream('out.txt'))
.on('finish', () => doSomethingSpecial())
```
Or object streams:
```js
const all = []
fs.createReadStream('data.csv')
.pipe(csv2())
.pipe(through2.obj(function (chunk, enc, callback) {
const data = {
name : chunk[0]
, address : chunk[3]
, phone : chunk[10]
}
this.push(data)
callback()
}))
.on('data', (data) => {
all.push(data)
})
.on('end', () => {
doSomethingSpecial(all)
})
```
Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.
## Do you need this?
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'`):
```js
const { Transform } = require('readable-stream')
const transformer = new Transform({
transform(chunk, enc, callback) {
// ...
}
})
```
## API
through2([ options, ] [ transformFunction ] [, flushFunction ])
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`).
### options
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()`).
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:
```js
fs.createReadStream('/tmp/important.dat')
.pipe(through2({ objectMode: true, allowHalfOpen: false },
(chunk, enc, cb) => {
cb(null, 'wut?') // note we can use the second argument on the callback
// to provide data as an alternative to this.push('wut?')
}
))
.pipe(fs.createWriteStream('/tmp/wut.txt'))
```
### transformFunction
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.
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.
Alternatively, you may use `callback(err, chunk)` as shorthand for emitting a single chunk or an error.
If you **do not provide a `transformFunction`** then you will get a simple pass-through stream.
### flushFunction
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.
```js
fs.createReadStream('/tmp/important.dat')
.pipe(through2(
(chunk, enc, cb) => cb(null, chunk), // transform is a noop
function (cb) { // flush function
this.push('tacking on an extra buffer to the end');
cb();
}
))
.pipe(fs.createWriteStream('/tmp/wut.txt'));
```
through2.ctor([ options, ] transformFunction[, flushFunction ])
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.
```js
const FToC = through2.ctor({objectMode: true}, function (record, encoding, callback) {
if (record.temp != null && record.unit == "F") {
record.temp = ( ( record.temp - 32 ) * 5 ) / 9
record.unit = "C"
}
this.push(record)
callback()
})
// Create instances of FToC like so:
const converter = new FToC()
// Or:
const converter = FToC()
// Or specify/override options when you instantiate, if you prefer:
const converter = FToC({objectMode: true})
```
## License
**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.
through2-4.0.2/package.json 0000664 0000000 0000000 00000002155 13676512115 0015557 0 ustar 00root root 0000000 0000000 {
"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": "https://github.com/rvagg/through2.git"
},
"keywords": [
"stream",
"streams2",
"through",
"transform"
],
"author": "Rod Vagg (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"
}
}
through2-4.0.2/test/ 0000775 0000000 0000000 00000000000 13676512115 0014245 5 ustar 00root root 0000000 0000000 through2-4.0.2/test/bench.js 0000664 0000000 0000000 00000001527 13676512115 0015667 0 ustar 00root root 0000000 0000000 const through2 = require('../')
const { Buffer } = require('buffer')
const bl = require('bl')
const crypto = require('crypto')
const assert = require('assert')
function run (callback) {
const bufs = Array.apply(null, Array(10)).map(() => crypto.randomBytes(32))
const stream = through2((chunk, env, callback) => {
callback(null, chunk.toString('hex'))
})
stream.pipe(bl((err, data) => {
assert(!err)
assert.strictEqual(data.toString(), Buffer.concat(bufs).toString('hex'))
callback()
}))
bufs.forEach((b) => stream.write(b))
stream.end()
}
let count = 0
const start = Date.now()
;(function exec () {
count++
run(() => {
if (Date.now() - start < 1000 * 10) {
return setImmediate(exec)
}
console.log('Ran', count, 'iterations in', Date.now() - start, 'ms')
})
}())
console.log('Running for ~10s')
through2-4.0.2/test/test.js 0000664 0000000 0000000 00000022337 13676512115 0015571 0 ustar 00root root 0000000 0000000 /* eslint-env mocha */
const test = it
const { assert: t } = require('chai')
const through2 = require('../')
const { Buffer } = require('buffer')
const bl = require('bl')
const spigot = require('stream-spigot')
function randomBytes (len) {
const bytes = new Uint8Array(len)
for (let i = 0; i < len; i++) {
bytes[i] = Math.floor(Math.random() * 0xff)
}
return bytes
}
test('plain through', (done) => {
const th2 = through2(function (chunk, enc, callback) {
if (!this._i) {
this._i = 97
} else { // 'a'
this._i++
}
const b = Buffer.alloc(chunk.length)
for (let i = 0; i < chunk.length; i++) {
b[i] = this._i
}
this.push(b)
callback()
})
th2.pipe(bl((err, b) => {
t.ifError(err)
const s = b.toString('ascii')
t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
done()
}))
th2.write(randomBytes(10))
th2.write(randomBytes(5))
th2.write(randomBytes(10))
th2.end()
})
test('pipeable through', (done) => {
const th2 = through2(function (chunk, enc, callback) {
if (!this._i) {
this._i = 97
} else { // 'a'
this._i++
}
const b = Buffer.alloc(chunk.length)
for (let i = 0; i < chunk.length; i++) {
b[i] = this._i
}
this.push(b)
callback()
})
th2.pipe(bl((err, b) => {
t.ifError(err)
const s = b.toString('ascii')
// bl() acts like a proper streams2 stream and passes as much as it's
// asked for, so we really only get one write with such a small amount
// of data
t.equal(s, 'aaaaaaaaaaaaaaaaaaaaaaaaa', 'got transformed string')
done()
}))
const bufs = bl()
bufs.append(randomBytes(10))
bufs.append(randomBytes(5))
bufs.append(randomBytes(10))
bufs.pipe(th2)
})
test('object through', (done) => {
const th2 = through2({ objectMode: true }, function (chunk, enc, callback) {
this.push({ out: chunk.in + 1 })
callback()
})
let e = 0
th2.on('data', (o) => {
t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object')
if (++e === 3) {
done()
}
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('object through with through2.obj', (done) => {
const th2 = through2.obj(function (chunk, enc, callback) {
this.push({ out: chunk.in + 1 })
callback()
})
let e = 0
th2.on('data', (o) => {
t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object')
if (++e === 3) {
done()
}
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('flushing through', (done) => {
const th2 = through2(function (chunk, enc, callback) {
if (!this._i) {
this._i = 97
} else { // 'a'
this._i++
}
const b = Buffer.alloc(chunk.length)
for (let i = 0; i < chunk.length; i++) {
b[i] = this._i
}
this.push(b)
callback()
}, function (callback) {
this.push(Buffer.from([101, 110, 100]))
callback()
})
th2.pipe(bl((err, b) => {
t.ifError(err)
const s = b.toString('ascii')
t.equal(s, 'aaaaaaaaaabbbbbccccccccccend', 'got transformed string')
done()
}))
th2.write(randomBytes(10))
th2.write(randomBytes(5))
th2.write(randomBytes(10))
th2.end()
})
test('plain through ctor', (done) => {
const Th2 = through2.ctor(function (chunk, enc, callback) {
if (!this._i) {
this._i = 97 // 'a'
} else {
this._i++
}
const b = Buffer.alloc(chunk.length)
for (let i = 0; i < chunk.length; i++) {
b[i] = this._i
}
this.push(b)
callback()
})
const th2 = new Th2()
th2.pipe(bl((err, b) => {
t.ifError(err)
const s = b.toString('ascii')
t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
done()
}))
th2.write(randomBytes(10))
th2.write(randomBytes(5))
th2.write(randomBytes(10))
th2.end()
})
test('reuse through ctor', (done) => {
let uses = 0
const Th2 = through2.ctor(function (chunk, enc, callback) {
if (!this._i) {
uses++
this._i = 97 // 'a'
} else {
this._i++
}
const b = Buffer.alloc(chunk.length)
for (let i = 0; i < chunk.length; i++) {
b[i] = this._i
}
this.push(b)
callback()
})
const th2 = Th2()
th2.pipe(bl((err, b) => {
t.ifError(err)
const s = b.toString('ascii')
t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
const newInstance = Th2()
newInstance.pipe(bl((err, b) => {
t.ifError(err)
const s = b.toString('ascii')
t.equal('aaaaaaabbbbccccccc', s, 'got transformed string')
t.equal(uses, 2)
done()
}))
newInstance.write(randomBytes(7))
newInstance.write(randomBytes(4))
newInstance.write(randomBytes(7))
newInstance.end()
}))
th2.write(randomBytes(10))
th2.write(randomBytes(5))
th2.write(randomBytes(10))
th2.end()
})
test('object through ctor', (done) => {
const Th2 = through2.ctor({ objectMode: true }, function (chunk, enc, callback) {
this.push({ out: chunk.in + 1 })
callback()
})
const th2 = new Th2()
let e = 0
th2.on('data', (o) => {
t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object')
if (++e === 3) {
done()
}
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('pipeable object through ctor', (done) => {
const Th2 = through2.ctor({ objectMode: true }, function (record, enc, callback) {
if (record.temp != null && record.unit === 'F') {
record.temp = ((record.temp - 32) * 5) / 9
record.unit = 'C'
}
this.push(record)
callback()
})
const th2 = Th2()
const expect = [-19, -40, 100, 22]
th2.on('data', (o) => {
t.deepEqual(o, { temp: expect.shift(), unit: 'C' }, 'got transformed object')
if (!expect.length) {
done()
}
})
spigot({ objectMode: true }, [
{ temp: -2.2, unit: 'F' },
{ temp: -40, unit: 'F' },
{ temp: 212, unit: 'F' },
{ temp: 22, unit: 'C' }
]).pipe(th2)
})
test('object through ctor override', (done) => {
const Th2 = through2.ctor(function (chunk, enc, callback) {
this.push({ out: chunk.in + 1 })
callback()
})
const th2 = Th2({ objectMode: true })
let e = 0
th2.on('data', (o) => {
t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object')
if (++e === 3) {
done()
}
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('object settings available in transform', (done) => {
const Th2 = through2.ctor({ objectMode: true, peek: true }, function (chunk, enc, callback) {
t.ok(this.options.peek, 'reading options from inside _transform')
this.push({ out: chunk.in + 1 })
callback()
})
const th2 = Th2()
let e = 0
th2.on('data', (o) => {
t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object')
if (++e === 3) {
done()
}
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('object settings available in transform override', (done) => {
const Th2 = through2.ctor(function (chunk, enc, callback) {
t.ok(this.options.peek, 'reading options from inside _transform')
this.push({ out: chunk.in + 1 })
callback()
})
const th2 = Th2({ objectMode: true, peek: true })
let e = 0
th2.on('data', (o) => {
t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object')
if (++e === 3) {
done()
}
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('object override extends options', (done) => {
const Th2 = through2.ctor({ objectMode: true }, function (chunk, enc, callback) {
t.ok(this.options.peek, 'reading options from inside _transform')
this.push({ out: chunk.in + 1 })
callback()
})
const th2 = Th2({ peek: true })
let e = 0
th2.on('data', (o) => {
t.deepEqual(o, { out: e === 0 ? 102 : e === 1 ? 203 : -99 }, 'got transformed object')
if (++e === 3) {
done()
}
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('ctor flush()', (done) => {
let chunkCalled = false
const th2 = through2.ctor(
(chunk, enc, callback) => {
t.equal(chunk.toString(), 'aa')
chunkCalled = true
callback()
}, function fl () {
t(chunkCalled)
done()
}
)()
th2.end('aa')
})
test('obj flush()', (done) => {
let chunkCalled = false
const th2 = through2.obj(
(chunk, enc, callback) => {
t.deepEqual(chunk, { a: 'a' })
chunkCalled = true
callback()
}, function fl () {
t(chunkCalled)
done()
}
)
th2.end({ a: 'a' })
})
test('can be destroyed', (done) => {
const th = through2()
th.on('close', () => {
t.ok(true, 'shoud emit close')
done()
})
th.destroy()
})
test('can be destroyed twice', (done) => {
const th = through2()
th.on('close', () => {
t.ok(true, 'shoud emit close')
done()
})
th.destroy()
th.destroy()
})
test('noop through', (done) => {
const th = through2()
th.pipe(bl((err, data) => {
t.ifError(err)
t.equal(data.toString(), 'eeee')
done()
}))
th.end('eeee')
})
through2-4.0.2/through2.js 0000664 0000000 0000000 00000003474 13676512115 0015376 0 ustar 00root root 0000000 0000000 const { Transform } = require('readable-stream')
function inherits (fn, sup) {
fn.super_ = sup
fn.prototype = Object.create(sup.prototype, {
constructor: { value: fn, enumerable: false, writable: true, configurable: true }
})
}
// create a new export function, used by both the main export and
// the .ctor export, contains common logic for dealing with arguments
function through2 (construct) {
return (options, transform, flush) => {
if (typeof options === 'function') {
flush = transform
transform = options
options = {}
}
if (typeof transform !== 'function') {
// noop
transform = (chunk, enc, cb) => cb(null, chunk)
}
if (typeof flush !== 'function') {
flush = null
}
return construct(options, transform, flush)
}
}
// main export, just make me a transform stream!
const make = through2((options, transform, flush) => {
const t2 = new Transform(options)
t2._transform = transform
if (flush) {
t2._flush = flush
}
return t2
})
// make me a reusable prototype that I can `new`, or implicitly `new`
// with a constructor call
const ctor = through2((options, transform, flush) => {
function Through2 (override) {
if (!(this instanceof Through2)) {
return new Through2(override)
}
this.options = Object.assign({}, options, override)
Transform.call(this, this.options)
this._transform = transform
if (flush) {
this._flush = flush
}
}
inherits(Through2, Transform)
return Through2
})
const obj = through2(function (options, transform, flush) {
const t2 = new Transform(Object.assign({ objectMode: true, highWaterMark: 16 }, options))
t2._transform = transform
if (flush) {
t2._flush = flush
}
return t2
})
module.exports = make
module.exports.ctor = ctor
module.exports.obj = obj