18 lines
1.1 KiB
JavaScript
18 lines
1.1 KiB
JavaScript
export function cookieId(cookie) {
|
|
return JSON.stringify([cookie.name, cookie.domain, cookie.path, !!cookie.hostOnly, !!cookie.secure]);
|
|
}
|
|
export async function listCookies(session) {
|
|
return (await session.cookies.get({})).map(cookie => ({ id: cookieId(cookie), name: cookie.name,
|
|
domain: cookie.domain, path: cookie.path, secure: cookie.secure, httpOnly: cookie.httpOnly }));
|
|
}
|
|
export async function removeCookie(session, id) {
|
|
if (typeof id !== 'string' || id.length > 8192) throw new Error('Invalid cookie selection.');
|
|
const cookie = (await session.cookies.get({})).find(item => cookieId(item) === id);
|
|
if (!cookie) throw new Error('This cookie no longer exists. Refresh the list.');
|
|
// Expire the exact domain/path tuple. cookies.remove(url, name) can match other paths.
|
|
await session.cookies.set({ url: `${cookie.secure ? 'https' : 'http'}://${cookie.domain.replace(/^\./, '')}${cookie.path}`,
|
|
name: cookie.name, value: '', ...(cookie.hostOnly ? {} : { domain: cookie.domain }), path: cookie.path,
|
|
secure: cookie.secure, httpOnly: cookie.httpOnly, sameSite: cookie.sameSite, expirationDate: 1 });
|
|
await session.cookies.flushStore();
|
|
}
|