Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string_decoder: refactor encoding validation #54957

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ function assertCrypto() {
throw new ERR_NO_CRYPTO();
}

// Return undefined if there is no match.
// Move the "slow cases" to a separate function to make sure this function gets
// inlined properly. That prioritizes the common case.
/**
* Move the "slow cases" to a separate function to make sure this function gets
* inlined properly. That prioritizes the common case.
* @param {unknown} enc
* @returns {string | undefined} Returns undefined if there is no match.
*/
function normalizeEncoding(enc) {
if (enc == null || enc === 'utf8' || enc === 'utf-8') return 'utf8';
return slowCases(enc);
Expand Down
32 changes: 6 additions & 26 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
anonrig marked this conversation as resolved.
Show resolved Hide resolved
/**
* 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
Expand All @@ -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];
}
Expand Down Expand Up @@ -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;
};

Expand Down
Loading