Add polyfills
This commit is contained in:
parent
7fdb22459a
commit
cfe296bcc9
|
@ -78,17 +78,45 @@ if (typeof btoa !== "function") {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TextDecoder {
|
if (typeof TextDecoder === "undefined") {
|
||||||
constructorArgs
|
globalThis.TextDecoder = class {
|
||||||
|
constructor(encoding = "utf8") {
|
||||||
constructor(...constructorArgs) {
|
if (encoding !== "utf8") {
|
||||||
this.constructorArgs = constructorArgs
|
throw new Error(
|
||||||
}
|
`Only UTF-8 is supported in this polyfill. Recieved: ${encoding}`
|
||||||
|
)
|
||||||
decode(...input) {
|
}
|
||||||
return textDecoderCb({
|
}
|
||||||
constructorArgs: this.constructorArgs,
|
decode(buffer) {
|
||||||
functionArgs: input,
|
return String.fromCharCode(...buffer)
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof TextEncoder === "undefined") {
|
||||||
|
globalThis.TextEncoder = class {
|
||||||
|
encode(str) {
|
||||||
|
const utf8 = []
|
||||||
|
for (const i = 0; i < str.length; i++) {
|
||||||
|
const codePoint = str.charCodeAt(i)
|
||||||
|
|
||||||
|
if (codePoint < 0x80) {
|
||||||
|
utf8.push(codePoint)
|
||||||
|
} else if (codePoint < 0x800) {
|
||||||
|
utf8.push(0xc0 | (codePoint >> 6))
|
||||||
|
utf8.push(0x80 | (codePoint & 0x3f))
|
||||||
|
} else if (codePoint < 0x10000) {
|
||||||
|
utf8.push(0xe0 | (codePoint >> 12))
|
||||||
|
utf8.push(0x80 | ((codePoint >> 6) & 0x3f))
|
||||||
|
utf8.push(0x80 | (codePoint & 0x3f))
|
||||||
|
} else {
|
||||||
|
utf8.push(0xf0 | (codePoint >> 18))
|
||||||
|
utf8.push(0x80 | ((codePoint >> 12) & 0x3f))
|
||||||
|
utf8.push(0x80 | ((codePoint >> 6) & 0x3f))
|
||||||
|
utf8.push(0x80 | (codePoint & 0x3f))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Uint8Array(utf8)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -161,21 +161,7 @@ export class IsolatedVM implements VM {
|
||||||
|
|
||||||
const bsonSource = loadBundle(BundleType.BSON)
|
const bsonSource = loadBundle(BundleType.BSON)
|
||||||
|
|
||||||
// "Polyfilling" text decoder and other utils. `bson.deserialize` requires decoding. We are creating a bridge function so we don't need to inject the full library
|
|
||||||
const bsonPolyfills = loadBundle(BundleType.BSON_POLYFILLS)
|
const bsonPolyfills = loadBundle(BundleType.BSON_POLYFILLS)
|
||||||
this.addToContext({
|
|
||||||
textDecoderCb: new ivm.Callback(
|
|
||||||
(args: {
|
|
||||||
constructorArgs: any
|
|
||||||
functionArgs: Parameters<InstanceType<typeof TextDecoder>["decode"]>
|
|
||||||
}) => {
|
|
||||||
const result = new TextDecoder(...args.constructorArgs).decode(
|
|
||||||
...args.functionArgs
|
|
||||||
)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
),
|
|
||||||
})
|
|
||||||
|
|
||||||
const script = this.isolate.compileScriptSync(
|
const script = this.isolate.compileScriptSync(
|
||||||
`${bsonPolyfills};${bsonSource}`
|
`${bsonPolyfills};${bsonSource}`
|
||||||
|
|
Loading…
Reference in New Issue