Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1fa084e

Browse filesBrowse files
anonrignodejs-github-bot
authored andcommitted
stream: use private properties for encoding
PR-URL: #47218 Reviewed-By: Erick Wendel <erick.workspace@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent 4e93247 commit 1fa084e
Copy full SHA for 1fa084e

File tree

Expand file treeCollapse file tree

2 files changed

+53
-73
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+53
-73
lines changed
Open diff view settings
Collapse file

‎lib/internal/webstreams/encoding.js‎

Copy file name to clipboardExpand all lines: lib/internal/webstreams/encoding.js
+37-65Lines 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-
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-
5438
class 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

141121
class 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
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-whatwg-webstreams-encoding.js
+16-8Lines 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: /Cannot read private member/,
5253
});
5354
assert.throws(
5455
() => Reflect.get(TextDecoderStream.prototype, 'fatal', {}), {
55-
code: 'ERR_INVALID_THIS',
56+
name: 'TypeError',
57+
message: /Cannot read private member/,
5658
});
5759
assert.throws(
5860
() => Reflect.get(TextDecoderStream.prototype, 'ignoreBOM', {}), {
59-
code: 'ERR_INVALID_THIS',
61+
name: 'TypeError',
62+
message: /Cannot read private member/,
6063
});
6164
assert.throws(
6265
() => Reflect.get(TextDecoderStream.prototype, 'readable', {}), {
63-
code: 'ERR_INVALID_THIS',
66+
name: 'TypeError',
67+
message: /Cannot read private member/,
6468
});
6569
assert.throws(
6670
() => Reflect.get(TextDecoderStream.prototype, 'writable', {}), {
67-
code: 'ERR_INVALID_THIS',
71+
name: 'TypeError',
72+
message: /Cannot read private member/,
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: /Cannot read private member/,
9399
});
94100
assert.throws(
95101
() => Reflect.get(TextEncoderStream.prototype, 'readable', {}), {
96-
code: 'ERR_INVALID_THIS',
102+
name: 'TypeError',
103+
message: /Cannot read private member/,
97104
});
98105
assert.throws(
99106
() => Reflect.get(TextEncoderStream.prototype, 'writable', {}), {
100-
code: 'ERR_INVALID_THIS',
107+
name: 'TypeError',
108+
message: /Cannot read private member/,
101109
});
102110
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.