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
170 lines (140 loc) · 3.64 KB
/
cookies.js
File metadata and controls
170 lines (140 loc) · 3.64 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
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 set (key, val, opts = {}, ssr) {
let expire, expireValue
if (opts.expires !== void 0) {
expireValue = parseInt(opts.expires, 10)
if (isNaN(expireValue)) {
console.error('Quasar cookie: expires needs to be a number')
return
}
expire = new Date()
expire.setMilliseconds(expire.getMilliseconds() + expireValue * 864e+5)
}
const keyValue = `${encode(key)}=${stringifyCookieValue(val)}`
const cookie = [
keyValue,
expire !== void 0 ? '; Expires=' + expire.toUTCString() : '', // use expires attribute, max-age is not supported by IE
opts.path ? '; Path=' + opts.path : '',
opts.domain ? '; Domain=' + opts.domain : '',
opts.httpOnly ? '; HttpOnly' : '',
opts.secure ? '; Secure' : ''
].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 !== undefined) {
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) {
let
result = key ? undefined : {},
cookieSource = ssr ? ssr.req.headers : document,
cookies = cookieSource.cookie ? cookieSource.cookie.split('; ') : [],
i = 0,
l = cookies.length,
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,
'',
Object.assign({}, options, { expires: -1 }),
ssr
)
}
function has (key, ssr) {
return get(key, ssr) !== undefined
}
export function getObject (ctx = {}) {
const ssr = ctx.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),
all: () => get(null, ssr)
}
}
export default {
parseSSR (/* ssrContext */ ssr) {
return ssr ? getObject({ ssr }) : this
},
install ({ $q, queues }) {
if (isSSR) {
queues.server.push((q, ctx) => {
q.cookies = getObject(ctx)
})
}
else {
Object.assign(this, getObject())
$q.cookies = this
}
}
}