mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 14:30:02 -04:00
scuffed as fuck blob rewriting
This commit is contained in:
parent
9b9905f640
commit
12b17bc3b4
2 changed files with 42 additions and 1 deletions
|
@ -6,7 +6,18 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
construct({ args }) {
|
construct({ args }) {
|
||||||
if (args[0] instanceof URL) args[0] = args[0].href;
|
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].startsWith("data:")) {
|
||||||
// TODO
|
if (args[0].startsWith("blob:")) {
|
||||||
|
args[0] =
|
||||||
|
"data:application/javascript;base64," + btoa(syncfetch(args[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
args[0] = "/scramjet/worker?data=" + args[0];
|
||||||
|
if (args[1] && args[1].type === "module") {
|
||||||
|
args[0] += "&type=module";
|
||||||
|
}
|
||||||
|
|
||||||
|
args[0] += "&origin=" + encodeURIComponent(client.url.origin);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,3 +29,11 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function syncfetch(url: string) {
|
||||||
|
const xhr = new XMLHttpRequest();
|
||||||
|
xhr.open("GET", url, false);
|
||||||
|
xhr.send();
|
||||||
|
|
||||||
|
return xhr.responseText;
|
||||||
|
}
|
||||||
|
|
|
@ -13,6 +13,28 @@ export async function swfetch(
|
||||||
this: ScramjetServiceWorker,
|
this: ScramjetServiceWorker,
|
||||||
{ request }: FetchEvent
|
{ request }: FetchEvent
|
||||||
) {
|
) {
|
||||||
|
if (new URL(request.url).pathname.startsWith("/scramjet/worker")) {
|
||||||
|
const dataurl = new URL(request.url).searchParams.get("data");
|
||||||
|
const res = await fetch(dataurl);
|
||||||
|
const ab = await res.arrayBuffer();
|
||||||
|
|
||||||
|
const ismodule = new URL(request.url).searchParams.get("type") === "module";
|
||||||
|
|
||||||
|
const origin = new URL(
|
||||||
|
decodeURIComponent(new URL(request.url).searchParams.get("origin"))
|
||||||
|
);
|
||||||
|
|
||||||
|
if (ismodule) origin.searchParams.set("type", "module");
|
||||||
|
|
||||||
|
const rewritten = rewriteWorkers(ab, new URL(origin));
|
||||||
|
|
||||||
|
return new Response(rewritten, {
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/javascript",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const urlParam = new URLSearchParams(new URL(request.url).search);
|
const urlParam = new URLSearchParams(new URL(request.url).search);
|
||||||
|
|
||||||
if (urlParam.has("url")) {
|
if (urlParam.has("url")) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue