Skip to content

Commit da820ab

Browse files
anonrigtpoisseau
authored andcommitted
string_decoder: refactor encoding validation
PR-URL: nodejs#54957 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent ecb19ef commit da820ab

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

lib/internal/util.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,12 @@ function assertCrypto() {
197197
throw new ERR_NO_CRYPTO();
198198
}
199199

200-
// Return undefined if there is no match.
201-
// Move the "slow cases" to a separate function to make sure this function gets
202-
// inlined properly. That prioritizes the common case.
200+
/**
201+
* Move the "slow cases" to a separate function to make sure this function gets
202+
* inlined properly. That prioritizes the common case.
203+
* @param {unknown} enc
204+
* @returns {string | undefined} Returns undefined if there is no match.
205+
*/
203206
function normalizeEncoding(enc) {
204207
if (enc == null || enc === 'utf8' || enc === 'utf-8') return 'utf8';
205208
return slowCases(enc);

lib/string_decoder.js

+6-26
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,17 @@ const {
4040
flush,
4141
} = internalBinding('string_decoder');
4242
const {
43-
kIsEncodingSymbol,
4443
encodingsMap,
45-
normalizeEncoding: _normalizeEncoding,
44+
normalizeEncoding,
4645
} = require('internal/util');
4746
const {
4847
ERR_INVALID_ARG_TYPE,
4948
ERR_INVALID_THIS,
5049
ERR_UNKNOWN_ENCODING,
5150
} = require('internal/errors').codes;
52-
const isEncoding = Buffer[kIsEncodingSymbol];
5351

5452
const kNativeDecoder = Symbol('kNativeDecoder');
5553

56-
// Do not cache `Buffer.isEncoding` when checking encoding names as some
57-
// modules monkey-patch it to support additional encodings
58-
/**
59-
* Normalize encoding notation
60-
* @param {string} enc
61-
* @returns {"utf8" | "utf16le" | "hex" | "ascii"
62-
* | "base64" | "latin1" | "base64url"}
63-
* @throws {TypeError} Throws an error when encoding is invalid
64-
*/
65-
function normalizeEncoding(enc) {
66-
const nenc = _normalizeEncoding(enc);
67-
if (nenc === undefined) {
68-
if (Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc))
69-
throw new ERR_UNKNOWN_ENCODING(enc);
70-
return enc;
71-
}
72-
return nenc;
73-
}
74-
7554
/**
7655
* StringDecoder provides an interface for efficiently splitting a series of
7756
* buffers into a series of JS strings without breaking apart multi-byte
@@ -80,6 +59,9 @@ function normalizeEncoding(enc) {
8059
*/
8160
function StringDecoder(encoding) {
8261
this.encoding = normalizeEncoding(encoding);
62+
if (this.encoding === undefined) {
63+
throw new ERR_UNKNOWN_ENCODING(encoding);
64+
}
8365
this[kNativeDecoder] = Buffer.alloc(kSize);
8466
this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding];
8567
}
@@ -112,11 +94,9 @@ StringDecoder.prototype.write = function write(buf) {
11294
* @returns {string}
11395
*/
11496
StringDecoder.prototype.end = function end(buf) {
115-
let ret = '';
116-
if (buf !== undefined)
117-
ret = this.write(buf);
97+
const ret = buf === undefined ? '' : this.write(buf);
11898
if (this[kNativeDecoder][kBufferedBytes] > 0)
119-
ret += flush(this[kNativeDecoder]);
99+
return ret + flush(this[kNativeDecoder]);
120100
return ret;
121101
};
122102

0 commit comments

Comments
 (0)