_stream_readable.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. 'use strict';
  22. module.exports = Readable;
  23. /*<replacement>*/
  24. var Duplex;
  25. /*</replacement>*/
  26. Readable.ReadableState = ReadableState;
  27. /*<replacement>*/
  28. var EE = require('events').EventEmitter;
  29. var EElistenerCount = function EElistenerCount(emitter, type) {
  30. return emitter.listeners(type).length;
  31. };
  32. /*</replacement>*/
  33. /*<replacement>*/
  34. var Stream = require('./internal/streams/stream');
  35. /*</replacement>*/
  36. var Buffer = require('buffer').Buffer;
  37. var OurUint8Array = (typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : {}).Uint8Array || function () {};
  38. function _uint8ArrayToBuffer(chunk) {
  39. return Buffer.from(chunk);
  40. }
  41. function _isUint8Array(obj) {
  42. return Buffer.isBuffer(obj) || obj instanceof OurUint8Array;
  43. }
  44. /*<replacement>*/
  45. var debugUtil = require('util');
  46. var debug;
  47. if (debugUtil && debugUtil.debuglog) {
  48. debug = debugUtil.debuglog('stream');
  49. } else {
  50. debug = function debug() {};
  51. }
  52. /*</replacement>*/
  53. var BufferList = require('./internal/streams/buffer_list');
  54. var destroyImpl = require('./internal/streams/destroy');
  55. var _require = require('./internal/streams/state'),
  56. getHighWaterMark = _require.getHighWaterMark;
  57. var _require$codes = require('../errors').codes,
  58. ERR_INVALID_ARG_TYPE = _require$codes.ERR_INVALID_ARG_TYPE,
  59. ERR_STREAM_PUSH_AFTER_EOF = _require$codes.ERR_STREAM_PUSH_AFTER_EOF,
  60. ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED,
  61. ERR_STREAM_UNSHIFT_AFTER_END_EVENT = _require$codes.ERR_STREAM_UNSHIFT_AFTER_END_EVENT;
  62. // Lazy loaded to improve the startup performance.
  63. var StringDecoder;
  64. var createReadableStreamAsyncIterator;
  65. var from;
  66. require('inherits')(Readable, Stream);
  67. var errorOrDestroy = destroyImpl.errorOrDestroy;
  68. var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume'];
  69. function prependListener(emitter, event, fn) {
  70. // Sadly this is not cacheable as some libraries bundle their own
  71. // event emitter implementation with them.
  72. if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn);
  73. // This is a hack to make sure that our error handler is attached before any
  74. // userland ones. NEVER DO THIS. This is here only because this code needs
  75. // to continue to work with older versions of Node.js that do not include
  76. // the prependListener() method. The goal is to eventually remove this hack.
  77. if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (Array.isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]];
  78. }
  79. function ReadableState(options, stream, isDuplex) {
  80. Duplex = Duplex || require('./_stream_duplex');
  81. options = options || {};
  82. // Duplex streams are both readable and writable, but share
  83. // the same options object.
  84. // However, some cases require setting options to different
  85. // values for the readable and the writable sides of the duplex stream.
  86. // These options can be provided separately as readableXXX and writableXXX.
  87. if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex;
  88. // object stream flag. Used to make read(n) ignore n and to
  89. // make all the buffer merging and length checks go away
  90. this.objectMode = !!options.objectMode;
  91. if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
  92. // the point at which it stops calling _read() to fill the buffer
  93. // Note: 0 is a valid value, means "don't call _read preemptively ever"
  94. this.highWaterMark = getHighWaterMark(this, options, 'readableHighWaterMark', isDuplex);
  95. // A linked list is used to store data chunks instead of an array because the
  96. // linked list can remove elements from the beginning faster than
  97. // array.shift()
  98. this.buffer = new BufferList();
  99. this.length = 0;
  100. this.pipes = null;
  101. this.pipesCount = 0;
  102. this.flowing = null;
  103. this.ended = false;
  104. this.endEmitted = false;
  105. this.reading = false;
  106. // a flag to be able to tell if the event 'readable'/'data' is emitted
  107. // immediately, or on a later tick. We set this to true at first, because
  108. // any actions that shouldn't happen until "later" should generally also
  109. // not happen before the first read call.
  110. this.sync = true;
  111. // whenever we return null, then we set a flag to say
  112. // that we're awaiting a 'readable' event emission.
  113. this.needReadable = false;
  114. this.emittedReadable = false;
  115. this.readableListening = false;
  116. this.resumeScheduled = false;
  117. this.paused = true;
  118. // Should close be emitted on destroy. Defaults to true.
  119. this.emitClose = options.emitClose !== false;
  120. // Should .destroy() be called after 'end' (and potentially 'finish')
  121. this.autoDestroy = !!options.autoDestroy;
  122. // has it been destroyed
  123. this.destroyed = false;
  124. // Crypto is kind of old and crusty. Historically, its default string
  125. // encoding is 'binary' so we have to make this configurable.
  126. // Everything else in the universe uses 'utf8', though.
  127. this.defaultEncoding = options.defaultEncoding || 'utf8';
  128. // the number of writers that are awaiting a drain event in .pipe()s
  129. this.awaitDrain = 0;
  130. // if true, a maybeReadMore has been scheduled
  131. this.readingMore = false;
  132. this.decoder = null;
  133. this.encoding = null;
  134. if (options.encoding) {
  135. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  136. this.decoder = new StringDecoder(options.encoding);
  137. this.encoding = options.encoding;
  138. }
  139. }
  140. function Readable(options) {
  141. Duplex = Duplex || require('./_stream_duplex');
  142. if (!(this instanceof Readable)) return new Readable(options);
  143. // Checking for a Stream.Duplex instance is faster here instead of inside
  144. // the ReadableState constructor, at least with V8 6.5
  145. var isDuplex = this instanceof Duplex;
  146. this._readableState = new ReadableState(options, this, isDuplex);
  147. // legacy
  148. this.readable = true;
  149. if (options) {
  150. if (typeof options.read === 'function') this._read = options.read;
  151. if (typeof options.destroy === 'function') this._destroy = options.destroy;
  152. }
  153. Stream.call(this);
  154. }
  155. Object.defineProperty(Readable.prototype, 'destroyed', {
  156. // making it explicit this property is not enumerable
  157. // because otherwise some prototype manipulation in
  158. // userland will fail
  159. enumerable: false,
  160. get: function get() {
  161. if (this._readableState === undefined) {
  162. return false;
  163. }
  164. return this._readableState.destroyed;
  165. },
  166. set: function set(value) {
  167. // we ignore the value if the stream
  168. // has not been initialized yet
  169. if (!this._readableState) {
  170. return;
  171. }
  172. // backward compatibility, the user is explicitly
  173. // managing destroyed
  174. this._readableState.destroyed = value;
  175. }
  176. });
  177. Readable.prototype.destroy = destroyImpl.destroy;
  178. Readable.prototype._undestroy = destroyImpl.undestroy;
  179. Readable.prototype._destroy = function (err, cb) {
  180. cb(err);
  181. };
  182. // Manually shove something into the read() buffer.
  183. // This returns true if the highWaterMark has not been hit yet,
  184. // similar to how Writable.write() returns true if you should
  185. // write() some more.
  186. Readable.prototype.push = function (chunk, encoding) {
  187. var state = this._readableState;
  188. var skipChunkCheck;
  189. if (!state.objectMode) {
  190. if (typeof chunk === 'string') {
  191. encoding = encoding || state.defaultEncoding;
  192. if (encoding !== state.encoding) {
  193. chunk = Buffer.from(chunk, encoding);
  194. encoding = '';
  195. }
  196. skipChunkCheck = true;
  197. }
  198. } else {
  199. skipChunkCheck = true;
  200. }
  201. return readableAddChunk(this, chunk, encoding, false, skipChunkCheck);
  202. };
  203. // Unshift should *always* be something directly out of read()
  204. Readable.prototype.unshift = function (chunk) {
  205. return readableAddChunk(this, chunk, null, true, false);
  206. };
  207. function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) {
  208. debug('readableAddChunk', chunk);
  209. var state = stream._readableState;
  210. if (chunk === null) {
  211. state.reading = false;
  212. onEofChunk(stream, state);
  213. } else {
  214. var er;
  215. if (!skipChunkCheck) er = chunkInvalid(state, chunk);
  216. if (er) {
  217. errorOrDestroy(stream, er);
  218. } else if (state.objectMode || chunk && chunk.length > 0) {
  219. if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) {
  220. chunk = _uint8ArrayToBuffer(chunk);
  221. }
  222. if (addToFront) {
  223. if (state.endEmitted) errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());else addChunk(stream, state, chunk, true);
  224. } else if (state.ended) {
  225. errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF());
  226. } else if (state.destroyed) {
  227. return false;
  228. } else {
  229. state.reading = false;
  230. if (state.decoder && !encoding) {
  231. chunk = state.decoder.write(chunk);
  232. if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state);
  233. } else {
  234. addChunk(stream, state, chunk, false);
  235. }
  236. }
  237. } else if (!addToFront) {
  238. state.reading = false;
  239. maybeReadMore(stream, state);
  240. }
  241. }
  242. // We can push more data if we are below the highWaterMark.
  243. // Also, if we have no data yet, we can stand some more bytes.
  244. // This is to work around cases where hwm=0, such as the repl.
  245. return !state.ended && (state.length < state.highWaterMark || state.length === 0);
  246. }
  247. function addChunk(stream, state, chunk, addToFront) {
  248. if (state.flowing && state.length === 0 && !state.sync) {
  249. state.awaitDrain = 0;
  250. stream.emit('data', chunk);
  251. } else {
  252. // update the buffer info.
  253. state.length += state.objectMode ? 1 : chunk.length;
  254. if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
  255. if (state.needReadable) emitReadable(stream);
  256. }
  257. maybeReadMore(stream, state);
  258. }
  259. function chunkInvalid(state, chunk) {
  260. var er;
  261. if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) {
  262. er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
  263. }
  264. return er;
  265. }
  266. Readable.prototype.isPaused = function () {
  267. return this._readableState.flowing === false;
  268. };
  269. // backwards compatibility.
  270. Readable.prototype.setEncoding = function (enc) {
  271. if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder;
  272. var decoder = new StringDecoder(enc);
  273. this._readableState.decoder = decoder;
  274. // If setEncoding(null), decoder.encoding equals utf8
  275. this._readableState.encoding = this._readableState.decoder.encoding;
  276. // Iterate over current buffer to convert already stored Buffers:
  277. var p = this._readableState.buffer.head;
  278. var content = '';
  279. while (p !== null) {
  280. content += decoder.write(p.data);
  281. p = p.next;
  282. }
  283. this._readableState.buffer.clear();
  284. if (content !== '') this._readableState.buffer.push(content);
  285. this._readableState.length = content.length;
  286. return this;
  287. };
  288. // Don't raise the hwm > 1GB
  289. var MAX_HWM = 0x40000000;
  290. function computeNewHighWaterMark(n) {
  291. if (n >= MAX_HWM) {
  292. // TODO(ronag): Throw ERR_VALUE_OUT_OF_RANGE.
  293. n = MAX_HWM;
  294. } else {
  295. // Get the next highest power of 2 to prevent increasing hwm excessively in
  296. // tiny amounts
  297. n--;
  298. n |= n >>> 1;
  299. n |= n >>> 2;
  300. n |= n >>> 4;
  301. n |= n >>> 8;
  302. n |= n >>> 16;
  303. n++;
  304. }
  305. return n;
  306. }
  307. // This function is designed to be inlinable, so please take care when making
  308. // changes to the function body.
  309. function howMuchToRead(n, state) {
  310. if (n <= 0 || state.length === 0 && state.ended) return 0;
  311. if (state.objectMode) return 1;
  312. if (n !== n) {
  313. // Only flow one buffer at a time
  314. if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
  315. }
  316. // If we're asking for more than the current hwm, then raise the hwm.
  317. if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
  318. if (n <= state.length) return n;
  319. // Don't have enough
  320. if (!state.ended) {
  321. state.needReadable = true;
  322. return 0;
  323. }
  324. return state.length;
  325. }
  326. // you can override either this method, or the async _read(n) below.
  327. Readable.prototype.read = function (n) {
  328. debug('read', n);
  329. n = parseInt(n, 10);
  330. var state = this._readableState;
  331. var nOrig = n;
  332. if (n !== 0) state.emittedReadable = false;
  333. // if we're doing read(0) to trigger a readable event, but we
  334. // already have a bunch of data in the buffer, then just trigger
  335. // the 'readable' event and move on.
  336. if (n === 0 && state.needReadable && ((state.highWaterMark !== 0 ? state.length >= state.highWaterMark : state.length > 0) || state.ended)) {
  337. debug('read: emitReadable', state.length, state.ended);
  338. if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
  339. return null;
  340. }
  341. n = howMuchToRead(n, state);
  342. // if we've ended, and we're now clear, then finish it up.
  343. if (n === 0 && state.ended) {
  344. if (state.length === 0) endReadable(this);
  345. return null;
  346. }
  347. // All the actual chunk generation logic needs to be
  348. // *below* the call to _read. The reason is that in certain
  349. // synthetic stream cases, such as passthrough streams, _read
  350. // may be a completely synchronous operation which may change
  351. // the state of the read buffer, providing enough data when
  352. // before there was *not* enough.
  353. //
  354. // So, the steps are:
  355. // 1. Figure out what the state of things will be after we do
  356. // a read from the buffer.
  357. //
  358. // 2. If that resulting state will trigger a _read, then call _read.
  359. // Note that this may be asynchronous, or synchronous. Yes, it is
  360. // deeply ugly to write APIs this way, but that still doesn't mean
  361. // that the Readable class should behave improperly, as streams are
  362. // designed to be sync/async agnostic.
  363. // Take note if the _read call is sync or async (ie, if the read call
  364. // has returned yet), so that we know whether or not it's safe to emit
  365. // 'readable' etc.
  366. //
  367. // 3. Actually pull the requested chunks out of the buffer and return.
  368. // if we need a readable event, then we need to do some reading.
  369. var doRead = state.needReadable;
  370. debug('need readable', doRead);
  371. // if we currently have less than the highWaterMark, then also read some
  372. if (state.length === 0 || state.length - n < state.highWaterMark) {
  373. doRead = true;
  374. debug('length less than watermark', doRead);
  375. }
  376. // however, if we've ended, then there's no point, and if we're already
  377. // reading, then it's unnecessary.
  378. if (state.ended || state.reading) {
  379. doRead = false;
  380. debug('reading or ended', doRead);
  381. } else if (doRead) {
  382. debug('do read');
  383. state.reading = true;
  384. state.sync = true;
  385. // if the length is currently zero, then we *need* a readable event.
  386. if (state.length === 0) state.needReadable = true;
  387. // call internal read method
  388. this._read(state.highWaterMark);
  389. state.sync = false;
  390. // If _read pushed data synchronously, then `reading` will be false,
  391. // and we need to re-evaluate how much data we can return to the user.
  392. if (!state.reading) n = howMuchToRead(nOrig, state);
  393. }
  394. var ret;
  395. if (n > 0) ret = fromList(n, state);else ret = null;
  396. if (ret === null) {
  397. state.needReadable = state.length <= state.highWaterMark;
  398. n = 0;
  399. } else {
  400. state.length -= n;
  401. state.awaitDrain = 0;
  402. }
  403. if (state.length === 0) {
  404. // If we have nothing in the buffer, then we want to know
  405. // as soon as we *do* get something into the buffer.
  406. if (!state.ended) state.needReadable = true;
  407. // If we tried to read() past the EOF, then emit end on the next tick.
  408. if (nOrig !== n && state.ended) endReadable(this);
  409. }
  410. if (ret !== null) this.emit('data', ret);
  411. return ret;
  412. };
  413. function onEofChunk(stream, state) {
  414. debug('onEofChunk');
  415. if (state.ended) return;
  416. if (state.decoder) {
  417. var chunk = state.decoder.end();
  418. if (chunk && chunk.length) {
  419. state.buffer.push(chunk);
  420. state.length += state.objectMode ? 1 : chunk.length;
  421. }
  422. }
  423. state.ended = true;
  424. if (state.sync) {
  425. // if we are sync, wait until next tick to emit the data.
  426. // Otherwise we risk emitting data in the flow()
  427. // the readable code triggers during a read() call
  428. emitReadable(stream);
  429. } else {
  430. // emit 'readable' now to make sure it gets picked up.
  431. state.needReadable = false;
  432. if (!state.emittedReadable) {
  433. state.emittedReadable = true;
  434. emitReadable_(stream);
  435. }
  436. }
  437. }
  438. // Don't emit readable right away in sync mode, because this can trigger
  439. // another read() call => stack overflow. This way, it might trigger
  440. // a nextTick recursion warning, but that's not so bad.
  441. function emitReadable(stream) {
  442. var state = stream._readableState;
  443. debug('emitReadable', state.needReadable, state.emittedReadable);
  444. state.needReadable = false;
  445. if (!state.emittedReadable) {
  446. debug('emitReadable', state.flowing);
  447. state.emittedReadable = true;
  448. process.nextTick(emitReadable_, stream);
  449. }
  450. }
  451. function emitReadable_(stream) {
  452. var state = stream._readableState;
  453. debug('emitReadable_', state.destroyed, state.length, state.ended);
  454. if (!state.destroyed && (state.length || state.ended)) {
  455. stream.emit('readable');
  456. state.emittedReadable = false;
  457. }
  458. // The stream needs another readable event if
  459. // 1. It is not flowing, as the flow mechanism will take
  460. // care of it.
  461. // 2. It is not ended.
  462. // 3. It is below the highWaterMark, so we can schedule
  463. // another readable later.
  464. state.needReadable = !state.flowing && !state.ended && state.length <= state.highWaterMark;
  465. flow(stream);
  466. }
  467. // at this point, the user has presumably seen the 'readable' event,
  468. // and called read() to consume some data. that may have triggered
  469. // in turn another _read(n) call, in which case reading = true if
  470. // it's in progress.
  471. // However, if we're not ended, or reading, and the length < hwm,
  472. // then go ahead and try to read some more preemptively.
  473. function maybeReadMore(stream, state) {
  474. if (!state.readingMore) {
  475. state.readingMore = true;
  476. process.nextTick(maybeReadMore_, stream, state);
  477. }
  478. }
  479. function maybeReadMore_(stream, state) {
  480. // Attempt to read more data if we should.
  481. //
  482. // The conditions for reading more data are (one of):
  483. // - Not enough data buffered (state.length < state.highWaterMark). The loop
  484. // is responsible for filling the buffer with enough data if such data
  485. // is available. If highWaterMark is 0 and we are not in the flowing mode
  486. // we should _not_ attempt to buffer any extra data. We'll get more data
  487. // when the stream consumer calls read() instead.
  488. // - No data in the buffer, and the stream is in flowing mode. In this mode
  489. // the loop below is responsible for ensuring read() is called. Failing to
  490. // call read here would abort the flow and there's no other mechanism for
  491. // continuing the flow if the stream consumer has just subscribed to the
  492. // 'data' event.
  493. //
  494. // In addition to the above conditions to keep reading data, the following
  495. // conditions prevent the data from being read:
  496. // - The stream has ended (state.ended).
  497. // - There is already a pending 'read' operation (state.reading). This is a
  498. // case where the the stream has called the implementation defined _read()
  499. // method, but they are processing the call asynchronously and have _not_
  500. // called push() with new data. In this case we skip performing more
  501. // read()s. The execution ends in this method again after the _read() ends
  502. // up calling push() with more data.
  503. while (!state.reading && !state.ended && (state.length < state.highWaterMark || state.flowing && state.length === 0)) {
  504. var len = state.length;
  505. debug('maybeReadMore read 0');
  506. stream.read(0);
  507. if (len === state.length)
  508. // didn't get any data, stop spinning.
  509. break;
  510. }
  511. state.readingMore = false;
  512. }
  513. // abstract method. to be overridden in specific implementation classes.
  514. // call cb(er, data) where data is <= n in length.
  515. // for virtual (non-string, non-buffer) streams, "length" is somewhat
  516. // arbitrary, and perhaps not very meaningful.
  517. Readable.prototype._read = function (n) {
  518. errorOrDestroy(this, new ERR_METHOD_NOT_IMPLEMENTED('_read()'));
  519. };
  520. Readable.prototype.pipe = function (dest, pipeOpts) {
  521. var src = this;
  522. var state = this._readableState;
  523. switch (state.pipesCount) {
  524. case 0:
  525. state.pipes = dest;
  526. break;
  527. case 1:
  528. state.pipes = [state.pipes, dest];
  529. break;
  530. default:
  531. state.pipes.push(dest);
  532. break;
  533. }
  534. state.pipesCount += 1;
  535. debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
  536. var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr;
  537. var endFn = doEnd ? onend : unpipe;
  538. if (state.endEmitted) process.nextTick(endFn);else src.once('end', endFn);
  539. dest.on('unpipe', onunpipe);
  540. function onunpipe(readable, unpipeInfo) {
  541. debug('onunpipe');
  542. if (readable === src) {
  543. if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
  544. unpipeInfo.hasUnpiped = true;
  545. cleanup();
  546. }
  547. }
  548. }
  549. function onend() {
  550. debug('onend');
  551. dest.end();
  552. }
  553. // when the dest drains, it reduces the awaitDrain counter
  554. // on the source. This would be more elegant with a .once()
  555. // handler in flow(), but adding and removing repeatedly is
  556. // too slow.
  557. var ondrain = pipeOnDrain(src);
  558. dest.on('drain', ondrain);
  559. var cleanedUp = false;
  560. function cleanup() {
  561. debug('cleanup');
  562. // cleanup event handlers once the pipe is broken
  563. dest.removeListener('close', onclose);
  564. dest.removeListener('finish', onfinish);
  565. dest.removeListener('drain', ondrain);
  566. dest.removeListener('error', onerror);
  567. dest.removeListener('unpipe', onunpipe);
  568. src.removeListener('end', onend);
  569. src.removeListener('end', unpipe);
  570. src.removeListener('data', ondata);
  571. cleanedUp = true;
  572. // if the reader is waiting for a drain event from this
  573. // specific writer, then it would cause it to never start
  574. // flowing again.
  575. // So, if this is awaiting a drain, then we just call it now.
  576. // If we don't know, then assume that we are waiting for one.
  577. if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
  578. }
  579. src.on('data', ondata);
  580. function ondata(chunk) {
  581. debug('ondata');
  582. var ret = dest.write(chunk);
  583. debug('dest.write', ret);
  584. if (ret === false) {
  585. // If the user unpiped during `dest.write()`, it is possible
  586. // to get stuck in a permanently paused state if that write
  587. // also returned false.
  588. // => Check whether `dest` is still a piping destination.
  589. if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
  590. debug('false write response, pause', state.awaitDrain);
  591. state.awaitDrain++;
  592. }
  593. src.pause();
  594. }
  595. }
  596. // if the dest has an error, then stop piping into it.
  597. // however, don't suppress the throwing behavior for this.
  598. function onerror(er) {
  599. debug('onerror', er);
  600. unpipe();
  601. dest.removeListener('error', onerror);
  602. if (EElistenerCount(dest, 'error') === 0) errorOrDestroy(dest, er);
  603. }
  604. // Make sure our error handler is attached before userland ones.
  605. prependListener(dest, 'error', onerror);
  606. // Both close and finish should trigger unpipe, but only once.
  607. function onclose() {
  608. dest.removeListener('finish', onfinish);
  609. unpipe();
  610. }
  611. dest.once('close', onclose);
  612. function onfinish() {
  613. debug('onfinish');
  614. dest.removeListener('close', onclose);
  615. unpipe();
  616. }
  617. dest.once('finish', onfinish);
  618. function unpipe() {
  619. debug('unpipe');
  620. src.unpipe(dest);
  621. }
  622. // tell the dest that it's being piped to
  623. dest.emit('pipe', src);
  624. // start the flow if it hasn't been started already.
  625. if (!state.flowing) {
  626. debug('pipe resume');
  627. src.resume();
  628. }
  629. return dest;
  630. };
  631. function pipeOnDrain(src) {
  632. return function pipeOnDrainFunctionResult() {
  633. var state = src._readableState;
  634. debug('pipeOnDrain', state.awaitDrain);
  635. if (state.awaitDrain) state.awaitDrain--;
  636. if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) {
  637. state.flowing = true;
  638. flow(src);
  639. }
  640. };
  641. }
  642. Readable.prototype.unpipe = function (dest) {
  643. var state = this._readableState;
  644. var unpipeInfo = {
  645. hasUnpiped: false
  646. };
  647. // if we're not piping anywhere, then do nothing.
  648. if (state.pipesCount === 0) return this;
  649. // just one destination. most common case.
  650. if (state.pipesCount === 1) {
  651. // passed in one, but it's not the right one.
  652. if (dest && dest !== state.pipes) return this;
  653. if (!dest) dest = state.pipes;
  654. // got a match.
  655. state.pipes = null;
  656. state.pipesCount = 0;
  657. state.flowing = false;
  658. if (dest) dest.emit('unpipe', this, unpipeInfo);
  659. return this;
  660. }
  661. // slow case. multiple pipe destinations.
  662. if (!dest) {
  663. // remove all.
  664. var dests = state.pipes;
  665. var len = state.pipesCount;
  666. state.pipes = null;
  667. state.pipesCount = 0;
  668. state.flowing = false;
  669. for (var i = 0; i < len; i++) dests[i].emit('unpipe', this, {
  670. hasUnpiped: false
  671. });
  672. return this;
  673. }
  674. // try to find the right one.
  675. var index = indexOf(state.pipes, dest);
  676. if (index === -1) return this;
  677. state.pipes.splice(index, 1);
  678. state.pipesCount -= 1;
  679. if (state.pipesCount === 1) state.pipes = state.pipes[0];
  680. dest.emit('unpipe', this, unpipeInfo);
  681. return this;
  682. };
  683. // set up data events if they are asked for
  684. // Ensure readable listeners eventually get something
  685. Readable.prototype.on = function (ev, fn) {
  686. var res = Stream.prototype.on.call(this, ev, fn);
  687. var state = this._readableState;
  688. if (ev === 'data') {
  689. // update readableListening so that resume() may be a no-op
  690. // a few lines down. This is needed to support once('readable').
  691. state.readableListening = this.listenerCount('readable') > 0;
  692. // Try start flowing on next tick if stream isn't explicitly paused
  693. if (state.flowing !== false) this.resume();
  694. } else if (ev === 'readable') {
  695. if (!state.endEmitted && !state.readableListening) {
  696. state.readableListening = state.needReadable = true;
  697. state.flowing = false;
  698. state.emittedReadable = false;
  699. debug('on readable', state.length, state.reading);
  700. if (state.length) {
  701. emitReadable(this);
  702. } else if (!state.reading) {
  703. process.nextTick(nReadingNextTick, this);
  704. }
  705. }
  706. }
  707. return res;
  708. };
  709. Readable.prototype.addListener = Readable.prototype.on;
  710. Readable.prototype.removeListener = function (ev, fn) {
  711. var res = Stream.prototype.removeListener.call(this, ev, fn);
  712. if (ev === 'readable') {
  713. // We need to check if there is someone still listening to
  714. // readable and reset the state. However this needs to happen
  715. // after readable has been emitted but before I/O (nextTick) to
  716. // support once('readable', fn) cycles. This means that calling
  717. // resume within the same tick will have no
  718. // effect.
  719. process.nextTick(updateReadableListening, this);
  720. }
  721. return res;
  722. };
  723. Readable.prototype.removeAllListeners = function (ev) {
  724. var res = Stream.prototype.removeAllListeners.apply(this, arguments);
  725. if (ev === 'readable' || ev === undefined) {
  726. // We need to check if there is someone still listening to
  727. // readable and reset the state. However this needs to happen
  728. // after readable has been emitted but before I/O (nextTick) to
  729. // support once('readable', fn) cycles. This means that calling
  730. // resume within the same tick will have no
  731. // effect.
  732. process.nextTick(updateReadableListening, this);
  733. }
  734. return res;
  735. };
  736. function updateReadableListening(self) {
  737. var state = self._readableState;
  738. state.readableListening = self.listenerCount('readable') > 0;
  739. if (state.resumeScheduled && !state.paused) {
  740. // flowing needs to be set to true now, otherwise
  741. // the upcoming resume will not flow.
  742. state.flowing = true;
  743. // crude way to check if we should resume
  744. } else if (self.listenerCount('data') > 0) {
  745. self.resume();
  746. }
  747. }
  748. function nReadingNextTick(self) {
  749. debug('readable nexttick read 0');
  750. self.read(0);
  751. }
  752. // pause() and resume() are remnants of the legacy readable stream API
  753. // If the user uses them, then switch into old mode.
  754. Readable.prototype.resume = function () {
  755. var state = this._readableState;
  756. if (!state.flowing) {
  757. debug('resume');
  758. // we flow only if there is no one listening
  759. // for readable, but we still have to call
  760. // resume()
  761. state.flowing = !state.readableListening;
  762. resume(this, state);
  763. }
  764. state.paused = false;
  765. return this;
  766. };
  767. function resume(stream, state) {
  768. if (!state.resumeScheduled) {
  769. state.resumeScheduled = true;
  770. process.nextTick(resume_, stream, state);
  771. }
  772. }
  773. function resume_(stream, state) {
  774. debug('resume', state.reading);
  775. if (!state.reading) {
  776. stream.read(0);
  777. }
  778. state.resumeScheduled = false;
  779. stream.emit('resume');
  780. flow(stream);
  781. if (state.flowing && !state.reading) stream.read(0);
  782. }
  783. Readable.prototype.pause = function () {
  784. debug('call pause flowing=%j', this._readableState.flowing);
  785. if (this._readableState.flowing !== false) {
  786. debug('pause');
  787. this._readableState.flowing = false;
  788. this.emit('pause');
  789. }
  790. this._readableState.paused = true;
  791. return this;
  792. };
  793. function flow(stream) {
  794. var state = stream._readableState;
  795. debug('flow', state.flowing);
  796. while (state.flowing && stream.read() !== null);
  797. }
  798. // wrap an old-style stream as the async data source.
  799. // This is *not* part of the readable stream interface.
  800. // It is an ugly unfortunate mess of history.
  801. Readable.prototype.wrap = function (stream) {
  802. var _this = this;
  803. var state = this._readableState;
  804. var paused = false;
  805. stream.on('end', function () {
  806. debug('wrapped end');
  807. if (state.decoder && !state.ended) {
  808. var chunk = state.decoder.end();
  809. if (chunk && chunk.length) _this.push(chunk);
  810. }
  811. _this.push(null);
  812. });
  813. stream.on('data', function (chunk) {
  814. debug('wrapped data');
  815. if (state.decoder) chunk = state.decoder.write(chunk);
  816. // don't skip over falsy values in objectMode
  817. if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
  818. var ret = _this.push(chunk);
  819. if (!ret) {
  820. paused = true;
  821. stream.pause();
  822. }
  823. });
  824. // proxy all the other methods.
  825. // important when wrapping filters and duplexes.
  826. for (var i in stream) {
  827. if (this[i] === undefined && typeof stream[i] === 'function') {
  828. this[i] = function methodWrap(method) {
  829. return function methodWrapReturnFunction() {
  830. return stream[method].apply(stream, arguments);
  831. };
  832. }(i);
  833. }
  834. }
  835. // proxy certain important events.
  836. for (var n = 0; n < kProxyEvents.length; n++) {
  837. stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n]));
  838. }
  839. // when we try to consume some more bytes, simply unpause the
  840. // underlying stream.
  841. this._read = function (n) {
  842. debug('wrapped _read', n);
  843. if (paused) {
  844. paused = false;
  845. stream.resume();
  846. }
  847. };
  848. return this;
  849. };
  850. if (typeof Symbol === 'function') {
  851. Readable.prototype[Symbol.asyncIterator] = function () {
  852. if (createReadableStreamAsyncIterator === undefined) {
  853. createReadableStreamAsyncIterator = require('./internal/streams/async_iterator');
  854. }
  855. return createReadableStreamAsyncIterator(this);
  856. };
  857. }
  858. Object.defineProperty(Readable.prototype, 'readableHighWaterMark', {
  859. // making it explicit this property is not enumerable
  860. // because otherwise some prototype manipulation in
  861. // userland will fail
  862. enumerable: false,
  863. get: function get() {
  864. return this._readableState.highWaterMark;
  865. }
  866. });
  867. Object.defineProperty(Readable.prototype, 'readableBuffer', {
  868. // making it explicit this property is not enumerable
  869. // because otherwise some prototype manipulation in
  870. // userland will fail
  871. enumerable: false,
  872. get: function get() {
  873. return this._readableState && this._readableState.buffer;
  874. }
  875. });
  876. Object.defineProperty(Readable.prototype, 'readableFlowing', {
  877. // making it explicit this property is not enumerable
  878. // because otherwise some prototype manipulation in
  879. // userland will fail
  880. enumerable: false,
  881. get: function get() {
  882. return this._readableState.flowing;
  883. },
  884. set: function set(state) {
  885. if (this._readableState) {
  886. this._readableState.flowing = state;
  887. }
  888. }
  889. });
  890. // exposed for testing purposes only.
  891. Readable._fromList = fromList;
  892. Object.defineProperty(Readable.prototype, 'readableLength', {
  893. // making it explicit this property is not enumerable
  894. // because otherwise some prototype manipulation in
  895. // userland will fail
  896. enumerable: false,
  897. get: function get() {
  898. return this._readableState.length;
  899. }
  900. });
  901. // Pluck off n bytes from an array of buffers.
  902. // Length is the combined lengths of all the buffers in the list.
  903. // This function is designed to be inlinable, so please take care when making
  904. // changes to the function body.
  905. function fromList(n, state) {
  906. // nothing buffered
  907. if (state.length === 0) return null;
  908. var ret;
  909. if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
  910. // read it all, truncate the list
  911. if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.first();else ret = state.buffer.concat(state.length);
  912. state.buffer.clear();
  913. } else {
  914. // read part of list
  915. ret = state.buffer.consume(n, state.decoder);
  916. }
  917. return ret;
  918. }
  919. function endReadable(stream) {
  920. var state = stream._readableState;
  921. debug('endReadable', state.endEmitted);
  922. if (!state.endEmitted) {
  923. state.ended = true;
  924. process.nextTick(endReadableNT, state, stream);
  925. }
  926. }
  927. function endReadableNT(state, stream) {
  928. debug('endReadableNT', state.endEmitted, state.length);
  929. // Check that we didn't get one last unshift.
  930. if (!state.endEmitted && state.length === 0) {
  931. state.endEmitted = true;
  932. stream.readable = false;
  933. stream.emit('end');
  934. if (state.autoDestroy) {
  935. // In case of duplex streams we need a way to detect
  936. // if the writable side is ready for autoDestroy as well
  937. var wState = stream._writableState;
  938. if (!wState || wState.autoDestroy && wState.finished) {
  939. stream.destroy();
  940. }
  941. }
  942. }
  943. }
  944. if (typeof Symbol === 'function') {
  945. Readable.from = function (iterable, opts) {
  946. if (from === undefined) {
  947. from = require('./internal/streams/from');
  948. }
  949. return from(Readable, iterable, opts);
  950. };
  951. }
  952. function indexOf(xs, x) {
  953. for (var i = 0, l = xs.length; i < l; i++) {
  954. if (xs[i] === x) return i;
  955. }
  956. return -1;
  957. }