| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,7 +26,6 @@ const kHandle = Symbol('handle'); | |||
| 26 | 26 | const kFlags = Symbol('flags'); | |
| 27 | 27 | const kEncoding = Symbol('encoding'); | |
| 28 | 28 | const kDecoder = Symbol('decoder'); | |
| 29 | - const kEncoder = Symbol('encoder'); | ||
| 30 | 29 | const kFatal = Symbol('kFatal'); | |
| 31 | 30 | const kUTF8FastPath = Symbol('kUTF8FastPath'); | |
| 32 | 31 | const kLatin1FastPath = Symbol('kLatin1FastPath'); | |
@@ -61,11 +60,6 @@ const { | |||
| 61 | 60 | ||
| 62 | 61 | const { Buffer } = require('buffer'); | |
| 63 | 62 | ||
| 64 | - function validateEncoder(obj) { | ||
| 65 | - if (obj == null || obj[kEncoder] !== true) | ||
| 66 | - throw new ERR_INVALID_THIS('TextEncoder'); | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | 63 | function validateDecoder(obj) { | |
| 70 | 64 | if (obj == null || obj[kDecoder] !== true) | |
| 71 | 65 | throw new ERR_INVALID_THIS('TextDecoder'); | |
@@ -338,45 +332,50 @@ function getEncodingFromLabel(label) { | |||
| 338 | 332 | return encodings.get(trimAsciiWhitespace(label.toLowerCase())); | |
| 339 | 333 | } | |
| 340 | 334 | ||
| 335 | + let lazyInspect; | ||
| 336 | + | ||
| 341 | 337 | 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 }; | ||
| 344 | 350 | } | |
| 345 | 351 | ||
| 346 | 352 | get encoding() { | |
| 347 | - validateEncoder(this); | ||
| 348 | - return 'utf-8'; | ||
| 353 | + return this.#encoding; | ||
| 349 | 354 | } | |
| 350 | 355 | ||
| 351 | 356 | encode(input = '') { | |
| 352 | - validateEncoder(this); | ||
| 353 | - return encodeUtf8String(`${input}`); | ||
| 357 | + return this.#encode(input); | ||
| 354 | 358 | } | |
| 355 | 359 | ||
| 356 | 360 | encodeInto(src, dest) { | |
| 357 | - validateEncoder(this); | ||
| 358 | 361 | validateString(src, 'src'); | |
| 359 | 362 | if (!dest || !isUint8Array(dest)) | |
| 360 | 363 | throw new ERR_INVALID_ARG_TYPE('dest', 'Uint8Array', dest); | |
| 361 | 364 | ||
| 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); | ||
| 367 | 366 | } | |
| 368 | 367 | ||
| 369 | 368 | [inspect](depth, opts) { | |
| 370 | - validateEncoder(this); | ||
| 371 | 369 | if (typeof depth === 'number' && depth < 0) | |
| 372 | 370 | return this; | |
| 373 | 371 | const ctor = getConstructorOf(this); | |
| 374 | 372 | const obj = { __proto__: { | |
| 375 | 373 | constructor: ctor === null ? TextEncoder : ctor, | |
| 376 | 374 | } }; | |
| 377 | - obj.encoding = this.encoding; | ||
| 375 | + obj.encoding = this.#encoding; | ||
| 378 | 376 | // 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); | ||
| 380 | 379 | } | |
| 381 | 380 | } | |
| 382 | 381 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -46,9 +46,8 @@ assert(TextEncoder); | |||
| 46 | 46 | const instance = new TextEncoder(); | |
| 47 | 47 | ||
| 48 | 48 | const expectedError = { | |
| 49 | - code: 'ERR_INVALID_THIS', | ||
| 50 | 49 | name: 'TypeError', | |
| 51 | - message: 'Value of "this" must be of type TextEncoder' | ||
| 50 | + message: /from an object whose class did not declare it/, | ||
| 52 | 51 | }; | |
| 53 | 52 | ||
| 54 | 53 | inspectFn.call(instance, Infinity, {}); | |
@@ -58,7 +57,12 @@ assert(TextEncoder); | |||
| 58 | 57 | const invalidThisArgs = [{}, [], true, 1, '', new TextDecoder()]; | |
| 59 | 58 | for (const i of invalidThisArgs) { | |
| 60 | 59 | assert.throws(() => inspectFn.call(i, Infinity, {}), expectedError); | |
| 61 | - assert.throws(() => encodeFn.call(i), expectedError); | ||
| 62 | 60 | assert.throws(() => encodingGetter.call(i), expectedError); | |
| 63 | 61 | } | |
| 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 | + } | ||
| 64 | 68 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments