Skip to content

Commit

Permalink
optimize utf8Decode (#3085)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Apr 11, 2024
1 parent 21f6477 commit 836986d
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 836986d

Please sign in to comment.