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 ddbe136

Browse filesBrowse files
authored
util: reduce TextEncoder.encodeInto function size
PR-URL: #60339 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent b19525a commit ddbe136
Copy full SHA for ddbe136

2 files changed

+27-24Lines changed: 27 additions & 24 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/encoding.js‎

Copy file name to clipboardExpand all lines: lib/internal/encoding.js
+20-21Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const kHandle = Symbol('handle');
2626
const kFlags = Symbol('flags');
2727
const kEncoding = Symbol('encoding');
2828
const kDecoder = Symbol('decoder');
29-
const kEncoder = Symbol('encoder');
3029
const kFatal = Symbol('kFatal');
3130
const kUTF8FastPath = Symbol('kUTF8FastPath');
3231
const kLatin1FastPath = Symbol('kLatin1FastPath');
@@ -61,11 +60,6 @@ const {
6160

6261
const { Buffer } = require('buffer');
6362

64-
function validateEncoder(obj) {
65-
if (obj == null || obj[kEncoder] !== true)
66-
throw new ERR_INVALID_THIS('TextEncoder');
67-
}
68-
6963
function validateDecoder(obj) {
7064
if (obj == null || obj[kDecoder] !== true)
7165
throw new ERR_INVALID_THIS('TextDecoder');
@@ -338,45 +332,50 @@ function getEncodingFromLabel(label) {
338332
return encodings.get(trimAsciiWhitespace(label.toLowerCase()));
339333
}
340334

335+
let lazyInspect;
336+
341337
class TextEncoder {
342-
constructor() {
343-
this[kEncoder] = true;
338+
#encoding = 'utf-8';
339+
340+
#encode(input) {
341+
return encodeUtf8String(`${input}`);
342+
}
343+
344+
#encodeInto(input, dest) {
345+
encodeInto(input, dest);
346+
// We need to read from the binding here since the buffer gets refreshed
347+
// from the snapshot.
348+
const { 0: read, 1: written } = encodeIntoResults;
349+
return { read, written };
344350
}
345351

346352
get encoding() {
347-
validateEncoder(this);
348-
return 'utf-8';
353+
return this.#encoding;
349354
}
350355

351356
encode(input = '') {
352-
validateEncoder(this);
353-
return encodeUtf8String(`${input}`);
357+
return this.#encode(input);
354358
}
355359

356360
encodeInto(src, dest) {
357-
validateEncoder(this);
358361
validateString(src, 'src');
359362
if (!dest || !isUint8Array(dest))
360363
throw new ERR_INVALID_ARG_TYPE('dest', 'Uint8Array', dest);
361364

362-
encodeInto(src, dest);
363-
// We need to read from the binding here since the buffer gets refreshed
364-
// from the snapshot.
365-
const { 0: read, 1: written } = encodeIntoResults;
366-
return { read, written };
365+
return this.#encodeInto(src, dest);
367366
}
368367

369368
[inspect](depth, opts) {
370-
validateEncoder(this);
371369
if (typeof depth === 'number' && depth < 0)
372370
return this;
373371
const ctor = getConstructorOf(this);
374372
const obj = { __proto__: {
375373
constructor: ctor === null ? TextEncoder : ctor,
376374
} };
377-
obj.encoding = this.encoding;
375+
obj.encoding = this.#encoding;
378376
// Lazy to avoid circular dependency
379-
return require('internal/util/inspect').inspect(obj, opts);
377+
lazyInspect ??= require('internal/util/inspect').inspect;
378+
return lazyInspect(obj, opts);
380379
}
381380
}
382381

Collapse file

‎test/parallel/test-whatwg-encoding-custom-interop.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-whatwg-encoding-custom-interop.js
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ assert(TextEncoder);
4646
const instance = new TextEncoder();
4747

4848
const expectedError = {
49-
code: 'ERR_INVALID_THIS',
5049
name: 'TypeError',
51-
message: 'Value of "this" must be of type TextEncoder'
50+
message: /from an object whose class did not declare it/,
5251
};
5352

5453
inspectFn.call(instance, Infinity, {});
@@ -58,7 +57,12 @@ assert(TextEncoder);
5857
const invalidThisArgs = [{}, [], true, 1, '', new TextDecoder()];
5958
for (const i of invalidThisArgs) {
6059
assert.throws(() => inspectFn.call(i, Infinity, {}), expectedError);
61-
assert.throws(() => encodeFn.call(i), expectedError);
6260
assert.throws(() => encodingGetter.call(i), expectedError);
6361
}
62+
for (const i of invalidThisArgs) {
63+
assert.throws(() => encodeFn.call(i), {
64+
name: 'TypeError',
65+
message: 'Receiver must be an instance of class TextEncoder',
66+
});
67+
}
6468
}

0 commit comments

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