mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-15 07:20:02 -04:00
Revert "move cookie logic to cookie.ts"
This reverts commit d3337de575
.
This commit is contained in:
parent
3678965721
commit
cb3c433b69
2 changed files with 21 additions and 23 deletions
|
@ -15,26 +15,8 @@ export type Cookie = {
|
||||||
|
|
||||||
export class CookieStore {
|
export class CookieStore {
|
||||||
private cookies: Record<string, Cookie> = {};
|
private cookies: Record<string, Cookie> = {};
|
||||||
private db: IDBDatabase;
|
|
||||||
|
|
||||||
constructor() {
|
setCookies(cookies: string[], url: URL) {
|
||||||
// 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) {
|
|
||||||
for (const str of cookies) {
|
for (const str of cookies) {
|
||||||
const parsed = parse(str);
|
const parsed = parse(str);
|
||||||
const domain = parsed.domain;
|
const domain = parsed.domain;
|
||||||
|
@ -53,10 +35,6 @@ 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,6 +19,22 @@ 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;
|
||||||
|
|
||||||
|
@ -30,6 +46,10 @@ export class ScramjetServiceWorker extends EventTarget {
|
||||||
|
|
||||||
if (data.scramjet$type === "cookie") {
|
if (data.scramjet$type === "cookie") {
|
||||||
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