Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: remove defaultEncoding of readable #4737

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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