From 4b67f0be6d682845661662f43602b4c0245b02a0 Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 27 Feb 2013 19:32:19 -0800 Subject: [PATCH] stream: Add stream.unshift(chunk) --- lib/_stream_readable.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 9193f4f27b9..9f384a409e9 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -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);