Skip to content

Commit ba1ec00

Browse files
committed
string_decoder: refactor encoding validation
1 parent e4692ee commit ba1ec00

File tree

2 files changed

+11
-28
lines changed

2 files changed

+11
-28
lines changed

lib/internal/util.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,12 @@ function assertCrypto() {
199199
throw new ERR_NO_CRYPTO();
200200
}
201201

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

lib/string_decoder.js

Lines changed: 5 additions & 25 deletions
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,9 +94,7 @@ 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+
let ret = buf === undefined ? '' : this.write(buf);
11898
if (this[kNativeDecoder][kBufferedBytes] > 0)
11999
ret += flush(this[kNativeDecoder]);
120100
return ret;

0 commit comments

Comments
 (0)