react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / through2 / through2.js
80709 viewsvar Transform = require('readable-stream/transform')1, inherits = require('util').inherits2, xtend = require('xtend')34function DestroyableTransform(opts) {5Transform.call(this, opts)6this._destroyed = false7}89inherits(DestroyableTransform, Transform)1011DestroyableTransform.prototype.destroy = function(err) {12if (this._destroyed) return13this._destroyed = true1415var self = this16process.nextTick(function() {17if (err)18self.emit('error', err)19self.emit('close')20})21}2223// a noop _transform function24function noop (chunk, enc, callback) {25callback(null, chunk)26}272829// create a new export function, used by both the main export and30// the .ctor export, contains common logic for dealing with arguments31function through2 (construct) {32return function (options, transform, flush) {33if (typeof options == 'function') {34flush = transform35transform = options36options = {}37}3839if (typeof transform != 'function')40transform = noop4142if (typeof flush != 'function')43flush = null4445return construct(options, transform, flush)46}47}484950// main export, just make me a transform stream!51module.exports = through2(function (options, transform, flush) {52var t2 = new DestroyableTransform(options)5354t2._transform = transform5556if (flush)57t2._flush = flush5859return t260})616263// make me a reusable prototype that I can `new`, or implicitly `new`64// with a constructor call65module.exports.ctor = through2(function (options, transform, flush) {66function Through2 (override) {67if (!(this instanceof Through2))68return new Through2(override)6970this.options = xtend(options, override)7172DestroyableTransform.call(this, this.options)73}7475inherits(Through2, DestroyableTransform)7677Through2.prototype._transform = transform7879if (flush)80Through2.prototype._flush = flush8182return Through283})848586module.exports.obj = through2(function (options, transform, flush) {87var t2 = new DestroyableTransform(xtend({ objectMode: true, highWaterMark: 16 }, options))8889t2._transform = transform9091if (flush)92t2._flush = flush9394return t295})969798