mirror of
https://github.com/wukko/cobalt.git
synced 2024-11-15 12:50:01 +00:00
api/cookie/manager: pass cookiePath
to writeChanges()
also reordered functions to maintain the hierarchy
This commit is contained in:
parent
7798844755
commit
7c516c0468
1 changed files with 20 additions and 19 deletions
|
@ -1,26 +1,15 @@
|
||||||
import Cookie from './cookie.js';
|
import Cookie from './cookie.js';
|
||||||
|
|
||||||
import { readFile, writeFile } from 'fs/promises';
|
import { readFile, writeFile } from 'fs/promises';
|
||||||
import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser';
|
|
||||||
import { Green, Yellow } from '../../misc/console-text.js';
|
import { Green, Yellow } from '../../misc/console-text.js';
|
||||||
|
import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser';
|
||||||
|
|
||||||
const WRITE_INTERVAL = 60000,
|
const WRITE_INTERVAL = 60000,
|
||||||
COUNTER = Symbol('counter');
|
COUNTER = Symbol('counter');
|
||||||
|
|
||||||
let cookies = {}, dirty = false, intervalId;
|
let cookies = {}, dirty = false, intervalId;
|
||||||
|
|
||||||
export const setup = async (cookiePath) => {
|
function writeChanges(cookiePath) {
|
||||||
try {
|
|
||||||
cookies = await readFile(cookiePath, 'utf8');
|
|
||||||
cookies = JSON.parse(cookies);
|
|
||||||
intervalId = setInterval(writeChanges, WRITE_INTERVAL);
|
|
||||||
console.log(`${Green('[✓]')} cookies loaded successfully!`)
|
|
||||||
} catch(e) {
|
|
||||||
console.error(`${Yellow('[!]')} failed to load cookies.`);
|
|
||||||
console.error('error:', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function writeChanges() {
|
|
||||||
if (!dirty) return;
|
if (!dirty) return;
|
||||||
dirty = false;
|
dirty = false;
|
||||||
|
|
||||||
|
@ -29,6 +18,18 @@ function writeChanges() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const setup = async (cookiePath) => {
|
||||||
|
try {
|
||||||
|
cookies = await readFile(cookiePath, 'utf8');
|
||||||
|
cookies = JSON.parse(cookies);
|
||||||
|
intervalId = setInterval(() => writeChanges(cookiePath), WRITE_INTERVAL);
|
||||||
|
console.log(`${Green('[✓]')} cookies loaded successfully!`)
|
||||||
|
} catch(e) {
|
||||||
|
console.error(`${Yellow('[!]')} failed to load cookies.`);
|
||||||
|
console.error('error:', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function getCookie(service) {
|
export function getCookie(service) {
|
||||||
if (!cookies[service] || !cookies[service].length) return;
|
if (!cookies[service] || !cookies[service].length) return;
|
||||||
|
|
||||||
|
@ -46,6 +47,11 @@ export function getCookie(service) {
|
||||||
return cookies[service][n]
|
return cookies[service][n]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function updateCookieValues(cookie, values) {
|
||||||
|
cookie.set(values);
|
||||||
|
if (Object.keys(values).length) dirty = true
|
||||||
|
}
|
||||||
|
|
||||||
export function updateCookie(cookie, headers) {
|
export function updateCookie(cookie, headers) {
|
||||||
if (!cookie) return;
|
if (!cookie) return;
|
||||||
|
|
||||||
|
@ -58,8 +64,3 @@ export function updateCookie(cookie, headers) {
|
||||||
parsed.filter(c => !c.expires || c.expires > new Date()).forEach(c => values[c.name] = c.value);
|
parsed.filter(c => !c.expires || c.expires > new Date()).forEach(c => values[c.name] = c.value);
|
||||||
updateCookieValues(cookie, values);
|
updateCookieValues(cookie, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateCookieValues(cookie, values) {
|
|
||||||
cookie.set(values);
|
|
||||||
if (Object.keys(values).length) dirty = true
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue