From 5a5a65b3734554b47804ef5584dabbcab442bd93 Mon Sep 17 00:00:00 2001 From: jj Date: Sun, 27 Oct 2024 18:10:57 +0000 Subject: [PATCH] api/cookies: trigger cookie load from api entrypoint --- api/src/core/api.js | 5 +++++ api/src/processing/cookie/manager.js | 10 ++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/api/src/core/api.js b/api/src/core/api.js index b3123033..4811cd40 100644 --- a/api/src/core/api.js +++ b/api/src/core/api.js @@ -18,6 +18,7 @@ import { friendlyServiceName } from "../processing/service-alias.js"; import { verifyStream, getInternalStream } from "../stream/manage.js"; import { createResponse, normalizeRequest, getIP } from "../processing/request.js"; import * as APIKeys from "../security/api-keys.js"; +import * as Cookies from "../processing/cookie/manager.js"; const git = { branch: await getBranch(), @@ -348,6 +349,10 @@ export const runAPI = (express, app, __dirname) => { APIKeys.setup(env.apiKeyURL); } + if (env.cookiePath) { + Cookies.setup(env.cookiePath); + } + app.listen(env.apiPort, env.listenAddress, () => { console.log(`\n` + Bright(Cyan("cobalt ")) + Bright("API ^ω⁠^") + "\n" + diff --git a/api/src/processing/cookie/manager.js b/api/src/processing/cookie/manager.js index 25bf9c90..3afb4bfd 100644 --- a/api/src/processing/cookie/manager.js +++ b/api/src/processing/cookie/manager.js @@ -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;