mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-15 07:20:02 -04:00
move cookie logic to cookie.ts
This commit is contained in:
parent
ca427c830c
commit
d3337de575
2 changed files with 20 additions and 21 deletions
|
@ -15,6 +15,24 @@ export type Cookie = {
|
||||||
|
|
||||||
export class CookieStore {
|
export class CookieStore {
|
||||||
private cookies: Record<string, Cookie> = {};
|
private cookies: Record<string, Cookie> = {};
|
||||||
|
private db: IDBDatabase;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
// TOOD: Something is seriously broken with this system, I'm not sure what, but mutating multiple cookies on the same domain seems to break things?
|
||||||
|
const request = indexedDB.open("$scramjet", 1);
|
||||||
|
|
||||||
|
request.onsuccess = () => {
|
||||||
|
this.db = request.result;
|
||||||
|
|
||||||
|
const tx = this.db.transaction("cookies", "readonly");
|
||||||
|
const store = tx.objectStore("cookies");
|
||||||
|
const req = store.get("cookies");
|
||||||
|
|
||||||
|
req.onsuccess = () => {
|
||||||
|
if (req.result) this.cookies = req.result;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async setCookies(cookies: string[], url: URL) {
|
async setCookies(cookies: string[], url: URL) {
|
||||||
for (const str of cookies) {
|
for (const str of cookies) {
|
||||||
|
@ -35,6 +53,7 @@ export class CookieStore {
|
||||||
|
|
||||||
const id = `${cookie.domain}@${cookie.path}@${cookie.name}`;
|
const id = `${cookie.domain}@${cookie.path}@${cookie.name}`;
|
||||||
this.cookies[id] = cookie;
|
this.cookies[id] = cookie;
|
||||||
|
this.db.transaction("cookies", "readwrite").objectStore("cookies").put(this.cookies, "cookies");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,22 +19,6 @@ export class ScramjetServiceWorker extends EventTarget {
|
||||||
super();
|
super();
|
||||||
this.client = new $scramjet.shared.util.BareClient();
|
this.client = new $scramjet.shared.util.BareClient();
|
||||||
|
|
||||||
const db = indexedDB.open("$scramjet", 1);
|
|
||||||
|
|
||||||
db.onsuccess = () => {
|
|
||||||
const res = db.result;
|
|
||||||
const tx = res.transaction("cookies", "readonly");
|
|
||||||
const store = tx.objectStore("cookies");
|
|
||||||
const cookies = store.get("cookies");
|
|
||||||
|
|
||||||
cookies.onsuccess = () => {
|
|
||||||
if (cookies.result) {
|
|
||||||
this.cookieStore.load(cookies.result);
|
|
||||||
dbg.log("Loaded cookies from IDB!");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
addEventListener("message", async ({ data }: { data: MessageC2W }) => {
|
addEventListener("message", async ({ data }: { data: MessageC2W }) => {
|
||||||
if (!("scramjet$type" in data)) return;
|
if (!("scramjet$type" in data)) return;
|
||||||
|
|
||||||
|
@ -45,11 +29,7 @@ export class ScramjetServiceWorker extends EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.scramjet$type === "cookie") {
|
if (data.scramjet$type === "cookie") {
|
||||||
await this.cookieStore.setCookies([data.cookie], new URL(data.url));
|
this.cookieStore.setCookies([data.cookie], new URL(data.url));
|
||||||
const res = db.result;
|
|
||||||
const tx = res.transaction("cookies", "readwrite");
|
|
||||||
const store = tx.objectStore("cookies");
|
|
||||||
store.put(this.cookieStore.dump(), "cookies");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue