mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
fix: worker proxying
This commit is contained in:
parent
3a7a7e95a5
commit
2a943adf91
1 changed files with 15 additions and 49 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import { iswindow } from "..";
|
||||||
import { BareMuxConnection } from "../../shared";
|
import { BareMuxConnection } from "../../shared";
|
||||||
import { encodeUrl } from "../../shared/rewriters/url";
|
import { encodeUrl } from "../../shared/rewriters/url";
|
||||||
import type { MessageC2W } from "../../worker";
|
import type { MessageC2W } from "../../worker";
|
||||||
|
@ -6,7 +7,7 @@ import { ScramjetClient } from "../client";
|
||||||
const workerpostmessage = Worker.prototype.postMessage;
|
const workerpostmessage = Worker.prototype.postMessage;
|
||||||
|
|
||||||
export default function (client: ScramjetClient, self: typeof globalThis) {
|
export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
client.Proxy("Worker", {
|
const handler = {
|
||||||
construct({ args, call }) {
|
construct({ args, call }) {
|
||||||
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:")) {
|
||||||
|
@ -48,57 +49,22 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
);
|
);
|
||||||
})();
|
})();
|
||||||
},
|
},
|
||||||
});
|
};
|
||||||
|
|
||||||
client.Proxy("Worklet.prototype.addModule", {
|
if (self.Worker) {
|
||||||
apply(ctx) {
|
client.Proxy("Worker", handler);
|
||||||
if (ctx.args[0]) ctx.args[0] = encodeUrl(ctx.args[0], client.meta);
|
}
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
client.Proxy("SharedWorker", {
|
if (iswindow) {
|
||||||
construct({ args, call }) {
|
client.Proxy("Worklet.prototype.addModule", {
|
||||||
if (args[0] instanceof URL) args[0] = args[0].href;
|
apply(ctx) {
|
||||||
if (args[0].startsWith("blob:") || args[0].startsWith("data:")) {
|
if (ctx.args[0]) ctx.args[0] = encodeUrl(ctx.args[0], client.meta);
|
||||||
const data = syncfetch(client, args[0]);
|
},
|
||||||
const id = Math.random().toString(8).slice(5);
|
});
|
||||||
|
|
||||||
args[0] = "/scramjet/worker?id=" + id;
|
// sharedworkers can only be constructed from window
|
||||||
if (args[1] && args[1].type === "module") {
|
client.Proxy("SharedWorker", handler);
|
||||||
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