-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.js
98 lines (75 loc) · 1.77 KB
/
middleware.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
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
import Crypto from "crypto";
export function helpers(_, res, next)
{
res.status = c =>
{
res.statusCode = c;
return res;
};
res.json = o =>
{
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(o));
};
res.redirect = u =>
{
res.writeHead(302, { "Location": u });
res.end();
};
next();
}
export function json(req, _, next)
{
req.body = {};
const chunks = [];
req.on("data", chunk => chunks.push(chunk));
req.on("end", () =>
{
try { req.body = JSON.parse(Buffer.concat(chunks).toString()); }
catch(_) {}
next();
});
req.on("error", next);
}
function parseCookies(string)
{
const cookies = {};
for (const pair of string.split(";"))
{
const i = pair.indexOf("=");
if (i < 0) continue;
const key = pair.slice(0, i).trim();
const value = pair.slice(i + 1);
cookies[key] = value;
}
return cookies;
}
export function session({ maxAge = 30 * 24 * 60 * 60, secure = true, httpOnly = true, sameSite = "Strict" } = {})
{
const sessionMap = new Map();
return (req, res, next) =>
{
res.createSession = data =>
{
const sessionId = Crypto.randomBytes(36).toString("base64");
const session = Object.assign({ sessionId }, data);
sessionMap.set(sessionId, session);
let cookie = [ `sessionId=${sessionId}`, "Path=/" ];
if (maxAge) cookie.push(`Max-Age=${maxAge}`);
if (secure) cookie.push("Secure");
if (httpOnly) cookie.push("HttpOnly");
if (sameSite) cookie.push(`SameSite=${sameSite}`);
res.setHeader("Set-Cookie", cookie.join("; "));
return session;
};
res.deleteSession = sessionId =>
{
sessionMap.delete(sessionId);
};
if (!req.session && typeof req.headers.cookie === "string")
{
req.session = sessionMap.get(parseCookies(req.headers.cookie).sessionId);
}
next();
};
}