properly proxy worker postmessae

This commit is contained in:
velzie 2024-09-02 16:21:48 -04:00
parent 35eb307d4d
commit 4f5ae7b18a
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
4 changed files with 36 additions and 5 deletions

View file

@ -98,9 +98,13 @@ export class ScramjetClient {
} else { } else {
this.bare = new BareClient( this.bare = new BareClient(
new Promise((resolve) => { new Promise((resolve) => {
addEventListener("message", (e) => { addEventListener("message", ({ data }) => {
if (e.data instanceof MessagePort) { if (typeof data !== "object") return;
resolve(e.data); if (
"$scramjet$type" in data &&
data.$scramjet$type === "baremuxinit"
) {
resolve(data.port);
} }
}); });
}) })

View file

@ -1,3 +1,4 @@
import { iswindow } from "..";
import { SCRAMJETCLIENT } from "../../symbols"; import { SCRAMJETCLIENT } from "../../symbols";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { POLLUTANT } from "../shared/realm"; import { POLLUTANT } from "../shared/realm";
@ -32,6 +33,7 @@ export default function (client: ScramjetClient) {
); );
ctx.args[0] = { ctx.args[0] = {
$scramjet$messagetype: "window",
$scramjet$origin: callerClient.url.origin, $scramjet$origin: callerClient.url.origin,
$scramjet$data: ctx.args[0], $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],
};
},
});
} }

View file

@ -23,6 +23,8 @@ export default function (client: ScramjetClient, self: Self) {
return this.ports; return this.ports;
}, },
source() { source() {
if (this.source === null) return null;
const scram: ScramjetClient = this.source[SCRAMJETCLIENT]; const scram: ScramjetClient = this.source[SCRAMJETCLIENT];
if (scram) return scram.globalProxy; 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) if (typeof this.data === "object" && "$scramjet$origin" in this.data)
return this.data.$scramjet$origin; return this.data.$scramjet$origin;
// then it must be from a worker, which we aren't currently rewriting
return client.url.origin; return client.url.origin;
}, },
data() { data() {

View file

@ -33,9 +33,16 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
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();
worker.postMessage(port, [port]); worker.postMessage(
{
$scramjet$type: "baremuxinit",
port,
},
[port]
);
})(); })();
}, },
}); });