|
|
@ -69,7 +69,9 @@ function ReadableState(options, stream) { |
|
|
|
this.endEmitted = false; |
|
|
|
this.reading = false; |
|
|
|
this.sync = false; |
|
|
|
this.onread = onread.bind(stream); |
|
|
|
this.onread = function(er, data) { |
|
|
|
onread(stream, er, data); |
|
|
|
}; |
|
|
|
|
|
|
|
// whenever we return null, then we set a flag to say
|
|
|
|
// that we're awaiting a 'readable' event emission.
|
|
|
@ -208,13 +210,13 @@ Readable.prototype.read = function(n) { |
|
|
|
return ret; |
|
|
|
}; |
|
|
|
|
|
|
|
function onread(er, chunk) { |
|
|
|
var state = this._readableState; |
|
|
|
function onread(stream, er, chunk) { |
|
|
|
var state = stream._readableState; |
|
|
|
var sync = state.sync; |
|
|
|
|
|
|
|
state.reading = false; |
|
|
|
if (er) |
|
|
|
return this.emit('error', er); |
|
|
|
return stream.emit('error', er); |
|
|
|
|
|
|
|
if (!chunk || !chunk.length) { |
|
|
|
// eof
|
|
|
@ -233,10 +235,10 @@ function onread(er, chunk) { |
|
|
|
state.needReadable = false; |
|
|
|
if (!state.emittedReadable) { |
|
|
|
state.emittedReadable = true; |
|
|
|
this.emit('readable'); |
|
|
|
stream.emit('readable'); |
|
|
|
} |
|
|
|
} else |
|
|
|
endReadable(this); |
|
|
|
endReadable(stream); |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
@ -257,7 +259,7 @@ function onread(er, chunk) { |
|
|
|
// another _read(n,cb) before this one returns!
|
|
|
|
if (state.length <= state.lowWaterMark) { |
|
|
|
state.reading = true; |
|
|
|
this._read(state.bufferSize, state.onread); |
|
|
|
stream._read(state.bufferSize, state.onread); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
@ -265,7 +267,7 @@ function onread(er, chunk) { |
|
|
|
state.needReadable = false; |
|
|
|
if (!state.emittedReadable) { |
|
|
|
state.emittedReadable = true; |
|
|
|
this.emit('readable'); |
|
|
|
stream.emit('readable'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -275,7 +277,9 @@ function onread(er, chunk) { |
|
|
|
// for virtual (non-string, non-buffer) streams, "length" is somewhat
|
|
|
|
// arbitrary, and perhaps not very meaningful.
|
|
|
|
Readable.prototype._read = function(n, cb) { |
|
|
|
process.nextTick(cb.bind(this, new Error('not implemented'))); |
|
|
|
process.nextTick(function() { |
|
|
|
cb(new Error('not implemented')); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
Readable.prototype.pipe = function(dest, pipeOpts) { |
|
|
@ -316,7 +320,9 @@ Readable.prototype.pipe = function(dest, pipeOpts) { |
|
|
|
// start the flow.
|
|
|
|
if (!state.flowing) { |
|
|
|
state.flowing = true; |
|
|
|
process.nextTick(flow.bind(null, src, pipeOpts)); |
|
|
|
process.nextTick(function() { |
|
|
|
flow(src, pipeOpts); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
return dest; |
|
|
@ -371,7 +377,9 @@ function flow(src, pipeOpts) { |
|
|
|
|
|
|
|
// at this point, no one needed a drain, so we just ran out of data
|
|
|
|
// on the next readable event, start it over again.
|
|
|
|
src.once('readable', flow.bind(null, src, pipeOpts)); |
|
|
|
src.once('readable', function() { |
|
|
|
flow(src, pipeOpts); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
Readable.prototype.unpipe = function(dest) { |
|
|
@ -504,6 +512,7 @@ Readable.prototype.wrap = function(stream) { |
|
|
|
var state = this._readableState; |
|
|
|
var paused = false; |
|
|
|
|
|
|
|
var self = this; |
|
|
|
stream.on('end', function() { |
|
|
|
state.ended = true; |
|
|
|
if (state.decoder) { |
|
|
@ -515,10 +524,10 @@ Readable.prototype.wrap = function(stream) { |
|
|
|
} |
|
|
|
|
|
|
|
if (state.length > 0) |
|
|
|
this.emit('readable'); |
|
|
|
self.emit('readable'); |
|
|
|
else |
|
|
|
endReadable(this); |
|
|
|
}.bind(this)); |
|
|
|
endReadable(self); |
|
|
|
}); |
|
|
|
|
|
|
|
stream.on('data', function(chunk) { |
|
|
|
if (state.decoder) |
|
|
@ -528,14 +537,14 @@ Readable.prototype.wrap = function(stream) { |
|
|
|
|
|
|
|
state.buffer.push(chunk); |
|
|
|
state.length += chunk.length; |
|
|
|
this.emit('readable'); |
|
|
|
self.emit('readable'); |
|
|
|
|
|
|
|
// if not consumed, then pause the stream.
|
|
|
|
if (state.length > state.lowWaterMark && !paused) { |
|
|
|
paused = true; |
|
|
|
stream.pause(); |
|
|
|
} |
|
|
|
}.bind(this)); |
|
|
|
}); |
|
|
|
|
|
|
|
// proxy all the other methods.
|
|
|
|
// important when wrapping filters and duplexes.
|
|
|
@ -551,8 +560,8 @@ Readable.prototype.wrap = function(stream) { |
|
|
|
// proxy certain important events.
|
|
|
|
var events = ['error', 'close', 'destroy', 'pause', 'resume']; |
|
|
|
events.forEach(function(ev) { |
|
|
|
stream.on(ev, this.emit.bind(this, ev)); |
|
|
|
}.bind(this)); |
|
|
|
stream.on(ev, self.emit.bind(self, ev)); |
|
|
|
}); |
|
|
|
|
|
|
|
// consume some bytes. if not all is consumed, then
|
|
|
|
// pause the underlying stream.
|
|
|
@ -660,5 +669,7 @@ function endReadable(stream) { |
|
|
|
return; |
|
|
|
state.ended = true; |
|
|
|
state.endEmitted = true; |
|
|
|
process.nextTick(stream.emit.bind(stream, 'end')); |
|
|
|
process.nextTick(function() { |
|
|
|
stream.emit('end'); |
|
|
|
}); |
|
|
|
} |
|
|
|