2012-10-03 00:44:50 +02:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
module.exports = Readable;
|
2012-11-13 08:30:10 +01:00
|
|
|
Readable.ReadableState = ReadableState;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
var Stream = require('stream');
|
|
|
|
var util = require('util');
|
2012-10-04 01:52:14 +02:00
|
|
|
var StringDecoder;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
util.inherits(Readable, Stream);
|
|
|
|
|
2012-10-31 22:30:30 +01:00
|
|
|
function ReadableState(options, stream) {
|
2012-10-03 00:44:50 +02:00
|
|
|
options = options || {};
|
|
|
|
|
2012-11-13 08:31:25 +01:00
|
|
|
// the argument passed to this._read(n,cb)
|
2012-12-19 03:49:42 +01:00
|
|
|
this.bufferSize = options.bufferSize || 16 * 1024;
|
2012-11-13 08:31:25 +01:00
|
|
|
|
|
|
|
// the point at which it stops calling _read() to fill the buffer
|
2012-12-19 03:49:42 +01:00
|
|
|
// Note: 0 is a valid value, means "don't call _read preemptively ever"
|
|
|
|
var hwm = options.highWaterMark;
|
|
|
|
this.highWaterMark = (hwm || hwm === 0) ? hwm : 16 * 1024;
|
2012-11-13 08:31:25 +01:00
|
|
|
|
|
|
|
// the minimum number of bytes to buffer before emitting 'readable'
|
|
|
|
// default to pushing everything out as fast as possible.
|
2012-12-19 03:49:42 +01:00
|
|
|
this.lowWaterMark = options.lowWaterMark || 0;
|
2012-11-13 08:31:25 +01:00
|
|
|
|
|
|
|
// cast to ints.
|
|
|
|
this.bufferSize = ~~this.bufferSize;
|
|
|
|
this.lowWaterMark = ~~this.lowWaterMark;
|
|
|
|
this.highWaterMark = ~~this.highWaterMark;
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
this.buffer = [];
|
|
|
|
this.length = 0;
|
2012-11-17 04:27:41 +01:00
|
|
|
this.pipes = null;
|
|
|
|
this.pipesCount = 0;
|
2012-10-03 00:44:50 +02:00
|
|
|
this.flowing = false;
|
|
|
|
this.ended = false;
|
2012-10-04 01:52:14 +02:00
|
|
|
this.endEmitted = false;
|
2012-10-03 00:44:50 +02:00
|
|
|
this.reading = false;
|
2012-10-31 22:30:30 +01:00
|
|
|
this.sync = false;
|
2012-11-17 05:24:14 +01:00
|
|
|
this.onread = function(er, data) {
|
|
|
|
onread(stream, er, data);
|
|
|
|
};
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
// whenever we return null, then we set a flag to say
|
|
|
|
// that we're awaiting a 'readable' event emission.
|
|
|
|
this.needReadable = false;
|
2012-11-13 08:32:05 +01:00
|
|
|
this.emittedReadable = false;
|
2012-10-04 01:52:14 +02:00
|
|
|
|
2012-11-28 03:20:16 +01:00
|
|
|
// when piping, we only care about 'readable' events that happen
|
|
|
|
// after read()ing all the bytes and not getting any pushback.
|
|
|
|
this.ranOut = false;
|
2012-11-28 10:25:39 +01:00
|
|
|
|
|
|
|
// the number of writers that are awaiting a drain event in .pipe()s
|
|
|
|
this.awaitDrain = 0;
|
2012-11-28 19:46:24 +01:00
|
|
|
this.pipeChunkSize = null;
|
2012-11-28 03:20:16 +01:00
|
|
|
|
2012-10-04 01:52:14 +02:00
|
|
|
this.decoder = null;
|
|
|
|
if (options.encoding) {
|
|
|
|
if (!StringDecoder)
|
|
|
|
StringDecoder = require('string_decoder').StringDecoder;
|
|
|
|
this.decoder = new StringDecoder(options.encoding);
|
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function Readable(options) {
|
2012-10-08 23:43:17 +02:00
|
|
|
if (!(this instanceof Readable))
|
|
|
|
return new Readable(options);
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
this._readableState = new ReadableState(options, this);
|
2012-11-28 03:21:05 +01:00
|
|
|
|
|
|
|
// legacy
|
|
|
|
this.readable = true;
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
Stream.apply(this);
|
|
|
|
}
|
|
|
|
|
2012-10-04 01:52:14 +02:00
|
|
|
// backwards compatibility.
|
|
|
|
Readable.prototype.setEncoding = function(enc) {
|
|
|
|
if (!StringDecoder)
|
|
|
|
StringDecoder = require('string_decoder').StringDecoder;
|
|
|
|
this._readableState.decoder = new StringDecoder(enc);
|
|
|
|
};
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-10-07 22:12:21 +02:00
|
|
|
function howMuchToRead(n, state) {
|
|
|
|
if (state.length === 0 && state.ended)
|
|
|
|
return 0;
|
|
|
|
|
2012-11-28 19:46:24 +01:00
|
|
|
if (isNaN(n) || n === null)
|
2012-10-07 22:12:21 +02:00
|
|
|
return state.length;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-10-07 22:12:21 +02:00
|
|
|
if (n <= 0)
|
|
|
|
return 0;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
// don't have that much. return null, unless we've ended.
|
|
|
|
if (n > state.length) {
|
2012-10-07 22:12:21 +02:00
|
|
|
if (!state.ended) {
|
2012-10-03 00:44:50 +02:00
|
|
|
state.needReadable = true;
|
2012-10-07 22:12:21 +02:00
|
|
|
return 0;
|
2012-10-03 00:44:50 +02:00
|
|
|
} else
|
2012-10-07 22:12:21 +02:00
|
|
|
return state.length;
|
2012-10-03 00:44:50 +02:00
|
|
|
}
|
|
|
|
|
2012-10-07 22:12:21 +02:00
|
|
|
return n;
|
|
|
|
}
|
2012-10-04 01:52:14 +02:00
|
|
|
|
2012-10-07 22:12:21 +02:00
|
|
|
// you can override either this method, or _read(n, cb) below.
|
|
|
|
Readable.prototype.read = function(n) {
|
|
|
|
var state = this._readableState;
|
|
|
|
var nOrig = n;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-11-13 08:32:05 +01:00
|
|
|
if (typeof n !== 'number' || n > 0)
|
|
|
|
state.emittedReadable = false;
|
|
|
|
|
2012-10-07 22:12:21 +02:00
|
|
|
n = howMuchToRead(n, state);
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-10-07 22:12:21 +02:00
|
|
|
// if we've ended, and we're now clear, then finish it up.
|
|
|
|
if (n === 0 && state.ended) {
|
|
|
|
endReadable(this);
|
|
|
|
return null;
|
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-10-07 22:12:21 +02:00
|
|
|
// All the actual chunk generation logic needs to be
|
|
|
|
// *below* the call to _read. The reason is that in certain
|
|
|
|
// synthetic stream cases, such as passthrough streams, _read
|
|
|
|
// may be a completely synchronous operation which may change
|
|
|
|
// the state of the read buffer, providing enough data when
|
|
|
|
// before there was *not* enough.
|
|
|
|
//
|
|
|
|
// So, the steps are:
|
|
|
|
// 1. Figure out what the state of things will be after we do
|
|
|
|
// a read from the buffer.
|
|
|
|
//
|
|
|
|
// 2. If that resulting state will trigger a _read, then call _read.
|
|
|
|
// Note that this may be asynchronous, or synchronous. Yes, it is
|
|
|
|
// deeply ugly to write APIs this way, but that still doesn't mean
|
|
|
|
// that the Readable class should behave improperly, as streams are
|
|
|
|
// designed to be sync/async agnostic.
|
|
|
|
// Take note if the _read call is sync or async (ie, if the read call
|
|
|
|
// has returned yet), so that we know whether or not it's safe to emit
|
|
|
|
// 'readable' etc.
|
|
|
|
//
|
|
|
|
// 3. Actually pull the requested chunks out of the buffer and return.
|
|
|
|
|
|
|
|
// if we need a readable event, then we need to do some reading.
|
|
|
|
var doRead = state.needReadable;
|
2012-11-13 08:31:25 +01:00
|
|
|
|
|
|
|
// if we currently have less than the highWaterMark, then also read some
|
|
|
|
if (state.length - n <= state.highWaterMark)
|
2012-10-07 22:12:21 +02:00
|
|
|
doRead = true;
|
2012-11-13 08:31:25 +01:00
|
|
|
|
2012-10-07 22:12:21 +02:00
|
|
|
// however, if we've ended, then there's no point, and if we're already
|
|
|
|
// reading, then it's unnecessary.
|
|
|
|
if (state.ended || state.reading)
|
|
|
|
doRead = false;
|
|
|
|
|
|
|
|
if (doRead) {
|
2012-10-03 00:44:50 +02:00
|
|
|
state.reading = true;
|
2012-10-31 22:30:30 +01:00
|
|
|
state.sync = true;
|
2012-12-13 07:03:19 +01:00
|
|
|
// if the length is currently zero, then we *need* a readable event.
|
|
|
|
if (state.length === 0)
|
|
|
|
state.needReadable = true;
|
2012-10-03 00:44:50 +02:00
|
|
|
// call internal read method
|
2012-10-31 22:30:30 +01:00
|
|
|
this._read(state.bufferSize, state.onread);
|
|
|
|
state.sync = false;
|
2012-10-03 00:44:50 +02:00
|
|
|
}
|
|
|
|
|
2012-10-07 22:12:21 +02:00
|
|
|
// If _read called its callback synchronously, then `reading`
|
|
|
|
// will be false, and we need to re-evaluate how much data we
|
|
|
|
// can return to the user.
|
|
|
|
if (doRead && !state.reading)
|
|
|
|
n = howMuchToRead(nOrig, state);
|
|
|
|
|
|
|
|
var ret;
|
|
|
|
if (n > 0)
|
|
|
|
ret = fromList(n, state.buffer, state.length, !!state.decoder);
|
|
|
|
else
|
|
|
|
ret = null;
|
|
|
|
|
|
|
|
if (ret === null || ret.length === 0) {
|
|
|
|
state.needReadable = true;
|
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
state.length -= n;
|
|
|
|
|
2012-12-13 07:03:19 +01:00
|
|
|
// If we have nothing in the buffer, then we want to know
|
|
|
|
// as soon as we *do* get something into the buffer.
|
|
|
|
if (state.length === 0 && !state.ended)
|
|
|
|
state.needReadable = true;
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2012-11-17 05:24:14 +01:00
|
|
|
function onread(stream, er, chunk) {
|
|
|
|
var state = stream._readableState;
|
2012-10-31 22:30:30 +01:00
|
|
|
var sync = state.sync;
|
|
|
|
|
|
|
|
state.reading = false;
|
|
|
|
if (er)
|
2012-11-17 05:24:14 +01:00
|
|
|
return stream.emit('error', er);
|
2012-10-31 22:30:30 +01:00
|
|
|
|
|
|
|
if (!chunk || !chunk.length) {
|
|
|
|
// eof
|
|
|
|
state.ended = true;
|
|
|
|
if (state.decoder) {
|
|
|
|
chunk = state.decoder.end();
|
|
|
|
if (chunk && chunk.length) {
|
|
|
|
state.buffer.push(chunk);
|
|
|
|
state.length += chunk.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we've ended and we have some data left, then emit
|
|
|
|
// 'readable' now to make sure it gets picked up.
|
|
|
|
if (!sync) {
|
2012-11-13 08:32:05 +01:00
|
|
|
if (state.length > 0) {
|
|
|
|
state.needReadable = false;
|
|
|
|
if (!state.emittedReadable) {
|
|
|
|
state.emittedReadable = true;
|
2012-11-17 05:24:14 +01:00
|
|
|
stream.emit('readable');
|
2012-11-13 08:32:05 +01:00
|
|
|
}
|
|
|
|
} else
|
2012-11-17 05:24:14 +01:00
|
|
|
endReadable(stream);
|
2012-10-31 22:30:30 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.decoder)
|
|
|
|
chunk = state.decoder.write(chunk);
|
|
|
|
|
|
|
|
// update the buffer info.
|
|
|
|
if (chunk) {
|
|
|
|
state.length += chunk.length;
|
|
|
|
state.buffer.push(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we haven't gotten enough to pass the lowWaterMark,
|
|
|
|
// and we haven't ended, then don't bother telling the user
|
2012-11-28 19:46:24 +01:00
|
|
|
// that it's time to read more data. Otherwise, emitting 'readable'
|
|
|
|
// probably will trigger another stream.read(), which can trigger
|
2012-10-31 22:30:30 +01:00
|
|
|
// another _read(n,cb) before this one returns!
|
|
|
|
if (state.length <= state.lowWaterMark) {
|
|
|
|
state.reading = true;
|
2012-11-17 05:24:14 +01:00
|
|
|
stream._read(state.bufferSize, state.onread);
|
2012-10-31 22:30:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.needReadable && !sync) {
|
|
|
|
state.needReadable = false;
|
2012-11-13 08:32:05 +01:00
|
|
|
if (!state.emittedReadable) {
|
|
|
|
state.emittedReadable = true;
|
2012-11-17 05:24:14 +01:00
|
|
|
stream.emit('readable');
|
2012-11-13 08:32:05 +01:00
|
|
|
}
|
2012-10-31 22:30:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
// abstract method. to be overridden in specific implementation classes.
|
|
|
|
// call cb(er, data) where data is <= n in length.
|
|
|
|
// for virtual (non-string, non-buffer) streams, "length" is somewhat
|
|
|
|
// arbitrary, and perhaps not very meaningful.
|
|
|
|
Readable.prototype._read = function(n, cb) {
|
2012-11-17 05:24:14 +01:00
|
|
|
process.nextTick(function() {
|
|
|
|
cb(new Error('not implemented'));
|
|
|
|
});
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Readable.prototype.pipe = function(dest, pipeOpts) {
|
|
|
|
var src = this;
|
|
|
|
var state = this._readableState;
|
2012-11-17 04:27:41 +01:00
|
|
|
|
|
|
|
switch (state.pipesCount) {
|
|
|
|
case 0:
|
|
|
|
state.pipes = dest;
|
|
|
|
break;
|
|
|
|
case 1:
|
2012-12-06 19:21:22 +01:00
|
|
|
state.pipes = [state.pipes, dest];
|
2012-11-17 04:27:41 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state.pipes.push(dest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state.pipesCount += 1;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
if ((!pipeOpts || pipeOpts.end !== false) &&
|
|
|
|
dest !== process.stdout &&
|
|
|
|
dest !== process.stderr) {
|
|
|
|
src.once('end', onend);
|
2012-12-22 16:14:42 +01:00
|
|
|
dest.on('unpipe', function unpipe(readable) {
|
|
|
|
if (readable === src) {
|
2012-10-03 00:44:50 +02:00
|
|
|
src.removeListener('end', onend);
|
2012-12-22 16:14:42 +01:00
|
|
|
dest.removeListener('unpipe', unpipe);
|
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-11-28 03:20:16 +01:00
|
|
|
if (pipeOpts && pipeOpts.chunkSize)
|
2012-11-28 19:46:24 +01:00
|
|
|
state.pipeChunkSize = pipeOpts.chunkSize;
|
2012-11-28 03:20:16 +01:00
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
function onend() {
|
|
|
|
dest.end();
|
|
|
|
}
|
|
|
|
|
2012-11-28 19:46:24 +01:00
|
|
|
// when the dest drains, it reduces the awaitDrain counter
|
|
|
|
// on the source. This would be more elegant with a .once()
|
|
|
|
// handler in flow(), but adding and removing repeatedly is
|
|
|
|
// too slow.
|
|
|
|
var ondrain = pipeOnDrain(src);
|
|
|
|
dest.on('drain', ondrain);
|
2012-12-22 16:14:42 +01:00
|
|
|
dest.on('unpipe', function cleanup(readable) {
|
2012-12-22 18:07:08 +01:00
|
|
|
if (readable === src) {
|
2012-12-22 16:14:42 +01:00
|
|
|
dest.removeListener('unpipe', cleanup);
|
2012-11-28 19:46:24 +01:00
|
|
|
|
2012-12-22 18:07:08 +01:00
|
|
|
// if the reader is waiting for a drain event from this
|
|
|
|
// specific writer, then it would cause it to never start
|
|
|
|
// flowing again.
|
|
|
|
// So, if this is awaiting a drain, then we just call it now.
|
|
|
|
// If we don't know, then assume that we are waiting for one.
|
|
|
|
if (!dest._writableState || dest._writableState.needDrain)
|
|
|
|
ondrain();
|
|
|
|
}
|
2012-11-28 19:46:24 +01:00
|
|
|
});
|
|
|
|
|
2012-11-29 05:45:16 +01:00
|
|
|
// if the dest has an error, then stop piping into it.
|
|
|
|
// however, don't suppress the throwing behavior for this.
|
2012-12-22 16:14:42 +01:00
|
|
|
function onerror(er) {
|
2012-11-29 07:09:28 +01:00
|
|
|
unpipe();
|
2012-11-29 05:45:16 +01:00
|
|
|
if (dest.listeners('error').length === 0)
|
|
|
|
dest.emit('error', er);
|
2012-12-22 16:14:42 +01:00
|
|
|
}
|
|
|
|
dest.once('error', onerror);
|
2012-11-29 05:45:16 +01:00
|
|
|
|
2012-11-29 07:09:28 +01:00
|
|
|
// if the dest emits close, then presumably there's no point writing
|
|
|
|
// to it any more.
|
2012-12-22 16:14:42 +01:00
|
|
|
dest.once('close', unpipe);
|
|
|
|
function onfinish() {
|
2012-11-29 07:09:28 +01:00
|
|
|
dest.removeListener('close', unpipe);
|
2012-12-22 16:14:42 +01:00
|
|
|
}
|
|
|
|
dest.once('finish', onfinish);
|
2012-11-29 07:09:28 +01:00
|
|
|
|
|
|
|
function unpipe() {
|
|
|
|
src.unpipe(dest);
|
|
|
|
}
|
|
|
|
|
2012-12-22 16:14:42 +01:00
|
|
|
// cleanup event handlers once the pipe is broken
|
|
|
|
dest.once('unpipe', function cleanup(readable) {
|
|
|
|
if (readable !== src) return;
|
|
|
|
|
|
|
|
dest.removeListener('close', unpipe);
|
|
|
|
dest.removeListener('finish', onfinish);
|
|
|
|
dest.removeListener('drain', ondrain);
|
|
|
|
dest.removeListener('error', onerror);
|
|
|
|
dest.removeListener('unpipe', cleanup);
|
|
|
|
});
|
|
|
|
|
2012-11-28 19:46:24 +01:00
|
|
|
// tell the dest that it's being piped to
|
2012-10-03 00:44:50 +02:00
|
|
|
dest.emit('pipe', src);
|
|
|
|
|
2012-11-28 19:46:24 +01:00
|
|
|
// start the flow if it hasn't been started already.
|
2012-10-04 02:43:27 +02:00
|
|
|
if (!state.flowing) {
|
2012-11-28 03:20:16 +01:00
|
|
|
// the handler that waits for readable events after all
|
|
|
|
// the data gets sucked out in flow.
|
|
|
|
// This would be easier to follow with a .once() handler
|
|
|
|
// in flow(), but that is too slow.
|
|
|
|
this.on('readable', pipeOnReadable);
|
|
|
|
|
2012-10-04 02:43:27 +02:00
|
|
|
state.flowing = true;
|
2012-11-17 05:24:14 +01:00
|
|
|
process.nextTick(function() {
|
2012-11-28 03:20:16 +01:00
|
|
|
flow(src);
|
2012-11-17 05:24:14 +01:00
|
|
|
});
|
2012-10-04 02:43:27 +02:00
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
return dest;
|
|
|
|
};
|
|
|
|
|
2012-11-28 10:25:39 +01:00
|
|
|
function pipeOnDrain(src) {
|
|
|
|
return function() {
|
|
|
|
var dest = this;
|
|
|
|
var state = src._readableState;
|
2012-12-06 19:21:22 +01:00
|
|
|
state.awaitDrain--;
|
2012-11-28 10:25:39 +01:00
|
|
|
if (state.awaitDrain === 0)
|
|
|
|
flow(src);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-11-28 03:20:16 +01:00
|
|
|
function flow(src) {
|
2012-10-03 00:44:50 +02:00
|
|
|
var state = src._readableState;
|
|
|
|
var chunk;
|
2012-11-28 10:25:39 +01:00
|
|
|
state.awaitDrain = 0;
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2012-11-17 04:27:41 +01:00
|
|
|
function write(dest, i, list) {
|
|
|
|
var written = dest.write(chunk);
|
|
|
|
if (false === written) {
|
2012-11-28 10:25:39 +01:00
|
|
|
state.awaitDrain++;
|
2012-11-17 04:27:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (state.pipesCount &&
|
2012-11-28 03:20:16 +01:00
|
|
|
null !== (chunk = src.read(state.pipeChunkSize))) {
|
2012-11-17 04:27:41 +01:00
|
|
|
|
|
|
|
if (state.pipesCount === 1)
|
|
|
|
write(state.pipes, 0, null);
|
|
|
|
else
|
|
|
|
state.pipes.forEach(write);
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
src.emit('data', chunk);
|
|
|
|
|
|
|
|
// if anyone needs a drain, then we have to wait for that.
|
2012-11-28 10:25:39 +01:00
|
|
|
if (state.awaitDrain > 0)
|
2012-10-03 00:44:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if every destination was unpiped, either before entering this
|
|
|
|
// function, or in the while loop, then stop flowing.
|
|
|
|
//
|
|
|
|
// NB: This is a pretty rare edge case.
|
2012-11-17 04:27:41 +01:00
|
|
|
if (state.pipesCount === 0) {
|
2012-10-03 00:44:50 +02:00
|
|
|
state.flowing = false;
|
|
|
|
|
|
|
|
// if there were data event listeners added, then switch to old mode.
|
2012-10-12 19:03:03 +02:00
|
|
|
if (src.listeners('data').length)
|
|
|
|
emitDataEvents(src);
|
2012-10-03 00:44:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// at this point, no one needed a drain, so we just ran out of data
|
|
|
|
// on the next readable event, start it over again.
|
2012-11-28 03:20:16 +01:00
|
|
|
state.ranOut = true;
|
2012-10-03 00:44:50 +02:00
|
|
|
}
|
|
|
|
|
2012-11-28 03:20:16 +01:00
|
|
|
function pipeOnReadable() {
|
|
|
|
if (this._readableState.ranOut) {
|
|
|
|
this._readableState.ranOut = false;
|
|
|
|
flow(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
Readable.prototype.unpipe = function(dest) {
|
|
|
|
var state = this._readableState;
|
2012-11-17 04:27:41 +01:00
|
|
|
|
|
|
|
// if we're not piping anywhere, then do nothing.
|
|
|
|
if (state.pipesCount === 0)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
// just one destination. most common case.
|
|
|
|
if (state.pipesCount === 1) {
|
|
|
|
// passed in one, but it's not the right one.
|
|
|
|
if (dest && dest !== state.pipes)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
if (!dest)
|
|
|
|
dest = state.pipes;
|
|
|
|
|
|
|
|
// got a match.
|
|
|
|
state.pipes = null;
|
|
|
|
state.pipesCount = 0;
|
2012-11-28 03:20:16 +01:00
|
|
|
this.removeListener('readable', pipeOnReadable);
|
2012-11-17 04:27:41 +01:00
|
|
|
if (dest)
|
2012-10-03 00:44:50 +02:00
|
|
|
dest.emit('unpipe', this);
|
2012-11-17 04:27:41 +01:00
|
|
|
return this;
|
2012-10-03 00:44:50 +02:00
|
|
|
}
|
2012-11-17 04:27:41 +01:00
|
|
|
|
|
|
|
// slow case. multiple pipe destinations.
|
|
|
|
|
|
|
|
if (!dest) {
|
|
|
|
// remove all.
|
|
|
|
var dests = state.pipes;
|
|
|
|
var len = state.pipesCount;
|
|
|
|
state.pipes = null;
|
|
|
|
state.pipesCount = 0;
|
2012-11-28 03:20:16 +01:00
|
|
|
this.removeListener('readable', pipeOnReadable);
|
2012-11-17 04:27:41 +01:00
|
|
|
|
|
|
|
for (var i = 0; i < len; i++)
|
|
|
|
dests[i].emit('unpipe', this);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to find the right one.
|
|
|
|
var i = state.pipes.indexOf(dest);
|
|
|
|
if (i === -1)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
state.pipes.splice(i, 1);
|
|
|
|
state.pipesCount -= 1;
|
|
|
|
if (state.pipesCount === 1)
|
|
|
|
state.pipes = state.pipes[0];
|
|
|
|
|
|
|
|
dest.emit('unpipe', this);
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
// kludge for on('data', fn) consumers. Sad.
|
|
|
|
// This is *not* part of the new readable stream interface.
|
|
|
|
// It is an ugly unfortunate mess of history.
|
|
|
|
Readable.prototype.on = function(ev, fn) {
|
|
|
|
// https://github.com/isaacs/readable-stream/issues/16
|
|
|
|
// if we're already flowing, then no need to set up data events.
|
|
|
|
if (ev === 'data' && !this._readableState.flowing)
|
|
|
|
emitDataEvents(this);
|
|
|
|
|
|
|
|
return Stream.prototype.on.call(this, ev, fn);
|
|
|
|
};
|
|
|
|
Readable.prototype.addListener = Readable.prototype.on;
|
|
|
|
|
|
|
|
// pause() and resume() are remnants of the legacy readable stream API
|
|
|
|
// If the user uses them, then switch into old mode.
|
|
|
|
Readable.prototype.resume = function() {
|
|
|
|
emitDataEvents(this);
|
2012-12-11 00:58:23 +01:00
|
|
|
this.read(0);
|
2012-12-13 07:17:57 +01:00
|
|
|
this.emit('resume');
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Readable.prototype.pause = function() {
|
2012-12-05 02:34:17 +01:00
|
|
|
emitDataEvents(this, true);
|
2012-12-13 07:17:57 +01:00
|
|
|
this.emit('pause');
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|
|
|
|
|
2012-12-05 02:34:17 +01:00
|
|
|
function emitDataEvents(stream, startPaused) {
|
2012-10-03 00:44:50 +02:00
|
|
|
var state = stream._readableState;
|
|
|
|
|
|
|
|
if (state.flowing) {
|
|
|
|
// https://github.com/isaacs/readable-stream/issues/16
|
|
|
|
throw new Error('Cannot switch to old mode now.');
|
|
|
|
}
|
|
|
|
|
2012-12-05 02:34:17 +01:00
|
|
|
var paused = startPaused || false;
|
2012-10-03 00:44:50 +02:00
|
|
|
var readable = false;
|
|
|
|
|
|
|
|
// convert to an old-style stream.
|
|
|
|
stream.readable = true;
|
|
|
|
stream.pipe = Stream.prototype.pipe;
|
|
|
|
stream.on = stream.addEventListener = Stream.prototype.on;
|
|
|
|
|
|
|
|
stream.on('readable', function() {
|
|
|
|
readable = true;
|
2012-12-05 03:19:07 +01:00
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
var c;
|
|
|
|
while (!paused && (null !== (c = stream.read())))
|
|
|
|
stream.emit('data', c);
|
|
|
|
|
|
|
|
if (c === null) {
|
|
|
|
readable = false;
|
|
|
|
stream._readableState.needReadable = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.pause = function() {
|
|
|
|
paused = true;
|
2012-12-13 07:17:57 +01:00
|
|
|
this.emit('pause');
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
stream.resume = function() {
|
|
|
|
paused = false;
|
|
|
|
if (readable)
|
2012-12-05 03:19:07 +01:00
|
|
|
process.nextTick(function() {
|
|
|
|
stream.emit('readable');
|
|
|
|
});
|
2012-12-11 00:58:23 +01:00
|
|
|
else
|
|
|
|
this.read(0);
|
2012-12-13 07:17:57 +01:00
|
|
|
this.emit('resume');
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// now make it start, just in case it hadn't already.
|
2012-12-05 02:20:12 +01:00
|
|
|
stream.emit('readable');
|
2012-10-03 00:44:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrap an old-style stream as the async data source.
|
|
|
|
// This is *not* part of the readable stream interface.
|
|
|
|
// It is an ugly unfortunate mess of history.
|
|
|
|
Readable.prototype.wrap = function(stream) {
|
|
|
|
var state = this._readableState;
|
|
|
|
var paused = false;
|
|
|
|
|
2012-11-17 05:24:14 +01:00
|
|
|
var self = this;
|
2012-10-03 00:44:50 +02:00
|
|
|
stream.on('end', function() {
|
|
|
|
state.ended = true;
|
2012-10-12 20:45:17 +02:00
|
|
|
if (state.decoder) {
|
|
|
|
var chunk = state.decoder.end();
|
|
|
|
if (chunk && chunk.length) {
|
|
|
|
state.buffer.push(chunk);
|
|
|
|
state.length += chunk.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.length > 0)
|
2012-11-17 05:24:14 +01:00
|
|
|
self.emit('readable');
|
2012-10-12 20:45:17 +02:00
|
|
|
else
|
2012-11-17 05:24:14 +01:00
|
|
|
endReadable(self);
|
|
|
|
});
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
stream.on('data', function(chunk) {
|
2012-10-12 20:45:17 +02:00
|
|
|
if (state.decoder)
|
|
|
|
chunk = state.decoder.write(chunk);
|
|
|
|
if (!chunk || !chunk.length)
|
|
|
|
return;
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
state.buffer.push(chunk);
|
|
|
|
state.length += chunk.length;
|
2012-11-17 05:24:14 +01:00
|
|
|
self.emit('readable');
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
// if not consumed, then pause the stream.
|
|
|
|
if (state.length > state.lowWaterMark && !paused) {
|
|
|
|
paused = true;
|
|
|
|
stream.pause();
|
|
|
|
}
|
2012-11-17 05:24:14 +01:00
|
|
|
});
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
// proxy all the other methods.
|
|
|
|
// important when wrapping filters and duplexes.
|
|
|
|
for (var i in stream) {
|
|
|
|
if (typeof stream[i] === 'function' &&
|
|
|
|
typeof this[i] === 'undefined') {
|
|
|
|
this[i] = function(method) { return function() {
|
|
|
|
return stream[method].apply(stream, arguments);
|
|
|
|
}}(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// proxy certain important events.
|
|
|
|
var events = ['error', 'close', 'destroy', 'pause', 'resume'];
|
|
|
|
events.forEach(function(ev) {
|
2012-11-17 05:24:14 +01:00
|
|
|
stream.on(ev, self.emit.bind(self, ev));
|
|
|
|
});
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
// consume some bytes. if not all is consumed, then
|
|
|
|
// pause the underlying stream.
|
|
|
|
this.read = function(n) {
|
|
|
|
if (state.length === 0) {
|
|
|
|
state.needReadable = true;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNaN(n) || n <= 0)
|
|
|
|
n = state.length;
|
|
|
|
|
|
|
|
if (n > state.length) {
|
|
|
|
if (!state.ended) {
|
|
|
|
state.needReadable = true;
|
|
|
|
return null;
|
|
|
|
} else
|
|
|
|
n = state.length;
|
|
|
|
}
|
|
|
|
|
2012-10-04 01:52:14 +02:00
|
|
|
var ret = fromList(n, state.buffer, state.length, !!state.decoder);
|
2012-10-03 00:44:50 +02:00
|
|
|
state.length -= n;
|
|
|
|
|
2012-12-13 07:03:19 +01:00
|
|
|
if (state.length === 0 && !state.ended)
|
|
|
|
state.needReadable = true;
|
|
|
|
|
2012-10-05 16:43:34 +02:00
|
|
|
if (state.length <= state.lowWaterMark && paused) {
|
2012-10-03 00:44:50 +02:00
|
|
|
stream.resume();
|
|
|
|
paused = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.length === 0 && state.ended)
|
2012-10-04 01:52:14 +02:00
|
|
|
endReadable(this);
|
2012-10-03 00:44:50 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exposed for testing purposes only.
|
|
|
|
Readable._fromList = fromList;
|
|
|
|
|
|
|
|
// Pluck off n bytes from an array of buffers.
|
|
|
|
// Length is the combined lengths of all the buffers in the list.
|
2012-10-04 01:52:14 +02:00
|
|
|
function fromList(n, list, length, stringMode) {
|
2012-10-03 00:44:50 +02:00
|
|
|
var ret;
|
|
|
|
|
|
|
|
// nothing in the list, definitely empty.
|
|
|
|
if (list.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-10-04 01:52:14 +02:00
|
|
|
if (length === 0)
|
2012-10-03 00:44:50 +02:00
|
|
|
ret = null;
|
2012-10-04 01:52:14 +02:00
|
|
|
else if (!n || n >= length) {
|
2012-10-03 00:44:50 +02:00
|
|
|
// read it all, truncate the array.
|
2012-10-04 01:52:14 +02:00
|
|
|
if (stringMode)
|
|
|
|
ret = list.join('');
|
|
|
|
else
|
|
|
|
ret = Buffer.concat(list, length);
|
2012-10-03 00:44:50 +02:00
|
|
|
list.length = 0;
|
|
|
|
} else {
|
|
|
|
// read just some of it.
|
|
|
|
if (n < list[0].length) {
|
|
|
|
// just take a part of the first list item.
|
2012-10-04 01:52:14 +02:00
|
|
|
// slice is the same for buffers and strings.
|
2012-10-03 00:44:50 +02:00
|
|
|
var buf = list[0];
|
|
|
|
ret = buf.slice(0, n);
|
|
|
|
list[0] = buf.slice(n);
|
|
|
|
} else if (n === list[0].length) {
|
|
|
|
// first list is a perfect match
|
|
|
|
ret = list.shift();
|
|
|
|
} else {
|
|
|
|
// complex case.
|
|
|
|
// we have enough to cover it, but it spans past the first buffer.
|
2012-10-04 01:52:14 +02:00
|
|
|
if (stringMode)
|
|
|
|
ret = '';
|
|
|
|
else
|
|
|
|
ret = new Buffer(n);
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
var c = 0;
|
|
|
|
for (var i = 0, l = list.length; i < l && c < n; i++) {
|
|
|
|
var buf = list[0];
|
|
|
|
var cpy = Math.min(n - c, buf.length);
|
2012-10-04 01:52:14 +02:00
|
|
|
|
|
|
|
if (stringMode)
|
|
|
|
ret += buf.slice(0, cpy);
|
|
|
|
else
|
|
|
|
buf.copy(ret, c, 0, cpy);
|
|
|
|
|
|
|
|
if (cpy < buf.length)
|
2012-10-03 00:44:50 +02:00
|
|
|
list[0] = buf.slice(cpy);
|
2012-10-04 01:52:14 +02:00
|
|
|
else
|
2012-10-03 00:44:50 +02:00
|
|
|
list.shift();
|
2012-10-04 01:52:14 +02:00
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
c += cpy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2012-10-04 01:52:14 +02:00
|
|
|
|
|
|
|
function endReadable(stream) {
|
|
|
|
var state = stream._readableState;
|
|
|
|
if (state.endEmitted)
|
|
|
|
return;
|
|
|
|
state.ended = true;
|
|
|
|
state.endEmitted = true;
|
2012-11-17 05:24:14 +01:00
|
|
|
process.nextTick(function() {
|
2012-12-05 20:27:46 +01:00
|
|
|
stream.readable = false;
|
2012-11-17 05:24:14 +01:00
|
|
|
stream.emit('end');
|
|
|
|
});
|
2012-10-04 01:52:14 +02:00
|
|
|
}
|