_stream_readable.js 35 KB

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