Skip to content

Commit d7f8c96

Browse files
committed
unify non-icu TextDecoder wih icu-based a bit
1 parent 3557542 commit d7f8c96

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

lib/internal/encoding.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ function makeTextDecoderJS() {
492492
const kBOMSeen = Symbol('BOM seen');
493493

494494
function hasConverter(encoding) {
495-
return encoding === 'utf-8' || encoding === 'utf-16le' || isSinglebyteEncoding(encoding);
495+
return encoding === 'utf-8' || encoding === 'utf-16le';
496496
}
497497

498498
class TextDecoder {
@@ -501,27 +501,30 @@ function makeTextDecoderJS() {
501501
validateObject(options, 'options', kValidateObjectAllowObjectsAndNull);
502502

503503
const enc = getEncodingFromLabel(encoding);
504-
if (enc === undefined || !hasConverter(enc))
504+
if (enc === undefined)
505505
throw new ERR_ENCODING_NOT_SUPPORTED(encoding);
506506

507507
let flags = 0;
508508
if (options !== null) {
509-
if (options.fatal) {
510-
throw new ERR_NO_ICU('"fatal" option');
511-
}
509+
flags |= options.fatal ? CONVERTER_FLAGS_FATAL : 0;
512510
flags |= options.ignoreBOM ? CONVERTER_FLAGS_IGNORE_BOM : 0;
513511
}
514512

515513
this[kDecoder] = true;
516-
// StringDecoder will normalize WHATWG encoding to Node.js encoding.
517-
this[kHandle] = new (lazyStringDecoder())(enc);
518514
this[kFlags] = flags;
519515
this[kEncoding] = enc;
516+
this[kIgnoreBOM] = Boolean(options?.ignoreBOM);
517+
this[kFatal] = Boolean(options?.fatal);
520518
this[kBOMSeen] = false;
521519
this[kMethod] = undefined;
522520

523-
if (isSinglebyteEncoding(this.encoding)) {
524-
this[kMethod] = createSinglebyteDecoder(this.encoding, this[kFatal]);
521+
if (isSinglebyteEncoding(enc)) {
522+
this[kMethod] = createSinglebyteDecoder(enc, this[kFatal]);
523+
} else {
524+
if (!hasConverter(enc)) throw new ERR_ENCODING_NOT_SUPPORTED(encoding);
525+
if (this[kFatal]) throw new ERR_NO_ICU('"fatal" option');
526+
// StringDecoder will normalize WHATWG encoding to Node.js encoding.
527+
this[kHandle] = new (lazyStringDecoder())(enc);
525528
}
526529
}
527530

@@ -546,9 +549,7 @@ function makeTextDecoderJS() {
546549
this[kHandle].end(input) :
547550
this[kHandle].write(input);
548551

549-
if (result.length > 0 &&
550-
!this[kBOMSeen] &&
551-
!(this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM)) {
552+
if (result.length > 0 && !this[kBOMSeen] && !this[kIgnoreBOM]) {
552553
// If the very first result in the stream is a BOM, and we are not
553554
// explicitly told to ignore it, then we discard it.
554555
if (result[0] === '\ufeff') {

0 commit comments

Comments
 (0)