Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit d515857

Browse files
committed
stream: Make default encoding configurable
Pretty much everything assumes strings to be utf-8, but crypto traditionally used binary strings, so we need to keep the default that way until most users get off of that pattern.
1 parent bdb78b9 commit d515857

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/_stream_readable.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ function ReadableState(options, stream) {
7272
// make all the buffer merging and length checks go away
7373
this.objectMode = !!options.objectMode;
7474

75+
// Crypto is kind of old and crusty. Historically, its default string
76+
// encoding is 'binary' so we have to make this configurable.
77+
// Everything else in the universe uses 'utf8', though.
78+
this.defaultEncoding = options.defaultEncoding || 'utf8';
79+
7580
// when piping, we only care about 'readable' events that happen
7681
// after read()ing all the bytes and not getting any pushback.
7782
this.ranOut = false;
@@ -112,7 +117,7 @@ Readable.prototype.push = function(chunk, encoding) {
112117
var state = this._readableState;
113118

114119
if (typeof chunk === 'string' && !state.objectMode) {
115-
encoding = encoding || 'utf8';
120+
encoding = encoding || state.defaultEncoding;
116121
if (encoding !== state.encoding) {
117122
chunk = new Buffer(chunk, encoding);
118123
encoding = '';

lib/_stream_writable.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ function WritableState(options, stream) {
6868
var noDecode = options.decodeStrings === false;
6969
this.decodeStrings = !noDecode;
7070

71+
// Crypto is kind of old and crusty. Historically, its default string
72+
// encoding is 'binary' so we have to make this configurable.
73+
// Everything else in the universe uses 'utf8', though.
74+
this.defaultEncoding = options.defaultEncoding || 'utf8';
75+
7176
// not an actual buffer we keep track of, but a measurement
7277
// of how much we're waiting to get pushed to some underlying
7378
// socket or file.
@@ -160,8 +165,11 @@ Writable.prototype.write = function(chunk, encoding, cb) {
160165
cb = encoding;
161166
encoding = null;
162167
}
163-
if (!encoding)
164-
encoding = 'utf8';
168+
169+
if (Buffer.isBuffer(chunk))
170+
encoding = 'buffer';
171+
else if (!encoding)
172+
encoding = state.defaultEncoding;
165173

166174
if (typeof cb !== 'function')
167175
cb = function() {};

0 commit comments

Comments
 (0)