diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 8cad1f1a37d37f..e1bebc38bed724 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -52,11 +52,6 @@ function ReadableState(options, stream) { this.emittedReadable = false; this.readableListening = false; - // Crypto is kind of old and crusty. Historically, its default string - // encoding is 'binary' so we have to make this configurable. - // Everything else in the universe uses 'utf8', though. - this.defaultEncoding = options.defaultEncoding || 'utf8'; - // when piping, we only care about 'readable' events that happen // after read()ing all the bytes and not getting any pushback. this.ranOut = false; @@ -69,6 +64,7 @@ function ReadableState(options, stream) { this.decoder = null; this.encoding = null; + if (options.encoding) { if (!StringDecoder) StringDecoder = require('string_decoder').StringDecoder; @@ -102,11 +98,8 @@ Readable.prototype.push = function(chunk, encoding) { var state = this._readableState; if (!state.objectMode && typeof chunk === 'string') { - encoding = encoding || state.defaultEncoding; - if (encoding !== state.encoding) { - chunk = new Buffer(chunk, encoding); - encoding = ''; - } + chunk = new Buffer(chunk, encoding); + encoding = ''; } return readableAddChunk(this, state, chunk, encoding, false); diff --git a/test/parallel/test-stream2-readable-bytesread.js b/test/parallel/test-stream2-readable-bytesread.js index 6f56af2f4e2c3e..c34894f5a06e6b 100644 --- a/test/parallel/test-stream2-readable-bytesread.js +++ b/test/parallel/test-stream2-readable-bytesread.js @@ -30,6 +30,63 @@ const Transform = require('stream').Transform; }); })(); +(function() { + const readable = new Readable({ + read: function(n) { + var i = this._index++; + if (i > this._max) + this.push(null); + else + this.push('中文', 'utf8'); + } + }); + + readable._max = 5; + readable._index = 1; + + var total = 0; + readable.setEncoding('utf8'); + readable.on('data', function(chunk) { + total += Buffer.byteLength(chunk); + }); + + readable.on('end', function() { + assert.equal(total, readable.bytesRead); + assert.equal(30, readable.bytesRead); + }); +})(); + +(function() { + // complex case + const readable = new Readable({ + read: function(n) { + var i = this._index++; + if (i === 3) + this.push(null); + else if (i === 1) { + this.push(new Buffer([0xe4, 0xb8, 0xad, 0xe6, 0x96])); // 5 bytes + } else if (i === 2) { + this.push('中文'); // 6 bytes + } + } + }); + + readable._index = 1; + + var total = 0; + readable.setEncoding('utf8'); + readable.on('data', function(chunk) { + console.log(chunk); + total += Buffer.byteLength(chunk); + console.log(Buffer.byteLength(chunk)); + }); + + readable.on('end', function() { + assert.equal(11, readable.bytesRead); + assert.equal(total, readable.bytesRead); + }); +})(); + (function() { const readable = new Readable({ read: function(n) {