|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict'; |
|
|
|
|
|
|
|
var pna = require('process-nextick-args'); |
|
|
|
|
|
module.exports = Readable; |
|
|
|
|
|
var isArray = require('isarray'); |
|
|
|
|
|
|
|
var Duplex; |
|
|
|
|
|
Readable.ReadableState = ReadableState; |
|
|
|
|
|
var EE = require('events').EventEmitter; |
|
|
|
var EElistenerCount = function (emitter, type) { |
|
return emitter.listeners(type).length; |
|
}; |
|
|
|
|
|
|
|
var Stream = require('./internal/streams/stream'); |
|
|
|
|
|
|
|
|
|
var Buffer = require('safe-buffer').Buffer; |
|
var OurUint8Array = (typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : {}).Uint8Array || function () {}; |
|
function _uint8ArrayToBuffer(chunk) { |
|
return Buffer.from(chunk); |
|
} |
|
function _isUint8Array(obj) { |
|
return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; |
|
} |
|
|
|
|
|
|
|
|
|
var util = Object.create(require('core-util-is')); |
|
util.inherits = require('inherits'); |
|
|
|
|
|
|
|
var debugUtil = require('util'); |
|
var debug = void 0; |
|
if (debugUtil && debugUtil.debuglog) { |
|
debug = debugUtil.debuglog('stream'); |
|
} else { |
|
debug = function () {}; |
|
} |
|
|
|
|
|
var BufferList = require('./internal/streams/BufferList'); |
|
var destroyImpl = require('./internal/streams/destroy'); |
|
var StringDecoder; |
|
|
|
util.inherits(Readable, Stream); |
|
|
|
var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume']; |
|
|
|
function prependListener(emitter, event, fn) { |
|
|
|
|
|
if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn); |
|
|
|
|
|
|
|
|
|
|
|
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]]; |
|
} |
|
|
|
function ReadableState(options, stream) { |
|
Duplex = Duplex || require('./_stream_duplex'); |
|
|
|
options = options || {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
var isDuplex = stream instanceof Duplex; |
|
|
|
|
|
|
|
this.objectMode = !!options.objectMode; |
|
|
|
if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode; |
|
|
|
|
|
|
|
var hwm = options.highWaterMark; |
|
var readableHwm = options.readableHighWaterMark; |
|
var defaultHwm = this.objectMode ? 16 : 16 * 1024; |
|
|
|
if (hwm || hwm === 0) this.highWaterMark = hwm;else if (isDuplex && (readableHwm || readableHwm === 0)) this.highWaterMark = readableHwm;else this.highWaterMark = defaultHwm; |
|
|
|
|
|
this.highWaterMark = Math.floor(this.highWaterMark); |
|
|
|
|
|
|
|
|
|
this.buffer = new BufferList(); |
|
this.length = 0; |
|
this.pipes = null; |
|
this.pipesCount = 0; |
|
this.flowing = null; |
|
this.ended = false; |
|
this.endEmitted = false; |
|
this.reading = false; |
|
|
|
|
|
|
|
|
|
|
|
this.sync = true; |
|
|
|
|
|
|
|
this.needReadable = false; |
|
this.emittedReadable = false; |
|
this.readableListening = false; |
|
this.resumeScheduled = false; |
|
|
|
|
|
this.destroyed = false; |
|
|
|
|
|
|
|
|
|
this.defaultEncoding = options.defaultEncoding || 'utf8'; |
|
|
|
|
|
this.awaitDrain = 0; |
|
|
|
|
|
this.readingMore = false; |
|
|
|
this.decoder = null; |
|
this.encoding = null; |
|
if (options.encoding) { |
|
if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; |
|
this.decoder = new StringDecoder(options.encoding); |
|
this.encoding = options.encoding; |
|
} |
|
} |
|
|
|
function Readable(options) { |
|
Duplex = Duplex || require('./_stream_duplex'); |
|
|
|
if (!(this instanceof Readable)) return new Readable(options); |
|
|
|
this._readableState = new ReadableState(options, this); |
|
|
|
|
|
this.readable = true; |
|
|
|
if (options) { |
|
if (typeof options.read === 'function') this._read = options.read; |
|
|
|
if (typeof options.destroy === 'function') this._destroy = options.destroy; |
|
} |
|
|
|
Stream.call(this); |
|
} |
|
|
|
Object.defineProperty(Readable.prototype, 'destroyed', { |
|
get: function () { |
|
if (this._readableState === undefined) { |
|
return false; |
|
} |
|
return this._readableState.destroyed; |
|
}, |
|
set: function (value) { |
|
|
|
|
|
if (!this._readableState) { |
|
return; |
|
} |
|
|
|
|
|
|
|
this._readableState.destroyed = value; |
|
} |
|
}); |
|
|
|
Readable.prototype.destroy = destroyImpl.destroy; |
|
Readable.prototype._undestroy = destroyImpl.undestroy; |
|
Readable.prototype._destroy = function (err, cb) { |
|
this.push(null); |
|
cb(err); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
Readable.prototype.push = function (chunk, encoding) { |
|
var state = this._readableState; |
|
var skipChunkCheck; |
|
|
|
if (!state.objectMode) { |
|
if (typeof chunk === 'string') { |
|
encoding = encoding || state.defaultEncoding; |
|
if (encoding !== state.encoding) { |
|
chunk = Buffer.from(chunk, encoding); |
|
encoding = ''; |
|
} |
|
skipChunkCheck = true; |
|
} |
|
} else { |
|
skipChunkCheck = true; |
|
} |
|
|
|
return readableAddChunk(this, chunk, encoding, false, skipChunkCheck); |
|
}; |
|
|
|
|
|
Readable.prototype.unshift = function (chunk) { |
|
return readableAddChunk(this, chunk, null, true, false); |
|
}; |
|
|
|
function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) { |
|
var state = stream._readableState; |
|
if (chunk === null) { |
|
state.reading = false; |
|
onEofChunk(stream, state); |
|
} else { |
|
var er; |
|
if (!skipChunkCheck) er = chunkInvalid(state, chunk); |
|
if (er) { |
|
stream.emit('error', er); |
|
} else if (state.objectMode || chunk && chunk.length > 0) { |
|
if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) { |
|
chunk = _uint8ArrayToBuffer(chunk); |
|
} |
|
|
|
if (addToFront) { |
|
if (state.endEmitted) stream.emit('error', new Error('stream.unshift() after end event'));else addChunk(stream, state, chunk, true); |
|
} else if (state.ended) { |
|
stream.emit('error', new Error('stream.push() after EOF')); |
|
} else { |
|
state.reading = false; |
|
if (state.decoder && !encoding) { |
|
chunk = state.decoder.write(chunk); |
|
if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state); |
|
} else { |
|
addChunk(stream, state, chunk, false); |
|
} |
|
} |
|
} else if (!addToFront) { |
|
state.reading = false; |
|
} |
|
} |
|
|
|
return needMoreData(state); |
|
} |
|
|
|
function addChunk(stream, state, chunk, addToFront) { |
|
if (state.flowing && state.length === 0 && !state.sync) { |
|
stream.emit('data', chunk); |
|
stream.read(0); |
|
} else { |
|
|
|
state.length += state.objectMode ? 1 : chunk.length; |
|
if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk); |
|
|
|
if (state.needReadable) emitReadable(stream); |
|
} |
|
maybeReadMore(stream, state); |
|
} |
|
|
|
function chunkInvalid(state, chunk) { |
|
var er; |
|
if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { |
|
er = new TypeError('Invalid non-string/buffer chunk'); |
|
} |
|
return er; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function needMoreData(state) { |
|
return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0); |
|
} |
|
|
|
Readable.prototype.isPaused = function () { |
|
return this._readableState.flowing === false; |
|
}; |
|
|
|
|
|
Readable.prototype.setEncoding = function (enc) { |
|
if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; |
|
this._readableState.decoder = new StringDecoder(enc); |
|
this._readableState.encoding = enc; |
|
return this; |
|
}; |
|
|
|
|
|
var MAX_HWM = 0x800000; |
|
function computeNewHighWaterMark(n) { |
|
if (n >= MAX_HWM) { |
|
n = MAX_HWM; |
|
} else { |
|
|
|
|
|
n--; |
|
n |= n >>> 1; |
|
n |= n >>> 2; |
|
n |= n >>> 4; |
|
n |= n >>> 8; |
|
n |= n >>> 16; |
|
n++; |
|
} |
|
return n; |
|
} |
|
|
|
|
|
|
|
function howMuchToRead(n, state) { |
|
if (n <= 0 || state.length === 0 && state.ended) return 0; |
|
if (state.objectMode) return 1; |
|
if (n !== n) { |
|
|
|
if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length; |
|
} |
|
|
|
if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); |
|
if (n <= state.length) return n; |
|
|
|
if (!state.ended) { |
|
state.needReadable = true; |
|
return 0; |
|
} |
|
return state.length; |
|
} |
|
|
|
|
|
Readable.prototype.read = function (n) { |
|
debug('read', n); |
|
n = parseInt(n, 10); |
|
var state = this._readableState; |
|
var nOrig = n; |
|
|
|
if (n !== 0) state.emittedReadable = false; |
|
|
|
|
|
|
|
|
|
if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { |
|
debug('read: emitReadable', state.length, state.ended); |
|
if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this); |
|
return null; |
|
} |
|
|
|
n = howMuchToRead(n, state); |
|
|
|
|
|
if (n === 0 && state.ended) { |
|
if (state.length === 0) endReadable(this); |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var doRead = state.needReadable; |
|
debug('need readable', doRead); |
|
|
|
|
|
if (state.length === 0 || state.length - n < state.highWaterMark) { |
|
doRead = true; |
|
debug('length less than watermark', doRead); |
|
} |
|
|
|
|
|
|
|
if (state.ended || state.reading) { |
|
doRead = false; |
|
debug('reading or ended', doRead); |
|
} else if (doRead) { |
|
debug('do read'); |
|
state.reading = true; |
|
state.sync = true; |
|
|
|
if (state.length === 0) state.needReadable = true; |
|
|
|
this._read(state.highWaterMark); |
|
state.sync = false; |
|
|
|
|
|
if (!state.reading) n = howMuchToRead(nOrig, state); |
|
} |
|
|
|
var ret; |
|
if (n > 0) ret = fromList(n, state);else ret = null; |
|
|
|
if (ret === null) { |
|
state.needReadable = true; |
|
n = 0; |
|
} else { |
|
state.length -= n; |
|
} |
|
|
|
if (state.length === 0) { |
|
|
|
|
|
if (!state.ended) state.needReadable = true; |
|
|
|
|
|
if (nOrig !== n && state.ended) endReadable(this); |
|
} |
|
|
|
if (ret !== null) this.emit('data', ret); |
|
|
|
return ret; |
|
}; |
|
|
|
function onEofChunk(stream, state) { |
|
if (state.ended) return; |
|
if (state.decoder) { |
|
var chunk = state.decoder.end(); |
|
if (chunk && chunk.length) { |
|
state.buffer.push(chunk); |
|
state.length += state.objectMode ? 1 : chunk.length; |
|
} |
|
} |
|
state.ended = true; |
|
|
|
|
|
emitReadable(stream); |
|
} |
|
|
|
|
|
|
|
|
|
function emitReadable(stream) { |
|
var state = stream._readableState; |
|
state.needReadable = false; |
|
if (!state.emittedReadable) { |
|
debug('emitReadable', state.flowing); |
|
state.emittedReadable = true; |
|
if (state.sync) pna.nextTick(emitReadable_, stream);else emitReadable_(stream); |
|
} |
|
} |
|
|
|
function emitReadable_(stream) { |
|
debug('emit readable'); |
|
stream.emit('readable'); |
|
flow(stream); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function maybeReadMore(stream, state) { |
|
if (!state.readingMore) { |
|
state.readingMore = true; |
|
pna.nextTick(maybeReadMore_, stream, state); |
|
} |
|
} |
|
|
|
function maybeReadMore_(stream, state) { |
|
var len = state.length; |
|
while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) { |
|
debug('maybeReadMore read 0'); |
|
stream.read(0); |
|
if (len === state.length) |
|
|
|
break;else len = state.length; |
|
} |
|
state.readingMore = false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
Readable.prototype._read = function (n) { |
|
this.emit('error', new Error('_read() is not implemented')); |
|
}; |
|
|
|
Readable.prototype.pipe = function (dest, pipeOpts) { |
|
var src = this; |
|
var state = this._readableState; |
|
|
|
switch (state.pipesCount) { |
|
case 0: |
|
state.pipes = dest; |
|
break; |
|
case 1: |
|
state.pipes = [state.pipes, dest]; |
|
break; |
|
default: |
|
state.pipes.push(dest); |
|
break; |
|
} |
|
state.pipesCount += 1; |
|
debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); |
|
|
|
var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; |
|
|
|
var endFn = doEnd ? onend : unpipe; |
|
if (state.endEmitted) pna.nextTick(endFn);else src.once('end', endFn); |
|
|
|
dest.on('unpipe', onunpipe); |
|
function onunpipe(readable, unpipeInfo) { |
|
debug('onunpipe'); |
|
if (readable === src) { |
|
if (unpipeInfo && unpipeInfo.hasUnpiped === false) { |
|
unpipeInfo.hasUnpiped = true; |
|
cleanup(); |
|
} |
|
} |
|
} |
|
|
|
function onend() { |
|
debug('onend'); |
|
dest.end(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
var ondrain = pipeOnDrain(src); |
|
dest.on('drain', ondrain); |
|
|
|
var cleanedUp = false; |
|
function cleanup() { |
|
debug('cleanup'); |
|
|
|
dest.removeListener('close', onclose); |
|
dest.removeListener('finish', onfinish); |
|
dest.removeListener('drain', ondrain); |
|
dest.removeListener('error', onerror); |
|
dest.removeListener('unpipe', onunpipe); |
|
src.removeListener('end', onend); |
|
src.removeListener('end', unpipe); |
|
src.removeListener('data', ondata); |
|
|
|
cleanedUp = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
var increasedAwaitDrain = false; |
|
src.on('data', ondata); |
|
function ondata(chunk) { |
|
debug('ondata'); |
|
increasedAwaitDrain = false; |
|
var ret = dest.write(chunk); |
|
if (false === ret && !increasedAwaitDrain) { |
|
|
|
|
|
|
|
|
|
if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) { |
|
debug('false write response, pause', state.awaitDrain); |
|
state.awaitDrain++; |
|
increasedAwaitDrain = true; |
|
} |
|
src.pause(); |
|
} |
|
} |
|
|
|
|
|
|
|
function onerror(er) { |
|
debug('onerror', er); |
|
unpipe(); |
|
dest.removeListener('error', onerror); |
|
if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er); |
|
} |
|
|
|
|
|
prependListener(dest, 'error', onerror); |
|
|
|
|
|
function onclose() { |
|
dest.removeListener('finish', onfinish); |
|
unpipe(); |
|
} |
|
dest.once('close', onclose); |
|
function onfinish() { |
|
debug('onfinish'); |
|
dest.removeListener('close', onclose); |
|
unpipe(); |
|
} |
|
dest.once('finish', onfinish); |
|
|
|
function unpipe() { |
|
debug('unpipe'); |
|
src.unpipe(dest); |
|
} |
|
|
|
|
|
dest.emit('pipe', src); |
|
|
|
|
|
if (!state.flowing) { |
|
debug('pipe resume'); |
|
src.resume(); |
|
} |
|
|
|
return dest; |
|
}; |
|
|
|
function pipeOnDrain(src) { |
|
return function () { |
|
var state = src._readableState; |
|
debug('pipeOnDrain', state.awaitDrain); |
|
if (state.awaitDrain) state.awaitDrain--; |
|
if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { |
|
state.flowing = true; |
|
flow(src); |
|
} |
|
}; |
|
} |
|
|
|
Readable.prototype.unpipe = function (dest) { |
|
var state = this._readableState; |
|
var unpipeInfo = { hasUnpiped: false }; |
|
|
|
|
|
if (state.pipesCount === 0) return this; |
|
|
|
|
|
if (state.pipesCount === 1) { |
|
|
|
if (dest && dest !== state.pipes) return this; |
|
|
|
if (!dest) dest = state.pipes; |
|
|
|
|
|
state.pipes = null; |
|
state.pipesCount = 0; |
|
state.flowing = false; |
|
if (dest) dest.emit('unpipe', this, unpipeInfo); |
|
return this; |
|
} |
|
|
|
|
|
|
|
if (!dest) { |
|
|
|
var dests = state.pipes; |
|
var len = state.pipesCount; |
|
state.pipes = null; |
|
state.pipesCount = 0; |
|
state.flowing = false; |
|
|
|
for (var i = 0; i < len; i++) { |
|
dests[i].emit('unpipe', this, { hasUnpiped: false }); |
|
}return this; |
|
} |
|
|
|
|
|
var index = indexOf(state.pipes, dest); |
|
if (index === -1) return this; |
|
|
|
state.pipes.splice(index, 1); |
|
state.pipesCount -= 1; |
|
if (state.pipesCount === 1) state.pipes = state.pipes[0]; |
|
|
|
dest.emit('unpipe', this, unpipeInfo); |
|
|
|
return this; |
|
}; |
|
|
|
|
|
|
|
Readable.prototype.on = function (ev, fn) { |
|
var res = Stream.prototype.on.call(this, ev, fn); |
|
|
|
if (ev === 'data') { |
|
|
|
if (this._readableState.flowing !== false) this.resume(); |
|
} else if (ev === 'readable') { |
|
var state = this._readableState; |
|
if (!state.endEmitted && !state.readableListening) { |
|
state.readableListening = state.needReadable = true; |
|
state.emittedReadable = false; |
|
if (!state.reading) { |
|
pna.nextTick(nReadingNextTick, this); |
|
} else if (state.length) { |
|
emitReadable(this); |
|
} |
|
} |
|
} |
|
|
|
return res; |
|
}; |
|
Readable.prototype.addListener = Readable.prototype.on; |
|
|
|
function nReadingNextTick(self) { |
|
debug('readable nexttick read 0'); |
|
self.read(0); |
|
} |
|
|
|
|
|
|
|
Readable.prototype.resume = function () { |
|
var state = this._readableState; |
|
if (!state.flowing) { |
|
debug('resume'); |
|
state.flowing = true; |
|
resume(this, state); |
|
} |
|
return this; |
|
}; |
|
|
|
function resume(stream, state) { |
|
if (!state.resumeScheduled) { |
|
state.resumeScheduled = true; |
|
pna.nextTick(resume_, stream, state); |
|
} |
|
} |
|
|
|
function resume_(stream, state) { |
|
if (!state.reading) { |
|
debug('resume read 0'); |
|
stream.read(0); |
|
} |
|
|
|
state.resumeScheduled = false; |
|
state.awaitDrain = 0; |
|
stream.emit('resume'); |
|
flow(stream); |
|
if (state.flowing && !state.reading) stream.read(0); |
|
} |
|
|
|
Readable.prototype.pause = function () { |
|
debug('call pause flowing=%j', this._readableState.flowing); |
|
if (false !== this._readableState.flowing) { |
|
debug('pause'); |
|
this._readableState.flowing = false; |
|
this.emit('pause'); |
|
} |
|
return this; |
|
}; |
|
|
|
function flow(stream) { |
|
var state = stream._readableState; |
|
debug('flow', state.flowing); |
|
while (state.flowing && stream.read() !== null) {} |
|
} |
|
|
|
|
|
|
|
|
|
Readable.prototype.wrap = function (stream) { |
|
var _this = this; |
|
|
|
var state = this._readableState; |
|
var paused = false; |
|
|
|
stream.on('end', function () { |
|
debug('wrapped end'); |
|
if (state.decoder && !state.ended) { |
|
var chunk = state.decoder.end(); |
|
if (chunk && chunk.length) _this.push(chunk); |
|
} |
|
|
|
_this.push(null); |
|
}); |
|
|
|
stream.on('data', function (chunk) { |
|
debug('wrapped data'); |
|
if (state.decoder) chunk = state.decoder.write(chunk); |
|
|
|
|
|
if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return; |
|
|
|
var ret = _this.push(chunk); |
|
if (!ret) { |
|
paused = true; |
|
stream.pause(); |
|
} |
|
}); |
|
|
|
|
|
|
|
for (var i in stream) { |
|
if (this[i] === undefined && typeof stream[i] === 'function') { |
|
this[i] = function (method) { |
|
return function () { |
|
return stream[method].apply(stream, arguments); |
|
}; |
|
}(i); |
|
} |
|
} |
|
|
|
|
|
for (var n = 0; n < kProxyEvents.length; n++) { |
|
stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n])); |
|
} |
|
|
|
|
|
|
|
this._read = function (n) { |
|
debug('wrapped _read', n); |
|
if (paused) { |
|
paused = false; |
|
stream.resume(); |
|
} |
|
}; |
|
|
|
return this; |
|
}; |
|
|
|
Object.defineProperty(Readable.prototype, 'readableHighWaterMark', { |
|
|
|
|
|
|
|
enumerable: false, |
|
get: function () { |
|
return this._readableState.highWaterMark; |
|
} |
|
}); |
|
|
|
|
|
Readable._fromList = fromList; |
|
|
|
|
|
|
|
|
|
|
|
function fromList(n, state) { |
|
|
|
if (state.length === 0) return null; |
|
|
|
var ret; |
|
if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) { |
|
|
|
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); |
|
state.buffer.clear(); |
|
} else { |
|
|
|
ret = fromListPartial(n, state.buffer, state.decoder); |
|
} |
|
|
|
return ret; |
|
} |
|
|
|
|
|
|
|
|
|
function fromListPartial(n, list, hasStrings) { |
|
var ret; |
|
if (n < list.head.data.length) { |
|
|
|
ret = list.head.data.slice(0, n); |
|
list.head.data = list.head.data.slice(n); |
|
} else if (n === list.head.data.length) { |
|
|
|
ret = list.shift(); |
|
} else { |
|
|
|
ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list); |
|
} |
|
return ret; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function copyFromBufferString(n, list) { |
|
var p = list.head; |
|
var c = 1; |
|
var ret = p.data; |
|
n -= ret.length; |
|
while (p = p.next) { |
|
var str = p.data; |
|
var nb = n > str.length ? str.length : n; |
|
if (nb === str.length) ret += str;else ret += str.slice(0, n); |
|
n -= nb; |
|
if (n === 0) { |
|
if (nb === str.length) { |
|
++c; |
|
if (p.next) list.head = p.next;else list.head = list.tail = null; |
|
} else { |
|
list.head = p; |
|
p.data = str.slice(nb); |
|
} |
|
break; |
|
} |
|
++c; |
|
} |
|
list.length -= c; |
|
return ret; |
|
} |
|
|
|
|
|
|
|
|
|
function copyFromBuffer(n, list) { |
|
var ret = Buffer.allocUnsafe(n); |
|
var p = list.head; |
|
var c = 1; |
|
p.data.copy(ret); |
|
n -= p.data.length; |
|
while (p = p.next) { |
|
var buf = p.data; |
|
var nb = n > buf.length ? buf.length : n; |
|
buf.copy(ret, ret.length - n, 0, nb); |
|
n -= nb; |
|
if (n === 0) { |
|
if (nb === buf.length) { |
|
++c; |
|
if (p.next) list.head = p.next;else list.head = list.tail = null; |
|
} else { |
|
list.head = p; |
|
p.data = buf.slice(nb); |
|
} |
|
break; |
|
} |
|
++c; |
|
} |
|
list.length -= c; |
|
return ret; |
|
} |
|
|
|
function endReadable(stream) { |
|
var state = stream._readableState; |
|
|
|
|
|
|
|
if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream'); |
|
|
|
if (!state.endEmitted) { |
|
state.ended = true; |
|
pna.nextTick(endReadableNT, state, stream); |
|
} |
|
} |
|
|
|
function endReadableNT(state, stream) { |
|
|
|
if (!state.endEmitted && state.length === 0) { |
|
state.endEmitted = true; |
|
stream.readable = false; |
|
stream.emit('end'); |
|
} |
|
} |
|
|
|
function indexOf(xs, x) { |
|
for (var i = 0, l = xs.length; i < l; i++) { |
|
if (xs[i] === x) return i; |
|
} |
|
return -1; |
|
} |