mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 14:30:02 -04:00
cookie communication
This commit is contained in:
parent
bac00de12b
commit
c1aecdf3a8
3 changed files with 32 additions and 7 deletions
|
@ -1,12 +1,28 @@
|
|||
import { ScramjetClient } from "../client";
|
||||
|
||||
export default function (client: ScramjetClient, self: typeof window) {
|
||||
client.serviceworker.addEventListener("message", ({ data }) => {
|
||||
if (!("scramjet$type" in data)) return;
|
||||
|
||||
if (data.scramjet$type === "cookie") {
|
||||
this.cookieStore.setCookies([data.cookie], new URL(data.url));
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
client.Trap("Document.prototype.cookie", {
|
||||
get() {
|
||||
return client.cookieStore.getCookies(client.url, true);
|
||||
},
|
||||
set(ctx, value: string) {
|
||||
client.cookieStore.setCookies([value], client.url);
|
||||
|
||||
// TODO hardcode because scoping whatever
|
||||
client.serviceworker.controller!.postMessage({
|
||||
scramjet$type: "cookie",
|
||||
cookie: value,
|
||||
url: client.url.href,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -16,8 +16,7 @@ dbg.log("scrammin");
|
|||
if (!(ScramjetClient.SCRAMJET in self)) {
|
||||
const client = new ScramjetClient(self);
|
||||
|
||||
client.loadcookies(self.COOKIE);
|
||||
delete self.COOKIE;
|
||||
if (self.COOKIE) client.loadcookies(self.COOKIE);
|
||||
|
||||
client.hook();
|
||||
|
||||
|
|
|
@ -31,12 +31,16 @@ export class ScramjetServiceWorker {
|
|||
|
||||
if (data.scramjet$type === "registerServiceWorker") {
|
||||
this.serviceWorkers.push(new FakeServiceWorker(data.port, data.origin));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const resolve = this.syncPool[data.scramjet$token];
|
||||
delete this.syncPool[data.scramjet$token];
|
||||
if (data.scramjet$type === "cookie") {
|
||||
this.cookieStore.setCookies([data.cookie], new URL(data.url));
|
||||
return;
|
||||
}
|
||||
|
||||
// const resolve = this.syncPool[data.scramjet$token];
|
||||
// delete this.syncPool[data.scramjet$token];
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -119,13 +123,19 @@ type RegisterServiceWorkerMessage = {
|
|||
origin: string;
|
||||
};
|
||||
|
||||
type CookieMessage = {
|
||||
scramjet$type: "cookie";
|
||||
cookie: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
type MessageCommon = {
|
||||
scramjet$type: string;
|
||||
scramjet$token: number;
|
||||
};
|
||||
|
||||
type MessageTypeC2W = RegisterServiceWorkerMessage;
|
||||
type MessageTypeW2C = never;
|
||||
type MessageTypeC2W = RegisterServiceWorkerMessage | CookieMessage;
|
||||
type MessageTypeW2C = CookieMessage;
|
||||
|
||||
// c2w: client to (service) worker
|
||||
export type MessageC2W = MessageCommon & MessageTypeC2W;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue