Skip to content

Commit de5cc70

Browse files
committed
crypto: do not overwrite _writableState.defaultEncoding
This only affects the writable side of LazyTransform and should not change the behavior of any LazyTransform streams (Cipher, Decipher, Cipheriv, Decipheriv, Hash, Hmac). If the user does not set defaultEncoding when creating a transform stream, WritableState uses 'utf8' by default. Only LazyTransform overwrites this with the return value of getDefaultEncoding(). This was necessary when crypto.DEFAULT_ENCODING still existed. Now that DEFAULT_ENCODING has been removed, getDefaultEncoding() always returns 'buffer'. The writable side of LazyTransform appears to treat 'utf8' and 'buffer' in exactly the same way. Therefore, there seems to be no need to overwrite _writableState.defaultEncoding at this point. Nevertheless, because Node.js has failed to hide implementation details such as _writableState from the ecosystem, we may want to consider this a breaking change. Refs: #47182 Refs: #8611
1 parent 87af913 commit de5cc70

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

lib/internal/streams/lazy_transform.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ const {
1111

1212
const stream = require('stream');
1313

14-
const {
15-
getDefaultEncoding,
16-
} = require('internal/crypto/util');
17-
1814
module.exports = LazyTransform;
1915

2016
function LazyTransform(options) {
@@ -27,11 +23,6 @@ function makeGetter(name) {
2723
return function() {
2824
stream.Transform.call(this, this._options);
2925
this._writableState.decodeStrings = false;
30-
31-
if (!this._options || !this._options.defaultEncoding) {
32-
this._writableState.defaultEncoding = getDefaultEncoding();
33-
}
34-
3526
return this[name];
3627
};
3728
}

test/parallel/test-crypto.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ function testEncoding(options, assertionHash) {
298298
let hashValue = '';
299299

300300
hash.on('data', (data) => {
301+
// The defaultEncoding has no effect on the hash value. It only affects data
302+
// consumed by the Hash transform stream.
303+
assert(Buffer.isBuffer(data));
301304
hashValue += data.toString('hex');
302305
});
303306

@@ -307,6 +310,8 @@ function testEncoding(options, assertionHash) {
307310

308311
hash.write('öäü');
309312
hash.end();
313+
314+
assert.strictEqual(hash._writableState.defaultEncoding, options?.defaultEncoding ?? 'utf8');
310315
}
311316

312317
// Hash of "öäü" in utf8 format

0 commit comments

Comments
 (0)