Initial commit
[yaffs-website] / node_modules / through2 / node_modules / readable-stream / lib / _stream_readable.js
1 'use strict';
2
3 module.exports = Readable;
4
5 /*<replacement>*/
6 var processNextTick = require('process-nextick-args');
7 /*</replacement>*/
8
9 /*<replacement>*/
10 var isArray = require('isarray');
11 /*</replacement>*/
12
13 /*<replacement>*/
14 var Duplex;
15 /*</replacement>*/
16
17 Readable.ReadableState = ReadableState;
18
19 /*<replacement>*/
20 var EE = require('events').EventEmitter;
21
22 var EElistenerCount = function (emitter, type) {
23   return emitter.listeners(type).length;
24 };
25 /*</replacement>*/
26
27 /*<replacement>*/
28 var Stream;
29 (function () {
30   try {
31     Stream = require('st' + 'ream');
32   } catch (_) {} finally {
33     if (!Stream) Stream = require('events').EventEmitter;
34   }
35 })();
36 /*</replacement>*/
37
38 var Buffer = require('buffer').Buffer;
39 /*<replacement>*/
40 var bufferShim = require('buffer-shims');
41 /*</replacement>*/
42
43 /*<replacement>*/
44 var util = require('core-util-is');
45 util.inherits = require('inherits');
46 /*</replacement>*/
47
48 /*<replacement>*/
49 var debugUtil = require('util');
50 var debug = void 0;
51 if (debugUtil && debugUtil.debuglog) {
52   debug = debugUtil.debuglog('stream');
53 } else {
54   debug = function () {};
55 }
56 /*</replacement>*/
57
58 var BufferList = require('./internal/streams/BufferList');
59 var StringDecoder;
60
61 util.inherits(Readable, Stream);
62
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);
68   } else {
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]];
74   }
75 }
76
77 function ReadableState(options, stream) {
78   Duplex = Duplex || require('./_stream_duplex');
79
80   options = options || {};
81
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;
85
86   if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
87
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;
93
94   // cast to ints.
95   this.highWaterMark = ~ ~this.highWaterMark;
96
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
99   // array.shift()
100   this.buffer = new BufferList();
101   this.length = 0;
102   this.pipes = null;
103   this.pipesCount = 0;
104   this.flowing = null;
105   this.ended = false;
106   this.endEmitted = false;
107   this.reading = false;
108
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.
113   this.sync = true;
114
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;
121
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';
126
127   // when piping, we only care about 'readable' events that happen
128   // after read()ing all the bytes and not getting any pushback.
129   this.ranOut = false;
130
131   // the number of writers that are awaiting a drain event in .pipe()s
132   this.awaitDrain = 0;
133
134   // if true, a maybeReadMore has been scheduled
135   this.readingMore = false;
136
137   this.decoder = null;
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;
143   }
144 }
145
146 function Readable(options) {
147   Duplex = Duplex || require('./_stream_duplex');
148
149   if (!(this instanceof Readable)) return new Readable(options);
150
151   this._readableState = new ReadableState(options, this);
152
153   // legacy
154   this.readable = true;
155
156   if (options && typeof options.read === 'function') this._read = options.read;
157
158   Stream.call(this);
159 }
160
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;
167
168   if (!state.objectMode && typeof chunk === 'string') {
169     encoding = encoding || state.defaultEncoding;
170     if (encoding !== state.encoding) {
171       chunk = bufferShim.from(chunk, encoding);
172       encoding = '';
173     }
174   }
175
176   return readableAddChunk(this, state, chunk, encoding, false);
177 };
178
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);
183 };
184
185 Readable.prototype.isPaused = function () {
186   return this._readableState.flowing === false;
187 };
188
189 function readableAddChunk(stream, state, chunk, encoding, addToFront) {
190   var er = chunkInvalid(state, chunk);
191   if (er) {
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);
203     } else {
204       var skipAdd;
205       if (state.decoder && !addToFront && !encoding) {
206         chunk = state.decoder.write(chunk);
207         skipAdd = !state.objectMode && chunk.length === 0;
208       }
209
210       if (!addToFront) state.reading = false;
211
212       // Don't add to the buffer if we've decoded to an empty string chunk and
213       // we're not in object mode
214       if (!skipAdd) {
215         // if we want the data now, just emit it.
216         if (state.flowing && state.length === 0 && !state.sync) {
217           stream.emit('data', chunk);
218           stream.read(0);
219         } else {
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);
223
224           if (state.needReadable) emitReadable(stream);
225         }
226       }
227
228       maybeReadMore(stream, state);
229     }
230   } else if (!addToFront) {
231     state.reading = false;
232   }
233
234   return needMoreData(state);
235 }
236
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);
246 }
247
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;
253   return this;
254 };
255
256 // Don't raise the hwm > 8MB
257 var MAX_HWM = 0x800000;
258 function computeNewHighWaterMark(n) {
259   if (n >= MAX_HWM) {
260     n = MAX_HWM;
261   } else {
262     // Get the next highest power of 2 to prevent increasing hwm excessively in
263     // tiny amounts
264     n--;
265     n |= n >>> 1;
266     n |= n >>> 2;
267     n |= n >>> 4;
268     n |= n >>> 8;
269     n |= n >>> 16;
270     n++;
271   }
272   return n;
273 }
274
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;
280   if (n !== n) {
281     // Only flow one buffer at a time
282     if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
283   }
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;
287   // Don't have enough
288   if (!state.ended) {
289     state.needReadable = true;
290     return 0;
291   }
292   return state.length;
293 }
294
295 // you can override either this method, or the async _read(n) below.
296 Readable.prototype.read = function (n) {
297   debug('read', n);
298   n = parseInt(n, 10);
299   var state = this._readableState;
300   var nOrig = n;
301
302   if (n !== 0) state.emittedReadable = false;
303
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);
310     return null;
311   }
312
313   n = howMuchToRead(n, state);
314
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);
318     return null;
319   }
320
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.
327   //
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.
331   //
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
339   // 'readable' etc.
340   //
341   // 3. Actually pull the requested chunks out of the buffer and return.
342
343   // if we need a readable event, then we need to do some reading.
344   var doRead = state.needReadable;
345   debug('need readable', doRead);
346
347   // if we currently have less than the highWaterMark, then also read some
348   if (state.length === 0 || state.length - n < state.highWaterMark) {
349     doRead = true;
350     debug('length less than watermark', doRead);
351   }
352
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) {
356     doRead = false;
357     debug('reading or ended', doRead);
358   } else if (doRead) {
359     debug('do read');
360     state.reading = true;
361     state.sync = 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);
366     state.sync = false;
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);
370   }
371
372   var ret;
373   if (n > 0) ret = fromList(n, state);else ret = null;
374
375   if (ret === null) {
376     state.needReadable = true;
377     n = 0;
378   } else {
379     state.length -= n;
380   }
381
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;
386
387     // If we tried to read() past the EOF, then emit end on the next tick.
388     if (nOrig !== n && state.ended) endReadable(this);
389   }
390
391   if (ret !== null) this.emit('data', ret);
392
393   return ret;
394 };
395
396 function chunkInvalid(state, chunk) {
397   var er = null;
398   if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) {
399     er = new TypeError('Invalid non-string/buffer chunk');
400   }
401   return er;
402 }
403
404 function onEofChunk(stream, state) {
405   if (state.ended) return;
406   if (state.decoder) {
407     var chunk = state.decoder.end();
408     if (chunk && chunk.length) {
409       state.buffer.push(chunk);
410       state.length += state.objectMode ? 1 : chunk.length;
411     }
412   }
413   state.ended = true;
414
415   // emit 'readable' now to make sure it gets picked up.
416   emitReadable(stream);
417 }
418
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);
429   }
430 }
431
432 function emitReadable_(stream) {
433   debug('emit readable');
434   stream.emit('readable');
435   flow(stream);
436 }
437
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
441 // it's in progress.
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);
448   }
449 }
450
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');
455     stream.read(0);
456     if (len === state.length)
457       // didn't get any data, stop spinning.
458       break;else len = state.length;
459   }
460   state.readingMore = false;
461 }
462
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'));
469 };
470
471 Readable.prototype.pipe = function (dest, pipeOpts) {
472   var src = this;
473   var state = this._readableState;
474
475   switch (state.pipesCount) {
476     case 0:
477       state.pipes = dest;
478       break;
479     case 1:
480       state.pipes = [state.pipes, dest];
481       break;
482     default:
483       state.pipes.push(dest);
484       break;
485   }
486   state.pipesCount += 1;
487   debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
488
489   var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
490
491   var endFn = doEnd ? onend : cleanup;
492   if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn);
493
494   dest.on('unpipe', onunpipe);
495   function onunpipe(readable) {
496     debug('onunpipe');
497     if (readable === src) {
498       cleanup();
499     }
500   }
501
502   function onend() {
503     debug('onend');
504     dest.end();
505   }
506
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
510   // too slow.
511   var ondrain = pipeOnDrain(src);
512   dest.on('drain', ondrain);
513
514   var cleanedUp = false;
515   function cleanup() {
516     debug('cleanup');
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);
526
527     cleanedUp = true;
528
529     // if the reader is waiting for a drain event from this
530     // specific writer, then it would cause it to never start
531     // flowing again.
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();
535   }
536
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) {
544     debug('ondata');
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;
556       }
557       src.pause();
558     }
559   }
560
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);
565     unpipe();
566     dest.removeListener('error', onerror);
567     if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er);
568   }
569
570   // Make sure our error handler is attached before userland ones.
571   prependListener(dest, 'error', onerror);
572
573   // Both close and finish should trigger unpipe, but only once.
574   function onclose() {
575     dest.removeListener('finish', onfinish);
576     unpipe();
577   }
578   dest.once('close', onclose);
579   function onfinish() {
580     debug('onfinish');
581     dest.removeListener('close', onclose);
582     unpipe();
583   }
584   dest.once('finish', onfinish);
585
586   function unpipe() {
587     debug('unpipe');
588     src.unpipe(dest);
589   }
590
591   // tell the dest that it's being piped to
592   dest.emit('pipe', src);
593
594   // start the flow if it hasn't been started already.
595   if (!state.flowing) {
596     debug('pipe resume');
597     src.resume();
598   }
599
600   return dest;
601 };
602
603 function pipeOnDrain(src) {
604   return function () {
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;
610       flow(src);
611     }
612   };
613 }
614
615 Readable.prototype.unpipe = function (dest) {
616   var state = this._readableState;
617
618   // if we're not piping anywhere, then do nothing.
619   if (state.pipesCount === 0) return this;
620
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;
625
626     if (!dest) dest = state.pipes;
627
628     // got a match.
629     state.pipes = null;
630     state.pipesCount = 0;
631     state.flowing = false;
632     if (dest) dest.emit('unpipe', this);
633     return this;
634   }
635
636   // slow case. multiple pipe destinations.
637
638   if (!dest) {
639     // remove all.
640     var dests = state.pipes;
641     var len = state.pipesCount;
642     state.pipes = null;
643     state.pipesCount = 0;
644     state.flowing = false;
645
646     for (var i = 0; i < len; i++) {
647       dests[i].emit('unpipe', this);
648     }return this;
649   }
650
651   // try to find the right one.
652   var index = indexOf(state.pipes, dest);
653   if (index === -1) return this;
654
655   state.pipes.splice(index, 1);
656   state.pipesCount -= 1;
657   if (state.pipesCount === 1) state.pipes = state.pipes[0];
658
659   dest.emit('unpipe', this);
660
661   return this;
662 };
663
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);
668
669   if (ev === 'data') {
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);
681       }
682     }
683   }
684
685   return res;
686 };
687 Readable.prototype.addListener = Readable.prototype.on;
688
689 function nReadingNextTick(self) {
690   debug('readable nexttick read 0');
691   self.read(0);
692 }
693
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) {
699     debug('resume');
700     state.flowing = true;
701     resume(this, state);
702   }
703   return this;
704 };
705
706 function resume(stream, state) {
707   if (!state.resumeScheduled) {
708     state.resumeScheduled = true;
709     processNextTick(resume_, stream, state);
710   }
711 }
712
713 function resume_(stream, state) {
714   if (!state.reading) {
715     debug('resume read 0');
716     stream.read(0);
717   }
718
719   state.resumeScheduled = false;
720   state.awaitDrain = 0;
721   stream.emit('resume');
722   flow(stream);
723   if (state.flowing && !state.reading) stream.read(0);
724 }
725
726 Readable.prototype.pause = function () {
727   debug('call pause flowing=%j', this._readableState.flowing);
728   if (false !== this._readableState.flowing) {
729     debug('pause');
730     this._readableState.flowing = false;
731     this.emit('pause');
732   }
733   return this;
734 };
735
736 function flow(stream) {
737   var state = stream._readableState;
738   debug('flow', state.flowing);
739   while (state.flowing && stream.read() !== null) {}
740 }
741
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;
747   var paused = false;
748
749   var self = this;
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);
755     }
756
757     self.push(null);
758   });
759
760   stream.on('data', function (chunk) {
761     debug('wrapped data');
762     if (state.decoder) chunk = state.decoder.write(chunk);
763
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;
766
767     var ret = self.push(chunk);
768     if (!ret) {
769       paused = true;
770       stream.pause();
771     }
772   });
773
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) {
779         return function () {
780           return stream[method].apply(stream, arguments);
781         };
782       }(i);
783     }
784   }
785
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));
790   });
791
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);
796     if (paused) {
797       paused = false;
798       stream.resume();
799     }
800   };
801
802   return self;
803 };
804
805 // exposed for testing purposes only.
806 Readable._fromList = fromList;
807
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) {
813   // nothing buffered
814   if (state.length === 0) return null;
815
816   var ret;
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();
821   } else {
822     // read part of list
823     ret = fromListPartial(n, state.buffer, state.decoder);
824   }
825
826   return ret;
827 }
828
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) {
833   var ret;
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
840     ret = list.shift();
841   } else {
842     // result spans more than one buffer
843     ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list);
844   }
845   return ret;
846 }
847
848 // Copies a specified amount of characters from the list of buffered data
849 // chunks.
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) {
853   var p = list.head;
854   var c = 1;
855   var ret = p.data;
856   n -= ret.length;
857   while (p = p.next) {
858     var str = p.data;
859     var nb = n > str.length ? str.length : n;
860     if (nb === str.length) ret += str;else ret += str.slice(0, n);
861     n -= nb;
862     if (n === 0) {
863       if (nb === str.length) {
864         ++c;
865         if (p.next) list.head = p.next;else list.head = list.tail = null;
866       } else {
867         list.head = p;
868         p.data = str.slice(nb);
869       }
870       break;
871     }
872     ++c;
873   }
874   list.length -= c;
875   return ret;
876 }
877
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);
883   var p = list.head;
884   var c = 1;
885   p.data.copy(ret);
886   n -= p.data.length;
887   while (p = p.next) {
888     var buf = p.data;
889     var nb = n > buf.length ? buf.length : n;
890     buf.copy(ret, ret.length - n, 0, nb);
891     n -= nb;
892     if (n === 0) {
893       if (nb === buf.length) {
894         ++c;
895         if (p.next) list.head = p.next;else list.head = list.tail = null;
896       } else {
897         list.head = p;
898         p.data = buf.slice(nb);
899       }
900       break;
901     }
902     ++c;
903   }
904   list.length -= c;
905   return ret;
906 }
907
908 function endReadable(stream) {
909   var state = stream._readableState;
910
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');
914
915   if (!state.endEmitted) {
916     state.ended = true;
917     processNextTick(endReadableNT, state, stream);
918   }
919 }
920
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;
926     stream.emit('end');
927   }
928 }
929
930 function forEach(xs, f) {
931   for (var i = 0, l = xs.length; i < l; i++) {
932     f(xs[i], i);
933   }
934 }
935
936 function indexOf(xs, x) {
937   for (var i = 0, l = xs.length; i < l; i++) {
938     if (xs[i] === x) return i;
939   }
940   return -1;
941 }