@@ -26,7 +26,6 @@ const kHandle = Symbol('handle');
2626const kFlags = Symbol ( 'flags' ) ;
2727const kEncoding = Symbol ( 'encoding' ) ;
2828const kDecoder = Symbol ( 'decoder' ) ;
29- const kEncoder = Symbol ( 'encoder' ) ;
3029const kFatal = Symbol ( 'kFatal' ) ;
3130const kUTF8FastPath = Symbol ( 'kUTF8FastPath' ) ;
3231const kLatin1FastPath = Symbol ( 'kLatin1FastPath' ) ;
@@ -61,11 +60,6 @@ const {
6160
6261const { Buffer } = require ( 'buffer' ) ;
6362
64- function validateEncoder ( obj ) {
65- if ( obj == null || obj [ kEncoder ] !== true )
66- throw new ERR_INVALID_THIS ( 'TextEncoder' ) ;
67- }
68-
6963function 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+
341337class 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
0 commit comments