Skip to content

Commit

Permalink
stream: remove defaultEncoding of ReadableState
Browse files Browse the repository at this point in the history
The defaultEncoding of ReadableState can not be set by any API.

Use new Buffer()'s default encoding should be more reasonable.
  • Loading branch information
JacksonTian committed Jan 18, 2016
1 parent 83d2b77 commit 8e359da
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
13 changes: 3 additions & 10 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -69,6 +64,7 @@ function ReadableState(options, stream) {

this.decoder = null;
this.encoding = null;

if (options.encoding) {
if (!StringDecoder)
StringDecoder = require('string_decoder').StringDecoder;
Expand Down Expand Up @@ -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);
Expand Down
57 changes: 57 additions & 0 deletions test/parallel/test-stream2-readable-bytesread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8e359da

Please sign in to comment.