-
Notifications
You must be signed in to change notification settings - Fork 2
/
es-cookie.ts
80 lines (63 loc) · 2.68 KB
/
es-cookie.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
import {CookieAttributes} from './CookieAttributes.js';
export {CookieAttributes};
function stringifyAttribute(name: string, value: string | boolean | undefined): string {
if (!value) {
return '';
}
let stringified = '; ' + name;
if (value === true) {
return stringified; // boolean attributes shouldn't have a value
}
return stringified + '=' + value;
}
function stringifyAttributes(attributes: CookieAttributes): string {
if (typeof attributes.expires === 'number') {
const milliseconds = Math.min(Date.now() + attributes.expires * 864e+5, 864e+13);
attributes.expires = new Date(milliseconds);
}
return stringifyAttribute('Expires', attributes.expires ? attributes.expires.toUTCString() : '')
+ stringifyAttribute('Domain', attributes.domain)
+ stringifyAttribute('Path', attributes.path)
+ stringifyAttribute('Secure', attributes.secure)
+ stringifyAttribute('Partitioned', attributes.partitioned)
+ stringifyAttribute('SameSite', attributes.sameSite);
}
export function encode(name: string, value: string, attributes: CookieAttributes): string {
return encodeURIComponent(name)
.replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent) // allowed special characters
.replace(/\(/g, '%28').replace(/\)/g, '%29') // replace opening and closing parens
+ '=' + encodeURIComponent(value)
// allowed special characters
.replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, decodeURIComponent)
+ stringifyAttributes(attributes);
}
export function parse(cookieString: string): {[name: string]: string} {
let result: {[name: string]: string} = {};
const cookies = cookieString ? cookieString.split('; ') : [];
for (let cookie of cookies) {
const parts = cookie.split('=');
let value = parts.slice(1).join('=');
if (value[0] === '"') {
value = value.slice(1, -1);
}
try {
const name = decodeURIComponent(parts[0]);
result[name] = value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
} catch (e) {
// ignore cookies with invalid name/value encoding
}
}
return result;
}
export function getAll(): {[name: string]: string} {
return parse(document.cookie);
}
export function get(name: string): string | undefined {
return getAll()[name];
}
export function set(name: string, value: string, attributes?: CookieAttributes): void {
document.cookie = encode(name, value, {path: '/', ...attributes});
}
export function remove(name: string, attributes?: CookieAttributes): void {
set(name, '', {...attributes, expires: -1});
}