forked from fastify/session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.js
67 lines (58 loc) · 1.53 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict'
const isConnectionSecure = require('./isConnectionSecure')
module.exports = class Cookie {
constructor (cookie, request) {
const originalMaxAge = cookie.originalMaxAge || cookie.maxAge || null
this.path = cookie.path || '/'
this.secure = cookie.secure ?? null
this.sameSite = cookie.sameSite || null
this.domain = cookie.domain || null
this.httpOnly = cookie.httpOnly !== undefined ? cookie.httpOnly : true
this._expires = null
if (originalMaxAge) {
this.maxAge = originalMaxAge
} else if (cookie.expires) {
this.expires = new Date(cookie.expires)
this.originalMaxAge = null
} else {
this.originalMaxAge = originalMaxAge
}
if (this.secure === 'auto') {
if (isConnectionSecure(request)) {
this.secure = true
} else {
this.sameSite = 'Lax'
this.secure = false
}
}
}
set expires (date) {
this._expires = date
}
get expires () {
return this._expires
}
set maxAge (ms) {
this.expires = new Date(Date.now() + ms)
// we force the same originalMaxAge to match old behavior
this.originalMaxAge = ms
}
get maxAge () {
if (this.expires instanceof Date) {
return this.expires.valueOf() - Date.now()
} else {
return null
}
}
toJSON () {
return {
expires: this._expires,
originalMaxAge: this.originalMaxAge,
sameSite: this.sameSite,
secure: this.secure,
path: this.path,
httpOnly: this.httpOnly,
domain: this.domain
}
}
}