Skip to content

Commit 1fa084e

Browse files
anonrignodejs-github-bot
authored andcommitted
stream: use private properties for encoding
PR-URL: #47218 Reviewed-By: Erick Wendel <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Robert Nagy <[email protected]>
1 parent 4e93247 commit 1fa084e

File tree

2 files changed

+53
-73
lines changed

2 files changed

+53
-73
lines changed

‎lib/internal/webstreams/encoding.js‎

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const{
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-
constkHandle=Symbol('kHandle');
35-
constkTransform=Symbol('kTransform');
36-
constkType=Symbol('kType');
37-
constkPendingHighSurrogate=Symbol('kPendingHighSurrogate');
38-
3933
/**
4034
* @typedef{import('./readablestream').ReadableStream} ReadableStream
4135
* @typedef{import('./writablestream').WritableStream} WritableStream
4236
*/
4337

44-
functionisTextEncoderStream(value){
45-
returntypeofvalue?.[kHandle]==='object'&&
46-
value?.[kType]==='TextEncoderStream';
47-
}
48-
49-
functionisTextDecoderStream(value){
50-
returntypeofvalue?.[kHandle]==='object'&&
51-
value?.[kType]==='TextDecoderStream';
52-
}
53-
5438
classTextEncoderStream{
39+
#pendingHighSurrogate =null;
40+
#handle;
41+
#transform;
42+
5543
constructor(){
56-
this[kPendingHighSurrogate]=null;
57-
this[kType]='TextEncoderStream';
58-
this[kHandle]=newTextEncoder();
59-
this[kTransform]=newTransformStream({
44+
this.#handle =newTextEncoder();
45+
this.#transform =newTransformStream({
6046
transform: (chunk,controller)=>{
6147
// https://encoding.spec.whatwg.org/#encode-and-enqueue-a-chunk
6248
chunk=String(chunk);
6349
letfinalChunk='';
6450
for(leti=0;i<chunk.length;i++){
6551
constitem=chunk[i];
6652
constcodeUnit=StringPrototypeCharCodeAt(item,0);
67-
if(this[kPendingHighSurrogate]!==null){
68-
consthighSurrogate=this[kPendingHighSurrogate];
69-
this[kPendingHighSurrogate]=null;
53+
if(this.#pendingHighSurrogate!==null){
54+
consthighSurrogate=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-
constvalue=this[kHandle].encode(finalChunk);
73+
constvalue=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(newUint8Array([0xEF,0xBF,0xBD]));
9581
}
9682
},
@@ -102,43 +88,40 @@ class TextEncoderStream{
10288
* @type{string}
10389
*/
10490
getencoding(){
105-
if(!isTextEncoderStream(this))
106-
thrownewERR_INVALID_THIS('TextEncoderStream');
107-
returnthis[kHandle].encoding;
91+
returnthis.#handle.encoding;
10892
}
10993

11094
/**
11195
* @readonly
11296
* @type{ReadableStream}
11397
*/
11498
getreadable(){
115-
if(!isTextEncoderStream(this))
116-
thrownewERR_INVALID_THIS('TextEncoderStream');
117-
returnthis[kTransform].readable;
99+
returnthis.#transform.readable;
118100
}
119101

120102
/**
121103
* @readonly
122104
* @type{WritableStream}
123105
*/
124106
getwritable(){
125-
if(!isTextEncoderStream(this))
126-
thrownewERR_INVALID_THIS('TextEncoderStream');
127-
returnthis[kTransform].writable;
107+
returnthis.#transform.writable;
128108
}
129109

130110
[kInspect](depth,options){
131-
if(!isTextEncoderStream(this))
111+
if(this==null)
132112
thrownewERR_INVALID_THIS('TextEncoderStream');
133113
returncustomInspect(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

141121
classTextDecoderStream{
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]=newTextDecoder(encoding,options);
152-
this[kTransform]=newTransformStream({
133+
this.#handle =newTextDecoder(encoding,options);
134+
this.#transform =newTransformStream({
153135
transform: (chunk,controller)=>{
154-
constvalue=this[kHandle].decode(chunk,{stream: true});
136+
constvalue=this.#handle.decode(chunk,{stream: true});
155137
if(value)
156138
controller.enqueue(value);
157139
},
158140
flush: (controller)=>{
159-
constvalue=this[kHandle].decode();
141+
constvalue=this.#handle.decode();
160142
if(value)
161143
controller.enqueue(value);
162144
controller.terminate();
@@ -169,60 +151,50 @@ class TextDecoderStream{
169151
* @type{string}
170152
*/
171153
getencoding(){
172-
if(!isTextDecoderStream(this))
173-
thrownewERR_INVALID_THIS('TextDecoderStream');
174-
returnthis[kHandle].encoding;
154+
returnthis.#handle.encoding;
175155
}
176156

177157
/**
178158
* @readonly
179159
* @type{boolean}
180160
*/
181161
getfatal(){
182-
if(!isTextDecoderStream(this))
183-
thrownewERR_INVALID_THIS('TextDecoderStream');
184-
returnthis[kHandle].fatal;
162+
returnthis.#handle.fatal;
185163
}
186164

187165
/**
188166
* @readonly
189167
* @type{boolean}
190168
*/
191169
getignoreBOM(){
192-
if(!isTextDecoderStream(this))
193-
thrownewERR_INVALID_THIS('TextDecoderStream');
194-
returnthis[kHandle].ignoreBOM;
170+
returnthis.#handle.ignoreBOM;
195171
}
196172

197173
/**
198174
* @readonly
199175
* @type{ReadableStream}
200176
*/
201177
getreadable(){
202-
if(!isTextDecoderStream(this))
203-
thrownewERR_INVALID_THIS('TextDecoderStream');
204-
returnthis[kTransform].readable;
178+
returnthis.#transform.readable;
205179
}
206180

207181
/**
208182
* @readonly
209183
* @type{WritableStream}
210184
*/
211185
getwritable(){
212-
if(!isTextDecoderStream(this))
213-
thrownewERR_INVALID_THIS('TextDecoderStream');
214-
returnthis[kTransform].writable;
186+
returnthis.#transform.writable;
215187
}
216188

217189
[kInspect](depth,options){
218-
if(!isTextDecoderStream(this))
190+
if(this==null)
219191
thrownewERR_INVALID_THIS('TextDecoderStream');
220192
returncustomInspect(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
}

‎test/parallel/test-whatwg-webstreams-encoding.js‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,28 @@ const kEuro = Buffer.from([0xe2, 0x82, 0xac]).toString();
4848

4949
assert.throws(
5050
()=>Reflect.get(TextDecoderStream.prototype,'encoding',{}),{
51-
code: 'ERR_INVALID_THIS',
51+
name: 'TypeError',
52+
message: /Cannotreadprivatemember/,
5253
});
5354
assert.throws(
5455
()=>Reflect.get(TextDecoderStream.prototype,'fatal',{}),{
55-
code: 'ERR_INVALID_THIS',
56+
name: 'TypeError',
57+
message: /Cannotreadprivatemember/,
5658
});
5759
assert.throws(
5860
()=>Reflect.get(TextDecoderStream.prototype,'ignoreBOM',{}),{
59-
code: 'ERR_INVALID_THIS',
61+
name: 'TypeError',
62+
message: /Cannotreadprivatemember/,
6063
});
6164
assert.throws(
6265
()=>Reflect.get(TextDecoderStream.prototype,'readable',{}),{
63-
code: 'ERR_INVALID_THIS',
66+
name: 'TypeError',
67+
message: /Cannotreadprivatemember/,
6468
});
6569
assert.throws(
6670
()=>Reflect.get(TextDecoderStream.prototype,'writable',{}),{
67-
code: 'ERR_INVALID_THIS',
71+
name: 'TypeError',
72+
message: /Cannotreadprivatemember/,
6873
});
6974
}
7075

@@ -89,14 +94,17 @@ const kEuro = Buffer.from([0xe2, 0x82, 0xac]).toString();
8994

9095
assert.throws(
9196
()=>Reflect.get(TextEncoderStream.prototype,'encoding',{}),{
92-
code: 'ERR_INVALID_THIS',
97+
name: 'TypeError',
98+
message: /Cannotreadprivatemember/,
9399
});
94100
assert.throws(
95101
()=>Reflect.get(TextEncoderStream.prototype,'readable',{}),{
96-
code: 'ERR_INVALID_THIS',
102+
name: 'TypeError',
103+
message: /Cannotreadprivatemember/,
97104
});
98105
assert.throws(
99106
()=>Reflect.get(TextEncoderStream.prototype,'writable',{}),{
100-
code: 'ERR_INVALID_THIS',
107+
name: 'TypeError',
108+
message: /Cannotreadprivatemember/,
101109
});
102110
}

0 commit comments

Comments
(0)