-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
CookieStore.ts
134 lines (117 loc) · 3.72 KB
/
CookieStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { CookieChangeEvent } from './CookieChangeEvent.js';
import { parse } from './parse.js';
import {
Cookie,
CookieListItem,
CookieStoreDeleteOptions,
CookieStoreGetOptions,
} from './types.js';
export class CookieStore extends EventTarget {
onchange?: (event: CookieChangeEvent) => void;
private cookieMap = new Map<string, Cookie>();
get [Symbol.toStringTag](): 'CookieStore' {
return 'CookieStore';
}
constructor(cookieString: string) {
super();
this.cookieMap = parse(cookieString);
}
async get(
init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions,
): Promise<Cookie | undefined> {
if (init == null) {
throw new TypeError('CookieStoreGetOptions must not be empty');
} else if (init instanceof Object && !Object.keys(init).length) {
throw new TypeError('CookieStoreGetOptions must not be empty');
}
return (await this.getAll(init))[0];
}
async set(init: CookieListItem | string, possibleValue?: string): Promise<void> {
const item: CookieListItem = {
name: '',
value: '',
path: '/',
secure: false,
sameSite: 'strict',
expires: null,
domain: null,
httpOnly: false,
};
if (typeof init === 'string') {
item.name = init as string;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
item.value = possibleValue!;
} else {
Object.assign(item, init);
if (item.path && !item.path.startsWith('/')) {
throw new TypeError('Cookie path must start with "/"');
}
if (item.domain?.startsWith('.')) {
throw new TypeError('Cookie domain cannot start with "."');
}
if (item.name?.startsWith('__Host') && item.domain) {
throw new TypeError('Cookie domain must not be specified for host cookies');
}
if (item.name?.startsWith('__Host') && item.path !== '/') {
throw new TypeError('Cookie path must not be specified for host cookies');
}
if (item.path && item.path.endsWith('/')) {
item.path = item.path.slice(0, -1);
}
if (item.path === '') {
item.path = '/';
}
}
if (item.name === '' && item.value && item.value.includes('=')) {
throw new TypeError("Cookie value cannot contain '=' if the name is empty");
}
if (item.name && item.name.startsWith('__Host')) {
item.secure = true;
}
const previousCookie = await this.get(item.name);
this.cookieMap.set(item.name || '', item as Cookie);
if (this.onchange) {
const changed = [];
const deleted = [];
if (previousCookie && !(await this.get(item))) {
deleted.push({ ...item, value: undefined });
} else {
changed.push(item);
}
const event = new CookieChangeEvent('change', { changed, deleted });
this.onchange(event);
}
}
async getAll(init?: CookieStoreGetOptions['name'] | CookieStoreGetOptions): Promise<Cookie[]> {
const cookies = Array.from(this.cookieMap.values());
if (init == null || Object.keys(init).length === 0) {
return cookies;
}
let name: string | undefined;
if (typeof init === 'string') {
name = init as string;
} else {
name = init.name;
}
return cookies.filter(cookie => cookie.name === name);
}
async delete(init: CookieStoreDeleteOptions['name'] | CookieStoreDeleteOptions): Promise<void> {
const item: CookieListItem = {
name: '',
value: '',
path: '/',
secure: false,
sameSite: 'strict',
expires: null,
domain: null,
httpOnly: false,
};
if (typeof init === 'string') {
item.name = init as string;
} else {
Object.assign(item, init);
}
item.expires = 0;
await this.set(item);
}
}