react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / form-data / node_modules / combined-stream / lib / combined_stream.js
81154 viewsvar util = require('util');1var Stream = require('stream').Stream;2var DelayedStream = require('delayed-stream');34module.exports = CombinedStream;5function CombinedStream() {6this.writable = false;7this.readable = true;8this.dataSize = 0;9this.maxDataSize = 2 * 1024 * 1024;10this.pauseStreams = true;1112this._released = false;13this._streams = [];14this._currentStream = null;15}16util.inherits(CombinedStream, Stream);1718CombinedStream.create = function(options) {19var combinedStream = new this();2021options = options || {};22for (var option in options) {23combinedStream[option] = options[option];24}2526return combinedStream;27};2829CombinedStream.isStreamLike = function(stream) {30return (typeof stream !== 'function')31&& (typeof stream !== 'string')32&& (typeof stream !== 'boolean')33&& (typeof stream !== 'number')34&& (!Buffer.isBuffer(stream));35};3637CombinedStream.prototype.append = function(stream) {38var isStreamLike = CombinedStream.isStreamLike(stream);3940if (isStreamLike) {41if (!(stream instanceof DelayedStream)) {42var newStream = DelayedStream.create(stream, {43maxDataSize: Infinity,44pauseStream: this.pauseStreams,45});46stream.on('data', this._checkDataSize.bind(this));47stream = newStream;48}4950this._handleErrors(stream);5152if (this.pauseStreams) {53stream.pause();54}55}5657this._streams.push(stream);58return this;59};6061CombinedStream.prototype.pipe = function(dest, options) {62Stream.prototype.pipe.call(this, dest, options);63this.resume();64return dest;65};6667CombinedStream.prototype._getNext = function() {68this._currentStream = null;69var stream = this._streams.shift();707172if (typeof stream == 'undefined') {73this.end();74return;75}7677if (typeof stream !== 'function') {78this._pipeNext(stream);79return;80}8182var getStream = stream;83getStream(function(stream) {84var isStreamLike = CombinedStream.isStreamLike(stream);85if (isStreamLike) {86stream.on('data', this._checkDataSize.bind(this));87this._handleErrors(stream);88}8990this._pipeNext(stream);91}.bind(this));92};9394CombinedStream.prototype._pipeNext = function(stream) {95this._currentStream = stream;9697var isStreamLike = CombinedStream.isStreamLike(stream);98if (isStreamLike) {99stream.on('end', this._getNext.bind(this));100stream.pipe(this, {end: false});101return;102}103104var value = stream;105this.write(value);106this._getNext();107};108109CombinedStream.prototype._handleErrors = function(stream) {110var self = this;111stream.on('error', function(err) {112self._emitError(err);113});114};115116CombinedStream.prototype.write = function(data) {117this.emit('data', data);118};119120CombinedStream.prototype.pause = function() {121if (!this.pauseStreams) {122return;123}124125if(this.pauseStreams && this._currentStream && typeof(this._currentStream.pause) == 'function') this._currentStream.pause();126this.emit('pause');127};128129CombinedStream.prototype.resume = function() {130if (!this._released) {131this._released = true;132this.writable = true;133this._getNext();134}135136if(this.pauseStreams && this._currentStream && typeof(this._currentStream.resume) == 'function') this._currentStream.resume();137this.emit('resume');138};139140CombinedStream.prototype.end = function() {141this._reset();142this.emit('end');143};144145CombinedStream.prototype.destroy = function() {146this._reset();147this.emit('close');148};149150CombinedStream.prototype._reset = function() {151this.writable = false;152this._streams = [];153this._currentStream = null;154};155156CombinedStream.prototype._checkDataSize = function() {157this._updateDataSize();158if (this.dataSize <= this.maxDataSize) {159return;160}161162var message =163'DelayedStream#maxDataSize of ' + this.maxDataSize + ' bytes exceeded.';164this._emitError(new Error(message));165};166167CombinedStream.prototype._updateDataSize = function() {168this.dataSize = 0;169170var self = this;171this._streams.forEach(function(stream) {172if (!stream.dataSize) {173return;174}175176self.dataSize += stream.dataSize;177});178179if (this._currentStream && this._currentStream.dataSize) {180this.dataSize += this._currentStream.dataSize;181}182};183184CombinedStream.prototype._emitError = function(err) {185this._reset();186this.emit('error', err);187};188189190