stream: Add stream.unshift(chunk)

pull/24507/merge
isaacs 2013-02-27 19:32:19 -08:00
parent 7764b84297
commit 4b67f0be6d
1 changed files with 11 additions and 3 deletions

View File

@ -106,10 +106,15 @@ function Readable(options) {
// write() some more.
Readable.prototype.push = function(chunk) {
var state = this._readableState;
return readableAddChunk(this, state, chunk);
return readableAddChunk(this, state, chunk, false);
};
function readableAddChunk(stream, state, chunk) {
Readable.prototype.unshift = function(chunk) {
var state = this._readableState;
return readableAddChunk(this, state, chunk, true);
};
function readableAddChunk(stream, state, chunk, addToFront) {
state.reading = false;
var er = chunkInvalid(state, chunk);
@ -123,7 +128,10 @@ function readableAddChunk(stream, state, chunk) {
// update the buffer info.
state.length += state.objectMode ? 1 : chunk.length;
state.buffer.push(chunk);
if (addToFront)
state.buffer.unshift(chunk);
else
state.buffer.push(chunk);
if (state.needReadable)
emitReadable(stream);