2019-04-18 00:50:03 +00:00
|
|
|
import crypto from 'crypto';
|
2021-07-22 23:04:56 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
import { database as db } from '../common/database';
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { BaseModel } from './index';
|
2017-11-10 10:50:39 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
export const name = 'Cookie Jar';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'CookieJar';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export const prefix = 'jar';
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = true;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
export const canSync = false;
|
2017-08-22 22:30:57 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export interface Cookie {
|
|
|
|
id: string;
|
|
|
|
key: string;
|
|
|
|
value: string;
|
|
|
|
expires: Date | string | number | null;
|
|
|
|
domain: string;
|
|
|
|
path: string;
|
|
|
|
secure: boolean;
|
|
|
|
httpOnly: boolean;
|
2021-05-18 20:32:18 +00:00
|
|
|
extensions?: any[];
|
2021-05-12 06:35:00 +00:00
|
|
|
creation?: Date;
|
|
|
|
creationIndex?: number;
|
|
|
|
hostOnly?: boolean;
|
|
|
|
pathIsDefault?: boolean;
|
|
|
|
lastAccessed?: Date;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BaseCookieJar {
|
|
|
|
name: string;
|
2021-05-18 20:32:18 +00:00
|
|
|
cookies: Cookie[];
|
2021-05-12 06:35:00 +00:00
|
|
|
}
|
2017-08-22 22:30:57 +00:00
|
|
|
|
|
|
|
export type CookieJar = BaseModel & BaseCookieJar;
|
|
|
|
|
2021-06-16 19:19:00 +00:00
|
|
|
export const isCookieJar = (model: Pick<BaseModel, 'type'>): model is CookieJar => (
|
|
|
|
model.type === type
|
|
|
|
);
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export function init() {
|
2016-11-10 01:15:27 +00:00
|
|
|
return {
|
2016-10-02 20:57:00 +00:00
|
|
|
name: 'Default Jar',
|
2018-12-12 17:36:11 +00:00
|
|
|
cookies: [],
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export function migrate(doc: CookieJar) {
|
2017-08-23 03:33:07 +00:00
|
|
|
doc = migrateCookieId(doc);
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function create(patch: Partial<CookieJar>) {
|
2017-11-10 10:50:39 +00:00
|
|
|
if (!patch.parentId) {
|
2018-10-17 16:42:33 +00:00
|
|
|
throw new Error(`New CookieJar missing \`parentId\`: ${JSON.stringify(patch)}`);
|
2017-11-10 10:50:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
return db.docCreate<CookieJar>(type, patch);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function getOrCreateForParentId(parentId: string) {
|
2021-05-12 06:35:00 +00:00
|
|
|
const cookieJars = await db.find<CookieJar>(type, { parentId });
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
if (cookieJars.length === 0) {
|
2019-04-18 00:50:03 +00:00
|
|
|
return create({
|
|
|
|
parentId,
|
|
|
|
// Deterministic ID. It helps reduce sync complexity since we won't have to
|
|
|
|
// de-duplicate environments.
|
2021-05-12 06:35:00 +00:00
|
|
|
_id: `${prefix}_${crypto.createHash('sha1').update(parentId).digest('hex')}`,
|
2019-04-18 00:50:03 +00:00
|
|
|
});
|
2016-10-02 20:57:00 +00:00
|
|
|
} else {
|
|
|
|
return cookieJars[0];
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function all() {
|
|
|
|
return db.all<BaseModel>(type);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2019-04-18 00:50:03 +00:00
|
|
|
export async function getById(id: string) {
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.get(type, id);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
export async function update(cookieJar: CookieJar, patch: Partial<CookieJar> = {}) {
|
2016-09-21 20:32:45 +00:00
|
|
|
return db.docUpdate(cookieJar, patch);
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2017-08-23 03:33:07 +00:00
|
|
|
|
|
|
|
/** Ensure every cookie has an ID property */
|
2018-06-25 17:42:50 +00:00
|
|
|
function migrateCookieId(cookieJar: CookieJar) {
|
2017-08-23 03:33:07 +00:00
|
|
|
for (const cookie of cookieJar.cookies) {
|
|
|
|
if (!cookie.id) {
|
2021-05-12 06:35:00 +00:00
|
|
|
cookie.id = Math.random().toString().replace('0.', '');
|
2017-08-23 03:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-08-23 03:33:07 +00:00
|
|
|
return cookieJar;
|
|
|
|
}
|