-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (140 loc) · 5.2 KB
/
index.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import http from 'http';
import https from 'https';
import http2 from 'http2';
import util from 'util';
import pathToRegex from './util/pathToRegex.js';
import generateKeyPair from "./util/generateSelfSignedCertificate.js";
const defaultOnClientError = (err, socket) => socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
const strategy = {http, https, http2};
class App {
#_middlewares = [];
#_routes = [];
#_server;
#_isSecure;
#_address;
get address() {
return this.#_address;
}
constructor() {
this._httpHandler = this._httpHandler.bind(this);
}
async init({protocol = 'http', selfSigned = false, http2Secure = true, ...options} = {}) {
const pkg = strategy[protocol] || strategy.http;
const pkg_method = (pkg === http2 && http2Secure) ? 'createSecureServer' : 'createServer';
if (pkg !== http && selfSigned) {
const {privateKey, certificate} = await generateKeyPair();
options.key = privateKey;
options.cert = certificate;
}
this.#_isSecure = pkg === https || (pkg === http2 && http2Secure);
this.#_server = pkg[pkg_method](options, this._httpHandler);
return this;
}
_httpHandler(request, response) {
const ctx = {request, response};
/** @type {MiddlewareItem[]} */ const middlewares = [].concat(this.#_middlewares, this.#_routes);
const middlewareOrchestrator = async () => {
let lastResult = void 0;
for (const middleware of middlewares) {
if (!middleware) continue;
if (!middleware.methods.includes(request.method)) continue;
if (!new RegExp(middleware.route.regex, 'g').test(request.url)) continue;
let matchs = new RegExp(middleware.route.regex, 'g').exec(request.url);
matchs = matchs || [];
const params = {};
if (middleware.route.keyToIndex) {
Object.getOwnPropertyNames(middleware.route.keyToIndex).forEach(key => {
params[key] = matchs[middleware.route.keyToIndex[key]];
});
} else {
matchs.shift();
matchs.forEach((match, index) => params[index + 1] = match);
}
ctx.params = params;
lastResult = await middleware.middleware(ctx, lastResult);
if (response.finished) break;
}
response.finished || response.end(typeof lastResult === 'object' ? JSON.stringify(lastResult) : lastResult.toString());
return ctx;
};
middlewareOrchestrator().catch(e => {
console.error(e);
response.finished || response.end(util.inspect(e));
});
}
async listen(port = void 0, host = void 0, backlog = void 0) {
return new Promise(resolve => {
const options = typeof port === 'object' ? port : {port, host, backlog};
const protocol = this.#_isSecure ? 'https' : 'http';
this.#_server.on('listening', () => {
let {port, family, address} = this.#_server.address();
if (family === 'IPv6') {
address = address === '::' ? '::1' : address;
address = `[${address}]`;
}
this.#_address = `${protocol}://${address}:${port}/`;
resolve(this.address);
});
this.#_server.listen(options);
});
}
applyOnClientError(callback = defaultOnClientError) {
this.#_server.on('clientError', callback);
return this;
}
static _use(methods, route, middleware) {
// switch arguments for support signatures
// _use(middleware: Middleware): ReturnPUse;
// _use(route: string|RegExp|Return, middleware: Middleware): ReturnPUse;
// _use(methods: string|string[], route: string|RegExp|Return, middleware: Middleware): ReturnPUse;
if (typeof methods === 'function') {
middleware = methods;
methods = void 0;
} else if (typeof route === 'function') {
middleware = route;
route = methods;
methods = void 0;
}
methods = methods || http.METHODS;
route = route || /^\/.*$/;
if (!Array.isArray(methods)) {
methods = [methods];
}
if (typeof route === 'string') {
route = pathToRegex(route);
} else if (route instanceof RegExp) {
route = {regex: route};
}
// return complete arity for .route or .use in good order
// with default value and optional transformations applied
return [methods, route, middleware];
}
use(methods, route, middleware) {
[methods, route, middleware] = App._use(methods, route, middleware);
this.#_middlewares.push({methods, route, middleware});
return this;
}
route(methods, route, middleware) {
[methods, route, middleware] = App._use(methods, route, middleware);
this.#_routes.push({methods, route, middleware});
return this;
}
get(route, middleware) {
return this.route(['GET'], route, middleware);
}
post(route, middleware) {
return this.route(['POST'], route, middleware);
}
put(route, middleware) {
return this.route(['POST'], route, middleware);
}
patch(route, middleware) {
return this.route(['PATCH'], route, middleware);
}
delete(route, middleware) {
return this.route(['DELETE'], route, middleware);
}
}
export default App;
export { default as parseCookie } from './middlewares/cookies.js';
export { default as sessionInMemory } from './middlewares/sessions.js';