mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 12:20:27 +01:00
util: reduce TextEncoder.encodeInto function size
PR-URL: https://github.com/nodejs/node/pull/60339 Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
parent
b19525a33c
commit
ddbe1365ff
|
|
@ -26,7 +26,6 @@ const kHandle = Symbol('handle');
|
||||||
const kFlags = Symbol('flags');
|
const kFlags = Symbol('flags');
|
||||||
const kEncoding = Symbol('encoding');
|
const kEncoding = Symbol('encoding');
|
||||||
const kDecoder = Symbol('decoder');
|
const kDecoder = Symbol('decoder');
|
||||||
const kEncoder = Symbol('encoder');
|
|
||||||
const kFatal = Symbol('kFatal');
|
const kFatal = Symbol('kFatal');
|
||||||
const kUTF8FastPath = Symbol('kUTF8FastPath');
|
const kUTF8FastPath = Symbol('kUTF8FastPath');
|
||||||
const kLatin1FastPath = Symbol('kLatin1FastPath');
|
const kLatin1FastPath = Symbol('kLatin1FastPath');
|
||||||
|
|
@ -61,11 +60,6 @@ const {
|
||||||
|
|
||||||
const { Buffer } = require('buffer');
|
const { Buffer } = require('buffer');
|
||||||
|
|
||||||
function validateEncoder(obj) {
|
|
||||||
if (obj == null || obj[kEncoder] !== true)
|
|
||||||
throw new ERR_INVALID_THIS('TextEncoder');
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateDecoder(obj) {
|
function validateDecoder(obj) {
|
||||||
if (obj == null || obj[kDecoder] !== true)
|
if (obj == null || obj[kDecoder] !== true)
|
||||||
throw new ERR_INVALID_THIS('TextDecoder');
|
throw new ERR_INVALID_THIS('TextDecoder');
|
||||||
|
|
@ -338,45 +332,50 @@ function getEncodingFromLabel(label) {
|
||||||
return encodings.get(trimAsciiWhitespace(label.toLowerCase()));
|
return encodings.get(trimAsciiWhitespace(label.toLowerCase()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let lazyInspect;
|
||||||
|
|
||||||
class TextEncoder {
|
class TextEncoder {
|
||||||
constructor() {
|
#encoding = 'utf-8';
|
||||||
this[kEncoder] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
get encoding() {
|
#encode(input) {
|
||||||
validateEncoder(this);
|
|
||||||
return 'utf-8';
|
|
||||||
}
|
|
||||||
|
|
||||||
encode(input = '') {
|
|
||||||
validateEncoder(this);
|
|
||||||
return encodeUtf8String(`${input}`);
|
return encodeUtf8String(`${input}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
encodeInto(src, dest) {
|
#encodeInto(input, dest) {
|
||||||
validateEncoder(this);
|
encodeInto(input, dest);
|
||||||
validateString(src, 'src');
|
|
||||||
if (!dest || !isUint8Array(dest))
|
|
||||||
throw new ERR_INVALID_ARG_TYPE('dest', 'Uint8Array', dest);
|
|
||||||
|
|
||||||
encodeInto(src, dest);
|
|
||||||
// We need to read from the binding here since the buffer gets refreshed
|
// We need to read from the binding here since the buffer gets refreshed
|
||||||
// from the snapshot.
|
// from the snapshot.
|
||||||
const { 0: read, 1: written } = encodeIntoResults;
|
const { 0: read, 1: written } = encodeIntoResults;
|
||||||
return { read, written };
|
return { read, written };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get encoding() {
|
||||||
|
return this.#encoding;
|
||||||
|
}
|
||||||
|
|
||||||
|
encode(input = '') {
|
||||||
|
return this.#encode(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
encodeInto(src, dest) {
|
||||||
|
validateString(src, 'src');
|
||||||
|
if (!dest || !isUint8Array(dest))
|
||||||
|
throw new ERR_INVALID_ARG_TYPE('dest', 'Uint8Array', dest);
|
||||||
|
|
||||||
|
return this.#encodeInto(src, dest);
|
||||||
|
}
|
||||||
|
|
||||||
[inspect](depth, opts) {
|
[inspect](depth, opts) {
|
||||||
validateEncoder(this);
|
|
||||||
if (typeof depth === 'number' && depth < 0)
|
if (typeof depth === 'number' && depth < 0)
|
||||||
return this;
|
return this;
|
||||||
const ctor = getConstructorOf(this);
|
const ctor = getConstructorOf(this);
|
||||||
const obj = { __proto__: {
|
const obj = { __proto__: {
|
||||||
constructor: ctor === null ? TextEncoder : ctor,
|
constructor: ctor === null ? TextEncoder : ctor,
|
||||||
} };
|
} };
|
||||||
obj.encoding = this.encoding;
|
obj.encoding = this.#encoding;
|
||||||
// Lazy to avoid circular dependency
|
// Lazy to avoid circular dependency
|
||||||
return require('internal/util/inspect').inspect(obj, opts);
|
lazyInspect ??= require('internal/util/inspect').inspect;
|
||||||
|
return lazyInspect(obj, opts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,9 +46,8 @@ assert(TextEncoder);
|
||||||
const instance = new TextEncoder();
|
const instance = new TextEncoder();
|
||||||
|
|
||||||
const expectedError = {
|
const expectedError = {
|
||||||
code: 'ERR_INVALID_THIS',
|
|
||||||
name: 'TypeError',
|
name: 'TypeError',
|
||||||
message: 'Value of "this" must be of type TextEncoder'
|
message: /from an object whose class did not declare it/,
|
||||||
};
|
};
|
||||||
|
|
||||||
inspectFn.call(instance, Infinity, {});
|
inspectFn.call(instance, Infinity, {});
|
||||||
|
|
@ -58,7 +57,12 @@ assert(TextEncoder);
|
||||||
const invalidThisArgs = [{}, [], true, 1, '', new TextDecoder()];
|
const invalidThisArgs = [{}, [], true, 1, '', new TextDecoder()];
|
||||||
for (const i of invalidThisArgs) {
|
for (const i of invalidThisArgs) {
|
||||||
assert.throws(() => inspectFn.call(i, Infinity, {}), expectedError);
|
assert.throws(() => inspectFn.call(i, Infinity, {}), expectedError);
|
||||||
assert.throws(() => encodeFn.call(i), expectedError);
|
|
||||||
assert.throws(() => encodingGetter.call(i), expectedError);
|
assert.throws(() => encodingGetter.call(i), expectedError);
|
||||||
}
|
}
|
||||||
|
for (const i of invalidThisArgs) {
|
||||||
|
assert.throws(() => encodeFn.call(i), {
|
||||||
|
name: 'TypeError',
|
||||||
|
message: 'Receiver must be an instance of class TextEncoder',
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user