This commit is contained in:
velzie 2024-08-10 15:03:54 -04:00
parent 7b8d5d09b2
commit 3bc8dd34b9
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
14 changed files with 144 additions and 93 deletions

View file

@ -1,37 +1,12 @@
import { parse } from "set-cookie-parser";
import { ScramjetClient } from "../client";
import IDBMapSync from "@webreflection/idb-map/sync";
export default function (client: ScramjetClient, self: typeof window) {
let cookieStore = new IDBMapSync(client.url.host, {
durability: "relaxed",
prefix: "Cookies",
});
client.Trap("Document.prototype.cookie", {
get(ctx) {
let cookies = cookieStore.entries();
if (client.url.protocol !== "https:") {
cookies = cookies.filter(([_k, v]) => !v.args.includes(["Secure"]));
}
cookies = cookies.filter(([_k, v]) => !v.args.includes(["HttpOnly"]));
cookies = Array.from(cookies.map(([k, v]) => `${k}=${v.value}`));
return cookies.join("; ");
get() {
return client.cookieStore.getCookies(client.url, true);
},
set(ctx, value: string) {
// dbg.debug("setting cookie", value);
const cookie = parse(value)[0];
let date = new Date();
let expires = cookie.expires;
// dbg.error("expires", expires);
// if (expires instanceof Date) {
// if (isNaN(expires.getTime())) return;
// if (expires.getTime() < date.getTime()) return;
// }
// set.call(document, `${cookie.name}=${cookie.value}`);
client.cookieStore.setCookies([value], client.url);
},
});