_stream_readable.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. 'use strict';
  2. module.exports = Readable;
  3. /*<replacement>*/
  4. var processNextTick = require('process-nextick-args');
  5. /*</replacement>*/
  6. /*<replacement>*/
  7. var isArray = require('isarray');
  8. /*</replacement>*/
  9. Readable.ReadableState = ReadableState;
  10. /*<replacement>*/
  11. var EE = require('events').EventEmitter;
  12. var EElistenerCount = function (emitter, type) {
  13. return emitter.listeners(type).length;
  14. };
  15. /*</replacement>*/
  16. /*<replacement>*/
  17. var Stream;
  18. (function () {
  19. try {
  20. Stream = require('st' + 'ream');
  21. } catch (_) {} finally {
  22. if (!Stream) Stream = require('events').EventEmitter;
  23. }
  24. })();
  25. /*</replacement>*/
  26. var Buffer = require('buffer').Buffer;
  27. /*<replacement>*/
  28. var bufferShim = require('buffer-shims');
  29. /*</replacement>*/
  30. /*<replacement>*/
  31. var util = require('core-util-is');
  32. util.inherits = require('inherits');
  33. /*</replacement>*/
  34. /*<replacement>*/
  35. var debugUtil = require('util');
  36. var debug = void 0;
  37. if (debugUtil && debugUtil.debuglog) {
  38. debug = debugUtil.debuglog('stream');
  39. } else {
  40. debug = function () {};
  41. }
  42. /*</replacement>*/
  43. var BufferList = require('./internal/streams/BufferList');
  44. var StringDecoder;
  45. util.inherits(Readable, Stream);
  46. function prependListener(emitter, event, fn) {
  47. if (typeof emitter.prependListener === 'function') {
  48. return emitter.prependListener(event, fn);
  49. } else {
  50. // This is a hack to make sure that our error handler is attached before any
  51. // userland ones. NEVER DO THIS. This is here only because this code needs
  52. // to continue to work with older versions of Node.js that do not include
  53. // the prependListener() method. The goal is to eventually remove this hack.
  54. 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]];
  55. }
  56. }
  57. var Duplex;
  58. function ReadableState(options, stream) {
  59. Duplex = Duplex || require('./_stream_duplex');
  60. options = options || {};
  61. // object stream flag. Used to make read(n) ignore n and to
  62. // make all the buffer merging and length checks go away
  63. this.objectMode = !!options.objectMode;
  64. if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
  65. // the point at which it stops calling _read() to fill the buffer
  66. // Note: 0 is a valid value, means "don't call _read preemptively ever"
  67. var hwm = options.highWaterMark;
  68. var defaultHwm = this.objectMode ? 16 : 16 * 1024;
  69. this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm;
  70. // cast to ints.
  71. this.highWaterMark = ~ ~this.highWaterMark;
  72. // A linked list is used to store data chunks instead of an array because the
  73. // linked list can remove elements from the beginning faster than
  74. // array.shift()
  75. this.buffer = new BufferList();
  76. this.length = 0;
  77. this.pipes = null;
  78. this.pipesCount = 0;
  79. this.flowing = null;
  80. this.ended = false;
  81. this.endEmitted = false;
  82. this.reading = false;
  83. // a flag to be able to tell if the onwrite cb is called immediately,
  84. // or on a later tick. We set this to true at first, because any
  85. // actions that shouldn't happen until "later" should generally also
  86. // not happen before the first write call.
  87. this.sync = true;
  88. // whenever we return null, then we set a flag to say
  89. // that we're awaiting a 'readable' event emission.
  90. this.needReadable = false;
  91. this.emittedReadable = false;
  92. this.readableListening = false;
  93. this.resumeScheduled = false;
  94. // Crypto is kind of old and crusty. Historically, its default string
  95. // encoding is 'binary' so we have to make this configurable.
  96. // Everything else in the universe uses 'utf8', though.
  97. this.defaultEncoding = options.defaultEncoding || 'utf8';
  98. // when piping, we only care about 'readable' events that happen
  99. // after read()ing all the bytes and not getting any pushback.
  100. this.ranOut = false;
  101. // the number of writers that are awaiting a drain event in .pipe()s
  102. this.awaitDrain = 0;
  103. // if true, a maybeReadMore has been scheduled
  104. this.readingMore = false;
  105. this.decoder = null;
  106. this.encoding = null;
  107. if (options.encoding) {
  108. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  109. this.decoder = new StringDecoder(options.encoding);
  110. this.encoding = options.encoding;
  111. }
  112. }
  113. var Duplex;
  114. function Readable(options) {
  115. Duplex = Duplex || require('./_stream_duplex');
  116. if (!(this instanceof Readable)) return new Readable(options);
  117. this._readableState = new ReadableState(options, this);
  118. // legacy
  119. this.readable = true;
  120. if (options && typeof options.read === 'function') this._read = options.read;
  121. Stream.call(this);
  122. }
  123. // Manually shove something into the read() buffer.
  124. // This returns true if the highWaterMark has not been hit yet,
  125. // similar to how Writable.write() returns true if you should
  126. // write() some more.
  127. Readable.prototype.push = function (chunk, encoding) {
  128. var state = this._readableState;
  129. if (!state.objectMode && typeof chunk === 'string') {
  130. encoding = encoding || state.defaultEncoding;
  131. if (encoding !== state.encoding) {
  132. chunk = bufferShim.from(chunk, encoding);
  133. encoding = '';
  134. }
  135. }
  136. return readableAddChunk(this, state, chunk, encoding, false);
  137. };
  138. // Unshift should *always* be something directly out of read()
  139. Readable.prototype.unshift = function (chunk) {
  140. var state = this._readableState;
  141. return readableAddChunk(this, state, chunk, '', true);
  142. };
  143. Readable.prototype.isPaused = function () {
  144. return this._readableState.flowing === false;
  145. };
  146. function readableAddChunk(stream, state, chunk, encoding, addToFront) {
  147. var er = chunkInvalid(state, chunk);
  148. if (er) {
  149. stream.emit('error', er);
  150. } else if (chunk === null) {
  151. state.reading = false;
  152. onEofChunk(stream, state);
  153. } else if (state.objectMode || chunk && chunk.length > 0) {
  154. if (state.ended && !addToFront) {
  155. var e = new Error('stream.push() after EOF');
  156. stream.emit('error', e);
  157. } else if (state.endEmitted && addToFront) {
  158. var _e = new Error('stream.unshift() after end event');
  159. stream.emit('error', _e);
  160. } else {
  161. var skipAdd;
  162. if (state.decoder && !addToFront && !encoding) {
  163. chunk = state.decoder.write(chunk);
  164. skipAdd = !state.objectMode && chunk.length === 0;
  165. }
  166. if (!addToFront) state.reading = false;
  167. // Don't add to the buffer if we've decoded to an empty string chunk and
  168. // we're not in object mode
  169. if (!skipAdd) {
  170. // if we want the data now, just emit it.
  171. if (state.flowing && state.length === 0 && !state.sync) {
  172. stream.emit('data', chunk);
  173. stream.read(0);
  174. } else {
  175. // update the buffer info.
  176. state.length += state.objectMode ? 1 : chunk.length;
  177. if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
  178. if (state.needReadable) emitReadable(stream);
  179. }
  180. }
  181. maybeReadMore(stream, state);
  182. }
  183. } else if (!addToFront) {
  184. state.reading = false;
  185. }
  186. return needMoreData(state);
  187. }
  188. // if it's past the high water mark, we can push in some more.
  189. // Also, if we have no data yet, we can stand some
  190. // more bytes. This is to work around cases where hwm=0,
  191. // such as the repl. Also, if the push() triggered a
  192. // readable event, and the user called read(largeNumber) such that
  193. // needReadable was set, then we ought to push more, so that another
  194. // 'readable' event will be triggered.
  195. function needMoreData(state) {
  196. return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0);
  197. }
  198. // backwards compatibility.
  199. Readable.prototype.setEncoding = function (enc) {
  200. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  201. this._readableState.decoder = new StringDecoder(enc);
  202. this._readableState.encoding = enc;
  203. return this;
  204. };
  205. // Don't raise the hwm > 8MB
  206. var MAX_HWM = 0x800000;
  207. function computeNewHighWaterMark(n) {
  208. if (n >= MAX_HWM) {
  209. n = MAX_HWM;
  210. } else {
  211. // Get the next highest power of 2 to prevent increasing hwm excessively in
  212. // tiny amounts
  213. n--;
  214. n |= n >>> 1;
  215. n |= n >>> 2;
  216. n |= n >>> 4;
  217. n |= n >>> 8;
  218. n |= n >>> 16;
  219. n++;
  220. }
  221. return n;
  222. }
  223. // This function is designed to be inlinable, so please take care when making
  224. // changes to the function body.
  225. function howMuchToRead(n, state) {
  226. if (n <= 0 || state.length === 0 && state.ended) return 0;
  227. if (state.objectMode) return 1;
  228. if (n !== n) {
  229. // Only flow one buffer at a time
  230. if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
  231. }
  232. // If we're asking for more than the current hwm, then raise the hwm.
  233. if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
  234. if (n <= state.length) return n;
  235. // Don't have enough
  236. if (!state.ended) {
  237. state.needReadable = true;
  238. return 0;
  239. }
  240. return state.length;
  241. }
  242. // you can override either this method, or the async _read(n) below.
  243. Readable.prototype.read = function (n) {
  244. debug('read', n);
  245. n = parseInt(n, 10);
  246. var state = this._readableState;
  247. var nOrig = n;
  248. if (n !== 0) state.emittedReadable = false;
  249. // if we're doing read(0) to trigger a readable event, but we
  250. // already have a bunch of data in the buffer, then just trigger
  251. // the 'readable' event and move on.
  252. if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) {
  253. debug('read: emitReadable', state.length, state.ended);
  254. if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
  255. return null;
  256. }
  257. n = howMuchToRead(n, state);
  258. // if we've ended, and we're now clear, then finish it up.
  259. if (n === 0 && state.ended) {
  260. if (state.length === 0) endReadable(this);
  261. return null;
  262. }
  263. // All the actual chunk generation logic needs to be
  264. // *below* the call to _read. The reason is that in certain
  265. // synthetic stream cases, such as passthrough streams, _read
  266. // may be a completely synchronous operation which may change
  267. // the state of the read buffer, providing enough data when
  268. // before there was *not* enough.
  269. //
  270. // So, the steps are:
  271. // 1. Figure out what the state of things will be after we do
  272. // a read from the buffer.
  273. //
  274. // 2. If that resulting state will trigger a _read, then call _read.
  275. // Note that this may be asynchronous, or synchronous. Yes, it is
  276. // deeply ugly to write APIs this way, but that still doesn't mean
  277. // that the Readable class should behave improperly, as streams are
  278. // designed to be sync/async agnostic.
  279. // Take note if the _read call is sync or async (ie, if the read call
  280. // has returned yet), so that we know whether or not it's safe to emit
  281. // 'readable' etc.
  282. //
  283. // 3. Actually pull the requested chunks out of the buffer and return.
  284. // if we need a readable event, then we need to do some reading.
  285. var doRead = state.needReadable;
  286. debug('need readable', doRead);
  287. // if we currently have less than the highWaterMark, then also read some
  288. if (state.length === 0 || state.length - n < state.highWaterMark) {
  289. doRead = true;
  290. debug('length less than watermark', doRead);
  291. }
  292. // however, if we've ended, then there's no point, and if we're already
  293. // reading, then it's unnecessary.
  294. if (state.ended || state.reading) {
  295. doRead = false;
  296. debug('reading or ended', doRead);
  297. } else if (doRead) {
  298. debug('do read');
  299. state.reading = true;
  300. state.sync = true;
  301. // if the length is currently zero, then we *need* a readable event.
  302. if (state.length === 0) state.needReadable = true;
  303. // call internal read method
  304. this._read(state.highWaterMark);
  305. state.sync = false;
  306. // If _read pushed data synchronously, then `reading` will be false,
  307. // and we need to re-evaluate how much data we can return to the user.
  308. if (!state.reading) n = howMuchToRead(nOrig, state);
  309. }
  310. var ret;
  311. if (n > 0) ret = fromList(n, state);else ret = null;
  312. if (ret === null) {
  313. state.needReadable = true;
  314. n = 0;
  315. } else {
  316. state.length -= n;
  317. }
  318. if (state.length === 0) {
  319. // If we have nothing in the buffer, then we want to know
  320. // as soon as we *do* get something into the buffer.
  321. if (!state.ended) state.needReadable = true;
  322. // If we tried to read() past the EOF, then emit end on the next tick.
  323. if (nOrig !== n && state.ended) endReadable(this);
  324. }
  325. if (ret !== null) this.emit('data', ret);
  326. return ret;
  327. };
  328. function chunkInvalid(state, chunk) {
  329. var er = null;
  330. if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) {
  331. er = new TypeError('Invalid non-string/buffer chunk');
  332. }
  333. return er;
  334. }
  335. function onEofChunk(stream, state) {
  336. if (state.ended) return;
  337. if (state.decoder) {
  338. var chunk = state.decoder.end();
  339. if (chunk && chunk.length) {
  340. state.buffer.push(chunk);
  341. state.length += state.objectMode ? 1 : chunk.length;
  342. }
  343. }
  344. state.ended = true;
  345. // emit 'readable' now to make sure it gets picked up.
  346. emitReadable(stream);
  347. }
  348. // Don't emit readable right away in sync mode, because this can trigger
  349. // another read() call => stack overflow. This way, it might trigger
  350. // a nextTick recursion warning, but that's not so bad.
  351. function emitReadable(stream) {
  352. var state = stream._readableState;
  353. state.needReadable = false;
  354. if (!state.emittedReadable) {
  355. debug('emitReadable', state.flowing);
  356. state.emittedReadable = true;
  357. if (state.sync) processNextTick(emitReadable_, stream);else emitReadable_(stream);
  358. }
  359. }
  360. function emitReadable_(stream) {
  361. debug('emit readable');
  362. stream.emit('readable');
  363. flow(stream);
  364. }
  365. // at this point, the user has presumably seen the 'readable' event,
  366. // and called read() to consume some data. that may have triggered
  367. // in turn another _read(n) call, in which case reading = true if
  368. // it's in progress.
  369. // However, if we're not ended, or reading, and the length < hwm,
  370. // then go ahead and try to read some more preemptively.
  371. function maybeReadMore(stream, state) {
  372. if (!state.readingMore) {
  373. state.readingMore = true;
  374. processNextTick(maybeReadMore_, stream, state);
  375. }
  376. }
  377. function maybeReadMore_(stream, state) {
  378. var len = state.length;
  379. while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) {
  380. debug('maybeReadMore read 0');
  381. stream.read(0);
  382. if (len === state.length)
  383. // didn't get any data, stop spinning.
  384. break;else len = state.length;
  385. }
  386. state.readingMore = false;
  387. }
  388. // abstract method. to be overridden in specific implementation classes.
  389. // call cb(er, data) where data is <= n in length.
  390. // for virtual (non-string, non-buffer) streams, "length" is somewhat
  391. // arbitrary, and perhaps not very meaningful.
  392. Readable.prototype._read = function (n) {
  393. this.emit('error', new Error('not implemented'));
  394. };
  395. Readable.prototype.pipe = function (dest, pipeOpts) {
  396. var src = this;
  397. var state = this._readableState;
  398. switch (state.pipesCount) {
  399. case 0:
  400. state.pipes = dest;
  401. break;
  402. case 1:
  403. state.pipes = [state.pipes, dest];
  404. break;
  405. default:
  406. state.pipes.push(dest);
  407. break;
  408. }
  409. state.pipesCount += 1;
  410. debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
  411. var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
  412. var endFn = doEnd ? onend : cleanup;
  413. if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn);
  414. dest.on('unpipe', onunpipe);
  415. function onunpipe(readable) {
  416. debug('onunpipe');
  417. if (readable === src) {
  418. cleanup();
  419. }
  420. }
  421. function onend() {
  422. debug('onend');
  423. dest.end();
  424. }
  425. // when the dest drains, it reduces the awaitDrain counter
  426. // on the source. This would be more elegant with a .once()
  427. // handler in flow(), but adding and removing repeatedly is
  428. // too slow.
  429. var ondrain = pipeOnDrain(src);
  430. dest.on('drain', ondrain);
  431. var cleanedUp = false;
  432. function cleanup() {
  433. debug('cleanup');
  434. // cleanup event handlers once the pipe is broken
  435. dest.removeListener('close', onclose);
  436. dest.removeListener('finish', onfinish);
  437. dest.removeListener('drain', ondrain);
  438. dest.removeListener('error', onerror);
  439. dest.removeListener('unpipe', onunpipe);
  440. src.removeListener('end', onend);
  441. src.removeListener('end', cleanup);
  442. src.removeListener('data', ondata);
  443. cleanedUp = true;
  444. // if the reader is waiting for a drain event from this
  445. // specific writer, then it would cause it to never start
  446. // flowing again.
  447. // So, if this is awaiting a drain, then we just call it now.
  448. // If we don't know, then assume that we are waiting for one.
  449. if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
  450. }
  451. // If the user pushes more data while we're writing to dest then we'll end up
  452. // in ondata again. However, we only want to increase awaitDrain once because
  453. // dest will only emit one 'drain' event for the multiple writes.
  454. // => Introduce a guard on increasing awaitDrain.
  455. var increasedAwaitDrain = false;
  456. src.on('data', ondata);
  457. function ondata(chunk) {
  458. debug('ondata');
  459. increasedAwaitDrain = false;
  460. var ret = dest.write(chunk);
  461. if (false === ret && !increasedAwaitDrain) {
  462. // If the user unpiped during `dest.write()`, it is possible
  463. // to get stuck in a permanently paused state if that write
  464. // also returned false.
  465. // => Check whether `dest` is still a piping destination.
  466. if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
  467. debug('false write response, pause', src._readableState.awaitDrain);
  468. src._readableState.awaitDrain++;
  469. increasedAwaitDrain = true;
  470. }
  471. src.pause();
  472. }
  473. }
  474. // if the dest has an error, then stop piping into it.
  475. // however, don't suppress the throwing behavior for this.
  476. function onerror(er) {
  477. debug('onerror', er);
  478. unpipe();
  479. dest.removeListener('error', onerror);
  480. if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er);
  481. }
  482. // Make sure our error handler is attached before userland ones.
  483. prependListener(dest, 'error', onerror);
  484. // Both close and finish should trigger unpipe, but only once.
  485. function onclose() {
  486. dest.removeListener('finish', onfinish);
  487. unpipe();
  488. }
  489. dest.once('close', onclose);
  490. function onfinish() {
  491. debug('onfinish');
  492. dest.removeListener('close', onclose);
  493. unpipe();
  494. }
  495. dest.once('finish', onfinish);
  496. function unpipe() {
  497. debug('unpipe');
  498. src.unpipe(dest);
  499. }
  500. // tell the dest that it's being piped to
  501. dest.emit('pipe', src);
  502. // start the flow if it hasn't been started already.
  503. if (!state.flowing) {
  504. debug('pipe resume');
  505. src.resume();
  506. }
  507. return dest;
  508. };
  509. function pipeOnDrain(src) {
  510. return function () {
  511. var state = src._readableState;
  512. debug('pipeOnDrain', state.awaitDrain);
  513. if (state.awaitDrain) state.awaitDrain--;
  514. if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
  515. state.flowing = true;
  516. flow(src);
  517. }
  518. };
  519. }
  520. Readable.prototype.unpipe = function (dest) {
  521. var state = this._readableState;
  522. // if we're not piping anywhere, then do nothing.
  523. if (state.pipesCount === 0) return this;
  524. // just one destination. most common case.
  525. if (state.pipesCount === 1) {
  526. // passed in one, but it's not the right one.
  527. if (dest && dest !== state.pipes) return this;
  528. if (!dest) dest = state.pipes;
  529. // got a match.
  530. state.pipes = null;
  531. state.pipesCount = 0;
  532. state.flowing = false;
  533. if (dest) dest.emit('unpipe', this);
  534. return this;
  535. }
  536. // slow case. multiple pipe destinations.
  537. if (!dest) {
  538. // remove all.
  539. var dests = state.pipes;
  540. var len = state.pipesCount;
  541. state.pipes = null;
  542. state.pipesCount = 0;
  543. state.flowing = false;
  544. for (var _i = 0; _i < len; _i++) {
  545. dests[_i].emit('unpipe', this);
  546. }return this;
  547. }
  548. // try to find the right one.
  549. var i = indexOf(state.pipes, dest);
  550. if (i === -1) return this;
  551. state.pipes.splice(i, 1);
  552. state.pipesCount -= 1;
  553. if (state.pipesCount === 1) state.pipes = state.pipes[0];
  554. dest.emit('unpipe', this);
  555. return this;
  556. };
  557. // set up data events if they are asked for
  558. // Ensure readable listeners eventually get something
  559. Readable.prototype.on = function (ev, fn) {
  560. var res = Stream.prototype.on.call(this, ev, fn);
  561. if (ev === 'data') {
  562. // Start flowing on next tick if stream isn't explicitly paused
  563. if (this._readableState.flowing !== false) this.resume();
  564. } else if (ev === 'readable') {
  565. var state = this._readableState;
  566. if (!state.endEmitted && !state.readableListening) {
  567. state.readableListening = state.needReadable = true;
  568. state.emittedReadable = false;
  569. if (!state.reading) {
  570. processNextTick(nReadingNextTick, this);
  571. } else if (state.length) {
  572. emitReadable(this, state);
  573. }
  574. }
  575. }
  576. return res;
  577. };
  578. Readable.prototype.addListener = Readable.prototype.on;
  579. function nReadingNextTick(self) {
  580. debug('readable nexttick read 0');
  581. self.read(0);
  582. }
  583. // pause() and resume() are remnants of the legacy readable stream API
  584. // If the user uses them, then switch into old mode.
  585. Readable.prototype.resume = function () {
  586. var state = this._readableState;
  587. if (!state.flowing) {
  588. debug('resume');
  589. state.flowing = true;
  590. resume(this, state);
  591. }
  592. return this;
  593. };
  594. function resume(stream, state) {
  595. if (!state.resumeScheduled) {
  596. state.resumeScheduled = true;
  597. processNextTick(resume_, stream, state);
  598. }
  599. }
  600. function resume_(stream, state) {
  601. if (!state.reading) {
  602. debug('resume read 0');
  603. stream.read(0);
  604. }
  605. state.resumeScheduled = false;
  606. state.awaitDrain = 0;
  607. stream.emit('resume');
  608. flow(stream);
  609. if (state.flowing && !state.reading) stream.read(0);
  610. }
  611. Readable.prototype.pause = function () {
  612. debug('call pause flowing=%j', this._readableState.flowing);
  613. if (false !== this._readableState.flowing) {
  614. debug('pause');
  615. this._readableState.flowing = false;
  616. this.emit('pause');
  617. }
  618. return this;
  619. };
  620. function flow(stream) {
  621. var state = stream._readableState;
  622. debug('flow', state.flowing);
  623. while (state.flowing && stream.read() !== null) {}
  624. }
  625. // wrap an old-style stream as the async data source.
  626. // This is *not* part of the readable stream interface.
  627. // It is an ugly unfortunate mess of history.
  628. Readable.prototype.wrap = function (stream) {
  629. var state = this._readableState;
  630. var paused = false;
  631. var self = this;
  632. stream.on('end', function () {
  633. debug('wrapped end');
  634. if (state.decoder && !state.ended) {
  635. var chunk = state.decoder.end();
  636. if (chunk && chunk.length) self.push(chunk);
  637. }
  638. self.push(null);
  639. });
  640. stream.on('data', function (chunk) {
  641. debug('wrapped data');
  642. if (state.decoder) chunk = state.decoder.write(chunk);
  643. // don't skip over falsy values in objectMode
  644. if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
  645. var ret = self.push(chunk);
  646. if (!ret) {
  647. paused = true;
  648. stream.pause();
  649. }
  650. });
  651. // proxy all the other methods.
  652. // important when wrapping filters and duplexes.
  653. for (var i in stream) {
  654. if (this[i] === undefined && typeof stream[i] === 'function') {
  655. this[i] = function (method) {
  656. return function () {
  657. return stream[method].apply(stream, arguments);
  658. };
  659. }(i);
  660. }
  661. }
  662. // proxy certain important events.
  663. var events = ['error', 'close', 'destroy', 'pause', 'resume'];
  664. forEach(events, function (ev) {
  665. stream.on(ev, self.emit.bind(self, ev));
  666. });
  667. // when we try to consume some more bytes, simply unpause the
  668. // underlying stream.
  669. self._read = function (n) {
  670. debug('wrapped _read', n);
  671. if (paused) {
  672. paused = false;
  673. stream.resume();
  674. }
  675. };
  676. return self;
  677. };
  678. // exposed for testing purposes only.
  679. Readable._fromList = fromList;
  680. // Pluck off n bytes from an array of buffers.
  681. // Length is the combined lengths of all the buffers in the list.
  682. // This function is designed to be inlinable, so please take care when making
  683. // changes to the function body.
  684. function fromList(n, state) {
  685. // nothing buffered
  686. if (state.length === 0) return null;
  687. var ret;
  688. if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
  689. // read it all, truncate the list
  690. 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);
  691. state.buffer.clear();
  692. } else {
  693. // read part of list
  694. ret = fromListPartial(n, state.buffer, state.decoder);
  695. }
  696. return ret;
  697. }
  698. // Extracts only enough buffered data to satisfy the amount requested.
  699. // This function is designed to be inlinable, so please take care when making
  700. // changes to the function body.
  701. function fromListPartial(n, list, hasStrings) {
  702. var ret;
  703. if (n < list.head.data.length) {
  704. // slice is the same for buffers and strings
  705. ret = list.head.data.slice(0, n);
  706. list.head.data = list.head.data.slice(n);
  707. } else if (n === list.head.data.length) {
  708. // first chunk is a perfect match
  709. ret = list.shift();
  710. } else {
  711. // result spans more than one buffer
  712. ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list);
  713. }
  714. return ret;
  715. }
  716. // Copies a specified amount of characters from the list of buffered data
  717. // chunks.
  718. // This function is designed to be inlinable, so please take care when making
  719. // changes to the function body.
  720. function copyFromBufferString(n, list) {
  721. var p = list.head;
  722. var c = 1;
  723. var ret = p.data;
  724. n -= ret.length;
  725. while (p = p.next) {
  726. var str = p.data;
  727. var nb = n > str.length ? str.length : n;
  728. if (nb === str.length) ret += str;else ret += str.slice(0, n);
  729. n -= nb;
  730. if (n === 0) {
  731. if (nb === str.length) {
  732. ++c;
  733. if (p.next) list.head = p.next;else list.head = list.tail = null;
  734. } else {
  735. list.head = p;
  736. p.data = str.slice(nb);
  737. }
  738. break;
  739. }
  740. ++c;
  741. }
  742. list.length -= c;
  743. return ret;
  744. }
  745. // Copies a specified amount of bytes from the list of buffered data chunks.
  746. // This function is designed to be inlinable, so please take care when making
  747. // changes to the function body.
  748. function copyFromBuffer(n, list) {
  749. var ret = bufferShim.allocUnsafe(n);
  750. var p = list.head;
  751. var c = 1;
  752. p.data.copy(ret);
  753. n -= p.data.length;
  754. while (p = p.next) {
  755. var buf = p.data;
  756. var nb = n > buf.length ? buf.length : n;
  757. buf.copy(ret, ret.length - n, 0, nb);
  758. n -= nb;
  759. if (n === 0) {
  760. if (nb === buf.length) {
  761. ++c;
  762. if (p.next) list.head = p.next;else list.head = list.tail = null;
  763. } else {
  764. list.head = p;
  765. p.data = buf.slice(nb);
  766. }
  767. break;
  768. }
  769. ++c;
  770. }
  771. list.length -= c;
  772. return ret;
  773. }
  774. function endReadable(stream) {
  775. var state = stream._readableState;
  776. // If we get here before consuming all the bytes, then that is a
  777. // bug in node. Should never happen.
  778. if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream');
  779. if (!state.endEmitted) {
  780. state.ended = true;
  781. processNextTick(endReadableNT, state, stream);
  782. }
  783. }
  784. function endReadableNT(state, stream) {
  785. // Check that we didn't get one last unshift.
  786. if (!state.endEmitted && state.length === 0) {
  787. state.endEmitted = true;
  788. stream.readable = false;
  789. stream.emit('end');
  790. }
  791. }
  792. function forEach(xs, f) {
  793. for (var i = 0, l = xs.length; i < l; i++) {
  794. f(xs[i], i);
  795. }
  796. }
  797. function indexOf(xs, x) {
  798. for (var i = 0, l = xs.length; i < l; i++) {
  799. if (xs[i] === x) return i;
  800. }
  801. return -1;
  802. }