@ -83,10 +83,12 @@ function ReadableState(options, stream) {
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 ;
}
}
@ -106,19 +108,27 @@ function Readable(options) {
// This returns true if the highWaterMark has not been hit yet,
// similar to how Writable.write() returns true if you should
// write() some more.
Readable . prototype . push = function ( chunk ) {
Readable . prototype . push = function ( chunk , encoding ) {
var state = this . _ readableState ;
if ( typeof chunk === 'string' && ! state . objectMode )
chunk = new Buffer ( chunk , arguments [ 1 ] ) ;
return readableAddChunk ( this , state , chunk , false ) ;
if ( typeof chunk === 'string' && ! state . objectMode ) {
encoding = encoding || 'utf8' ;
if ( encoding !== state . encoding ) {
chunk = new Buffer ( chunk , encoding ) ;
encoding = '' ;
}
}
return readableAddChunk ( this , state , chunk , encoding , false ) ;
} ;
// Unshift should *always* be something directly out of read()
Readable . prototype . unshift = function ( chunk ) {
var state = this . _ readableState ;
return readableAddChunk ( this , state , chunk , true ) ;
return readableAddChunk ( this , state , chunk , '' , true ) ;
} ;
function readableAddChunk ( stream , state , chunk , addToFront ) {
function readableAddChunk ( stream , state , chunk , encoding , addToFront ) {
var er = chunkInvalid ( state , chunk ) ;
if ( er ) {
stream . emit ( 'error' , er ) ;
@ -134,7 +144,7 @@ function readableAddChunk(stream, state, chunk, addToFront) {
var e = new Error ( 'stream.unshift() after end event' ) ;
stream . emit ( 'error' , e ) ;
} else {
if ( state . decoder && ! addToFront )
if ( state . decoder && ! addToFront && ! encoding )
chunk = state . decoder . write ( chunk ) ;
// update the buffer info.
@ -179,6 +189,7 @@ Readable.prototype.setEncoding = function(enc) {
if ( ! StringDecoder )
StringDecoder = require ( 'string_decoder' ) . StringDecoder ;
this . _ readableState . decoder = new StringDecoder ( enc ) ;
this . _ readableState . encoding = enc ;
} ;
// Don't raise the hwm > 128MB