Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
yea
  • Loading branch information
Sticks committed Sep 16, 2024
1 parent 3832c2b commit cd06223
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions api/src/processing/cookie/manager.js
Expand Up @@ -4,20 +4,27 @@ import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser';
import { env } from '../../config.js';

const WRITE_INTERVAL = 60000,
cookiePath = env.cookiePath,
COUNTER = Symbol('counter');
cookiePath = env.cookiePath,
COUNTER = Symbol('counter');

let cookies = {}, dirty = false, intervalId;
let cookies = {},
dirty = false,
intervalId;

const setup = async () => {
try {
console.log('Loading cookies from', cookiePath);
if (!cookiePath) return;

cookies = await readFile(cookiePath, 'utf8');
cookies = JSON.parse(cookies);
intervalId = setInterval(writeChanges, WRITE_INTERVAL)
} catch { /* no cookies for you */ }
}
intervalId = setInterval(writeChanges, WRITE_INTERVAL);

console.log('Loaded cookies! Cookie dump:', cookies);
} catch (e) {
console.log(`Failed to load cookies: ${e}`);
}
};

setup();

Expand All @@ -26,41 +33,49 @@ function writeChanges() {
dirty = false;

writeFile(cookiePath, JSON.stringify(cookies, null, 4)).catch(() => {
clearInterval(intervalId)
})
clearInterval(intervalId);
});
}

export function getCookie(service) {
console.log('Getting cookie for', service);
if (!cookies[service] || !cookies[service].length) return;

console.log('Cookies:', cookies[service]);
let n;
if (cookies[service][COUNTER] === undefined) {
n = cookies[service][COUNTER] = 0
n = cookies[service][COUNTER] = 0;
} else {
++cookies[service][COUNTER]
n = (cookies[service][COUNTER] %= cookies[service].length)
++cookies[service][COUNTER];
n = cookies[service][COUNTER] %= cookies[service].length;
}

const cookie = cookies[service][n];
if (typeof cookie === 'string') cookies[service][n] = Cookie.fromString(cookie);
if (typeof cookie === 'string')
cookies[service][n] = Cookie.fromString(cookie);

return cookies[service][n]
return cookies[service][n];
}

export function updateCookie(cookie, headers) {
if (!cookie) return;

const parsed = parseSetCookie(
splitCookiesString(headers.get('set-cookie')),
{ decodeValues: false }
), values = {}
splitCookiesString(headers.get('set-cookie')),
{ decodeValues: false },
),
values = {};

cookie.unset(parsed.filter(c => c.expires < new Date()).map(c => c.name));
parsed.filter(c => !c.expires || c.expires > new Date()).forEach(c => values[c.name] = c.value);
cookie.unset(
parsed.filter((c) => c.expires < new Date()).map((c) => c.name),
);
parsed
.filter((c) => !c.expires || c.expires > new Date())
.forEach((c) => (values[c.name] = c.value));
updateCookieValues(cookie, values);
}

export function updateCookieValues(cookie, values) {
cookie.set(values);
if (Object.keys(values).length) dirty = true
if (Object.keys(values).length) dirty = true;
}

0 comments on commit cd06223

Please sign in to comment.