mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-16 07:30:02 -04:00
start proxying sharedworkers
This commit is contained in:
parent
e7f1814431
commit
7b04e8a684
1 changed files with 44 additions and 0 deletions
|
@ -49,6 +49,50 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
})();
|
})();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.Proxy("SharedWorker", {
|
||||||
|
construct({ args, call }) {
|
||||||
|
if (args[0] instanceof URL) args[0] = args[0].href;
|
||||||
|
if (args[0].startsWith("blob:") || args[0].startsWith("data:")) {
|
||||||
|
const data = syncfetch(client, args[0]);
|
||||||
|
const id = Math.random().toString(8).slice(5);
|
||||||
|
|
||||||
|
args[0] = "/scramjet/worker?id=" + id;
|
||||||
|
if (args[1] && args[1].type === "module") {
|
||||||
|
args[0] += "&type=module";
|
||||||
|
}
|
||||||
|
|
||||||
|
args[0] += "&origin=" + encodeURIComponent(client.url.origin);
|
||||||
|
|
||||||
|
client.serviceWorker.controller?.postMessage({
|
||||||
|
scramjet$type: "dataworker",
|
||||||
|
data,
|
||||||
|
id,
|
||||||
|
} as MessageC2W);
|
||||||
|
} else {
|
||||||
|
args[0] = encodeUrl(args[0], client.meta) + "?dest=worker";
|
||||||
|
|
||||||
|
if (args[1] && args[1].type === "module") {
|
||||||
|
args[0] += "&type=module";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const worker = call();
|
||||||
|
const conn = new BareMuxConnection();
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const port = await conn.getInnerPort();
|
||||||
|
workerpostmessage.call(
|
||||||
|
worker,
|
||||||
|
{
|
||||||
|
$scramjet$type: "baremuxinit",
|
||||||
|
port,
|
||||||
|
},
|
||||||
|
[port]
|
||||||
|
);
|
||||||
|
})();
|
||||||
|
},
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function syncfetch(client: ScramjetClient, url: string) {
|
function syncfetch(client: ScramjetClient, url: string) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue