1 | 'use strict';
|
2 |
|
3 | Readable.ReadableState = ReadableState;
|
4 | import EventEmitter from 'events';
|
5 | import {inherits, debuglog} from 'util';
|
6 | import BufferList from './buffer-list';
|
7 | import {StringDecoder} from 'string_decoder';
|
8 | import {Duplex} from './duplex';
|
9 | import {nextTick} from 'process';
|
10 |
|
11 | var debug = debuglog('stream');
|
12 | inherits(Readable, EventEmitter);
|
13 |
|
14 | function prependListener(emitter, event, fn) {
|
15 |
|
16 |
|
17 | if (typeof emitter.prependListener === 'function') {
|
18 | return emitter.prependListener(event, fn);
|
19 | } else {
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | if (!emitter._events || !emitter._events[event])
|
25 | emitter.on(event, fn);
|
26 | else if (Array.isArray(emitter._events[event]))
|
27 | emitter._events[event].unshift(fn);
|
28 | else
|
29 | emitter._events[event] = [fn, emitter._events[event]];
|
30 | }
|
31 | }
|
32 | function listenerCount (emitter, type) {
|
33 | return emitter.listeners(type).length;
|
34 | }
|
35 | function ReadableState(options, stream) {
|
36 |
|
37 | options = options || {};
|
38 |
|
39 |
|
40 |
|
41 | this.objectMode = !!options.objectMode;
|
42 |
|
43 | if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode;
|
44 |
|
45 |
|
46 |
|
47 | var hwm = options.highWaterMark;
|
48 | var defaultHwm = this.objectMode ? 16 : 16 * 1024;
|
49 | this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm;
|
50 |
|
51 |
|
52 | this.highWaterMark = ~ ~this.highWaterMark;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | this.buffer = new BufferList();
|
58 | this.length = 0;
|
59 | this.pipes = null;
|
60 | this.pipesCount = 0;
|
61 | this.flowing = null;
|
62 | this.ended = false;
|
63 | this.endEmitted = false;
|
64 | this.reading = false;
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | this.sync = true;
|
71 |
|
72 |
|
73 |
|
74 | this.needReadable = false;
|
75 | this.emittedReadable = false;
|
76 | this.readableListening = false;
|
77 | this.resumeScheduled = false;
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | this.defaultEncoding = options.defaultEncoding || 'utf8';
|
83 |
|
84 |
|
85 |
|
86 | this.ranOut = false;
|
87 |
|
88 |
|
89 | this.awaitDrain = 0;
|
90 |
|
91 |
|
92 | this.readingMore = false;
|
93 |
|
94 | this.decoder = null;
|
95 | this.encoding = null;
|
96 | if (options.encoding) {
|
97 | this.decoder = new StringDecoder(options.encoding);
|
98 | this.encoding = options.encoding;
|
99 | }
|
100 | }
|
101 | export default Readable;
|
102 | export function Readable(options) {
|
103 |
|
104 | if (!(this instanceof Readable)) return new Readable(options);
|
105 |
|
106 | this._readableState = new ReadableState(options, this);
|
107 |
|
108 |
|
109 | this.readable = true;
|
110 |
|
111 | if (options && typeof options.read === 'function') this._read = options.read;
|
112 |
|
113 | EventEmitter.call(this);
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | Readable.prototype.push = function (chunk, encoding) {
|
121 | var state = this._readableState;
|
122 |
|
123 | if (!state.objectMode && typeof chunk === 'string') {
|
124 | encoding = encoding || state.defaultEncoding;
|
125 | if (encoding !== state.encoding) {
|
126 | chunk = Buffer.from(chunk, encoding);
|
127 | encoding = '';
|
128 | }
|
129 | }
|
130 |
|
131 | return readableAddChunk(this, state, chunk, encoding, false);
|
132 | };
|
133 |
|
134 |
|
135 | Readable.prototype.unshift = function (chunk) {
|
136 | var state = this._readableState;
|
137 | return readableAddChunk(this, state, chunk, '', true);
|
138 | };
|
139 |
|
140 | Readable.prototype.isPaused = function () {
|
141 | return this._readableState.flowing === false;
|
142 | };
|
143 |
|
144 | function readableAddChunk(stream, state, chunk, encoding, addToFront) {
|
145 | var er = chunkInvalid(state, chunk);
|
146 | if (er) {
|
147 | stream.emit('error', er);
|
148 | } else if (chunk === null) {
|
149 | state.reading = false;
|
150 | onEofChunk(stream, state);
|
151 | } else if (state.objectMode || chunk && chunk.length > 0) {
|
152 | if (state.ended && !addToFront) {
|
153 | var e = new Error('stream.push() after EOF');
|
154 | stream.emit('error', e);
|
155 | } else if (state.endEmitted && addToFront) {
|
156 | var _e = new Error('stream.unshift() after end event');
|
157 | stream.emit('error', _e);
|
158 | } else {
|
159 | var skipAdd;
|
160 | if (state.decoder && !addToFront && !encoding) {
|
161 | chunk = state.decoder.write(chunk);
|
162 | skipAdd = !state.objectMode && chunk.length === 0;
|
163 | }
|
164 |
|
165 | if (!addToFront) state.reading = false;
|
166 |
|
167 |
|
168 |
|
169 | if (!skipAdd) {
|
170 |
|
171 | if (state.flowing && state.length === 0 && !state.sync) {
|
172 | stream.emit('data', chunk);
|
173 | stream.read(0);
|
174 | } else {
|
175 |
|
176 | state.length += state.objectMode ? 1 : chunk.length;
|
177 | if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk);
|
178 |
|
179 | if (state.needReadable) emitReadable(stream);
|
180 | }
|
181 | }
|
182 |
|
183 | maybeReadMore(stream, state);
|
184 | }
|
185 | } else if (!addToFront) {
|
186 | state.reading = false;
|
187 | }
|
188 |
|
189 | return needMoreData(state);
|
190 | }
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | function needMoreData(state) {
|
200 | return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0);
|
201 | }
|
202 |
|
203 |
|
204 | Readable.prototype.setEncoding = function (enc) {
|
205 | this._readableState.decoder = new StringDecoder(enc);
|
206 | this._readableState.encoding = enc;
|
207 | return this;
|
208 | };
|
209 |
|
210 |
|
211 | var MAX_HWM = 0x800000;
|
212 | function computeNewHighWaterMark(n) {
|
213 | if (n >= MAX_HWM) {
|
214 | n = MAX_HWM;
|
215 | } else {
|
216 |
|
217 |
|
218 | n--;
|
219 | n |= n >>> 1;
|
220 | n |= n >>> 2;
|
221 | n |= n >>> 4;
|
222 | n |= n >>> 8;
|
223 | n |= n >>> 16;
|
224 | n++;
|
225 | }
|
226 | return n;
|
227 | }
|
228 |
|
229 |
|
230 |
|
231 | function howMuchToRead(n, state) {
|
232 | if (n <= 0 || state.length === 0 && state.ended) return 0;
|
233 | if (state.objectMode) return 1;
|
234 | if (n !== n) {
|
235 |
|
236 | if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length;
|
237 | }
|
238 |
|
239 | if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n);
|
240 | if (n <= state.length) return n;
|
241 |
|
242 | if (!state.ended) {
|
243 | state.needReadable = true;
|
244 | return 0;
|
245 | }
|
246 | return state.length;
|
247 | }
|
248 |
|
249 |
|
250 | Readable.prototype.read = function (n) {
|
251 | debug('read', n);
|
252 | n = parseInt(n, 10);
|
253 | var state = this._readableState;
|
254 | var nOrig = n;
|
255 |
|
256 | if (n !== 0) state.emittedReadable = false;
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) {
|
262 | debug('read: emitReadable', state.length, state.ended);
|
263 | if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this);
|
264 | return null;
|
265 | }
|
266 |
|
267 | n = howMuchToRead(n, state);
|
268 |
|
269 |
|
270 | if (n === 0 && state.ended) {
|
271 | if (state.length === 0) endReadable(this);
|
272 | return null;
|
273 | }
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | var doRead = state.needReadable;
|
299 | debug('need readable', doRead);
|
300 |
|
301 |
|
302 | if (state.length === 0 || state.length - n < state.highWaterMark) {
|
303 | doRead = true;
|
304 | debug('length less than watermark', doRead);
|
305 | }
|
306 |
|
307 |
|
308 |
|
309 | if (state.ended || state.reading) {
|
310 | doRead = false;
|
311 | debug('reading or ended', doRead);
|
312 | } else if (doRead) {
|
313 | debug('do read');
|
314 | state.reading = true;
|
315 | state.sync = true;
|
316 |
|
317 | if (state.length === 0) state.needReadable = true;
|
318 |
|
319 | this._read(state.highWaterMark);
|
320 | state.sync = false;
|
321 |
|
322 |
|
323 | if (!state.reading) n = howMuchToRead(nOrig, state);
|
324 | }
|
325 |
|
326 | var ret;
|
327 | if (n > 0) ret = fromList(n, state);else ret = null;
|
328 |
|
329 | if (ret === null) {
|
330 | state.needReadable = true;
|
331 | n = 0;
|
332 | } else {
|
333 | state.length -= n;
|
334 | }
|
335 |
|
336 | if (state.length === 0) {
|
337 |
|
338 |
|
339 | if (!state.ended) state.needReadable = true;
|
340 |
|
341 |
|
342 | if (nOrig !== n && state.ended) endReadable(this);
|
343 | }
|
344 |
|
345 | if (ret !== null) this.emit('data', ret);
|
346 |
|
347 | return ret;
|
348 | };
|
349 |
|
350 | function chunkInvalid(state, chunk) {
|
351 | var er = null;
|
352 | if (!Buffer.isBuffer(chunk) && typeof chunk !== 'string' && chunk !== null && chunk !== undefined && !state.objectMode) {
|
353 | er = new TypeError('Invalid non-string/buffer chunk');
|
354 | }
|
355 | return er;
|
356 | }
|
357 |
|
358 | function onEofChunk(stream, state) {
|
359 | if (state.ended) return;
|
360 | if (state.decoder) {
|
361 | var chunk = state.decoder.end();
|
362 | if (chunk && chunk.length) {
|
363 | state.buffer.push(chunk);
|
364 | state.length += state.objectMode ? 1 : chunk.length;
|
365 | }
|
366 | }
|
367 | state.ended = true;
|
368 |
|
369 |
|
370 | emitReadable(stream);
|
371 | }
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | function emitReadable(stream) {
|
377 | var state = stream._readableState;
|
378 | state.needReadable = false;
|
379 | if (!state.emittedReadable) {
|
380 | debug('emitReadable', state.flowing);
|
381 | state.emittedReadable = true;
|
382 | if (state.sync) nextTick(emitReadable_, stream);else emitReadable_(stream);
|
383 | }
|
384 | }
|
385 |
|
386 | function emitReadable_(stream) {
|
387 | debug('emit readable');
|
388 | stream.emit('readable');
|
389 | flow(stream);
|
390 | }
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | function maybeReadMore(stream, state) {
|
399 | if (!state.readingMore) {
|
400 | state.readingMore = true;
|
401 | nextTick(maybeReadMore_, stream, state);
|
402 | }
|
403 | }
|
404 |
|
405 | function maybeReadMore_(stream, state) {
|
406 | var len = state.length;
|
407 | while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) {
|
408 | debug('maybeReadMore read 0');
|
409 | stream.read(0);
|
410 | if (len === state.length)
|
411 |
|
412 | break;else len = state.length;
|
413 | }
|
414 | state.readingMore = false;
|
415 | }
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | Readable.prototype._read = function (n) {
|
422 | this.emit('error', new Error('not implemented'));
|
423 | };
|
424 |
|
425 | Readable.prototype.pipe = function (dest, pipeOpts) {
|
426 | var src = this;
|
427 | var state = this._readableState;
|
428 |
|
429 | switch (state.pipesCount) {
|
430 | case 0:
|
431 | state.pipes = dest;
|
432 | break;
|
433 | case 1:
|
434 | state.pipes = [state.pipes, dest];
|
435 | break;
|
436 | default:
|
437 | state.pipes.push(dest);
|
438 | break;
|
439 | }
|
440 | state.pipesCount += 1;
|
441 | debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts);
|
442 |
|
443 | var doEnd = (!pipeOpts || pipeOpts.end !== false);
|
444 |
|
445 | var endFn = doEnd ? onend : cleanup;
|
446 | if (state.endEmitted) nextTick(endFn);else src.once('end', endFn);
|
447 |
|
448 | dest.on('unpipe', onunpipe);
|
449 | function onunpipe(readable) {
|
450 | debug('onunpipe');
|
451 | if (readable === src) {
|
452 | cleanup();
|
453 | }
|
454 | }
|
455 |
|
456 | function onend() {
|
457 | debug('onend');
|
458 | dest.end();
|
459 | }
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 | var ondrain = pipeOnDrain(src);
|
466 | dest.on('drain', ondrain);
|
467 |
|
468 | var cleanedUp = false;
|
469 | function cleanup() {
|
470 | debug('cleanup');
|
471 |
|
472 | dest.removeListener('close', onclose);
|
473 | dest.removeListener('finish', onfinish);
|
474 | dest.removeListener('drain', ondrain);
|
475 | dest.removeListener('error', onerror);
|
476 | dest.removeListener('unpipe', onunpipe);
|
477 | src.removeListener('end', onend);
|
478 | src.removeListener('end', cleanup);
|
479 | src.removeListener('data', ondata);
|
480 |
|
481 | cleanedUp = true;
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 | if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain();
|
489 | }
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 | var increasedAwaitDrain = false;
|
496 | src.on('data', ondata);
|
497 | function ondata(chunk) {
|
498 | debug('ondata');
|
499 | increasedAwaitDrain = false;
|
500 | var ret = dest.write(chunk);
|
501 | if (false === ret && !increasedAwaitDrain) {
|
502 |
|
503 |
|
504 |
|
505 |
|
506 | if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) {
|
507 | debug('false write response, pause', src._readableState.awaitDrain);
|
508 | src._readableState.awaitDrain++;
|
509 | increasedAwaitDrain = true;
|
510 | }
|
511 | src.pause();
|
512 | }
|
513 | }
|
514 |
|
515 |
|
516 |
|
517 | function onerror(er) {
|
518 | debug('onerror', er);
|
519 | unpipe();
|
520 | dest.removeListener('error', onerror);
|
521 | if (listenerCount(dest, 'error') === 0) dest.emit('error', er);
|
522 | }
|
523 |
|
524 |
|
525 | prependListener(dest, 'error', onerror);
|
526 |
|
527 |
|
528 | function onclose() {
|
529 | dest.removeListener('finish', onfinish);
|
530 | unpipe();
|
531 | }
|
532 | dest.once('close', onclose);
|
533 | function onfinish() {
|
534 | debug('onfinish');
|
535 | dest.removeListener('close', onclose);
|
536 | unpipe();
|
537 | }
|
538 | dest.once('finish', onfinish);
|
539 |
|
540 | function unpipe() {
|
541 | debug('unpipe');
|
542 | src.unpipe(dest);
|
543 | }
|
544 |
|
545 |
|
546 | dest.emit('pipe', src);
|
547 |
|
548 |
|
549 | if (!state.flowing) {
|
550 | debug('pipe resume');
|
551 | src.resume();
|
552 | }
|
553 |
|
554 | return dest;
|
555 | };
|
556 |
|
557 | function pipeOnDrain(src) {
|
558 | return function () {
|
559 | var state = src._readableState;
|
560 | debug('pipeOnDrain', state.awaitDrain);
|
561 | if (state.awaitDrain) state.awaitDrain--;
|
562 | if (state.awaitDrain === 0 && src.listeners('data').length) {
|
563 | state.flowing = true;
|
564 | flow(src);
|
565 | }
|
566 | };
|
567 | }
|
568 |
|
569 | Readable.prototype.unpipe = function (dest) {
|
570 | var state = this._readableState;
|
571 |
|
572 |
|
573 | if (state.pipesCount === 0) return this;
|
574 |
|
575 |
|
576 | if (state.pipesCount === 1) {
|
577 |
|
578 | if (dest && dest !== state.pipes) return this;
|
579 |
|
580 | if (!dest) dest = state.pipes;
|
581 |
|
582 |
|
583 | state.pipes = null;
|
584 | state.pipesCount = 0;
|
585 | state.flowing = false;
|
586 | if (dest) dest.emit('unpipe', this);
|
587 | return this;
|
588 | }
|
589 |
|
590 |
|
591 |
|
592 | if (!dest) {
|
593 |
|
594 | var dests = state.pipes;
|
595 | var len = state.pipesCount;
|
596 | state.pipes = null;
|
597 | state.pipesCount = 0;
|
598 | state.flowing = false;
|
599 |
|
600 | for (var _i = 0; _i < len; _i++) {
|
601 | dests[_i].emit('unpipe', this);
|
602 | }return this;
|
603 | }
|
604 |
|
605 |
|
606 | var i = indexOf(state.pipes, dest);
|
607 | if (i === -1) return this;
|
608 |
|
609 | state.pipes.splice(i, 1);
|
610 | state.pipesCount -= 1;
|
611 | if (state.pipesCount === 1) state.pipes = state.pipes[0];
|
612 |
|
613 | dest.emit('unpipe', this);
|
614 |
|
615 | return this;
|
616 | };
|
617 |
|
618 |
|
619 |
|
620 | Readable.prototype.on = function (ev, fn) {
|
621 | var res = EventEmitter.prototype.on.call(this, ev, fn);
|
622 |
|
623 | if (ev === 'data') {
|
624 |
|
625 | if (this._readableState.flowing !== false) this.resume();
|
626 | } else if (ev === 'readable') {
|
627 | var state = this._readableState;
|
628 | if (!state.endEmitted && !state.readableListening) {
|
629 | state.readableListening = state.needReadable = true;
|
630 | state.emittedReadable = false;
|
631 | if (!state.reading) {
|
632 | nextTick(nReadingNextTick, this);
|
633 | } else if (state.length) {
|
634 | emitReadable(this, state);
|
635 | }
|
636 | }
|
637 | }
|
638 |
|
639 | return res;
|
640 | };
|
641 | Readable.prototype.addListener = Readable.prototype.on;
|
642 |
|
643 | function nReadingNextTick(self) {
|
644 | debug('readable nexttick read 0');
|
645 | self.read(0);
|
646 | }
|
647 |
|
648 |
|
649 |
|
650 | Readable.prototype.resume = function () {
|
651 | var state = this._readableState;
|
652 | if (!state.flowing) {
|
653 | debug('resume');
|
654 | state.flowing = true;
|
655 | resume(this, state);
|
656 | }
|
657 | return this;
|
658 | };
|
659 |
|
660 | function resume(stream, state) {
|
661 | if (!state.resumeScheduled) {
|
662 | state.resumeScheduled = true;
|
663 | nextTick(resume_, stream, state);
|
664 | }
|
665 | }
|
666 |
|
667 | function resume_(stream, state) {
|
668 | if (!state.reading) {
|
669 | debug('resume read 0');
|
670 | stream.read(0);
|
671 | }
|
672 |
|
673 | state.resumeScheduled = false;
|
674 | state.awaitDrain = 0;
|
675 | stream.emit('resume');
|
676 | flow(stream);
|
677 | if (state.flowing && !state.reading) stream.read(0);
|
678 | }
|
679 |
|
680 | Readable.prototype.pause = function () {
|
681 | debug('call pause flowing=%j', this._readableState.flowing);
|
682 | if (false !== this._readableState.flowing) {
|
683 | debug('pause');
|
684 | this._readableState.flowing = false;
|
685 | this.emit('pause');
|
686 | }
|
687 | return this;
|
688 | };
|
689 |
|
690 | function flow(stream) {
|
691 | var state = stream._readableState;
|
692 | debug('flow', state.flowing);
|
693 | while (state.flowing && stream.read() !== null) {}
|
694 | }
|
695 |
|
696 |
|
697 |
|
698 |
|
699 | Readable.prototype.wrap = function (stream) {
|
700 | var state = this._readableState;
|
701 | var paused = false;
|
702 |
|
703 | var self = this;
|
704 | stream.on('end', function () {
|
705 | debug('wrapped end');
|
706 | if (state.decoder && !state.ended) {
|
707 | var chunk = state.decoder.end();
|
708 | if (chunk && chunk.length) self.push(chunk);
|
709 | }
|
710 |
|
711 | self.push(null);
|
712 | });
|
713 |
|
714 | stream.on('data', function (chunk) {
|
715 | debug('wrapped data');
|
716 | if (state.decoder) chunk = state.decoder.write(chunk);
|
717 |
|
718 |
|
719 | if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return;
|
720 |
|
721 | var ret = self.push(chunk);
|
722 | if (!ret) {
|
723 | paused = true;
|
724 | stream.pause();
|
725 | }
|
726 | });
|
727 |
|
728 |
|
729 |
|
730 | for (var i in stream) {
|
731 | if (this[i] === undefined && typeof stream[i] === 'function') {
|
732 | this[i] = function (method) {
|
733 | return function () {
|
734 | return stream[method].apply(stream, arguments);
|
735 | };
|
736 | }(i);
|
737 | }
|
738 | }
|
739 |
|
740 |
|
741 | var events = ['error', 'close', 'destroy', 'pause', 'resume'];
|
742 | forEach(events, function (ev) {
|
743 | stream.on(ev, self.emit.bind(self, ev));
|
744 | });
|
745 |
|
746 |
|
747 |
|
748 | self._read = function (n) {
|
749 | debug('wrapped _read', n);
|
750 | if (paused) {
|
751 | paused = false;
|
752 | stream.resume();
|
753 | }
|
754 | };
|
755 |
|
756 | return self;
|
757 | };
|
758 |
|
759 |
|
760 | Readable._fromList = fromList;
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 | function fromList(n, state) {
|
767 |
|
768 | if (state.length === 0) return null;
|
769 |
|
770 | var ret;
|
771 | if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) {
|
772 |
|
773 | 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);
|
774 | state.buffer.clear();
|
775 | } else {
|
776 |
|
777 | ret = fromListPartial(n, state.buffer, state.decoder);
|
778 | }
|
779 |
|
780 | return ret;
|
781 | }
|
782 |
|
783 |
|
784 |
|
785 |
|
786 | function fromListPartial(n, list, hasStrings) {
|
787 | var ret;
|
788 | if (n < list.head.data.length) {
|
789 |
|
790 | ret = list.head.data.slice(0, n);
|
791 | list.head.data = list.head.data.slice(n);
|
792 | } else if (n === list.head.data.length) {
|
793 |
|
794 | ret = list.shift();
|
795 | } else {
|
796 |
|
797 | ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list);
|
798 | }
|
799 | return ret;
|
800 | }
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 | function copyFromBufferString(n, list) {
|
807 | var p = list.head;
|
808 | var c = 1;
|
809 | var ret = p.data;
|
810 | n -= ret.length;
|
811 | while (p = p.next) {
|
812 | var str = p.data;
|
813 | var nb = n > str.length ? str.length : n;
|
814 | if (nb === str.length) ret += str;else ret += str.slice(0, n);
|
815 | n -= nb;
|
816 | if (n === 0) {
|
817 | if (nb === str.length) {
|
818 | ++c;
|
819 | if (p.next) list.head = p.next;else list.head = list.tail = null;
|
820 | } else {
|
821 | list.head = p;
|
822 | p.data = str.slice(nb);
|
823 | }
|
824 | break;
|
825 | }
|
826 | ++c;
|
827 | }
|
828 | list.length -= c;
|
829 | return ret;
|
830 | }
|
831 |
|
832 |
|
833 |
|
834 |
|
835 | function copyFromBuffer(n, list) {
|
836 | var ret = Buffer.allocUnsafe(n);
|
837 | var p = list.head;
|
838 | var c = 1;
|
839 | p.data.copy(ret);
|
840 | n -= p.data.length;
|
841 | while (p = p.next) {
|
842 | var buf = p.data;
|
843 | var nb = n > buf.length ? buf.length : n;
|
844 | buf.copy(ret, ret.length - n, 0, nb);
|
845 | n -= nb;
|
846 | if (n === 0) {
|
847 | if (nb === buf.length) {
|
848 | ++c;
|
849 | if (p.next) list.head = p.next;else list.head = list.tail = null;
|
850 | } else {
|
851 | list.head = p;
|
852 | p.data = buf.slice(nb);
|
853 | }
|
854 | break;
|
855 | }
|
856 | ++c;
|
857 | }
|
858 | list.length -= c;
|
859 | return ret;
|
860 | }
|
861 |
|
862 | function endReadable(stream) {
|
863 | var state = stream._readableState;
|
864 |
|
865 |
|
866 |
|
867 | if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream');
|
868 |
|
869 | if (!state.endEmitted) {
|
870 | state.ended = true;
|
871 | nextTick(endReadableNT, state, stream);
|
872 | }
|
873 | }
|
874 |
|
875 | function endReadableNT(state, stream) {
|
876 |
|
877 | if (!state.endEmitted && state.length === 0) {
|
878 | state.endEmitted = true;
|
879 | stream.readable = false;
|
880 | stream.emit('end');
|
881 | }
|
882 | }
|
883 |
|
884 | function forEach(xs, f) {
|
885 | for (var i = 0, l = xs.length; i < l; i++) {
|
886 | f(xs[i], i);
|
887 | }
|
888 | }
|
889 |
|
890 | function indexOf(xs, x) {
|
891 | for (var i = 0, l = xs.length; i < l; i++) {
|
892 | if (xs[i] === x) return i;
|
893 | }
|
894 | return -1;
|
895 | }
|