-
Notifications
You must be signed in to change notification settings - Fork 0
/
createServer.js
147 lines (138 loc) · 4.28 KB
/
createServer.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
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
const methodsSymbols = require("./utils/methodsSymbols.js");
const {
MethodNotAllowedError,
NotFoundError,
InvalidResolverError,
} = require("./utils/errors.js");
const bindings = require("./utils/bindings.js");
const rootErrorHandler = async (req, res, data, next) => {
try {
await next();
} catch (err) {
if (res.writableEnded) return console.error(err);
try {
res.setHeader("content-type", "text/plain");
if ("httpStatusCode" in err) {
return res.setStatusCode(err.httpStatusCode).end(`${err.httpStatusCode} ${err.name}: ${err.message}`);
}
throw err;
} catch (err) {
console.error(err);
res.setStatusCode(500).end("Internal server error");
}
}
};
const createServer = (router) => {
const wrappedRouter = [
rootErrorHandler,
router,
];
return require("http").createServer(async (req, res) => {
bindings.statusCode(res);
bindings.contentType(res);
bindings.end(res);
bindings.reqCookie(req);
bindings.resCookie(res);
bindings.body(req);
bindings.query(req);
bindings.path(req);
bindings.headers(req);
bindings.pathParams(req);
const traverse = async (route, dividedPathTraversed, dividedPathToTraverse, data, next) => {
if (Array.isArray(route)) {
const resolver = route[0];
if (route.length === 1) {
return traverse(resolver, dividedPathTraversed, dividedPathToTraverse, data, null);
}
if (route.length > 1) {
if (typeof resolver === "function" && resolver.length < 4) {
await resolver(req, res, data);
return traverse(route.slice(1), dividedPathTraversed, dividedPathToTraverse, data, next);
}
return traverse(resolver, dividedPathTraversed, dividedPathToTraverse, data, () => (
traverse(route.slice(1), dividedPathTraversed, dividedPathToTraverse, data, next)
));
}
throw new InvalidResolverError(req.getPath());
}
if (typeof route === "function") {
return route(req, res, data, next);
}
if (typeof route === "object") {
if (dividedPathToTraverse.length === 0) {
if (req.method === "OPTIONS") {
const allowedMethods = Reflect.ownKeys(route).reduce((allowedMethods, routeKey) => {
if (typeof routeKey === "symbol") {
const method = routeKey.description;
allowedMethods.push(method);
if (method === "GET") allowedMethods.push("HEAD");
}
return allowedMethods;
}, ["OPTIONS"]);
if (allowedMethods.length <= 1) {
throw new NotFoundError(req.getPath());
}
if (req.getHeader("access-control-request-method")) {
return res.setStatusCode(204).setHeader(
"access-control-allow-headers",
"*",
).setHeader("access-control-allow-methods", allowedMethods.join(", ")).end();
} else {
return res.setStatusCode(204).setHeader("allow", allowedMethods.join(", ")).end();
}
}
const resolver = route[methodsSymbols[req.method === "HEAD" ? "GET" : req.method]];
if (!resolver) {
if (Object.values(methodsSymbols).some((methodSymbol) => route[methodSymbol])) {
throw new MethodNotAllowedError(`${req.method} ${req.getPath()}`);
}
throw new NotFoundError(req.getPath());
}
if (req.method === "HEAD") {
const original_end = res.end;
res.end = function(content) {
this.setHeader("content-length", content.length);
this.setStatusCode(204);
original_end.apply(this)
}
}
return traverse(resolver, dividedPathTraversed, dividedPathToTraverse, data, next);
}
const resolver = route[dividedPathToTraverse[0]];
if (!resolver) {
const resolver = route[":"];
if (!resolver) {
throw new NotFoundError(req.getPath());
}
return traverse(
resolver,
dividedPathTraversed.concat(dividedPathToTraverse[0]),
dividedPathToTraverse.slice(1),
data,
next,
);
}
return traverse(
resolver,
dividedPathTraversed.concat(dividedPathToTraverse[0]),
dividedPathToTraverse.slice(1),
data,
next,
);
}
if (typeof route === "string") {
req.setPathParam(route, dividedPathTraversed[dividedPathTraversed.length - 1]);
return next();
}
throw new InvalidResolverError(req.getPath());
};
traverse(
wrappedRouter,
[],
req.getDividedPath(),
{},
null
);
});
};
module.exports = createServer;