node-through2-2.0.3/ 0000775 0000000 0000000 00000000000 13017142405 0014176 5 ustar 00root root 0000000 0000000 node-through2-2.0.3/.gitignore 0000664 0000000 0000000 00000000014 13017142405 0016161 0 ustar 00root root 0000000 0000000 node_modules node-through2-2.0.3/.jshintrc 0000664 0000000 0000000 00000002173 13017142405 0016026 0 ustar 00root root 0000000 0000000 {
"predef": [ ]
, "bitwise": false
, "camelcase": false
, "curly": false
, "eqeqeq": false
, "forin": false
, "immed": false
, "latedef": false
, "noarg": true
, "noempty": true
, "nonew": true
, "plusplus": false
, "quotmark": true
, "regexp": false
, "undef": true
, "unused": true
, "strict": false
, "trailing": true
, "maxlen": 120
, "asi": true
, "boss": true
, "debug": true
, "eqnull": true
, "esnext": true
, "evil": true
, "expr": true
, "funcscope": false
, "globalstrict": false
, "iterator": false
, "lastsemic": true
, "laxbreak": true
, "laxcomma": true
, "loopfunc": true
, "multistr": false
, "onecase": false
, "proto": false
, "regexdash": false
, "scripturl": true
, "smarttabs": false
, "shadow": false
, "sub": true
, "supernew": false
, "validthis": true
, "browser": true
, "couch": false
, "devel": false
, "dojo": false
, "mootools": false
, "node": true
, "nonstandard": true
, "prototypejs": false
, "rhino": false
, "worker": true
, "wsh": false
, "nomen": false
, "onevar": false
, "passfail": false
} node-through2-2.0.3/.npmignore 0000664 0000000 0000000 00000000032 13017142405 0016170 0 ustar 00root root 0000000 0000000 test
.jshintrc
.travis.yml node-through2-2.0.3/.travis.yml 0000664 0000000 0000000 00000000553 13017142405 0016312 0 ustar 00root root 0000000 0000000 language: node_js
node_js:
- '0.10'
- '0.11'
branches:
only:
- master
notifications:
email:
- rod@vagg.org
script: npm test
env:
global:
- SAUCE_USER=through2-sauce
- secure: A8KyygIlklZOcTwTGhZHpkOu7Cn4tI1QQasg75CTSjudczGzkuh1pG2RDIkWqGcrWv9o5CjI6fFYYx0JvQu8o5PO+ZAtV1Z0AlnM965vE8kAx0XEC7giAV323bibfnHwyQu74vIhbOfdes7E4/cYerV2kAjK0T375D4DYEpDuvw=
node-through2-2.0.3/LICENSE.md 0000664 0000000 0000000 00000002154 13017142405 0015604 0 ustar 00root root 0000000 0000000 # The MIT License (MIT)
**Copyright (c) 2016 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. node-through2-2.0.3/README.md 0000664 0000000 0000000 00000013027 13017142405 0015460 0 ustar 00root root 0000000 0000000 # through2
[](https://nodei.co/npm/through2/)
**A tiny wrapper around Node streams.Transform (Streams2) 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) { ... })`.
Note: As 2.x.x this module starts using **Streams3** instead of Stream2. To continue using a Streams2 version use `npm install through2@0` to fetch the latest version of 0.x.x. More information about Streams2 vs Streams3 and recommendations see the article **[Why I don't use Node's core 'stream' module](http://r.va.gg/2014/06/why-i-dont-use-nodes-core-stream-module.html)**.
```js
fs.createReadStream('ex.txt')
.pipe(through2(function (chunk, enc, callback) {
for (var 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', function () {
doSomethingSpecial()
})
```
Or object streams:
```js
var all = []
fs.createReadStream('data.csv')
.pipe(csv2())
.pipe(through2.obj(function (chunk, enc, callback) {
var data = {
name : chunk[0]
, address : chunk[3]
, phone : chunk[10]
}
this.push(data)
callback()
}))
.on('data', function (data) {
all.push(data)
})
.on('end', function () {
doSomethingSpecial(all)
})
```
Note that `through2.obj(fn)` is a convenience wrapper around `through2({ objectMode: true }, fn)`.
## API
through2([ options, ] [ transformFunction ] [, flushFunction ])
Consult 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`).
### 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 },
function (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(
function (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
var 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:
var converter = new FToC()
// Or:
var converter = FToC()
// Or specify/override options when you instantiate, if you prefer:
var converter = FToC({objectMode: true})
```
## See Also
- [through2-map](https://github.com/brycebaril/through2-map) - Array.prototype.map analog for streams.
- [through2-filter](https://github.com/brycebaril/through2-filter) - Array.prototype.filter analog for streams.
- [through2-reduce](https://github.com/brycebaril/through2-reduce) - Array.prototype.reduce analog for streams.
- [through2-spy](https://github.com/brycebaril/through2-spy) - Wrapper for simple stream.PassThrough spies.
- the [mississippi stream utility collection](https://github.com/maxogden/mississippi) includes `through2` as well as many more useful stream modules similar to this one
## License
**through2** is Copyright (c) 2013 Rod Vagg [@rvagg](https://twitter.com/rvagg) 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.
node-through2-2.0.3/package.json 0000664 0000000 0000000 00000001357 13017142405 0016472 0 ustar 00root root 0000000 0000000 {
"name": "through2",
"version": "2.0.3",
"description": "A tiny wrapper around Node streams2 Transform to avoid explicit subclassing noise",
"main": "through2.js",
"scripts": {
"test": "node test/test.js | faucet",
"test-local": "brtapsauce-local test/basic-test.js"
},
"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": "^2.1.5",
"xtend": "~4.0.1"
},
"devDependencies": {
"bl": "~1.1.2",
"faucet": "0.0.1",
"stream-spigot": "~3.0.5",
"tape": "~4.6.2"
}
}
node-through2-2.0.3/test/ 0000775 0000000 0000000 00000000000 13017142405 0015155 5 ustar 00root root 0000000 0000000 node-through2-2.0.3/test/basic-test.js 0000664 0000000 0000000 00000020141 13017142405 0017547 0 ustar 00root root 0000000 0000000 const test = require('tape')
, through2 = require('../')
, crypto = require('crypto')
, bl = require('bl')
, spigot = require('stream-spigot')
test('plain through', function (t) {
var th2 = through2(function (chunk, enc, callback) {
if (!this._i)
this._i = 97 // 'a'
else
this._i++
var b = new Buffer(chunk.length)
for (var i = 0; i < chunk.length; i++)
b[i] = this._i
this.push(b)
callback()
})
th2.pipe(bl(function (err, b) {
var s = b.toString('ascii')
t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
t.end()
}))
th2.write(crypto.randomBytes(10))
th2.write(crypto.randomBytes(5))
th2.write(crypto.randomBytes(10))
th2.end()
})
test('pipeable through', function (t) {
var th2 = through2(function (chunk, enc, callback) {
if (!this._i)
this._i = 97 // 'a'
else
this._i++
var b = new Buffer(chunk.length)
for (var i = 0; i < chunk.length; i++)
b[i] = this._i
this.push(b)
callback()
})
th2.pipe(bl(function (err, b) {
var 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')
t.end()
}))
var bufs = bl()
bufs.append(crypto.randomBytes(10))
bufs.append(crypto.randomBytes(5))
bufs.append(crypto.randomBytes(10))
bufs.pipe(th2)
})
test('object through', function (t) {
t.plan(3)
var th2 = through2({ objectMode: true}, function (chunk, enc, callback) {
this.push({ out: chunk.in + 1 })
callback()
})
var e = 0
th2.on('data', function (o) {
t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
e++
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('object through with through2.obj', function (t) {
t.plan(3)
var th2 = through2.obj(function (chunk, enc, callback) {
this.push({ out: chunk.in + 1 })
callback()
})
var e = 0
th2.on('data', function (o) {
t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
e++
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('flushing through', function (t) {
var th2 = through2(function (chunk, enc, callback) {
if (!this._i)
this._i = 97 // 'a'
else
this._i++
var b = new Buffer(chunk.length)
for (var i = 0; i < chunk.length; i++)
b[i] = this._i
this.push(b)
callback()
}, function (callback) {
this.push(new Buffer([ 101, 110, 100 ]))
callback()
})
th2.pipe(bl(function (err, b) {
var s = b.toString('ascii')
t.equal(s, 'aaaaaaaaaabbbbbccccccccccend', 'got transformed string')
t.end()
}))
th2.write(crypto.randomBytes(10))
th2.write(crypto.randomBytes(5))
th2.write(crypto.randomBytes(10))
th2.end()
})
test('plain through ctor', function (t) {
var Th2 = through2.ctor(function (chunk, enc, callback) {
if (!this._i)
this._i = 97 // 'a'
else
this._i++
var b = new Buffer(chunk.length)
for (var i = 0; i < chunk.length; i++)
b[i] = this._i
this.push(b)
callback()
})
var th2 = new Th2()
th2.pipe(bl(function (err, b) {
var s = b.toString('ascii')
t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
t.end()
}))
th2.write(crypto.randomBytes(10))
th2.write(crypto.randomBytes(5))
th2.write(crypto.randomBytes(10))
th2.end()
})
test('reuse through ctor', function (t) {
t.plan(4)
var Th2 = through2.ctor(function (chunk, enc, callback) {
if (!this._i) {
t.ok(1, 'did not contain previous instance data (this._i)')
this._i = 97 // 'a'
} else
this._i++
var b = new Buffer(chunk.length)
for (var i = 0; i < chunk.length; i++)
b[i] = this._i
this.push(b)
callback()
})
var th2 = Th2()
th2.pipe(bl(function (err, b) {
var s = b.toString('ascii')
t.equal('aaaaaaaaaabbbbbcccccccccc', s, 'got transformed string')
var newInstance = Th2()
newInstance.pipe(bl(function (err, b) {
var s = b.toString('ascii')
t.equal('aaaaaaabbbbccccccc', s, 'got transformed string')
}))
newInstance.write(crypto.randomBytes(7))
newInstance.write(crypto.randomBytes(4))
newInstance.write(crypto.randomBytes(7))
newInstance.end()
}))
th2.write(crypto.randomBytes(10))
th2.write(crypto.randomBytes(5))
th2.write(crypto.randomBytes(10))
th2.end()
})
test('object through ctor', function (t) {
t.plan(3)
var Th2 = through2.ctor({ objectMode: true}, function (chunk, enc, callback) {
this.push({ out: chunk.in + 1 })
callback()
})
var th2 = new Th2()
var e = 0
th2.on('data', function (o) {
t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
e++
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('pipeable object through ctor', function (t) {
t.plan(4)
var 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()
})
var th2 = Th2()
var expect = [-19, -40, 100, 22]
th2.on('data', function (o) {
t.deepEqual(o, { temp: expect.shift(), unit: 'C' }, 'got transformed object')
})
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', function (t) {
t.plan(3)
var Th2 = through2.ctor(function (chunk, enc, callback) {
this.push({ out: chunk.in + 1 })
callback()
})
var th2 = Th2({objectMode: true})
var e = 0
th2.on('data', function (o) {
t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
e++
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('object settings available in transform', function (t) {
t.plan(6)
var 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()
})
var th2 = Th2()
var e = 0
th2.on('data', function (o) {
t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
e++
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('object settings available in transform override', function (t) {
t.plan(6)
var Th2 = through2.ctor(function (chunk, enc, callback) {
t.ok(this.options.peek, "reading options from inside _transform")
this.push({ out: chunk.in + 1 })
callback()
})
var th2 = Th2({objectMode: true, peek: true})
var e = 0
th2.on('data', function (o) {
t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
e++
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('object override extends options', function (t) {
t.plan(6)
var 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()
})
var th2 = Th2({peek: true})
var e = 0
th2.on('data', function (o) {
t.deepEqual(o, { out: e === 0 ? 102 : e == 1 ? 203 : -99 }, 'got transformed object')
e++
})
th2.write({ in: 101 })
th2.write({ in: 202 })
th2.write({ in: -100 })
th2.end()
})
test('can be destroyed', function(t) {
t.plan(1)
var th = through2()
th.on('close', function() {
t.ok(true, 'shoud emit close')
t.end()
})
th.destroy()
})
test('can be destroyed twice', function(t) {
t.plan(1)
var th = through2()
th.on('close', function() {
t.ok(true, 'shoud emit close')
t.end()
})
th.destroy()
th.destroy()
}) node-through2-2.0.3/test/bench.js 0000664 0000000 0000000 00000001661 13017142405 0016576 0 ustar 00root root 0000000 0000000 /*
on rvagg@fletcher:
@0.2.3: Ran 36719 iterations in 10000 ms
*/
const through2 = require('../')
, bl = require('bl')
, crypto = require('crypto')
, assert = require('assert')
function run (callback) {
var bufs = Array.apply(null, Array(10)).map(function () { return crypto.randomBytes(32) })
, stream = through2(function (chunk, env, callback) {
callback(null, chunk.toString('hex'))
})
stream.pipe(bl(function (err, data) {
assert(!err)
assert.equal(data.toString(), Buffer.concat(bufs).toString('hex'))
callback()
}))
bufs.forEach(function (b) {
stream.write(b)
})
stream.end()
}
var count = 0
, start = Date.now()
;(function exec () {
count++
run(function () {
if (Date.now() - start < 1000 * 10) {
return setImmediate(exec)
}
console.log('Ran', count, 'iterations in', Date.now() - start, 'ms')
})
}())
console.log('Running for ~10s') node-through2-2.0.3/test/sauce.js 0000664 0000000 0000000 00000003510 13017142405 0016612 0 ustar 00root root 0000000 0000000 #!/usr/bin/env node
var user = process.env.SAUCE_USER
, key = process.env.SAUCE_KEY
, path = require('path')
, brtapsauce
, testFile = path.join(__dirname, 'basic-test.js')
try {
brtapsauce = require('brtapsauce')
} catch (e) {
return
}
const capabilities = [
{ browserName: 'chrome' , platform: 'Windows XP', version: '' }
, { browserName: 'firefox' , platform: 'Windows 8' , version: '' }
, { browserName: 'firefox' , platform: 'Windows XP', version: '4' }
, { browserName: 'internet explorer' , platform: 'Windows 8' , version: '10' }
, { browserName: 'internet explorer' , platform: 'Windows 7' , version: '9' }
, { browserName: 'internet explorer' , platform: 'Windows 7' , version: '8' }
, { browserName: 'internet explorer' , platform: 'Windows XP', version: '7' }
, { browserName: 'internet explorer' , platform: 'Windows XP', version: '6' }
, { browserName: 'safari' , platform: 'Windows 7' , version: '5' }
, { browserName: 'safari' , platform: 'OS X 10.8' , version: '6' }
, { browserName: 'opera' , platform: 'Windows 7' , version: '' }
, { browserName: 'opera' , platform: 'Windows 7' , version: '11' }
, { browserName: 'ipad' , platform: 'OS X 10.8' , version: '6' }
, { browserName: 'android' , platform: 'Linux' , version: '4.0', 'device-type': 'tablet' }
]
if (!user)
throw new Error('Must set a SAUCE_USER env var')
if (!key)
throw new Error('Must set a SAUCE_KEY env var')
brtapsauce({
name : 'Traversty'
, user : user
, key : key
, brsrc : testFile
, capabilities : capabilities
, options : { timeout: 60 * 6 }
}) node-through2-2.0.3/test/test.js 0000664 0000000 0000000 00000000443 13017142405 0016473 0 ustar 00root root 0000000 0000000 require('./basic-test')
if (!process.env.SAUCE_KEY || !process.env.SAUCE_USER)
return console.log('SAUCE_KEY and/or SAUCE_USER not set, not running sauce tests')
if (!/v0\.10/.test(process.version))
return console.log('Not Node v0.10.x, not running sauce tests')
require('./sauce.js') node-through2-2.0.3/through2.js 0000664 0000000 0000000 00000004134 13017142405 0016300 0 ustar 00root root 0000000 0000000 var Transform = require('readable-stream/transform')
, inherits = require('util').inherits
, xtend = require('xtend')
function DestroyableTransform(opts) {
Transform.call(this, opts)
this._destroyed = false
}
inherits(DestroyableTransform, Transform)
DestroyableTransform.prototype.destroy = function(err) {
if (this._destroyed) return
this._destroyed = true
var self = this
process.nextTick(function() {
if (err)
self.emit('error', err)
self.emit('close')
})
}
// a noop _transform function
function noop (chunk, enc, callback) {
callback(null, chunk)
}
// 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 function (options, transform, flush) {
if (typeof options == 'function') {
flush = transform
transform = options
options = {}
}
if (typeof transform != 'function')
transform = noop
if (typeof flush != 'function')
flush = null
return construct(options, transform, flush)
}
}
// main export, just make me a transform stream!
module.exports = through2(function (options, transform, flush) {
var t2 = new DestroyableTransform(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
module.exports.ctor = through2(function (options, transform, flush) {
function Through2 (override) {
if (!(this instanceof Through2))
return new Through2(override)
this.options = xtend(options, override)
DestroyableTransform.call(this, this.options)
}
inherits(Through2, DestroyableTransform)
Through2.prototype._transform = transform
if (flush)
Through2.prototype._flush = flush
return Through2
})
module.exports.obj = through2(function (options, transform, flush) {
var t2 = new DestroyableTransform(xtend({ objectMode: true, highWaterMark: 16 }, options))
t2._transform = transform
if (flush)
t2._flush = flush
return t2
})