string_decoder: refactor encoding validation

PR-URL: https://github.com/nodejs/node/pull/54957
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Yagiz Nizipli
2024-09-23 18:49:53 -04:00
committed by GitHub
parent f43424ac2d
commit ffe0dc5b87
2 changed files with 12 additions and 29 deletions

View File

@@ -40,38 +40,17 @@ const {
flush,
} = internalBinding('string_decoder');
const {
kIsEncodingSymbol,
encodingsMap,
normalizeEncoding: _normalizeEncoding,
normalizeEncoding,
} = require('internal/util');
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_THIS,
ERR_UNKNOWN_ENCODING,
} = require('internal/errors').codes;
const isEncoding = Buffer[kIsEncodingSymbol];
const kNativeDecoder = Symbol('kNativeDecoder');
// Do not cache `Buffer.isEncoding` when checking encoding names as some
// modules monkey-patch it to support additional encodings
/**
* Normalize encoding notation
* @param {string} enc
* @returns {"utf8" | "utf16le" | "hex" | "ascii"
* | "base64" | "latin1" | "base64url"}
* @throws {TypeError} Throws an error when encoding is invalid
*/
function normalizeEncoding(enc) {
const nenc = _normalizeEncoding(enc);
if (nenc === undefined) {
if (Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc))
throw new ERR_UNKNOWN_ENCODING(enc);
return enc;
}
return nenc;
}
/**
* StringDecoder provides an interface for efficiently splitting a series of
* buffers into a series of JS strings without breaking apart multi-byte
@@ -80,6 +59,9 @@ function normalizeEncoding(enc) {
*/
function StringDecoder(encoding) {
this.encoding = normalizeEncoding(encoding);
if (this.encoding === undefined) {
throw new ERR_UNKNOWN_ENCODING(encoding);
}
this[kNativeDecoder] = Buffer.alloc(kSize);
this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding];
}
@@ -112,11 +94,9 @@ StringDecoder.prototype.write = function write(buf) {
* @returns {string}
*/
StringDecoder.prototype.end = function end(buf) {
let ret = '';
if (buf !== undefined)
ret = this.write(buf);
const ret = buf === undefined ? '' : this.write(buf);
if (this[kNativeDecoder][kBufferedBytes] > 0)
ret += flush(this[kNativeDecoder]);
return ret + flush(this[kNativeDecoder]);
return ret;
};