Skip to content

Commit d45004a

Browse files
committed
fix: support cross-context Uint8Array in TextEncoder#encodeInto
1 parent 3ef9b62 commit d45004a

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

fallback/encoding.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ function isAnyArrayBuffer(x) {
5555
return s === '[object ArrayBuffer]' || s === '[object SharedArrayBuffer]'
5656
}
5757

58+
function isAnyUint8Array(x) {
59+
if (x instanceof Uint8Array) return true
60+
if (!x || !ArrayBuffer.isView(x) || x.BYTES_PER_ELEMENT !== 1) return false
61+
return Object.prototype.toString.call(x) === '[object Uint8Array]'
62+
}
63+
5864
const fromSource = (x) => {
5965
if (x instanceof Uint8Array) return x
6066
if (ArrayBuffer.isView(x)) return new Uint8Array(x.buffer, x.byteOffset, x.byteLength)
@@ -217,7 +223,7 @@ export class TextEncoder {
217223

218224
encodeInto(str, target) {
219225
if (typeof str !== 'string') str = `${str}`
220-
if (!(target instanceof Uint8Array)) throw new TypeError('Target must be an Uint8Array')
226+
if (!isAnyUint8Array(target)) throw new TypeError('Target must be an Uint8Array')
221227
if (target.buffer.detached) return { read: 0, written: 0 } // Until https://github.com/whatwg/encoding/issues/324 is resolved
222228

223229
const tlen = target.length

0 commit comments

Comments
 (0)