_stream_readable.js 25 KB

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