From 5a0777776d2fa805aa493ea1a796e752fc29e366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sun, 27 Aug 2023 16:12:29 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/47182 Refs: https://github.com/nodejs/node/pull/8611 PR-URL: https://github.com/nodejs/node/pull/49140 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rafael Gonzaga Reviewed-By: Matteo Collina --- lib/internal/streams/lazy_transform.js | 5 ----- test/parallel/test-crypto.js | 5 +++++ 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/internal/streams/lazy_transform.js b/lib/internal/streams/lazy_transform.js index 204ad456cd64b3..ab5746ce6a4cf8 100644 --- a/lib/internal/streams/lazy_transform.js +++ b/lib/internal/streams/lazy_transform.js @@ -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]; }; } diff --git a/test/parallel/test-crypto.js b/test/parallel/test-crypto.js index a8ceb169de2b3d..8350401ba93707 100644 --- a/test/parallel/test-crypto.js +++ b/test/parallel/test-crypto.js @@ -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'); }); @@ -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