3 module.exports = Readable;
6 var processNextTick = require('process-nextick-args');
10 var isArray = require('isarray');
17 Readable.ReadableState = ReadableState;
20 var EE = require('events').EventEmitter;
22 var EElistenerCount = function (emitter, type) {
23 return emitter.listeners(type).length;
31 Stream = require('st' + 'ream');
32 } catch (_) {} finally {
33 if (!Stream) Stream = require('events').EventEmitter;
38 var Buffer = require('buffer').Buffer;
40 var bufferShim = require('buffer-shims');
44 var util = require('core-util-is');
45 util.inherits = require('inherits');
49 var debugUtil = require('util');
51 if (debugUtil && debugUtil.debuglog) {
52 debug = debugUtil.debuglog('stream');
54 debug = function () {};
58 var BufferList = require('./internal/streams/BufferList');
61 util.inherits(Readable, Stream);
63 function prependListener(emitter, event, fn) {
64 // Sadly this is not cacheable as some libraries bundle their own
65 // event emitter implementation with them.
66 if (typeof emitter.prependListener === 'function') {
67 return emitter.prependListener(event, fn);
69 // This is a hack to make sure that our error handler is attached before any
70 // userland ones. NEVER DO THIS. This is here only because this code needs
71 // to continue to work with older versions of Node.js that do not include
72 // the prependListener() method. The goal is to eventually remove this hack.
73 if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]];
77 function ReadableState(options, stream) {
78 Duplex = Duplex || require('./_stream_duplex');
80 options = options || {};
82 // object stream flag. Used to make read(n) ignore n and to
83 // make all the buffer merging and length checks go away
84 this.objectMode = !!options.objectMode;
86 if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
88 // the point at which it stops calling _read() to fill the buffer
89 // Note: 0 is a valid value, means "don't call _read preemptively ever"
90 var hwm = options.highWaterMark;
91 var defaultHwm = this.objectMode ? 16 : 16 * 1024;
92 this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm;
95 this.highWaterMark = ~ ~this.highWaterMark;
97 // A linked list is used to store data chunks instead of an array because the
98 // linked list can remove elements from the beginning faster than
100 this.buffer = new BufferList();
106 this.endEmitted = false;
107 this.reading = false;
109 // a flag to be able to tell if the onwrite cb is called immediately,
110 // or on a later tick. We set this to true at first, because any
111 // actions that shouldn't happen until "later" should generally also
112 // not happen before the first write call.
115 // whenever we return null, then we set a flag to say
116 // that we're awaiting a 'readable' event emission.
117 this.needReadable = false;
118 this.emittedReadable = false;
119 this.readableListening = false;
120 this.resumeScheduled = false;
122 // Crypto is kind of old and crusty. Historically, its default string
123 // encoding is 'binary' so we have to make this configurable.
124 // Everything else in the universe uses 'utf8', though.
125 this.defaultEncoding = options.defaultEncoding || 'utf8';
127 // when piping, we only care about 'readable' events that happen
128 // after read()ing all the bytes and not getting any pushback.
131 // the number of writers that are awaiting a drain event in .pipe()s
134 // if true, a maybeReadMore has been scheduled
135 this.readingMore = false;
138 this.encoding = null;
139 if (options.encoding) {
140 if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
141 this.decoder = new StringDecoder(options.encoding);
142 this.encoding = options.encoding;
146 function Readable(options) {
147 Duplex = Duplex || require('./_stream_duplex');
149 if (!(this instanceof Readable)) return new Readable(options);
151 this._readableState = new ReadableState(options, this);
154 this.readable = true;
156 if (options && typeof options.read === 'function') this._read = options.read;
161 // Manually shove something into the read() buffer.
162 // This returns true if the highWaterMark has not been hit yet,
163 // similar to how Writable.write() returns true if you should
164 // write() some more.
165 Readable.prototype.push = function (chunk, encoding) {
166 var state = this._readableState;
168 if (!state.objectMode && typeof chunk === 'string') {
169 encoding = encoding || state.defaultEncoding;
170 if (encoding !== state.encoding) {
171 chunk = bufferShim.from(chunk, encoding);
176 return readableAddChunk(this, state, chunk, encoding, false);
179 // Unshift should *always* be something directly out of read()
180 Readable.prototype.unshift = function (chunk) {
181 var state = this._readableState;
182 return readableAddChunk(this, state, chunk, '', true);
185 Readable.prototype.isPaused = function () {
186 return this._readableState.flowing === false;
189 function readableAddChunk(stream, state, chunk, encoding, addToFront) {
190 var er = chunkInvalid(state, chunk);
192 stream.emit('error', er);
193 } else if (chunk === null) {
194 state.reading = false;
195 onEofChunk(stream, state);
196 } else if (state.objectMode || chunk && chunk.length > 0) {
197 if (state.ended && !addToFront) {
198 var e = new Error('stream.push() after EOF');
199 stream.emit('error', e);
200 } else if (state.endEmitted && addToFront) {
201 var _e = new Error('stream.unshift() after end event');
202 stream.emit('error', _e);
205 if (state.decoder && !addToFront && !encoding) {
206 chunk = state.decoder.write(chunk);
207 skipAdd = !state.objectMode && chunk.length === 0;
210 if (!addToFront) state.reading = false;
212 // Don't add to the buffer if we've decoded to an empty string chunk and
213 // we're not in object mode
215 // if we want the data now, just emit it.
216 if (state.flowing && state.length === 0 && !state.sync) {
217 stream.emit('data', chunk);
220 // update the buffer info.
221 state.length += state.objectMode ? 1 : chunk.length;
222 if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
224 if (state.needReadable) emitReadable(stream);
228 maybeReadMore(stream, state);
230 } else if (!addToFront) {
231 state.reading = false;
234 return needMoreData(state);
237 // if it's past the high water mark, we can push in some more.
238 // Also, if we have no data yet, we can stand some
239 // more bytes. This is to work around cases where hwm=0,
240 // such as the repl. Also, if the push() triggered a
241 // readable event, and the user called read(largeNumber) such that
242 // needReadable was set, then we ought to push more, so that another
243 // 'readable' event will be triggered.
244 function needMoreData(state) {
245 return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0);
248 // backwards compatibility.
249 Readable.prototype.setEncoding = function (enc) {
250 if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
251 this._readableState.decoder = new StringDecoder(enc);
252 this._readableState.encoding = enc;
256 // Don't raise the hwm > 8MB
257 var MAX_HWM = 0x800000;
258 function computeNewHighWaterMark(n) {
262 // Get the next highest power of 2 to prevent increasing hwm excessively in
275 // This function is designed to be inlinable, so please take care when making
276 // changes to the function body.
277 function howMuchToRead(n, state) {
278 if (n <= 0 || state.length === 0 && state.ended) return 0;
279 if (state.objectMode) return 1;
281 // Only flow one buffer at a time
282 if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
284 // If we're asking for more than the current hwm, then raise the hwm.
285 if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
286 if (n <= state.length) return n;
289 state.needReadable = true;
295 // you can override either this method, or the async _read(n) below.
296 Readable.prototype.read = function (n) {
299 var state = this._readableState;
302 if (n !== 0) state.emittedReadable = false;
304 // if we're doing read(0) to trigger a readable event, but we
305 // already have a bunch of data in the buffer, then just trigger
306 // the 'readable' event and move on.
307 if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) {
308 debug('read: emitReadable', state.length, state.ended);
309 if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
313 n = howMuchToRead(n, state);
315 // if we've ended, and we're now clear, then finish it up.
316 if (n === 0 && state.ended) {
317 if (state.length === 0) endReadable(this);
321 // All the actual chunk generation logic needs to be
322 // *below* the call to _read. The reason is that in certain
323 // synthetic stream cases, such as passthrough streams, _read
324 // may be a completely synchronous operation which may change
325 // the state of the read buffer, providing enough data when
326 // before there was *not* enough.
328 // So, the steps are:
329 // 1. Figure out what the state of things will be after we do
330 // a read from the buffer.
332 // 2. If that resulting state will trigger a _read, then call _read.
333 // Note that this may be asynchronous, or synchronous. Yes, it is
334 // deeply ugly to write APIs this way, but that still doesn't mean
335 // that the Readable class should behave improperly, as streams are
336 // designed to be sync/async agnostic.
337 // Take note if the _read call is sync or async (ie, if the read call
338 // has returned yet), so that we know whether or not it's safe to emit
341 // 3. Actually pull the requested chunks out of the buffer and return.
343 // if we need a readable event, then we need to do some reading.
344 var doRead = state.needReadable;
345 debug('need readable', doRead);
347 // if we currently have less than the highWaterMark, then also read some
348 if (state.length === 0 || state.length - n < state.highWaterMark) {
350 debug('length less than watermark', doRead);
353 // however, if we've ended, then there's no point, and if we're already
354 // reading, then it's unnecessary.
355 if (state.ended || state.reading) {
357 debug('reading or ended', doRead);
360 state.reading = true;
362 // if the length is currently zero, then we *need* a readable event.
363 if (state.length === 0) state.needReadable = true;
364 // call internal read method
365 this._read(state.highWaterMark);
367 // If _read pushed data synchronously, then `reading` will be false,
368 // and we need to re-evaluate how much data we can return to the user.
369 if (!state.reading) n = howMuchToRead(nOrig, state);
373 if (n > 0) ret = fromList(n, state);else ret = null;
376 state.needReadable = true;
382 if (state.length === 0) {
383 // If we have nothing in the buffer, then we want to know
384 // as soon as we *do* get something into the buffer.
385 if (!state.ended) state.needReadable = true;
387 // If we tried to read() past the EOF, then emit end on the next tick.
388 if (nOrig !== n && state.ended) endReadable(this);
391 if (ret !== null) this.emit('data', ret);
396 function chunkInvalid(state, chunk) {
398 if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) {
399 er = new TypeError('Invalid non-string/buffer chunk');
404 function onEofChunk(stream, state) {
405 if (state.ended) return;
407 var chunk = state.decoder.end();
408 if (chunk && chunk.length) {
409 state.buffer.push(chunk);
410 state.length += state.objectMode ? 1 : chunk.length;
415 // emit 'readable' now to make sure it gets picked up.
416 emitReadable(stream);
419 // Don't emit readable right away in sync mode, because this can trigger
420 // another read() call => stack overflow. This way, it might trigger
421 // a nextTick recursion warning, but that's not so bad.
422 function emitReadable(stream) {
423 var state = stream._readableState;
424 state.needReadable = false;
425 if (!state.emittedReadable) {
426 debug('emitReadable', state.flowing);
427 state.emittedReadable = true;
428 if (state.sync) processNextTick(emitReadable_, stream);else emitReadable_(stream);
432 function emitReadable_(stream) {
433 debug('emit readable');
434 stream.emit('readable');
438 // at this point, the user has presumably seen the 'readable' event,
439 // and called read() to consume some data. that may have triggered
440 // in turn another _read(n) call, in which case reading = true if
442 // However, if we're not ended, or reading, and the length < hwm,
443 // then go ahead and try to read some more preemptively.
444 function maybeReadMore(stream, state) {
445 if (!state.readingMore) {
446 state.readingMore = true;
447 processNextTick(maybeReadMore_, stream, state);
451 function maybeReadMore_(stream, state) {
452 var len = state.length;
453 while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) {
454 debug('maybeReadMore read 0');
456 if (len === state.length)
457 // didn't get any data, stop spinning.
458 break;else len = state.length;
460 state.readingMore = false;
463 // abstract method. to be overridden in specific implementation classes.
464 // call cb(er, data) where data is <= n in length.
465 // for virtual (non-string, non-buffer) streams, "length" is somewhat
466 // arbitrary, and perhaps not very meaningful.
467 Readable.prototype._read = function (n) {
468 this.emit('error', new Error('_read() is not implemented'));
471 Readable.prototype.pipe = function (dest, pipeOpts) {
473 var state = this._readableState;
475 switch (state.pipesCount) {
480 state.pipes = [state.pipes, dest];
483 state.pipes.push(dest);
486 state.pipesCount += 1;
487 debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
489 var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
491 var endFn = doEnd ? onend : cleanup;
492 if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn);
494 dest.on('unpipe', onunpipe);
495 function onunpipe(readable) {
497 if (readable === src) {
507 // when the dest drains, it reduces the awaitDrain counter
508 // on the source. This would be more elegant with a .once()
509 // handler in flow(), but adding and removing repeatedly is
511 var ondrain = pipeOnDrain(src);
512 dest.on('drain', ondrain);
514 var cleanedUp = false;
517 // cleanup event handlers once the pipe is broken
518 dest.removeListener('close', onclose);
519 dest.removeListener('finish', onfinish);
520 dest.removeListener('drain', ondrain);
521 dest.removeListener('error', onerror);
522 dest.removeListener('unpipe', onunpipe);
523 src.removeListener('end', onend);
524 src.removeListener('end', cleanup);
525 src.removeListener('data', ondata);
529 // if the reader is waiting for a drain event from this
530 // specific writer, then it would cause it to never start
532 // So, if this is awaiting a drain, then we just call it now.
533 // If we don't know, then assume that we are waiting for one.
534 if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
537 // If the user pushes more data while we're writing to dest then we'll end up
538 // in ondata again. However, we only want to increase awaitDrain once because
539 // dest will only emit one 'drain' event for the multiple writes.
540 // => Introduce a guard on increasing awaitDrain.
541 var increasedAwaitDrain = false;
542 src.on('data', ondata);
543 function ondata(chunk) {
545 increasedAwaitDrain = false;
546 var ret = dest.write(chunk);
547 if (false === ret && !increasedAwaitDrain) {
548 // If the user unpiped during `dest.write()`, it is possible
549 // to get stuck in a permanently paused state if that write
550 // also returned false.
551 // => Check whether `dest` is still a piping destination.
552 if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
553 debug('false write response, pause', src._readableState.awaitDrain);
554 src._readableState.awaitDrain++;
555 increasedAwaitDrain = true;
561 // if the dest has an error, then stop piping into it.
562 // however, don't suppress the throwing behavior for this.
563 function onerror(er) {
564 debug('onerror', er);
566 dest.removeListener('error', onerror);
567 if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er);
570 // Make sure our error handler is attached before userland ones.
571 prependListener(dest, 'error', onerror);
573 // Both close and finish should trigger unpipe, but only once.
575 dest.removeListener('finish', onfinish);
578 dest.once('close', onclose);
579 function onfinish() {
581 dest.removeListener('close', onclose);
584 dest.once('finish', onfinish);
591 // tell the dest that it's being piped to
592 dest.emit('pipe', src);
594 // start the flow if it hasn't been started already.
595 if (!state.flowing) {
596 debug('pipe resume');
603 function pipeOnDrain(src) {
605 var state = src._readableState;
606 debug('pipeOnDrain', state.awaitDrain);
607 if (state.awaitDrain) state.awaitDrain--;
608 if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
609 state.flowing = true;
615 Readable.prototype.unpipe = function (dest) {
616 var state = this._readableState;
618 // if we're not piping anywhere, then do nothing.
619 if (state.pipesCount === 0) return this;
621 // just one destination. most common case.
622 if (state.pipesCount === 1) {
623 // passed in one, but it's not the right one.
624 if (dest && dest !== state.pipes) return this;
626 if (!dest) dest = state.pipes;
630 state.pipesCount = 0;
631 state.flowing = false;
632 if (dest) dest.emit('unpipe', this);
636 // slow case. multiple pipe destinations.
640 var dests = state.pipes;
641 var len = state.pipesCount;
643 state.pipesCount = 0;
644 state.flowing = false;
646 for (var i = 0; i < len; i++) {
647 dests[i].emit('unpipe', this);
651 // try to find the right one.
652 var index = indexOf(state.pipes, dest);
653 if (index === -1) return this;
655 state.pipes.splice(index, 1);
656 state.pipesCount -= 1;
657 if (state.pipesCount === 1) state.pipes = state.pipes[0];
659 dest.emit('unpipe', this);
664 // set up data events if they are asked for
665 // Ensure readable listeners eventually get something
666 Readable.prototype.on = function (ev, fn) {
667 var res = Stream.prototype.on.call(this, ev, fn);
670 // Start flowing on next tick if stream isn't explicitly paused
671 if (this._readableState.flowing !== false) this.resume();
672 } else if (ev === 'readable') {
673 var state = this._readableState;
674 if (!state.endEmitted && !state.readableListening) {
675 state.readableListening = state.needReadable = true;
676 state.emittedReadable = false;
677 if (!state.reading) {
678 processNextTick(nReadingNextTick, this);
679 } else if (state.length) {
680 emitReadable(this, state);
687 Readable.prototype.addListener = Readable.prototype.on;
689 function nReadingNextTick(self) {
690 debug('readable nexttick read 0');
694 // pause() and resume() are remnants of the legacy readable stream API
695 // If the user uses them, then switch into old mode.
696 Readable.prototype.resume = function () {
697 var state = this._readableState;
698 if (!state.flowing) {
700 state.flowing = true;
706 function resume(stream, state) {
707 if (!state.resumeScheduled) {
708 state.resumeScheduled = true;
709 processNextTick(resume_, stream, state);
713 function resume_(stream, state) {
714 if (!state.reading) {
715 debug('resume read 0');
719 state.resumeScheduled = false;
720 state.awaitDrain = 0;
721 stream.emit('resume');
723 if (state.flowing && !state.reading) stream.read(0);
726 Readable.prototype.pause = function () {
727 debug('call pause flowing=%j', this._readableState.flowing);
728 if (false !== this._readableState.flowing) {
730 this._readableState.flowing = false;
736 function flow(stream) {
737 var state = stream._readableState;
738 debug('flow', state.flowing);
739 while (state.flowing && stream.read() !== null) {}
742 // wrap an old-style stream as the async data source.
743 // This is *not* part of the readable stream interface.
744 // It is an ugly unfortunate mess of history.
745 Readable.prototype.wrap = function (stream) {
746 var state = this._readableState;
750 stream.on('end', function () {
751 debug('wrapped end');
752 if (state.decoder && !state.ended) {
753 var chunk = state.decoder.end();
754 if (chunk && chunk.length) self.push(chunk);
760 stream.on('data', function (chunk) {
761 debug('wrapped data');
762 if (state.decoder) chunk = state.decoder.write(chunk);
764 // don't skip over falsy values in objectMode
765 if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
767 var ret = self.push(chunk);
774 // proxy all the other methods.
775 // important when wrapping filters and duplexes.
776 for (var i in stream) {
777 if (this[i] === undefined && typeof stream[i] === 'function') {
778 this[i] = function (method) {
780 return stream[method].apply(stream, arguments);
786 // proxy certain important events.
787 var events = ['error', 'close', 'destroy', 'pause', 'resume'];
788 forEach(events, function (ev) {
789 stream.on(ev, self.emit.bind(self, ev));
792 // when we try to consume some more bytes, simply unpause the
793 // underlying stream.
794 self._read = function (n) {
795 debug('wrapped _read', n);
805 // exposed for testing purposes only.
806 Readable._fromList = fromList;
808 // Pluck off n bytes from an array of buffers.
809 // Length is the combined lengths of all the buffers in the list.
810 // This function is designed to be inlinable, so please take care when making
811 // changes to the function body.
812 function fromList(n, state) {
814 if (state.length === 0) return null;
817 if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
818 // read it all, truncate the list
819 if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length);
820 state.buffer.clear();
823 ret = fromListPartial(n, state.buffer, state.decoder);
829 // Extracts only enough buffered data to satisfy the amount requested.
830 // This function is designed to be inlinable, so please take care when making
831 // changes to the function body.
832 function fromListPartial(n, list, hasStrings) {
834 if (n < list.head.data.length) {
835 // slice is the same for buffers and strings
836 ret = list.head.data.slice(0, n);
837 list.head.data = list.head.data.slice(n);
838 } else if (n === list.head.data.length) {
839 // first chunk is a perfect match
842 // result spans more than one buffer
843 ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list);
848 // Copies a specified amount of characters from the list of buffered data
850 // This function is designed to be inlinable, so please take care when making
851 // changes to the function body.
852 function copyFromBufferString(n, list) {
859 var nb = n > str.length ? str.length : n;
860 if (nb === str.length) ret += str;else ret += str.slice(0, n);
863 if (nb === str.length) {
865 if (p.next) list.head = p.next;else list.head = list.tail = null;
868 p.data = str.slice(nb);
878 // Copies a specified amount of bytes from the list of buffered data chunks.
879 // This function is designed to be inlinable, so please take care when making
880 // changes to the function body.
881 function copyFromBuffer(n, list) {
882 var ret = bufferShim.allocUnsafe(n);
889 var nb = n > buf.length ? buf.length : n;
890 buf.copy(ret, ret.length - n, 0, nb);
893 if (nb === buf.length) {
895 if (p.next) list.head = p.next;else list.head = list.tail = null;
898 p.data = buf.slice(nb);
908 function endReadable(stream) {
909 var state = stream._readableState;
911 // If we get here before consuming all the bytes, then that is a
912 // bug in node. Should never happen.
913 if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream');
915 if (!state.endEmitted) {
917 processNextTick(endReadableNT, state, stream);
921 function endReadableNT(state, stream) {
922 // Check that we didn't get one last unshift.
923 if (!state.endEmitted && state.length === 0) {
924 state.endEmitted = true;
925 stream.readable = false;
930 function forEach(xs, f) {
931 for (var i = 0, l = xs.length; i < l; i++) {
936 function indexOf(xs, x) {
937 for (var i = 0, l = xs.length; i < l; i++) {
938 if (xs[i] === x) return i;