fix data url workers

This commit is contained in:
velzie 2024-08-31 18:26:17 -04:00
parent 6ea474c3d0
commit ec0fb8b70a
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
5 changed files with 75 additions and 43 deletions

View file

@ -1,4 +1,5 @@
import { encodeUrl, BareMuxConnection } from "../../shared";
import type { MessageC2W } from "../../worker";
import { ScramjetClient } from "../client";
export default function (client: ScramjetClient, self: typeof globalThis) {
@ -6,26 +7,27 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
construct({ args, call }) {
if (args[0] instanceof URL) args[0] = args[0].href;
if (args[0].startsWith("blob:") || args[0].startsWith("data:")) {
if (args[0].startsWith("blob:")) {
args[0] =
"data:application/javascript;base64," +
btoa(syncfetch(client, args[0]));
}
let data = syncfetch(client, args[0]);
let id = Math.random().toString(8).slice(5);
args[0] = "/scramjet/worker?data=" + args[0];
args[0] = "/scramjet/worker?id=" + id;
if (args[1] && args[1].type === "module") {
args[0] += "&type=module";
}
args[0] += "&origin=" + encodeURIComponent(client.url.origin);
return;
}
client.serviceWorker.controller?.postMessage({
scramjet$type: "dataworker",
data,
id,
} as MessageC2W);
} else {
args[0] = encodeUrl(args[0]) + "?dest=worker";
args[0] = encodeUrl(args[0]) + "?dest=worker";
if (args[1] && args[1].type === "module") {
args[0] += "&type=module";
if (args[1] && args[1].type === "module") {
args[0] += "&type=module";
}
}
const worker = call();