-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcookie.js
50 lines (42 loc) · 1.23 KB
/
cookie.js
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
const ObjectGet = require('lodash.get')
const kCookies = Symbol('cookies')
const kSignedCookies = Symbol('signedCookies')
const kRequest = Symbol('request')
const kResponse = Symbol('response')
const kJar = Symbol('jar')
class Cookie {
constructor(jar, cookies, signedCookies, request, response) {
Object.defineProperties(this, {
[kJar]: {
value: jar
},
[kSignedCookies]: {
value: signedCookies
},
[kRequest]: {
value: request,
},
[kResponse]: {
value: response
}
})
this[kCookies] = cookies;
}
get(key, defaultValue = null) {
return ObjectGet(this.all(), key, defaultValue);
}
set(key, value, options = {}) {
this[kResponse].append('Set-Cookie', this[kJar].createCookies(key, value, options, this[kRequest].secure()));
return this
}
clear(key, options = {}) {
return this.set(key, '', options);
}
forever(key, value) {
this.set(key, value, { maxAge: 1000 * 60 * 60 * 24 * 5 })
}
all() {
return { ...this[kCookies], ...this[kSignedCookies] }
}
}
module.exports = Cookie