api/cookies: trigger cookie load from api entrypoint

This commit is contained in:
jj
2024-10-27 18:10:57 +00:00
parent af50852815
commit 5a5a65b373
2 changed files with 7 additions and 8 deletions

View File

@@ -1,26 +1,20 @@
import Cookie from './cookie.js';
import { readFile, writeFile } from 'fs/promises';
import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser';
import { env } from '../../config.js';
const WRITE_INTERVAL = 60000,
cookiePath = env.cookiePath,
COUNTER = Symbol('counter');
let cookies = {}, dirty = false, intervalId;
const setup = async () => {
export const setup = async (cookiePath) => {
try {
if (!cookiePath) return;
cookies = await readFile(cookiePath, 'utf8');
cookies = JSON.parse(cookies);
intervalId = setInterval(writeChanges, WRITE_INTERVAL)
intervalId = setInterval(writeChanges, WRITE_INTERVAL);
} catch { /* no cookies for you */ }
}
setup();
function writeChanges() {
if (!dirty) return;
dirty = false;