mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 15:00:01 -04:00
properly proxy worker postmessae
This commit is contained in:
parent
35eb307d4d
commit
4f5ae7b18a
4 changed files with 36 additions and 5 deletions
|
@ -98,9 +98,13 @@ export class ScramjetClient {
|
|||
} else {
|
||||
this.bare = new BareClient(
|
||||
new Promise((resolve) => {
|
||||
addEventListener("message", (e) => {
|
||||
if (e.data instanceof MessagePort) {
|
||||
resolve(e.data);
|
||||
addEventListener("message", ({ data }) => {
|
||||
if (typeof data !== "object") return;
|
||||
if (
|
||||
"$scramjet$type" in data &&
|
||||
data.$scramjet$type === "baremuxinit"
|
||||
) {
|
||||
resolve(data.port);
|
||||
}
|
||||
});
|
||||
})
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { iswindow } from "..";
|
||||
import { SCRAMJETCLIENT } from "../../symbols";
|
||||
import { ScramjetClient } from "../client";
|
||||
import { POLLUTANT } from "../shared/realm";
|
||||
|
@ -32,6 +33,7 @@ export default function (client: ScramjetClient) {
|
|||
);
|
||||
|
||||
ctx.args[0] = {
|
||||
$scramjet$messagetype: "window",
|
||||
$scramjet$origin: callerClient.url.origin,
|
||||
$scramjet$data: ctx.args[0],
|
||||
};
|
||||
|
@ -44,4 +46,21 @@ export default function (client: ScramjetClient) {
|
|||
);
|
||||
},
|
||||
});
|
||||
|
||||
const toproxy = [
|
||||
"Worker.prototype.postMessage",
|
||||
"MessagePort.prototype.postMessage",
|
||||
];
|
||||
if (!iswindow) toproxy.push("self.postMessage"); // only do the generic version if we're in a worker
|
||||
|
||||
client.Proxy(toproxy, {
|
||||
apply(ctx) {
|
||||
// origin/source doesn't need to be preserved - it's null in the message event
|
||||
|
||||
ctx.args[0] = {
|
||||
$scramjet$messagetype: "worker",
|
||||
$scramjet$data: ctx.args[0],
|
||||
};
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
@ -23,6 +23,8 @@ export default function (client: ScramjetClient, self: Self) {
|
|||
return this.ports;
|
||||
},
|
||||
source() {
|
||||
if (this.source === null) return null;
|
||||
|
||||
const scram: ScramjetClient = this.source[SCRAMJETCLIENT];
|
||||
|
||||
if (scram) return scram.globalProxy;
|
||||
|
@ -33,7 +35,6 @@ export default function (client: ScramjetClient, self: Self) {
|
|||
if (typeof this.data === "object" && "$scramjet$origin" in this.data)
|
||||
return this.data.$scramjet$origin;
|
||||
|
||||
// then it must be from a worker, which we aren't currently rewriting
|
||||
return client.url.origin;
|
||||
},
|
||||
data() {
|
||||
|
|
|
@ -33,9 +33,16 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
|
|||
|
||||
const worker = call();
|
||||
const conn = new BareMuxConnection();
|
||||
|
||||
(async () => {
|
||||
const port = await conn.getInnerPort();
|
||||
worker.postMessage(port, [port]);
|
||||
worker.postMessage(
|
||||
{
|
||||
$scramjet$type: "baremuxinit",
|
||||
port,
|
||||
},
|
||||
[port]
|
||||
);
|
||||
})();
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue