1 var Transform = require('readable-stream/transform')
2 , inherits = require('util').inherits
3 , xtend = require('xtend')
5 function DestroyableTransform(opts) {
6 Transform.call(this, opts)
7 this._destroyed = false
10 inherits(DestroyableTransform, Transform)
12 DestroyableTransform.prototype.destroy = function(err) {
13 if (this._destroyed) return
14 this._destroyed = true
17 process.nextTick(function() {
19 self.emit('error', err)
24 // a noop _transform function
25 function noop (chunk, enc, callback) {
30 // create a new export function, used by both the main export and
31 // the .ctor export, contains common logic for dealing with arguments
32 function through2 (construct) {
33 return function (options, transform, flush) {
34 if (typeof options == 'function') {
40 if (typeof transform != 'function')
43 if (typeof flush != 'function')
46 return construct(options, transform, flush)
51 // main export, just make me a transform stream!
52 module.exports = through2(function (options, transform, flush) {
53 var t2 = new DestroyableTransform(options)
55 t2._transform = transform
64 // make me a reusable prototype that I can `new`, or implicitly `new`
65 // with a constructor call
66 module.exports.ctor = through2(function (options, transform, flush) {
67 function Through2 (override) {
68 if (!(this instanceof Through2))
69 return new Through2(override)
71 this.options = xtend(options, override)
73 DestroyableTransform.call(this, this.options)
76 inherits(Through2, DestroyableTransform)
78 Through2.prototype._transform = transform
81 Through2.prototype._flush = flush
87 module.exports.obj = through2(function (options, transform, flush) {
88 var t2 = new DestroyableTransform(xtend({ objectMode: true, highWaterMark: 16 }, options))
90 t2._transform = transform