react / wstein / node_modules / browserify / node_modules / labeled-stream-splicer / node_modules / stream-splicer / index.js
80536 viewsvar Duplex = require('readable-stream').Duplex;1var Readable = require('readable-stream').Readable;2var Pass = require('readable-stream').PassThrough;3var inherits = require('inherits');4var isArray = require('isarray');5var indexof = require('indexof');6var wrap = require('readable-wrap');78var nextTick = typeof setImmediate !== 'undefined'9? setImmediate : process.nextTick10;1112module.exports = Pipeline;13inherits(Pipeline, Duplex);1415module.exports.obj = function (streams, opts) {16if (!opts && !isArray(streams)) {17opts = streams;18streams = [];19}20if (!streams) streams = [];21if (!opts) opts = {};22opts.objectMode = true;23return new Pipeline(streams, opts);24};2526function Pipeline (streams, opts) {27if (!(this instanceof Pipeline)) return new Pipeline(streams, opts);28if (!opts && !isArray(streams)) {29opts = streams;30streams = [];31}32if (!streams) streams = [];33if (!opts) opts = {};34Duplex.call(this, opts);3536var self = this;37this._options = opts;38this._wrapOptions = { objectMode: opts.objectMode !== false };39this._streams = [];4041this.splice.apply(this, [ 0, 0 ].concat(streams));4243this.once('finish', function () {44self._streams[0].end();45});46}4748Pipeline.prototype._read = function () {49var self = this;50this._notEmpty();5152var r = this._streams[this._streams.length-1];53var buf, reads = 0;54while ((buf = r.read()) !== null) {55Duplex.prototype.push.call(this, buf);56reads ++;57}58if (reads === 0) {59var onreadable = function () {60r.removeListener('readable', onreadable);61self.removeListener('_mutate', onreadable);62self._read()63};64r.once('readable', onreadable);65self.once('_mutate', onreadable);66}67};6869Pipeline.prototype._write = function (buf, enc, next) {70this._notEmpty();71this._streams[0]._write(buf, enc, next);72};7374Pipeline.prototype._notEmpty = function () {75var self = this;76if (this._streams.length > 0) return;77var stream = new Pass(this._options);78stream.once('end', function () {79var ix = indexof(self._streams, stream);80if (ix >= 0 && ix === self._streams.length - 1) {81Duplex.prototype.push.call(self, null);82}83});84this._streams.push(stream);85this.length = this._streams.length;86};8788Pipeline.prototype.push = function (stream) {89var args = [ this._streams.length, 0 ].concat([].slice.call(arguments));90this.splice.apply(this, args);91return this._streams.length;92};9394Pipeline.prototype.pop = function () {95return this.splice(this._streams.length-1,1)[0];96};9798Pipeline.prototype.shift = function () {99return this.splice(0,1)[0];100};101102Pipeline.prototype.unshift = function () {103this.splice.apply(this, [0,0].concat([].slice.call(arguments)));104return this._streams.length;105};106107Pipeline.prototype.splice = function (start, removeLen) {108var self = this;109var len = this._streams.length;110start = start < 0 ? len - start : start;111if (removeLen === undefined) removeLen = len - start;112removeLen = Math.max(0, Math.min(len - start, removeLen));113114for (var i = start; i < start + removeLen; i++) {115if (self._streams[i-1]) {116self._streams[i-1].unpipe(self._streams[i]);117}118}119if (self._streams[i-1] && self._streams[i]) {120self._streams[i-1].unpipe(self._streams[i]);121}122var end = i;123124var reps = [], args = arguments;125for (var j = 2; j < args.length; j++) (function (stream) {126if (isArray(stream)) {127stream = new Pipeline(stream, self._options);128}129stream.on('error', function (err) {130err.stream = this;131self.emit('error', err);132});133stream = self._wrapStream(stream);134stream.once('end', function () {135var ix = indexof(self._streams, stream);136if (ix >= 0 && ix === self._streams.length - 1) {137Duplex.prototype.push.call(self, null);138}139});140reps.push(stream);141})(arguments[j]);142143for (var i = 0; i < reps.length - 1; i++) {144reps[i].pipe(reps[i+1]);145}146147if (reps.length && self._streams[end]) {148reps[reps.length-1].pipe(self._streams[end]);149}150if (reps[0] && self._streams[start-1]) {151self._streams[start-1].pipe(reps[0]);152}153154var sargs = [start,removeLen].concat(reps);155var removed = self._streams.splice.apply(self._streams, sargs);156157this.emit('_mutate');158this.length = this._streams.length;159return removed;160};161162Pipeline.prototype.get = function () {163if (arguments.length === 0) return undefined;164165var base = this;166for (var i = 0; i < arguments.length; i++) {167var index = arguments[i];168if (index < 0) {169base = base._streams[base._streams.length + index];170}171else {172base = base._streams[index];173}174if (!base) return undefined;175}176return base;177};178179Pipeline.prototype.indexOf = function (stream) {180return indexof(this._streams, stream);181};182183Pipeline.prototype._wrapStream = function (stream) {184if (typeof stream.read === 'function') return stream;185var w = wrap(stream, this._wrapOptions);186w._write = function (buf, enc, next) {187if (stream.write(buf) === false) {188stream.once('drain', next);189}190else nextTick(next);191};192return w;193};194195196