24
24
const {
25
25
ArrayBufferIsView,
26
26
ObjectDefineProperties,
27
- Symbol,
28
27
TypedArrayPrototypeSubarray,
29
28
} = primordials ;
30
29
@@ -43,128 +42,125 @@ const {
43
42
const internalUtil = require ( 'internal/util' ) ;
44
43
const {
45
44
ERR_INVALID_ARG_TYPE ,
46
- ERR_INVALID_THIS ,
47
45
ERR_UNKNOWN_ENCODING ,
48
46
} = require ( 'internal/errors' ) . codes ;
49
47
const isEncoding = Buffer [ internalUtil . kIsEncodingSymbol ] ;
50
48
51
- const kNativeDecoder = Symbol ( 'kNativeDecoder' ) ;
52
-
53
- // Do not cache `Buffer.isEncoding` when checking encoding names as some
54
- // modules monkey-patch it to support additional encodings
55
- /**
56
- * Normalize encoding notation
57
- *
58
- * @param {string } enc
59
- * @returns {"utf8" | "utf16le" | "hex" | "ascii"
60
- * | "base64" | "latin1" | "base64url"}
61
- * @throws {TypeError } Throws an error when encoding is invalid
62
- */
63
- function normalizeEncoding ( enc ) {
64
- const nenc = internalUtil . normalizeEncoding ( enc ) ;
65
- if ( nenc === undefined ) {
66
- if ( Buffer . isEncoding === isEncoding || ! Buffer . isEncoding ( enc ) )
67
- throw new ERR_UNKNOWN_ENCODING ( enc ) ;
68
- return enc ;
69
- }
70
- return nenc ;
71
- }
72
-
73
49
const encodingsMap = { } ;
74
50
for ( let i = 0 ; i < encodings . length ; ++ i )
75
51
encodingsMap [ encodings [ i ] ] = i ;
76
52
77
- /**
78
- * StringDecoder provides an interface for efficiently splitting a series of
79
- * buffers into a series of JS strings without breaking apart multi-byte
80
- * characters.
81
- *
82
- * @param {string } [encoding=utf-8]
83
- */
84
- function StringDecoder ( encoding ) {
85
- this . encoding = normalizeEncoding ( encoding ) ;
86
- this [ kNativeDecoder ] = Buffer . alloc ( kSize ) ;
87
- this [ kNativeDecoder ] [ kEncodingField ] = encodingsMap [ this . encoding ] ;
88
- }
53
+ class StringDecoder {
54
+ #nativeDecoder = Buffer . alloc ( kSize ) ;
55
+
56
+ /**
57
+ * StringDecoder provides an interface for efficiently splitting a series of
58
+ * buffers into a series of JS strings without breaking apart multi-byte
59
+ * characters.
60
+ *
61
+ * @param {string } [encoding=utf-8]
62
+ */
63
+ constructor ( encoding ) {
64
+ this . encoding = this . #normalizeEncoding( encoding ) ;
65
+ this . #nativeDecoder[ kEncodingField ] = encodingsMap [ this . encoding ] ;
66
+ }
67
+
68
+ // Do not cache `Buffer.isEncoding` when checking encoding names as some
69
+ // modules monkey-patch it to support additional encodings
70
+ /**
71
+ * Normalize encoding notation
72
+ *
73
+ * @param {string } enc
74
+ * @returns {"utf8" | "utf16le" | "hex" | "ascii"
75
+ * | "base64" | "latin1" | "base64url"}
76
+ * @throws {TypeError } Throws an error when encoding is invalid
77
+ */
78
+ #normalizeEncoding( enc ) {
79
+ const nenc = internalUtil . normalizeEncoding ( enc ) ;
80
+ if ( nenc === undefined ) {
81
+ if ( Buffer . isEncoding === isEncoding || ! Buffer . isEncoding ( enc ) )
82
+ throw new ERR_UNKNOWN_ENCODING ( enc ) ;
83
+ return enc ;
84
+ }
85
+ return nenc ;
86
+ }
89
87
90
- /**
91
- * Returns a decoded string, omitting any incomplete multi-bytes
92
- * characters at the end of the Buffer, or TypedArray, or DataView
93
- *
94
- * @param {string | Buffer | TypedArray | DataView } buf
95
- * @returns {string }
96
- * @throws {TypeError } Throws when buf is not in one of supported types
97
- */
98
- StringDecoder . prototype . write = function write ( buf ) {
99
- if ( typeof buf === 'string' )
100
- return buf ;
101
- if ( ! ArrayBufferIsView ( buf ) )
102
- throw new ERR_INVALID_ARG_TYPE ( 'buf' ,
103
- [ 'Buffer' , 'TypedArray' , 'DataView' ] ,
104
- buf ) ;
105
- if ( ! this [ kNativeDecoder ] ) {
106
- throw new ERR_INVALID_THIS ( 'StringDecoder' ) ;
88
+ /**
89
+ * Returns a decoded string, omitting any incomplete multi-bytes
90
+ * characters at the end of the Buffer, or TypedArray, or DataView
91
+ *
92
+ * @param {string | Buffer | TypedArray | DataView } buf
93
+ * @returns {string }
94
+ * @throws {TypeError } Throws when buf is not in one of supported types
95
+ */
96
+ write ( buf ) {
97
+ if ( typeof buf === 'string' )
98
+ return buf ;
99
+ if ( ! ArrayBufferIsView ( buf ) )
100
+ throw new ERR_INVALID_ARG_TYPE ( 'buf' ,
101
+ [ 'Buffer' , 'TypedArray' , 'DataView' ] ,
102
+ buf ) ;
103
+ return decode ( this . #nativeDecoder, buf ) ;
107
104
}
108
- return decode ( this [ kNativeDecoder ] , buf ) ;
109
- } ;
110
105
111
- /**
112
- * Returns any remaining input stored in the internal buffer as a string.
113
- * After end() is called, the stringDecoder object can be reused for new
114
- * input.
115
- *
116
- * @param {string | Buffer | TypedArray | DataView } [buf]
117
- * @returns {string }
118
- */
119
- StringDecoder . prototype . end = function end ( buf ) {
120
- let ret = '' ;
121
- if ( buf !== undefined )
122
- ret = this . write ( buf ) ;
123
- if ( this [ kNativeDecoder ] [ kBufferedBytes ] > 0 )
124
- ret += flush ( this [ kNativeDecoder ] ) ;
125
- return ret ;
126
- } ;
106
+ /**
107
+ * Returns any remaining input stored in the internal buffer as a string.
108
+ * After end() is called, the stringDecoder object can be reused for new
109
+ * input.
110
+ *
111
+ * @param {string | Buffer | TypedArray | DataView } [buf]
112
+ * @returns {string }
113
+ */
114
+ end ( buf ) {
115
+ let ret = '' ;
116
+ if ( buf !== undefined )
117
+ ret = this . write ( buf ) ;
118
+ if ( this . #nativeDecoder[ kBufferedBytes ] > 0 )
119
+ ret += flush ( this . #nativeDecoder) ;
120
+ return ret ;
121
+ }
122
+
123
+ /* Everything below this line is undocumented legacy stuff. */
124
+ /**
125
+ *
126
+ * @param {string | Buffer | TypedArray | DataView } buf
127
+ * @param {number } offset
128
+ * @returns {string }
129
+ */
130
+ text ( buf , offset ) {
131
+ this . #nativeDecoder[ kMissingBytes ] = 0 ;
132
+ this . #nativeDecoder[ kBufferedBytes ] = 0 ;
133
+ return this . write ( buf . slice ( offset ) ) ;
134
+ }
135
+
136
+ get lastChar ( ) {
137
+ return TypedArrayPrototypeSubarray ( this . #nativeDecoder, kIncompleteCharactersStart , kIncompleteCharactersEnd ) ;
138
+ }
127
139
128
- /* Everything below this line is undocumented legacy stuff. */
129
- /**
130
- *
131
- * @param {string | Buffer | TypedArray | DataView } buf
132
- * @param {number } offset
133
- * @returns {string }
134
- */
135
- StringDecoder . prototype . text = function text ( buf , offset ) {
136
- this [ kNativeDecoder ] [ kMissingBytes ] = 0 ;
137
- this [ kNativeDecoder ] [ kBufferedBytes ] = 0 ;
138
- return this . write ( buf . slice ( offset ) ) ;
139
- } ;
140
+ get lastNeed ( ) {
141
+ return this . #nativeDecoder[ kMissingBytes ] ;
142
+ }
143
+
144
+ get lastTotal ( ) {
145
+ return this . #nativeDecoder[ kBufferedBytes ] + this . #nativeDecoder[ kMissingBytes ] ;
146
+ }
147
+ }
140
148
141
149
ObjectDefineProperties ( StringDecoder . prototype , {
142
150
lastChar : {
143
151
__proto__ : null ,
144
152
configurable : true ,
145
153
enumerable : true ,
146
- get ( ) {
147
- return TypedArrayPrototypeSubarray ( this [ kNativeDecoder ] ,
148
- kIncompleteCharactersStart ,
149
- kIncompleteCharactersEnd ) ;
150
- } ,
151
154
} ,
152
155
lastNeed : {
153
156
__proto__ : null ,
154
157
configurable : true ,
155
158
enumerable : true ,
156
- get ( ) {
157
- return this [ kNativeDecoder ] [ kMissingBytes ] ;
158
- } ,
159
159
} ,
160
160
lastTotal : {
161
161
__proto__ : null ,
162
162
configurable : true ,
163
163
enumerable : true ,
164
- get ( ) {
165
- return this [ kNativeDecoder ] [ kBufferedBytes ] +
166
- this [ kNativeDecoder ] [ kMissingBytes ] ;
167
- } ,
168
164
} ,
169
165
} ) ;
170
166
0 commit comments