Skip to content

Commit

Permalink
crypto: do not overwrite _writableState.defaultEncoding
Browse files Browse the repository at this point in the history
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 <benjamingr@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
tniessen committed Aug 27, 2023
1 parent 56c3263 commit 5a07777
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
5 changes: 0 additions & 5 deletions lib/internal/streams/lazy_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ function makeGetter(name) {
return function() {
stream.Transform.call(this, this._options);
this._writableState.decodeStrings = false;

if (!this._options || !this._options.defaultEncoding) {
this._writableState.defaultEncoding = 'buffer'; // TODO(tniessen): remove
}

return this[name];
};
}
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ function testEncoding(options, assertionHash) {
let hashValue = '';

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

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

hash.write('öäü');
hash.end();

assert.strictEqual(hash._writableState.defaultEncoding, options?.defaultEncoding ?? 'utf8');
}

// Hash of "öäü" in utf8 format
Expand Down

0 comments on commit 5a07777

Please sign in to comment.