2017-08-22 22:30:57 +00:00
|
|
|
// @flow
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as db from '../common/database';
|
2017-08-22 22:30:57 +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';
|
2016-10-02 20:57:00 +00:00
|
|
|
export const type = 'CookieJar';
|
|
|
|
export const prefix = 'jar';
|
2017-03-23 22:10:42 +00:00
|
|
|
export const canDuplicate = true;
|
2017-08-22 22:30:57 +00:00
|
|
|
|
|
|
|
export type Cookie = {
|
2017-08-23 03:33:07 +00:00
|
|
|
id: string,
|
2017-08-22 22:30:57 +00:00
|
|
|
key: string,
|
|
|
|
value: string,
|
2017-11-01 11:23:22 +00:00
|
|
|
expires: Date | string | number | null,
|
|
|
|
domain: string,
|
|
|
|
path: string,
|
|
|
|
secure: boolean,
|
2017-08-22 23:54:31 +00:00
|
|
|
httpOnly: boolean,
|
2017-11-01 11:23:22 +00:00
|
|
|
|
|
|
|
extensions?: Array<any>,
|
|
|
|
creation?: Date,
|
|
|
|
creationIndex?: number,
|
|
|
|
hostOnly?: boolean,
|
|
|
|
pathIsDefault?: boolean,
|
|
|
|
lastAccessed?: Date
|
2017-08-22 22:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BaseCookieJar = {
|
|
|
|
name: string,
|
|
|
|
cookies: Array<Cookie>
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CookieJar = BaseModel & BaseCookieJar;
|
|
|
|
|
2016-10-02 20:57:00 +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',
|
|
|
|
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
|
|
|
|
2017-08-22 22:30:57 +00:00
|
|
|
export function migrate (doc: CookieJar): CookieJar {
|
2017-08-23 03:33:07 +00:00
|
|
|
doc = migrateCookieId(doc);
|
2016-11-22 19:42:10 +00:00
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
2017-08-22 22:30:57 +00:00
|
|
|
export function create (patch: Object = {}) {
|
2017-11-10 10:50:39 +00:00
|
|
|
if (!patch.parentId) {
|
|
|
|
throw new Error(`New CookieJar missing \`parentId\`: ${JSON.stringify(patch)}`);
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
return db.docCreate(type, patch);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-08-22 22:30:57 +00:00
|
|
|
export async function getOrCreateForParentId (parentId: string) {
|
2016-10-02 20:57:00 +00:00
|
|
|
const cookieJars = await db.find(type, {parentId});
|
|
|
|
if (cookieJars.length === 0) {
|
2017-03-03 20:09:08 +00:00
|
|
|
return await create({parentId});
|
2016-10-02 20:57:00 +00:00
|
|
|
} else {
|
|
|
|
return cookieJars[0];
|
|
|
|
}
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function all () {
|
|
|
|
return db.all(type);
|
|
|
|
}
|
2016-09-21 20:32:45 +00:00
|
|
|
|
2017-08-22 22:30:57 +00:00
|
|
|
export 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
|
|
|
|
2017-08-22 22:30:57 +00:00
|
|
|
export function update (cookieJar: CookieJar, patch: Object = {}) {
|
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 */
|
|
|
|
function migrateCookieId (cookieJar: CookieJar) {
|
|
|
|
for (const cookie of cookieJar.cookies) {
|
|
|
|
if (!cookie.id) {
|
2017-08-23 03:42:44 +00:00
|
|
|
cookie.id = Math.random().toString().replace('0.', '');
|
2017-08-23 03:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cookieJar;
|
|
|
|
}
|