Skip to content

Commit 5a07777

Browse files
authored
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 'buffer' for strict backward compatibility. This was necessary when crypto.DEFAULT_ENCODING still existed. Now that DEFAULT_ENCODING has been removed, defaultEncoding is always '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 PR-URL: #49140 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 56c3263 commit 5a07777

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

lib/internal/streams/lazy_transform.js

-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ function makeGetter(name) {
2323
return function() {
2424
stream.Transform.call(this, this._options);
2525
this._writableState.decodeStrings = false;
26-
27-
if (!this._options || !this._options.defaultEncoding) {
28-
this._writableState.defaultEncoding = 'buffer'; // TODO(tniessen): remove
29-
}
30-
3126
return this[name];
3227
};
3328
}

test/parallel/test-crypto.js

+5
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)