_stream_readable.js 31 KB

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