mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-17 16:10: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);
|
const module = context(key);
|
||||||
if (!key.endsWith(".ts")) continue;
|
if (!key.endsWith(".ts")) continue;
|
||||||
if (
|
if (
|
||||||
(key.startsWith("./dom/") && "window" in self) ||
|
(key.startsWith("./dom/") && "window" in this.global) ||
|
||||||
(key.startsWith("./worker/") && "WorkerGlobalScope" in self) ||
|
(key.startsWith("./worker/") && "WorkerGlobalScope" in this.global) ||
|
||||||
key.startsWith("./shared/")
|
key.startsWith("./shared/")
|
||||||
) {
|
) {
|
||||||
modules.push(module);
|
modules.push(module);
|
||||||
|
|
|
@ -10,22 +10,22 @@ if (self.Worker) {
|
||||||
workerpostmessage = Worker.prototype.postMessage;
|
workerpostmessage = Worker.prototype.postMessage;
|
||||||
}
|
}
|
||||||
export default function (client: ScramjetClient, self: typeof globalThis) {
|
export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
const handler = {
|
if (self.Worker) {
|
||||||
construct({ args, call }) {
|
client.Proxy("Worker", {
|
||||||
if (args[0] instanceof URL) args[0] = args[0].href;
|
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") {
|
if (args[1] && args[1].type === "module") {
|
||||||
args[0] += "&type=module";
|
args[0] += "&type=module";
|
||||||
}
|
}
|
||||||
|
|
||||||
const worker = call();
|
const worker = call();
|
||||||
const conn = new BareMuxConnection();
|
const conn = new BareMuxConnection();
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const port = await conn.getInnerPort();
|
const port = await conn.getInnerPort();
|
||||||
if (worker instanceof Worker) {
|
|
||||||
workerpostmessage.call(
|
workerpostmessage.call(
|
||||||
worker,
|
worker,
|
||||||
{
|
{
|
||||||
|
@ -34,23 +34,9 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
},
|
},
|
||||||
[port]
|
[port]
|
||||||
);
|
);
|
||||||
}
|
})();
|
||||||
if (worker instanceof SharedWorker) {
|
},
|
||||||
sharedworkerpostmessage.call(
|
});
|
||||||
worker.port,
|
|
||||||
{
|
|
||||||
$scramjet$type: "baremuxinit",
|
|
||||||
port,
|
|
||||||
},
|
|
||||||
[port]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
if (self.Worker) {
|
|
||||||
client.Proxy("Worker", handler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iswindow) {
|
if (iswindow) {
|
||||||
|
@ -61,6 +47,40 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// sharedworkers can only be constructed from window
|
// 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