mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-17 08:00:02 -04:00
feat: add origin to names of sharedworkers if provided
This commit is contained in:
parent
7233c60ac0
commit
d0046964e0
2 changed files with 52 additions and 32 deletions
|
@ -176,8 +176,8 @@ export class ScramjetClient {
|
|||
const module = context(key);
|
||||
if (!key.endsWith(".ts")) continue;
|
||||
if (
|
||||
(key.startsWith("./dom/") && "window" in self) ||
|
||||
(key.startsWith("./worker/") && "WorkerGlobalScope" in self) ||
|
||||
(key.startsWith("./dom/") && "window" in this.global) ||
|
||||
(key.startsWith("./worker/") && "WorkerGlobalScope" in this.global) ||
|
||||
key.startsWith("./shared/")
|
||||
) {
|
||||
modules.push(module);
|
||||
|
|
|
@ -10,7 +10,8 @@ if (self.Worker) {
|
|||
workerpostmessage = Worker.prototype.postMessage;
|
||||
}
|
||||
export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||
const handler = {
|
||||
if (self.Worker) {
|
||||
client.Proxy("Worker", {
|
||||
construct({ args, call }) {
|
||||
if (args[0] instanceof URL) args[0] = args[0].href;
|
||||
|
||||
|
@ -25,7 +26,6 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
|||
|
||||
(async () => {
|
||||
const port = await conn.getInnerPort();
|
||||
if (worker instanceof Worker) {
|
||||
workerpostmessage.call(
|
||||
worker,
|
||||
{
|
||||
|
@ -34,23 +34,9 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
|||
},
|
||||
[port]
|
||||
);
|
||||
}
|
||||
if (worker instanceof SharedWorker) {
|
||||
sharedworkerpostmessage.call(
|
||||
worker.port,
|
||||
{
|
||||
$scramjet$type: "baremuxinit",
|
||||
port,
|
||||
},
|
||||
[port]
|
||||
);
|
||||
}
|
||||
})();
|
||||
},
|
||||
};
|
||||
|
||||
if (self.Worker) {
|
||||
client.Proxy("Worker", handler);
|
||||
});
|
||||
}
|
||||
|
||||
if (iswindow) {
|
||||
|
@ -61,6 +47,40 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
|||
});
|
||||
|
||||
// sharedworkers can only be constructed from window
|
||||
client.Proxy("SharedWorker", handler);
|
||||
client.Proxy("SharedWorker", {
|
||||
construct({ args, call }) {
|
||||
if (args[0] instanceof URL) args[0] = args[0].href;
|
||||
|
||||
args[0] = rewriteUrl(args[0], client.meta) + "?dest=worker";
|
||||
|
||||
if (args[1] && typeof args[1] === "string")
|
||||
args[1] = `${client.url.origin}@${args[1]}`;
|
||||
|
||||
if (args[1] && typeof args[1] === "object") {
|
||||
if (args[1].type === "module") {
|
||||
args[0] += "&type=module";
|
||||
}
|
||||
|
||||
if (args[1].name === "name") {
|
||||
args[1].name = `${client.url.origin}@${args[1].name}`;
|
||||
}
|
||||
}
|
||||
|
||||
const worker = call();
|
||||
const conn = new BareMuxConnection();
|
||||
|
||||
(async () => {
|
||||
const port = await conn.getInnerPort();
|
||||
sharedworkerpostmessage.call(
|
||||
worker.port,
|
||||
{
|
||||
$scramjet$type: "baremuxinit",
|
||||
port,
|
||||
},
|
||||
[port]
|
||||
);
|
||||
})();
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue