-
Notifications
You must be signed in to change notification settings - Fork 0
/
injBrowser.ts
54 lines (51 loc) · 1.6 KB
/
injBrowser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
export const b64toUi8 = (str: string) => Uint8Array.from(atob(str), c => c.charCodeAt(0))
export const ui8tob64 = (ui8: Uint8Array) => btoa(String.fromCharCode.apply(null, ui8))
export function createWorker() {
const insideWorker = async info => {
let lib: typeof import(".") // eslint-disable-line
self.addEventListener("message", async (e: MessageEvent) => {
const data = e.data
while (!lib) await new Promise(r => setTimeout(r, 0))
//
if (data.cmd == "decode") {
try {
data.result = new lib.Decoder().decodeSync(data.args.input, data.args.options)
.toArray()
} catch (err) {
data.err = err.message
}
delete data.args
self.postMessage(data)
} else if (data.cmd == "decodeConfigCmd") {
try {
data.result = new lib.Decoder().decodeConfigCmdData(data.args.rawData)
} catch (err) {
data.err = err.message
}
delete data.args
self.postMessage(data)
} else if (data.cmd == "encode") {
try {
const bp = new lib.Blueprint().fillFromArray(data.args.input)
data.result = new lib.Encoder().encodeSync(bp)
} catch (err) {
data.err = err.message
}
delete data.args
self.postMessage(data)
}
//
})
if (info.bundleInfo.format == "iife") {
importScripts(info.path)
lib = globalThis[info.bundleInfo.globalName]
} else lib = await import(info.path)
}
const workerData = {
path: import.meta.url,
bundleInfo: null // @build_bundleInfo
}
return new Worker(URL.createObjectURL(new Blob([
`(${insideWorker.toString()})(${JSON.stringify(workerData)})`
], { type: "text/javascript;charset=utf-8" })))
}