From 766bd9c8cc27d64ad576fbdf3ea291a9f99c2a5c Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 23 Oct 2023 14:08:26 +0200 Subject: [PATCH] stream: avoid getter for defaultEncoding PR-URL: https://github.com/nodejs/node/pull/50203 Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Reviewed-By: Yagiz Nizipli Reviewed-By: Luigi Pinca --- lib/internal/streams/writable.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index f75d2ac5dc7015..8bf967e556bf80 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -445,16 +445,17 @@ function _write(stream, chunk, encoding, cb) { if (typeof encoding === 'function') { cb = encoding; - encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state.defaultEncoding; - } else { - if (!encoding) - encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state.defaultEncoding; - else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding)) - throw new ERR_UNKNOWN_ENCODING(encoding); - if (typeof cb !== 'function') - cb = nop; + encoding = null; } + if (!encoding) + encoding = (state[kState] & kDefaultUTF8Encoding) !== 0 ? 'utf8' : state[kDefaultEncodingValue]; + else if (encoding !== 'buffer' && !Buffer.isEncoding(encoding)) + throw new ERR_UNKNOWN_ENCODING(encoding); + + if (cb == null || typeof cb !== 'function') + cb = nop; + if (chunk === null) { throw new ERR_STREAM_NULL_VALUES(); } else if ((state[kState] & kObjectMode) === 0) {