Skip to content

Commit 0949a59

Browse files
committed
feat: TextEncoder.prototype.encodeInto polyfill
1 parent e0c250a commit 0949a59

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

bundler/modules/text-encoding-utf.cjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ TextEncoder.prototype.encode = function (str) {
5252
return new Uint8Array(buf.buffer, buf.byteOffset, buf.length)
5353
}
5454

55-
TextEncoder.prototype.encodeInto = function () {
56-
throw new Error('not supported')
55+
TextEncoder.prototype.encodeInto = function (str, ua) {
56+
if (!(ua instanceof Uint8Array)) throw new Error('second argument must be an Uint8Array')
57+
const buf = Buffer.from(str)
58+
if (ua.length < buf.length) throw new Error('Truncation not supported')
59+
ua.set(buf)
60+
return { read: str.length, written: buf.length }
5761
}
5862

5963
function TextDecoder(encoding = UTF8, options = {}) {

0 commit comments

Comments
 (0)