-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
129 lines (124 loc) · 3.12 KB
/
main.js
File metadata and controls
129 lines (124 loc) · 3.12 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
/* eslint-disable node/no-unsupported-features/es-syntax */
// REFERENCES
// https://tools.ietf.org/html/rfc6265
const DEFAULT_COOKIE_NAME = 'session'
const DEFAULT_COOKIE_OPTIONS = {
httpOnly: true,
}
const DEFAULT_TTL = 2 * 60 * 60 * 1e3
const SPACES_REGEXP = new RegExp('\\s+', 'g')
export const parseCookie = ({ cookie }) => {
return new Map(
cookie
.replace(SPACES_REGEXP, '')
.split(';')
.map((iterator) => {
const [name, value] = iterator.split('=')
if (value[0] === '"' && value[value.length - 1] === '"') {
return [name, value.slice(1, -1)]
}
return [name, value]
})
)
}
export const setCookie = ({ domain, expires, httpOnly, maxAge, name, path, sameSite, secure, value }) => {
const attributes = [`${name}=${value}`]
if (domain !== undefined) {
attributes.push(`Domain=${domain}`)
}
if (expires !== undefined) {
attributes.push(`Expires=${expires}`)
}
if (httpOnly === true) {
attributes.push('HttpOnly')
}
if (maxAge !== undefined) {
attributes.push(`Max-Age=${maxAge}`)
}
if (path !== undefined) {
attributes.push(`Path=${path}`)
}
if (sameSite !== undefined) {
attributes.push(`SameSite=${sameSite}`)
}
if (secure === true) {
attributes.push('Secure')
}
return attributes.join('; ')
}
export const sessionHandler = async (
{ cookieName = DEFAULT_COOKIE_NAME, cookieOptions = DEFAULT_COOKIE_OPTIONS, ttl = DEFAULT_TTL } = {
cookieName: DEFAULT_COOKIE_NAME,
cookieOptions: DEFAULT_COOKIE_OPTIONS,
ttl: DEFAULT_TTL,
}
) => {
const crypto = await import('crypto')
const sessions = new Map()
const generateSessionId = () => {
return new Promise((resolve) => {
crypto.randomBytes(16, (error, buffer) => {
resolve(buffer.toString('hex'))
})
})
}
const createSession = async ({ response }) => {
const session = {
expiresAt: Date.now() + ttl,
sessionId: await generateSessionId(),
storage: new Map(),
}
sessions.set(session.sessionId, session)
response.setHeader(
'Set-Cookie',
setCookie({
...cookieOptions,
name: cookieName,
value: session.sessionId,
})
)
return {
...session,
}
}
const handle = ({ request, response }) => {
if (request.aborted === true || response.writableEnded === true) {
return undefined
}
const now = Date.now()
if ('cookie' in request.headers === false) {
return createSession({
response,
})
}
const cookie = parseCookie({
cookie: request.headers.cookie,
})
const sessionId = cookie.get(cookieName)
if (sessionId === undefined) {
return createSession({
response,
})
}
for (const { expiresAt, sessionId } of sessions.values()) {
if (expiresAt > now) {
continue
}
sessions.delete(sessionId)
}
const session = sessions.get(sessionId)
if (session === undefined) {
return createSession({
response,
})
}
session.expiresAt = now + ttl
return {
...session,
}
}
return {
handle,
sessions,
}
}