forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookies.js
More file actions
204 lines (172 loc) · 4.65 KB
/
Cookies.js
File metadata and controls
204 lines (172 loc) · 4.65 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { isSSR } from './Platform.js'
function encode (string) {
return encodeURIComponent(string)
}
function decode (string) {
return decodeURIComponent(string)
}
function stringifyCookieValue (value) {
return encode(value === Object(value) ? JSON.stringify(value) : '' + value)
}
function read (string) {
if (string === '') {
return string
}
if (string.indexOf('"') === 0) {
// This is a quoted cookie as according to RFC2068, unescape...
string = string.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\')
}
// Replace server-side written pluses with spaces.
// If we can't decode the cookie, ignore it, it's unusable.
// If we can't parse the cookie, ignore it, it's unusable.
string = decode(string.replace(/\+/g, ' '))
try {
string = JSON.parse(string)
}
catch (e) {}
return string
}
function getString (msOffset) {
const time = new Date()
time.setMilliseconds(time.getMilliseconds() + msOffset)
return time.toUTCString()
}
function parseExpireString (str) {
let timestamp = 0
const days = str.match(/(\d+)d/)
const hours = str.match(/(\d+)h/)
const minutes = str.match(/(\d+)m/)
const seconds = str.match(/(\d+)s/)
if (days) { timestamp += days[1] * 864e+5 }
if (hours) { timestamp += hours[1] * 36e+5 }
if (minutes) { timestamp += minutes[1] * 6e+4 }
if (seconds) { timestamp += seconds[1] * 1000 }
return timestamp === 0
? str
: getString(timestamp)
}
function set (key, val, opts = {}, ssr) {
let expire, expireValue
if (opts.expires !== void 0) {
// if it's a Date Object
if (Object.prototype.toString.call(opts.expires) === '[object Date]') {
expire = opts.expires.toUTCString()
}
// if it's a String (eg. "15m", "1h", "13d", "1d 15m", "31s")
// possible units: d (days), h (hours), m (minutes), s (seconds)
else if (typeof opts.expires === 'string') {
expire = parseExpireString(opts.expires)
}
// otherwise it must be a Number (defined in days)
else {
expireValue = parseFloat(opts.expires)
expire = isNaN(expireValue) === false
? getString(expireValue * 864e+5)
: opts.expires
}
}
const keyValue = `${encode(key)}=${stringifyCookieValue(val)}`
const cookie = [
keyValue,
expire !== void 0 ? '; Expires=' + expire : '', // use expires attribute, max-age is not supported by IE
opts.path ? '; Path=' + opts.path : '',
opts.domain ? '; Domain=' + opts.domain : '',
opts.sameSite ? '; SameSite=' + opts.sameSite : '',
opts.httpOnly ? '; HttpOnly' : '',
opts.secure ? '; Secure' : '',
opts.other ? '; ' + opts.other : ''
].join('')
if (ssr) {
if (ssr.req.qCookies) {
ssr.req.qCookies.push(cookie)
}
else {
ssr.req.qCookies = [ cookie ]
}
ssr.res.setHeader('Set-Cookie', ssr.req.qCookies)
// make temporary update so future get()
// within same SSR timeframe would return the set value
let all = ssr.req.headers.cookie || ''
if (expire !== void 0 && expireValue < 0) {
const val = get(key, ssr)
if (val !== void 0) {
all = all
.replace(`${key}=${val}; `, '')
.replace(`; ${key}=${val}`, '')
.replace(`${key}=${val}`, '')
}
}
else {
all = all
? `${keyValue}; ${all}`
: cookie
}
ssr.req.headers.cookie = all
}
else {
document.cookie = cookie
}
}
function get (key, ssr) {
const
cookieSource = ssr ? ssr.req.headers : document,
cookies = cookieSource.cookie ? cookieSource.cookie.split('; ') : [],
l = cookies.length
let
result = key ? null : {},
i = 0,
parts,
name,
cookie
for (; i < l; i++) {
parts = cookies[i].split('=')
name = decode(parts.shift())
cookie = parts.join('=')
if (!key) {
result[name] = cookie
}
else if (key === name) {
result = read(cookie)
break
}
}
return result
}
function remove (key, options, ssr) {
set(
key,
'',
{ expires: -1, ...options },
ssr
)
}
function has (key, ssr) {
return get(key, ssr) !== null
}
export function getObject (ssr) {
return {
get: key => get(key, ssr),
set: (key, val, opts) => set(key, val, opts, ssr),
has: key => has(key, ssr),
remove: (key, options) => remove(key, options, ssr),
getAll: () => get(null, ssr)
}
}
export default {
parseSSR (ssrContext) {
return ssrContext !== void 0
? getObject(ssrContext)
: this
},
install ({ $q, queues }) {
if (isSSR === true) {
queues.server.push((q, ctx) => {
q.cookies = getObject(ctx.ssr)
})
}
else {
Object.assign(this, getObject())
$q.cookies = this
}
}
}