@@ -47,6 +47,10 @@ export function normalizeEncoding(label) {
4747
4848const define = ( obj , key , value ) => Object . defineProperty ( obj , key , { value, writable : false } )
4949
50+ // TODO: make this more strict against Symbol.toStringTag
51+ // Is not very significant though, anything faking Symbol.toStringTag could as well override
52+ // prototypes, which is not something we protect against
53+
5054function isAnyArrayBuffer ( x ) {
5155 if ( x instanceof ArrayBuffer ) return true
5256 if ( globalThis . SharedArrayBuffer && x instanceof SharedArrayBuffer ) return true
@@ -55,6 +59,12 @@ function isAnyArrayBuffer(x) {
5559 return s === '[object ArrayBuffer]' || s === '[object SharedArrayBuffer]'
5660}
5761
62+ function isAnyUint8Array ( x ) {
63+ if ( x instanceof Uint8Array ) return true
64+ if ( ! x || ! ArrayBuffer . isView ( x ) || x . BYTES_PER_ELEMENT !== 1 ) return false
65+ return Object . prototype . toString . call ( x ) === '[object Uint8Array]'
66+ }
67+
5868const fromSource = ( x ) => {
5969 if ( x instanceof Uint8Array ) return x
6070 if ( ArrayBuffer . isView ( x ) ) return new Uint8Array ( x . buffer , x . byteOffset , x . byteLength )
@@ -217,7 +227,7 @@ export class TextEncoder {
217227
218228 encodeInto ( str , target ) {
219229 if ( typeof str !== 'string' ) str = `${ str } `
220- if ( ! ( target instanceof Uint8Array ) ) throw new TypeError ( 'Target must be an Uint8Array' )
230+ if ( ! isAnyUint8Array ( target ) ) throw new TypeError ( 'Target must be an Uint8Array' )
221231 if ( target . buffer . detached ) return { read : 0 , written : 0 } // Until https://github.com/whatwg/encoding/issues/324 is resolved
222232
223233 const tlen = target . length
0 commit comments