4
4
ObjectDefineProperties,
5
5
String,
6
6
StringPrototypeCharCodeAt,
7
- Symbol,
8
7
Uint8Array,
9
8
} = primordials ;
10
9
@@ -31,50 +30,37 @@ const {
31
30
kEnumerableProperty,
32
31
} = require ( 'internal/util' ) ;
33
32
34
- const kHandle = Symbol ( 'kHandle' ) ;
35
- const kTransform = Symbol ( 'kTransform' ) ;
36
- const kType = Symbol ( 'kType' ) ;
37
- const kPendingHighSurrogate = Symbol ( 'kPendingHighSurrogate' ) ;
38
-
39
33
/**
40
34
* @typedef {import('./readablestream').ReadableStream } ReadableStream
41
35
* @typedef {import('./writablestream').WritableStream } WritableStream
42
36
*/
43
37
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
-
54
38
class TextEncoderStream {
39
+ #pendingHighSurrogate = null ;
40
+ #handle;
41
+ #transform;
42
+
55
43
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 ( {
60
46
transform : ( chunk , controller ) => {
61
47
// https://encoding.spec.whatwg.org/#encode-and-enqueue-a-chunk
62
48
chunk = String ( chunk ) ;
63
49
let finalChunk = '' ;
64
50
for ( let i = 0 ; i < chunk . length ; i ++ ) {
65
51
const item = chunk [ i ] ;
66
52
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 ;
70
56
if ( 0xDC00 <= codeUnit && codeUnit <= 0xDFFF ) {
71
57
finalChunk += highSurrogate + item ;
72
58
continue ;
73
59
}
74
60
finalChunk += '\uFFFD' ;
75
61
}
76
62
if ( 0xD800 <= codeUnit && codeUnit <= 0xDBFF ) {
77
- this [ kPendingHighSurrogate ] = item ;
63
+ this . #pendingHighSurrogate = item ;
78
64
continue ;
79
65
}
80
66
if ( 0xDC00 <= codeUnit && codeUnit <= 0xDFFF ) {
@@ -84,13 +70,13 @@ class TextEncoderStream {
84
70
finalChunk += item ;
85
71
}
86
72
if ( finalChunk ) {
87
- const value = this [ kHandle ] . encode ( finalChunk ) ;
73
+ const value = this . #handle . encode ( finalChunk ) ;
88
74
controller . enqueue ( value ) ;
89
75
}
90
76
} ,
91
77
flush : ( controller ) => {
92
78
// https://encoding.spec.whatwg.org/#encode-and-flush
93
- if ( this [ kPendingHighSurrogate ] !== null ) {
79
+ if ( this . #pendingHighSurrogate !== null ) {
94
80
controller . enqueue ( new Uint8Array ( [ 0xEF , 0xBF , 0xBD ] ) ) ;
95
81
}
96
82
} ,
@@ -102,43 +88,40 @@ class TextEncoderStream {
102
88
* @type {string }
103
89
*/
104
90
get encoding ( ) {
105
- if ( ! isTextEncoderStream ( this ) )
106
- throw new ERR_INVALID_THIS ( 'TextEncoderStream' ) ;
107
- return this [ kHandle ] . encoding ;
91
+ return this . #handle. encoding ;
108
92
}
109
93
110
94
/**
111
95
* @readonly
112
96
* @type {ReadableStream }
113
97
*/
114
98
get readable ( ) {
115
- if ( ! isTextEncoderStream ( this ) )
116
- throw new ERR_INVALID_THIS ( 'TextEncoderStream' ) ;
117
- return this [ kTransform ] . readable ;
99
+ return this . #transform. readable ;
118
100
}
119
101
120
102
/**
121
103
* @readonly
122
104
* @type {WritableStream }
123
105
*/
124
106
get writable ( ) {
125
- if ( ! isTextEncoderStream ( this ) )
126
- throw new ERR_INVALID_THIS ( 'TextEncoderStream' ) ;
127
- return this [ kTransform ] . writable ;
107
+ return this . #transform. writable ;
128
108
}
129
109
130
110
[ kInspect ] ( depth , options ) {
131
- if ( ! isTextEncoderStream ( this ) )
111
+ if ( this == null )
132
112
throw new ERR_INVALID_THIS ( 'TextEncoderStream' ) ;
133
113
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 ,
137
117
} ) ;
138
118
}
139
119
}
140
120
141
121
class TextDecoderStream {
122
+ #handle;
123
+ #transform;
124
+
142
125
/**
143
126
* @param {string } [encoding]
144
127
* @param {{
@@ -147,16 +130,15 @@ class TextDecoderStream {
147
130
* }} [options]
148
131
*/
149
132
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 ( {
153
135
transform : ( chunk , controller ) => {
154
- const value = this [ kHandle ] . decode ( chunk , { stream : true } ) ;
136
+ const value = this . #handle . decode ( chunk , { stream : true } ) ;
155
137
if ( value )
156
138
controller . enqueue ( value ) ;
157
139
} ,
158
140
flush : ( controller ) => {
159
- const value = this [ kHandle ] . decode ( ) ;
141
+ const value = this . #handle . decode ( ) ;
160
142
if ( value )
161
143
controller . enqueue ( value ) ;
162
144
controller . terminate ( ) ;
@@ -169,60 +151,50 @@ class TextDecoderStream {
169
151
* @type {string }
170
152
*/
171
153
get encoding ( ) {
172
- if ( ! isTextDecoderStream ( this ) )
173
- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
174
- return this [ kHandle ] . encoding ;
154
+ return this . #handle. encoding ;
175
155
}
176
156
177
157
/**
178
158
* @readonly
179
159
* @type {boolean }
180
160
*/
181
161
get fatal ( ) {
182
- if ( ! isTextDecoderStream ( this ) )
183
- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
184
- return this [ kHandle ] . fatal ;
162
+ return this . #handle. fatal ;
185
163
}
186
164
187
165
/**
188
166
* @readonly
189
167
* @type {boolean }
190
168
*/
191
169
get ignoreBOM ( ) {
192
- if ( ! isTextDecoderStream ( this ) )
193
- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
194
- return this [ kHandle ] . ignoreBOM ;
170
+ return this . #handle. ignoreBOM ;
195
171
}
196
172
197
173
/**
198
174
* @readonly
199
175
* @type {ReadableStream }
200
176
*/
201
177
get readable ( ) {
202
- if ( ! isTextDecoderStream ( this ) )
203
- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
204
- return this [ kTransform ] . readable ;
178
+ return this . #transform. readable ;
205
179
}
206
180
207
181
/**
208
182
* @readonly
209
183
* @type {WritableStream }
210
184
*/
211
185
get writable ( ) {
212
- if ( ! isTextDecoderStream ( this ) )
213
- throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
214
- return this [ kTransform ] . writable ;
186
+ return this . #transform. writable ;
215
187
}
216
188
217
189
[ kInspect ] ( depth , options ) {
218
- if ( ! isTextDecoderStream ( this ) )
190
+ if ( this == null )
219
191
throw new ERR_INVALID_THIS ( 'TextDecoderStream' ) ;
220
192
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 ,
226
198
} ) ;
227
199
}
228
200
}
0 commit comments