3 module.exports = Readable;
6 var processNextTick = require('process-nextick-args');
11 var isArray = require('isarray');
16 var Buffer = require('buffer').Buffer;
19 Readable.ReadableState = ReadableState;
21 var EE = require('events').EventEmitter;
24 if (!EE.listenerCount) EE.listenerCount = function(emitter, type) {
25 return emitter.listeners(type).length;
34 Stream = require('st' + 'ream');
37 Stream = require('events').EventEmitter;
41 var Buffer = require('buffer').Buffer;
44 var util = require('core-util-is');
45 util.inherits = require('inherits');
51 var debug = require('util');
52 if (debug && debug.debuglog) {
53 debug = debug.debuglog('stream');
55 debug = function () {};
61 util.inherits(Readable, Stream);
63 function ReadableState(options, stream) {
64 var Duplex = require('./_stream_duplex');
66 options = options || {};
68 // object stream flag. Used to make read(n) ignore n and to
69 // make all the buffer merging and length checks go away
70 this.objectMode = !!options.objectMode;
72 if (stream instanceof Duplex)
73 this.objectMode = this.objectMode || !!options.readableObjectMode;
75 // the point at which it stops calling _read() to fill the buffer
76 // Note: 0 is a valid value, means "don't call _read preemptively ever"
77 var hwm = options.highWaterMark;
78 var defaultHwm = this.objectMode ? 16 : 16 * 1024;
79 this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
82 this.highWaterMark = ~~this.highWaterMark;
90 this.endEmitted = false;
93 // a flag to be able to tell if the onwrite cb is called immediately,
94 // or on a later tick. We set this to true at first, because any
95 // actions that shouldn't happen until "later" should generally also
96 // not happen before the first write call.
99 // whenever we return null, then we set a flag to say
100 // that we're awaiting a 'readable' event emission.
101 this.needReadable = false;
102 this.emittedReadable = false;
103 this.readableListening = false;
105 // Crypto is kind of old and crusty. Historically, its default string
106 // encoding is 'binary' so we have to make this configurable.
107 // Everything else in the universe uses 'utf8', though.
108 this.defaultEncoding = options.defaultEncoding || 'utf8';
110 // when piping, we only care about 'readable' events that happen
111 // after read()ing all the bytes and not getting any pushback.
114 // the number of writers that are awaiting a drain event in .pipe()s
117 // if true, a maybeReadMore has been scheduled
118 this.readingMore = false;
121 this.encoding = null;
122 if (options.encoding) {
124 StringDecoder = require('string_decoder/').StringDecoder;
125 this.decoder = new StringDecoder(options.encoding);
126 this.encoding = options.encoding;
130 function Readable(options) {
131 var Duplex = require('./_stream_duplex');
133 if (!(this instanceof Readable))
134 return new Readable(options);
136 this._readableState = new ReadableState(options, this);
139 this.readable = true;
141 if (options && typeof options.read === 'function')
142 this._read = options.read;
147 // Manually shove something into the read() buffer.
148 // This returns true if the highWaterMark has not been hit yet,
149 // similar to how Writable.write() returns true if you should
150 // write() some more.
151 Readable.prototype.push = function(chunk, encoding) {
152 var state = this._readableState;
154 if (!state.objectMode && typeof chunk === 'string') {
155 encoding = encoding || state.defaultEncoding;
156 if (encoding !== state.encoding) {
157 chunk = new Buffer(chunk, encoding);
162 return readableAddChunk(this, state, chunk, encoding, false);
165 // Unshift should *always* be something directly out of read()
166 Readable.prototype.unshift = function(chunk) {
167 var state = this._readableState;
168 return readableAddChunk(this, state, chunk, '', true);
171 Readable.prototype.isPaused = function() {
172 return this._readableState.flowing === false;
175 function readableAddChunk(stream, state, chunk, encoding, addToFront) {
176 var er = chunkInvalid(state, chunk);
178 stream.emit('error', er);
179 } else if (chunk === null) {
180 state.reading = false;
181 onEofChunk(stream, state);
182 } else if (state.objectMode || chunk && chunk.length > 0) {
183 if (state.ended && !addToFront) {
184 var e = new Error('stream.push() after EOF');
185 stream.emit('error', e);
186 } else if (state.endEmitted && addToFront) {
187 var e = new Error('stream.unshift() after end event');
188 stream.emit('error', e);
190 if (state.decoder && !addToFront && !encoding)
191 chunk = state.decoder.write(chunk);
194 state.reading = false;
196 // if we want the data now, just emit it.
197 if (state.flowing && state.length === 0 && !state.sync) {
198 stream.emit('data', chunk);
201 // update the buffer info.
202 state.length += state.objectMode ? 1 : chunk.length;
204 state.buffer.unshift(chunk);
206 state.buffer.push(chunk);
208 if (state.needReadable)
209 emitReadable(stream);
212 maybeReadMore(stream, state);
214 } else if (!addToFront) {
215 state.reading = false;
218 return needMoreData(state);
223 // if it's past the high water mark, we can push in some more.
224 // Also, if we have no data yet, we can stand some
225 // more bytes. This is to work around cases where hwm=0,
226 // such as the repl. Also, if the push() triggered a
227 // readable event, and the user called read(largeNumber) such that
228 // needReadable was set, then we ought to push more, so that another
229 // 'readable' event will be triggered.
230 function needMoreData(state) {
231 return !state.ended &&
232 (state.needReadable ||
233 state.length < state.highWaterMark ||
237 // backwards compatibility.
238 Readable.prototype.setEncoding = function(enc) {
240 StringDecoder = require('string_decoder/').StringDecoder;
241 this._readableState.decoder = new StringDecoder(enc);
242 this._readableState.encoding = enc;
246 // Don't raise the hwm > 128MB
247 var MAX_HWM = 0x800000;
248 function roundUpToNextPowerOf2(n) {
252 // Get the next highest power of 2
254 for (var p = 1; p < 32; p <<= 1) n |= n >> p;
260 function howMuchToRead(n, state) {
261 if (state.length === 0 && state.ended)
264 if (state.objectMode)
265 return n === 0 ? 0 : 1;
267 if (n === null || isNaN(n)) {
268 // only flow one buffer at a time
269 if (state.flowing && state.buffer.length)
270 return state.buffer[0].length;
278 // If we're asking for more than the target buffer level,
279 // then raise the water mark. Bump up to the next highest
280 // power of 2, to prevent increasing it excessively in tiny
282 if (n > state.highWaterMark)
283 state.highWaterMark = roundUpToNextPowerOf2(n);
285 // don't have that much. return null, unless we've ended.
286 if (n > state.length) {
288 state.needReadable = true;
298 // you can override either this method, or the async _read(n) below.
299 Readable.prototype.read = function(n) {
301 var state = this._readableState;
304 if (typeof n !== 'number' || n > 0)
305 state.emittedReadable = false;
307 // if we're doing read(0) to trigger a readable event, but we
308 // already have a bunch of data in the buffer, then just trigger
309 // the 'readable' event and move on.
311 state.needReadable &&
312 (state.length >= state.highWaterMark || state.ended)) {
313 debug('read: emitReadable', state.length, state.ended);
314 if (state.length === 0 && state.ended)
321 n = howMuchToRead(n, state);
323 // if we've ended, and we're now clear, then finish it up.
324 if (n === 0 && state.ended) {
325 if (state.length === 0)
330 // All the actual chunk generation logic needs to be
331 // *below* the call to _read. The reason is that in certain
332 // synthetic stream cases, such as passthrough streams, _read
333 // may be a completely synchronous operation which may change
334 // the state of the read buffer, providing enough data when
335 // before there was *not* enough.
337 // So, the steps are:
338 // 1. Figure out what the state of things will be after we do
339 // a read from the buffer.
341 // 2. If that resulting state will trigger a _read, then call _read.
342 // Note that this may be asynchronous, or synchronous. Yes, it is
343 // deeply ugly to write APIs this way, but that still doesn't mean
344 // that the Readable class should behave improperly, as streams are
345 // designed to be sync/async agnostic.
346 // Take note if the _read call is sync or async (ie, if the read call
347 // has returned yet), so that we know whether or not it's safe to emit
350 // 3. Actually pull the requested chunks out of the buffer and return.
352 // if we need a readable event, then we need to do some reading.
353 var doRead = state.needReadable;
354 debug('need readable', doRead);
356 // if we currently have less than the highWaterMark, then also read some
357 if (state.length === 0 || state.length - n < state.highWaterMark) {
359 debug('length less than watermark', doRead);
362 // however, if we've ended, then there's no point, and if we're already
363 // reading, then it's unnecessary.
364 if (state.ended || state.reading) {
366 debug('reading or ended', doRead);
371 state.reading = true;
373 // if the length is currently zero, then we *need* a readable event.
374 if (state.length === 0)
375 state.needReadable = true;
376 // call internal read method
377 this._read(state.highWaterMark);
381 // If _read pushed data synchronously, then `reading` will be false,
382 // and we need to re-evaluate how much data we can return to the user.
383 if (doRead && !state.reading)
384 n = howMuchToRead(nOrig, state);
388 ret = fromList(n, state);
393 state.needReadable = true;
399 // If we have nothing in the buffer, then we want to know
400 // as soon as we *do* get something into the buffer.
401 if (state.length === 0 && !state.ended)
402 state.needReadable = true;
404 // If we tried to read() past the EOF, then emit end on the next tick.
405 if (nOrig !== n && state.ended && state.length === 0)
409 this.emit('data', ret);
414 function chunkInvalid(state, chunk) {
416 if (!(Buffer.isBuffer(chunk)) &&
417 typeof chunk !== 'string' &&
419 chunk !== undefined &&
421 er = new TypeError('Invalid non-string/buffer chunk');
427 function onEofChunk(stream, state) {
428 if (state.ended) return;
430 var chunk = state.decoder.end();
431 if (chunk && chunk.length) {
432 state.buffer.push(chunk);
433 state.length += state.objectMode ? 1 : chunk.length;
438 // emit 'readable' now to make sure it gets picked up.
439 emitReadable(stream);
442 // Don't emit readable right away in sync mode, because this can trigger
443 // another read() call => stack overflow. This way, it might trigger
444 // a nextTick recursion warning, but that's not so bad.
445 function emitReadable(stream) {
446 var state = stream._readableState;
447 state.needReadable = false;
448 if (!state.emittedReadable) {
449 debug('emitReadable', state.flowing);
450 state.emittedReadable = true;
452 processNextTick(emitReadable_, stream);
454 emitReadable_(stream);
458 function emitReadable_(stream) {
459 debug('emit readable');
460 stream.emit('readable');
465 // at this point, the user has presumably seen the 'readable' event,
466 // and called read() to consume some data. that may have triggered
467 // in turn another _read(n) call, in which case reading = true if
469 // However, if we're not ended, or reading, and the length < hwm,
470 // then go ahead and try to read some more preemptively.
471 function maybeReadMore(stream, state) {
472 if (!state.readingMore) {
473 state.readingMore = true;
474 processNextTick(maybeReadMore_, stream, state);
478 function maybeReadMore_(stream, state) {
479 var len = state.length;
480 while (!state.reading && !state.flowing && !state.ended &&
481 state.length < state.highWaterMark) {
482 debug('maybeReadMore read 0');
484 if (len === state.length)
485 // didn't get any data, stop spinning.
490 state.readingMore = false;
493 // abstract method. to be overridden in specific implementation classes.
494 // call cb(er, data) where data is <= n in length.
495 // for virtual (non-string, non-buffer) streams, "length" is somewhat
496 // arbitrary, and perhaps not very meaningful.
497 Readable.prototype._read = function(n) {
498 this.emit('error', new Error('not implemented'));
501 Readable.prototype.pipe = function(dest, pipeOpts) {
503 var state = this._readableState;
505 switch (state.pipesCount) {
510 state.pipes = [state.pipes, dest];
513 state.pipes.push(dest);
516 state.pipesCount += 1;
517 debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
519 var doEnd = (!pipeOpts || pipeOpts.end !== false) &&
520 dest !== process.stdout &&
521 dest !== process.stderr;
523 var endFn = doEnd ? onend : cleanup;
524 if (state.endEmitted)
525 processNextTick(endFn);
527 src.once('end', endFn);
529 dest.on('unpipe', onunpipe);
530 function onunpipe(readable) {
532 if (readable === src) {
542 // when the dest drains, it reduces the awaitDrain counter
543 // on the source. This would be more elegant with a .once()
544 // handler in flow(), but adding and removing repeatedly is
546 var ondrain = pipeOnDrain(src);
547 dest.on('drain', ondrain);
551 // cleanup event handlers once the pipe is broken
552 dest.removeListener('close', onclose);
553 dest.removeListener('finish', onfinish);
554 dest.removeListener('drain', ondrain);
555 dest.removeListener('error', onerror);
556 dest.removeListener('unpipe', onunpipe);
557 src.removeListener('end', onend);
558 src.removeListener('end', cleanup);
559 src.removeListener('data', ondata);
561 // if the reader is waiting for a drain event from this
562 // specific writer, then it would cause it to never start
564 // So, if this is awaiting a drain, then we just call it now.
565 // If we don't know, then assume that we are waiting for one.
566 if (state.awaitDrain &&
567 (!dest._writableState || dest._writableState.needDrain))
571 src.on('data', ondata);
572 function ondata(chunk) {
574 var ret = dest.write(chunk);
576 debug('false write response, pause',
577 src._readableState.awaitDrain);
578 src._readableState.awaitDrain++;
583 // if the dest has an error, then stop piping into it.
584 // however, don't suppress the throwing behavior for this.
585 function onerror(er) {
586 debug('onerror', er);
588 dest.removeListener('error', onerror);
589 if (EE.listenerCount(dest, 'error') === 0)
590 dest.emit('error', er);
592 // This is a brutally ugly hack to make sure that our error handler
593 // is attached before any userland ones. NEVER DO THIS.
594 if (!dest._events || !dest._events.error)
595 dest.on('error', onerror);
596 else if (isArray(dest._events.error))
597 dest._events.error.unshift(onerror);
599 dest._events.error = [onerror, dest._events.error];
603 // Both close and finish should trigger unpipe, but only once.
605 dest.removeListener('finish', onfinish);
608 dest.once('close', onclose);
609 function onfinish() {
611 dest.removeListener('close', onclose);
614 dest.once('finish', onfinish);
621 // tell the dest that it's being piped to
622 dest.emit('pipe', src);
624 // start the flow if it hasn't been started already.
625 if (!state.flowing) {
626 debug('pipe resume');
633 function pipeOnDrain(src) {
635 var state = src._readableState;
636 debug('pipeOnDrain', state.awaitDrain);
637 if (state.awaitDrain)
639 if (state.awaitDrain === 0 && EE.listenerCount(src, 'data')) {
640 state.flowing = true;
647 Readable.prototype.unpipe = function(dest) {
648 var state = this._readableState;
650 // if we're not piping anywhere, then do nothing.
651 if (state.pipesCount === 0)
654 // just one destination. most common case.
655 if (state.pipesCount === 1) {
656 // passed in one, but it's not the right one.
657 if (dest && dest !== state.pipes)
665 state.pipesCount = 0;
666 state.flowing = false;
668 dest.emit('unpipe', this);
672 // slow case. multiple pipe destinations.
676 var dests = state.pipes;
677 var len = state.pipesCount;
679 state.pipesCount = 0;
680 state.flowing = false;
682 for (var i = 0; i < len; i++)
683 dests[i].emit('unpipe', this);
687 // try to find the right one.
688 var i = indexOf(state.pipes, dest);
692 state.pipes.splice(i, 1);
693 state.pipesCount -= 1;
694 if (state.pipesCount === 1)
695 state.pipes = state.pipes[0];
697 dest.emit('unpipe', this);
702 // set up data events if they are asked for
703 // Ensure readable listeners eventually get something
704 Readable.prototype.on = function(ev, fn) {
705 var res = Stream.prototype.on.call(this, ev, fn);
707 // If listening to data, and it has not explicitly been paused,
708 // then call resume to start the flow of data on the next tick.
709 if (ev === 'data' && false !== this._readableState.flowing) {
713 if (ev === 'readable' && this.readable) {
714 var state = this._readableState;
715 if (!state.readableListening) {
716 state.readableListening = true;
717 state.emittedReadable = false;
718 state.needReadable = true;
719 if (!state.reading) {
720 processNextTick(nReadingNextTick, this);
721 } else if (state.length) {
722 emitReadable(this, state);
729 Readable.prototype.addListener = Readable.prototype.on;
731 function nReadingNextTick(self) {
732 debug('readable nexttick read 0');
736 // pause() and resume() are remnants of the legacy readable stream API
737 // If the user uses them, then switch into old mode.
738 Readable.prototype.resume = function() {
739 var state = this._readableState;
740 if (!state.flowing) {
742 state.flowing = true;
748 function resume(stream, state) {
749 if (!state.resumeScheduled) {
750 state.resumeScheduled = true;
751 processNextTick(resume_, stream, state);
755 function resume_(stream, state) {
756 if (!state.reading) {
757 debug('resume read 0');
761 state.resumeScheduled = false;
762 stream.emit('resume');
764 if (state.flowing && !state.reading)
768 Readable.prototype.pause = function() {
769 debug('call pause flowing=%j', this._readableState.flowing);
770 if (false !== this._readableState.flowing) {
772 this._readableState.flowing = false;
778 function flow(stream) {
779 var state = stream._readableState;
780 debug('flow', state.flowing);
783 var chunk = stream.read();
784 } while (null !== chunk && state.flowing);
788 // wrap an old-style stream as the async data source.
789 // This is *not* part of the readable stream interface.
790 // It is an ugly unfortunate mess of history.
791 Readable.prototype.wrap = function(stream) {
792 var state = this._readableState;
796 stream.on('end', function() {
797 debug('wrapped end');
798 if (state.decoder && !state.ended) {
799 var chunk = state.decoder.end();
800 if (chunk && chunk.length)
807 stream.on('data', function(chunk) {
808 debug('wrapped data');
810 chunk = state.decoder.write(chunk);
812 // don't skip over falsy values in objectMode
813 if (state.objectMode && (chunk === null || chunk === undefined))
815 else if (!state.objectMode && (!chunk || !chunk.length))
818 var ret = self.push(chunk);
825 // proxy all the other methods.
826 // important when wrapping filters and duplexes.
827 for (var i in stream) {
828 if (this[i] === undefined && typeof stream[i] === 'function') {
829 this[i] = function(method) { return function() {
830 return stream[method].apply(stream, arguments);
835 // proxy certain important events.
836 var events = ['error', 'close', 'destroy', 'pause', 'resume'];
837 forEach(events, function(ev) {
838 stream.on(ev, self.emit.bind(self, ev));
841 // when we try to consume some more bytes, simply unpause the
842 // underlying stream.
843 self._read = function(n) {
844 debug('wrapped _read', n);
856 // exposed for testing purposes only.
857 Readable._fromList = fromList;
859 // Pluck off n bytes from an array of buffers.
860 // Length is the combined lengths of all the buffers in the list.
861 function fromList(n, state) {
862 var list = state.buffer;
863 var length = state.length;
864 var stringMode = !!state.decoder;
865 var objectMode = !!state.objectMode;
868 // nothing in the list, definitely empty.
869 if (list.length === 0)
876 else if (!n || n >= length) {
877 // read it all, truncate the array.
881 ret = Buffer.concat(list, length);
884 // read just some of it.
885 if (n < list[0].length) {
886 // just take a part of the first list item.
887 // slice is the same for buffers and strings.
889 ret = buf.slice(0, n);
890 list[0] = buf.slice(n);
891 } else if (n === list[0].length) {
892 // first list is a perfect match
896 // we have enough to cover it, but it spans past the first buffer.
903 for (var i = 0, l = list.length; i < l && c < n; i++) {
905 var cpy = Math.min(n - c, buf.length);
908 ret += buf.slice(0, cpy);
910 buf.copy(ret, c, 0, cpy);
912 if (cpy < buf.length)
913 list[0] = buf.slice(cpy);
925 function endReadable(stream) {
926 var state = stream._readableState;
928 // If we get here before consuming all the bytes, then that is a
929 // bug in node. Should never happen.
930 if (state.length > 0)
931 throw new Error('endReadable called on non-empty stream');
933 if (!state.endEmitted) {
935 processNextTick(endReadableNT, state, stream);
939 function endReadableNT(state, stream) {
940 // Check that we didn't get one last unshift.
941 if (!state.endEmitted && state.length === 0) {
942 state.endEmitted = true;
943 stream.readable = false;
948 function forEach (xs, f) {
949 for (var i = 0, l = xs.length; i < l; i++) {
954 function indexOf (xs, x) {
955 for (var i = 0, l = xs.length; i < l; i++) {
956 if (xs[i] === x) return i;