File size: 2,015 Bytes
bee6636 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
// thnank you node unblocker guy
import parse from "set-cookie-parser";
export type Cookie = {
name: string;
value: string;
path?: string;
expires?: string;
maxAge?: number;
domain?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: "strict" | "lax" | "none";
};
export class CookieStore {
private cookies: Record<string, Cookie> = {};
setCookies(cookies: string[], url: URL) {
for (const str of cookies) {
const parsed = parse(str);
const domain = parsed.domain;
const sameSite = parsed.sameSite;
const cookie: Cookie = {
domain,
sameSite,
...parsed[0],
};
if (!cookie.domain) cookie.domain = "." + url.hostname;
if (!cookie.domain.startsWith(".")) cookie.domain = "." + cookie.domain;
if (!cookie.path) cookie.path = "/";
if (!cookie.sameSite) cookie.sameSite = "lax";
if (cookie.expires) cookie.expires = cookie.expires.toString();
const id = `${cookie.domain}@${cookie.path}@${cookie.name}`;
this.cookies[id] = cookie;
}
}
getCookies(url: URL, fromJs: boolean): string {
const now = new Date();
const cookies = Object.values(this.cookies);
const validCookies: Cookie[] = [];
for (const cookie of cookies) {
if (cookie.expires && new Date(cookie.expires) < now) {
delete this.cookies[`${cookie.domain}@${cookie.path}@${cookie.name}`];
continue;
}
if (cookie.secure && url.protocol !== "https:") continue;
if (cookie.httpOnly && fromJs) continue;
if (!url.pathname.startsWith(cookie.path)) continue;
if (cookie.domain.startsWith(".")) {
if (!url.hostname.endsWith(cookie.domain.slice(1))) continue;
}
validCookies.push(cookie);
}
return validCookies
.map((cookie) => `${cookie.name}=${cookie.value}`)
.join("; ");
}
load(cookies: string) {
if (typeof cookies === "object") return cookies;
this.cookies = JSON.parse(cookies);
}
dump(): string {
return JSON.stringify(this.cookies);
}
}
|