readable.js 27 KB

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