44 ObjectDefineProperties,
55 String,
66 StringPrototypeCharCodeAt,
7- Symbol,
87 Uint8Array,
98} = primordials ;
109
@@ -31,50 +30,37 @@ const {
3130 kEnumerableProperty,
3231} = require ( 'internal/util' ) ;
3332
34- const kHandle = Symbol ( 'kHandle' ) ;
35- const kTransform = Symbol ( 'kTransform' ) ;
36- const kType = Symbol ( 'kType' ) ;
37- const kPendingHighSurrogate = Symbol ( 'kPendingHighSurrogate' ) ;
38-
3933/**
4034 * @typedef {import('./readablestream').ReadableStream } ReadableStream
4135 * @typedef {import('./writablestream').WritableStream } WritableStream
4236 */
4337
44- function isTextEncoderStream ( value ) {
45- return typeof value ?. [ kHandle ] === 'object' &&
46- value ?. [ kType ] === 'TextEncoderStream' ;
47- }
48-
49- function isTextDecoderStream ( value ) {
50- return typeof value ?. [ kHandle ] === 'object' &&
51- value ?. [ kType ] === 'TextDecoderStream' ;
52- }
53-
5438class TextEncoderStream {
39+ #pendingHighSurrogate = null ;
40+ #handle;
41+ #transform;
42+
5543 constructor ( ) {
56- this [ kPendingHighSurrogate ] = null ;
57- this [ kType ] = 'TextEncoderStream' ;
58- this [ kHandle ] = new TextEncoder ( ) ;
59- this [ kTransform ] = new TransformStream ( {
44+ this . #handle = new TextEncoder ( ) ;
45+ this . #transform = new TransformStream ( {
6046 transform : ( chunk , controller ) => {
6147 // https://encoding.spec.whatwg.org/#encode-and-enqueue-a-chunk
6248 chunk = String ( chunk ) ;
6349 let finalChunk = '' ;
6450 for ( let i = 0 ; i < chunk . length ; i ++ ) {
6551 const item = chunk [ i ] ;
6652 const codeUnit = StringPrototypeCharCodeAt ( item , 0 ) ;
67- if ( this [ kPendingHighSurrogate ] !== null ) {
68- const highSurrogate = this [ kPendingHighSurrogate ] ;
69- this [ kPendingHighSurrogate ] = null ;
53+ if ( this . #pendingHighSurrogate !== null ) {
54+ const highSurrogate = this . #pendingHighSurrogate ;
55+ this . #pendingHighSurrogate = null ;
7056 if ( 0xDC00 <= codeUnit && codeUnit <= 0xDFFF ) {
7157 finalChunk += highSurrogate + item ;
7258 continue ;
7359 }
7460 finalChunk += '\uFFFD' ;
7561 }
7662 if ( 0xD800 <= codeUnit && codeUnit <= 0xDBFF ) {
77- this [ kPendingHighSurrogate ] = item ;
63+ this . #pendingHighSurrogate = item ;
7864 continue ;
7965 }
8066 if ( 0xDC00 <= codeUnit && codeUnit <= 0xDFFF ) {
@@ -84,13 +70,13 @@ class TextEncoderStream {
8470 finalChunk += item ;
8571 }
8672 if ( finalChunk ) {
87- const value = this [ kHandle ] . encode ( finalChunk ) ;
73+ const value = this . #handle . encode ( finalChunk ) ;
8874 controller . enqueue ( value ) ;
8975 }
9076 } ,
9177 flush : ( controller ) => {
9278 // https://encoding.spec.whatwg.org/#encode-and-flush
93- if ( this [ kPendingHighSurrogate ] !== null ) {
79+ if ( this . #pendingHighSurrogate !== null ) {
9480 controller . enqueue ( new Uint8Array ( [ 0xEF , 0xBF , 0xBD ] ) ) ;
9581 }
9682 } ,
@@ -102,43 +88,40 @@ class TextEncoderStream {
10288 * @type {string }
10389 */
10490 get encoding ( ) {
105- if ( ! isTextEncoderStream ( this ) )
106- throw new ERR_INVALID_THIS ( 'TextEncoderStream' ) ;
107- return this [ kHandle ] . encoding ;
91+ return this . #handle. encoding ;
10892 }
10993
11094 /**
11195 * @readonly
11296 * @type {ReadableStream }
11397 */
11498 get readable ( ) {
115- if ( ! isTextEncoderStream ( this ) )
116- throw new ERR_INVALID_THIS ( 'TextEncoderStream' ) ;
117- return this [ kTransform ] . readable ;
99+ return this . #transform. readable ;
118100 }
119101
120102 /**
121103 * @readonly
122104 * @type {WritableStream }
123105 */
124106 get writable ( ) {
125- if ( ! isTextEncoderStream ( this ) )
126- throw new ERR_INVALID_THIS ( 'TextEncoderStream' ) ;
127- return this [ kTransform ] . writable ;
107+ return this . #transform. writable ;
128108 }
129109
130110 [ kInspect ] ( depth , options ) {
131- if ( ! isTextEncoderStream ( this ) )
111+ if ( this == null )
132112 throw new ERR_INVALID_THIS ( 'TextEncoderStream' ) ;
133113 return customInspect ( depth , options , 'TextEncoderStream' , {
134- encoding : this [ kHandle ] . encoding ,
135- readable : this [ kTransform ] . readable ,
136- writable : this [ kTransform ] . writable ,
114+ encoding : this . #handle . encoding ,
115+ readable : this . #transform . readable ,
116+ writable : this . #transform . writable ,
137117 } ) ;
138118 }
139119}
140120
141121class TextDecoderStream {
122+ #handle;
123+ #transform;
124+
142125 /**
143126 * @param {string } [encoding]
144127 * @param {{
@@ -147,16 +130,15 @@ class TextDecoderStream {
147130 * }} [options]
148131 */
149132 constructor ( encoding = 'utf-8' , options = kEmptyObject ) {
150- this [ kType ] = 'TextDecoderStream' ;
151- this [ kHandle ] = new TextDecoder ( encoding , options ) ;
152- this [ kTransform ] = new TransformStream ( {
133+ this . #handle = new TextDecoder ( encoding , options ) ;
134+ this . #transform = new TransformStream ( {
153135 transform : ( chunk , controller ) => {
154- const value = this [ kHandle ] . decode ( chunk , { stream : true } ) ;
136+ const value = this . #handle . decode ( chunk , { stream : true } ) ;
155137 if ( value )
156138 controller . enqueue ( value ) ;
157139 } ,
158140 flush : ( controller ) => {
159- const value = this [ kHandle ] . decode ( ) ;
141+ const value = this . #handle . decode ( ) ;
160142 if ( value )
161143 controller . enqueue ( value ) ;
162144 controller . terminate ( ) ;
@@ -169,60 +151,50 @@ class TextDecoderStream {
169151 * @type {string }
170152 */
171153 get encoding ( ) {
172- if ( ! isTextDecoderStream ( this ) )
173- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
174- return this [ kHandle ] . encoding ;
154+ return this . #handle. encoding ;
175155 }
176156
177157 /**
178158 * @readonly
179159 * @type {boolean }
180160 */
181161 get fatal ( ) {
182- if ( ! isTextDecoderStream ( this ) )
183- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
184- return this [ kHandle ] . fatal ;
162+ return this . #handle. fatal ;
185163 }
186164
187165 /**
188166 * @readonly
189167 * @type {boolean }
190168 */
191169 get ignoreBOM ( ) {
192- if ( ! isTextDecoderStream ( this ) )
193- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
194- return this [ kHandle ] . ignoreBOM ;
170+ return this . #handle. ignoreBOM ;
195171 }
196172
197173 /**
198174 * @readonly
199175 * @type {ReadableStream }
200176 */
201177 get readable ( ) {
202- if ( ! isTextDecoderStream ( this ) )
203- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
204- return this [ kTransform ] . readable ;
178+ return this . #transform. readable ;
205179 }
206180
207181 /**
208182 * @readonly
209183 * @type {WritableStream }
210184 */
211185 get writable ( ) {
212- if ( ! isTextDecoderStream ( this ) )
213- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
214- return this [ kTransform ] . writable ;
186+ return this . #transform. writable ;
215187 }
216188
217189 [ kInspect ] ( depth , options ) {
218- if ( ! isTextDecoderStream ( this ) )
190+ if ( this == null )
219191 throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
220192 return customInspect ( depth , options , 'TextDecoderStream' , {
221- encoding : this [ kHandle ] . encoding ,
222- fatal : this [ kHandle ] . fatal ,
223- ignoreBOM : this [ kHandle ] . ignoreBOM ,
224- readable : this [ kTransform ] . readable ,
225- writable : this [ kTransform ] . writable ,
193+ encoding : this . #handle . encoding ,
194+ fatal : this . #handle . fatal ,
195+ ignoreBOM : this . #handle . ignoreBOM ,
196+ readable : this . #transform . readable ,
197+ writable : this . #transform . writable ,
226198 } ) ;
227199 }
228200}
0 commit comments