44// https://encoding.spec.whatwg.org
55
66const {
7+ ArrayPrototypeMap,
78 Boolean,
89 ObjectDefineProperties,
910 ObjectGetOwnPropertyDescriptors,
1011 ObjectSetPrototypeOf,
1112 ObjectValues,
13+ SafeArrayIterator,
1214 SafeMap,
1315 StringPrototypeSlice,
1416 Symbol,
@@ -32,8 +34,6 @@ const kFatal = Symbol('kFatal');
3234const kUTF8FastPath = Symbol ( 'kUTF8FastPath' ) ;
3335const kIgnoreBOM = Symbol ( 'kIgnoreBOM' ) ;
3436
35- const { isSinglebyteEncoding, createSinglebyteDecoder } = require ( 'internal/encoding/single-byte' ) ;
36-
3737const {
3838 getConstructorOf,
3939 customInspectSymbol : inspect ,
@@ -58,6 +58,7 @@ const {
5858 encodeIntoResults,
5959 encodeUtf8String,
6060 decodeUTF8,
61+ decodeSingleByte,
6162} = binding ;
6263
6364function validateDecoder ( obj ) {
@@ -71,6 +72,47 @@ const CONVERTER_FLAGS_IGNORE_BOM = 0x4;
7172
7273const empty = new FastBuffer ( ) ;
7374
75+ // Has to be synced with src/
76+ const encodingsSinglebyte = new SafeMap ( new SafeArrayIterator ( ArrayPrototypeMap ( [
77+ 'ibm866' ,
78+ 'koi8-r' ,
79+ 'koi8-u' ,
80+ 'macintosh' ,
81+ 'x-mac-cyrillic' ,
82+ 'iso-8859-2' ,
83+ 'iso-8859-3' ,
84+ 'iso-8859-4' ,
85+ 'iso-8859-5' ,
86+ 'iso-8859-6' ,
87+ 'iso-8859-7' ,
88+ 'iso-8859-8' ,
89+ 'iso-8859-8-i' ,
90+ 'iso-8859-10' ,
91+ 'iso-8859-13' ,
92+ 'iso-8859-14' ,
93+ 'iso-8859-15' ,
94+ 'iso-8859-16' ,
95+ 'windows-874' ,
96+ 'windows-1250' ,
97+ 'windows-1251' ,
98+ 'windows-1252' ,
99+ 'windows-1253' ,
100+ 'windows-1254' ,
101+ 'windows-1255' ,
102+ 'windows-1256' ,
103+ 'windows-1257' ,
104+ 'windows-1258' ,
105+ 'x-user-defined' , // Has to be last, special case
106+ ] , ( e , i ) => [ e , i ] ) ) ) ;
107+
108+ const isSinglebyteEncoding = ( enc ) => encodingsSinglebyte . has ( enc ) ;
109+
110+ function createSinglebyteDecoder ( encoding , fatal ) {
111+ const key = encodingsSinglebyte . get ( encoding ) ;
112+ if ( key === undefined ) throw new ERR_ENCODING_NOT_SUPPORTED ( encoding ) ;
113+ return ( buf ) => decodeSingleByte ( buf , key , fatal ) ;
114+ }
115+
74116const encodings = new SafeMap ( [
75117 [ 'unicode-1-1-utf-8' , 'utf-8' ] ,
76118 [ 'unicode11utf8' , 'utf-8' ] ,
@@ -462,7 +504,7 @@ function makeTextDecoderICU() {
462504 validateDecoder ( this ) ;
463505 validateObject ( options , 'options' , kValidateObjectAllowObjectsAndNull ) ;
464506
465- if ( this [ kMethod ] ) return this [ kMethod ] ( parseInput ( input ) ) ;
507+ if ( this [ kMethod ] ) return this [ kMethod ] ( input ) ;
466508
467509 this [ kUTF8FastPath ] &&= ! ( options ?. stream ) ;
468510
@@ -532,11 +574,12 @@ function makeTextDecoderJS() {
532574
533575 decode ( input = empty , options = kEmptyObject ) {
534576 validateDecoder ( this ) ;
535- input = parseInput ( input ) ;
536577 validateObject ( options , 'options' , kValidateObjectAllowObjectsAndNull ) ;
537578
538579 if ( this [ kMethod ] ) return this [ kMethod ] ( input ) ;
539580
581+ input = parseInput ( input ) ;
582+
540583 if ( this [ kFlags ] & CONVERTER_FLAGS_FLUSH ) {
541584 this [ kBOMSeen ] = false ;
542585 }
0 commit comments