forked from Becklyn/mojave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.js
More file actions
149 lines (125 loc) · 3.28 KB
/
cookie.js
File metadata and controls
149 lines (125 loc) · 3.28 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { merge } from "./extend";
/**
* @typedef {{
* domain: string,
* path: string,
* secure: boolean,
* ?expires: number|Date,
* }} mojave.CookieOptions
*/
/**
* Sets a cookie's value. If the value is an object or array, it'll be JSON.stringify and saved as a string
*
* @param {string} key
* @param {*} value
* @param {mojave.CookieOptions} options
*/
export function setCookie (key, value, options = {})
{
document.cookie = formatCookieString(key, value, options);
}
/**
* Formats the cookie string
*
* @private
* @param {string} key
* @param {*} value
* @param {mojave.CookieOptions} options
* @returns {string}
*/
export function formatCookieString (key, value, options = {})
{
options = merge({
path: "/",
secure: window.location.protocol === "https",
expires: 30,
}, options);
if (typeof options.expires === "number")
{
options.expires = new Date((new Date()).getTime() + (options.expires * 864e+5));
}
options.expires = !options.expires ? "" : options.expires.toUTCString();
const encodedKey = encodeCookieKey(key);
const encodedValue = encodeCookieValue(value);
const encodedOptions = encodeCookieOptions(options);
return `${encodedKey}=${encodedValue};${encodedOptions}`;
}
/**
* Retrieves a cookie's value
*
* @param {string} key
*
* @returns {*|null}
*/
export function getCookie (key)
{
const encodedSearchCookieKey = encodeCookieKey(key);
const matcher = new RegExp(`; ${encodedSearchCookieKey}=([^;]+)`);
const match = matcher.exec(`; ${document.cookie}`);
return null !== match
? JSON.parse(decodeURIComponent(match[1]))
: null;
}
/**
* Removes the given cookie
*
* @param {string} key
*/
export function removeCookie (key)
{
setCookie(key, "", {
expires: -1,
});
}
/**
* @private
* @param {string} key
*
* @return {string}
*/
function encodeCookieKey (key)
{
return encodeURIComponent("" + key)
.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent)
.replace(/[()]/g, escape);
}
/**
* @private
* @param {*} value
*
* @return {string}
*/
function encodeCookieValue (value)
{
return encodeURIComponent(JSON.stringify(value))
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
}
/**
* @private
* @param {Object.<string, string|boolean>} options
*
* @return {string}
*/
function encodeCookieOptions (options)
{
const encodedOptions = [];
// eslint-disable-next-line guard-for-in
for (let optionName in options)
{
const optionValue = options[optionName];
// Flags with a `false` value needs to be omitted
if (!options.hasOwnProperty(optionName) || optionValue === false)
{
continue;
}
// RFC 6265 section 5.2:
// =====================
// 3. If the remaining unparsed-attributes contains a %x3B (";") character:
// Consume the characters of the unparsed-attributes up to, not including, the first %x3B (";") character.
const sanitizedOption = (optionValue === true)
? optionName
: `${optionName}=${("" + optionValue).split(";")[0]}`;
encodedOptions.push(sanitizedOption);
}
return encodedOptions.join(" ;");
}