feat: add origin to names of sharedworkers if provided

This commit is contained in:
Percs 2024-10-15 19:17:01 -05:00
parent 7233c60ac0
commit d0046964e0
2 changed files with 52 additions and 32 deletions

View file

@ -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);

View file

@ -10,22 +10,22 @@ if (self.Worker) {
workerpostmessage = Worker.prototype.postMessage;
}
export default function (client: ScramjetClient, self: typeof globalThis) {
const handler = {
construct({ args, call }) {
if (args[0] instanceof URL) args[0] = args[0].href;
if (self.Worker) {
client.Proxy("Worker", {
construct({ args, call }) {
if (args[0] instanceof URL) args[0] = args[0].href;
args[0] = rewriteUrl(args[0], client.meta) + "?dest=worker";
args[0] = rewriteUrl(args[0], client.meta) + "?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();
const conn = new BareMuxConnection();
const worker = call();
const conn = new BareMuxConnection();
(async () => {
const port = await conn.getInnerPort();
if (worker instanceof Worker) {
(async () => {
const port = await conn.getInnerPort();
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]
);
})();
},
});
}
}