From 17964376eb34f4dc6d99c7f7c8f8bb36f819bbf4 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Wed, 10 Apr 2024 15:54:31 +0200 Subject: [PATCH] optimize utf8Decode --- lib/web/websocket/util.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/web/websocket/util.js b/lib/web/websocket/util.js index 05f879e6e14..437a842cd96 100644 --- a/lib/web/websocket/util.js +++ b/lib/web/websocket/util.js @@ -209,24 +209,21 @@ const fatalDecoder = hasIntl ? new TextDecoder('utf-8', { fatal: true }) : undef * Converts a Buffer to utf-8, even on platforms without icu. * @param {Buffer} buffer */ -function utf8Decode (buffer) { - if (hasIntl) { - return fatalDecoder.decode(buffer) - } else { - if (!isUtf8?.(buffer)) { - // TODO: remove once node 18 or < node v18.14.0 is dropped - if (!isUtf8) { +const utf8Decode = hasIntl + ? fatalDecoder.decode.bind(fatalDecoder) + : !isUtf8 + ? function () { // TODO: remove once node 18 or < node v18.14.0 is dropped process.emitWarning('ICU is not supported and no fallback exists. Please upgrade to at least Node v18.14.0.', { code: 'UNDICI-WS-NO-ICU' }) + throw new TypeError('Invalid utf-8 received.') + } + : function (buffer) { + if (isUtf8(buffer)) { + return buffer.toString('utf-8') + } + throw new TypeError('Invalid utf-8 received.') } - - throw new TypeError('Invalid utf-8 received.') - } - - return buffer.toString('utf-8') - } -} module.exports = { isConnecting,