mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 22:40:01 -04:00
feat: improve sharedworker compatibility
This commit is contained in:
parent
7c9a9e5441
commit
13e656e0dc
3 changed files with 41 additions and 18 deletions
|
@ -63,10 +63,9 @@ export default function (client: ScramjetClient) {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const toproxy = [
|
const toproxy = ["MessagePort.prototype.postMessage"];
|
||||||
"Worker.prototype.postMessage",
|
|
||||||
"MessagePort.prototype.postMessage",
|
if (self.Worker) toproxy.push("Worker.prototype.postMessage");
|
||||||
];
|
|
||||||
if (!iswindow) toproxy.push("self.postMessage"); // only do the generic version if we're in a worker
|
if (!iswindow) toproxy.push("self.postMessage"); // only do the generic version if we're in a worker
|
||||||
|
|
||||||
client.Proxy(toproxy, {
|
client.Proxy(toproxy, {
|
||||||
|
|
|
@ -1,9 +1,18 @@
|
||||||
import { config, decodeUrl, encodeUrl, rewriteHeaders } from "../../../shared";
|
import { config, decodeUrl, encodeUrl } from "../../../shared";
|
||||||
import { ScramjetClient } from "../../client";
|
import { ScramjetClient } from "../../client";
|
||||||
const nativeworker = Worker;
|
let nativeworker;
|
||||||
const postmessage = Worker.prototype.postMessage;
|
let postmessage;
|
||||||
|
|
||||||
|
if (self.Worker) {
|
||||||
|
nativeworker = Worker;
|
||||||
|
postmessage = Worker.prototype.postMessage;
|
||||||
|
}
|
||||||
|
|
||||||
export default function (client: ScramjetClient, self: Self) {
|
export default function (client: ScramjetClient, self: Self) {
|
||||||
const worker = new nativeworker(config.sync);
|
let worker;
|
||||||
|
if (self.Worker) {
|
||||||
|
worker = new nativeworker(config.sync);
|
||||||
|
}
|
||||||
const ARGS = Symbol("xhr original args");
|
const ARGS = Symbol("xhr original args");
|
||||||
const HEADERS = Symbol("xhr headers");
|
const HEADERS = Symbol("xhr headers");
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,11 @@ import { encodeUrl } from "../../shared/rewriters/url";
|
||||||
import type { MessageC2W } from "../../worker";
|
import type { MessageC2W } from "../../worker";
|
||||||
import { ScramjetClient } from "../client";
|
import { ScramjetClient } from "../client";
|
||||||
|
|
||||||
const workerpostmessage = Worker.prototype.postMessage;
|
const sharedworkerpostmessage = MessagePort.prototype.postMessage;
|
||||||
|
let workerpostmessage;
|
||||||
|
if (self.Worker) {
|
||||||
|
workerpostmessage = Worker.prototype.postMessage;
|
||||||
|
}
|
||||||
export default function (client: ScramjetClient, self: typeof globalThis) {
|
export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
const handler = {
|
const handler = {
|
||||||
construct({ args, call }) {
|
construct({ args, call }) {
|
||||||
|
@ -39,6 +42,7 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const port = await conn.getInnerPort();
|
const port = await conn.getInnerPort();
|
||||||
|
if (worker instanceof Worker) {
|
||||||
workerpostmessage.call(
|
workerpostmessage.call(
|
||||||
worker,
|
worker,
|
||||||
{
|
{
|
||||||
|
@ -47,6 +51,17 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
||||||
},
|
},
|
||||||
[port]
|
[port]
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
if (worker instanceof SharedWorker) {
|
||||||
|
sharedworkerpostmessage.call(
|
||||||
|
worker.port,
|
||||||
|
{
|
||||||
|
$scramjet$type: "baremuxinit",
|
||||||
|
port,
|
||||||
|
},
|
||||||
|
[port]
|
||||||
|
);
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue