ultimate-express 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/test.yml +27 -0
- package/EXPRESS_LICENSE +26 -0
- package/README.md +269 -0
- package/package.json +76 -0
- package/src/application.js +295 -0
- package/src/index.js +23 -0
- package/src/middlewares.js +95 -0
- package/src/request.js +324 -0
- package/src/response.js +686 -0
- package/src/router.js +490 -0
- package/src/utils.js +285 -0
- package/src/view.js +103 -0
- package/src/workers/fs.js +14 -0
- package/tests/index.js +64 -0
- package/tests/parts/.test/index.html +11 -0
- package/tests/parts/big.jpg +0 -0
- package/tests/parts/index.art +12 -0
- package/tests/parts/index.dot +12 -0
- package/tests/parts/index.ejs +12 -0
- package/tests/parts/index.handlebars +2 -0
- package/tests/parts/index.html +12 -0
- package/tests/parts/index.mustache +12 -0
- package/tests/parts/index.pug +6 -0
- package/tests/parts/index.swig +12 -0
- package/tests/parts/layouts/main.handlebars +12 -0
- package/tests/preload.cjs +5 -0
- package/tests/singular.js +41 -0
- package/tests/tests/app/app-engine.js +8 -0
- package/tests/tests/app/app-on-mount.js +24 -0
- package/tests/tests/app/app-path.js +21 -0
- package/tests/tests/app/app-route.js +30 -0
- package/tests/tests/app/options.js +16 -0
- package/tests/tests/app/setting-inheritance.js +16 -0
- package/tests/tests/engines/art-template.js +33 -0
- package/tests/tests/engines/dot.js +28 -0
- package/tests/tests/engines/ejs.js +25 -0
- package/tests/tests/engines/handlebars.js +28 -0
- package/tests/tests/engines/mustache.js +28 -0
- package/tests/tests/engines/pug.js +25 -0
- package/tests/tests/engines/swig.js +28 -0
- package/tests/tests/errors/error-handling-middleware.js +22 -0
- package/tests/tests/errors/next-error-optimized.js +26 -0
- package/tests/tests/errors/next-error.js +26 -0
- package/tests/tests/errors/unexpected-error-handling.js +18 -0
- package/tests/tests/listen/listen-random.js +11 -0
- package/tests/tests/listen/listen-specific.js +17 -0
- package/tests/tests/middlewares/body-json.js +31 -0
- package/tests/tests/middlewares/body-raw-deflate.js +43 -0
- package/tests/tests/middlewares/body-raw-gzip.js +43 -0
- package/tests/tests/middlewares/body-raw.js +41 -0
- package/tests/tests/middlewares/body-text.js +27 -0
- package/tests/tests/middlewares/body-urlencoded.js +30 -0
- package/tests/tests/middlewares/cookie-parser-signed.js +31 -0
- package/tests/tests/middlewares/cookie-parser.js +28 -0
- package/tests/tests/middlewares/cookie-session.js +40 -0
- package/tests/tests/middlewares/cors.js +29 -0
- package/tests/tests/middlewares/errorhandler.js +26 -0
- package/tests/tests/middlewares/express-fileupload-temp.js +46 -0
- package/tests/tests/middlewares/express-fileupload.js +28 -0
- package/tests/tests/middlewares/express-rate-limit.js +33 -0
- package/tests/tests/middlewares/express-session.js +37 -0
- package/tests/tests/middlewares/express-static-options.js +72 -0
- package/tests/tests/middlewares/express-static.js +40 -0
- package/tests/tests/middlewares/method-override.js +33 -0
- package/tests/tests/middlewares/multer.js +43 -0
- package/tests/tests/middlewares/multiple-middlewares.js +37 -0
- package/tests/tests/middlewares/response-time.js +29 -0
- package/tests/tests/middlewares/serve-index.js +38 -0
- package/tests/tests/middlewares/serve-static.js +38 -0
- package/tests/tests/middlewares/vhost.js +50 -0
- package/tests/tests/params/array-param.js +30 -0
- package/tests/tests/params/nested-params.js +24 -0
- package/tests/tests/params/param-errors.js +56 -0
- package/tests/tests/params/param-function.js +49 -0
- package/tests/tests/params/param-next-route.js +48 -0
- package/tests/tests/params/param-optimized.js +38 -0
- package/tests/tests/params/param-use.js +39 -0
- package/tests/tests/params/param.js +68 -0
- package/tests/tests/params/params-regex.js +26 -0
- package/tests/tests/params/params-use.js +20 -0
- package/tests/tests/params/params.js +35 -0
- package/tests/tests/req/req-accepts-charsets.js +40 -0
- package/tests/tests/req/req-accepts-encodings.js +36 -0
- package/tests/tests/req/req-accepts-languages.js +41 -0
- package/tests/tests/req/req-accepts.js +41 -0
- package/tests/tests/req/req-app.js +17 -0
- package/tests/tests/req/req-baseurl.js +38 -0
- package/tests/tests/req/req-connection.js +19 -0
- package/tests/tests/req/req-fresh.js +59 -0
- package/tests/tests/req/req-get.js +78 -0
- package/tests/tests/req/req-headers-distinct.js +72 -0
- package/tests/tests/req/req-headers.js +72 -0
- package/tests/tests/req/req-host.js +45 -0
- package/tests/tests/req/req-hostname.js +45 -0
- package/tests/tests/req/req-ip.js +19 -0
- package/tests/tests/req/req-is.js +44 -0
- package/tests/tests/req/req-original-url.js +29 -0
- package/tests/tests/req/req-param.js +29 -0
- package/tests/tests/req/req-protocol.js +20 -0
- package/tests/tests/req/req-query.js +23 -0
- package/tests/tests/req/req-range.js +48 -0
- package/tests/tests/req/req-raw-headers.js +72 -0
- package/tests/tests/req/req-subdomains.js +48 -0
- package/tests/tests/req/req-url-nested.js +27 -0
- package/tests/tests/req/req-url-optimized-router.js +26 -0
- package/tests/tests/req/req-url-optimized.js +23 -0
- package/tests/tests/req/req-url.js +36 -0
- package/tests/tests/req/req-xhr.js +23 -0
- package/tests/tests/res/head-content-length.js +18 -0
- package/tests/tests/res/head.js +47 -0
- package/tests/tests/res/injecting.js +25 -0
- package/tests/tests/res/piping.js +23 -0
- package/tests/tests/res/res-app.js +17 -0
- package/tests/tests/res/res-append.js +24 -0
- package/tests/tests/res/res-attachment.js +19 -0
- package/tests/tests/res/res-clear-cookie.js +18 -0
- package/tests/tests/res/res-cookie.js +22 -0
- package/tests/tests/res/res-download.js +36 -0
- package/tests/tests/res/res-format.js +57 -0
- package/tests/tests/res/res-get.js +18 -0
- package/tests/tests/res/res-headers-sent.js +18 -0
- package/tests/tests/res/res-json.js +17 -0
- package/tests/tests/res/res-jsonp.js +25 -0
- package/tests/tests/res/res-links.js +21 -0
- package/tests/tests/res/res-location.js +34 -0
- package/tests/tests/res/res-redirect.js +46 -0
- package/tests/tests/res/res-remove-header.js +19 -0
- package/tests/tests/res/res-send-file.js +17 -0
- package/tests/tests/res/res-send-status.js +17 -0
- package/tests/tests/res/res-send.js +69 -0
- package/tests/tests/res/res-set.js +28 -0
- package/tests/tests/res/res-status.js +18 -0
- package/tests/tests/res/res-type.js +19 -0
- package/tests/tests/res/res-vary.js +19 -0
- package/tests/tests/res/res-write.js +29 -0
- package/tests/tests/routers/complex-routers.js +34 -0
- package/tests/tests/routers/empty-router.js +25 -0
- package/tests/tests/routers/lot-of-routes.js +38 -0
- package/tests/tests/routers/mergeparams.js +42 -0
- package/tests/tests/routers/nested-routers.js +52 -0
- package/tests/tests/routers/router-options.js +68 -0
- package/tests/tests/routers/routers.js +45 -0
- package/tests/tests/routers/simple-routers.js +35 -0
- package/tests/tests/routing/all.js +47 -0
- package/tests/tests/routing/array-arguments.js +35 -0
- package/tests/tests/routing/array-use.js +33 -0
- package/tests/tests/routing/async-use.js +25 -0
- package/tests/tests/routing/complex-routes.js +50 -0
- package/tests/tests/routing/lot-of-param-routes.js +26 -0
- package/tests/tests/routing/lot-of-routes.js +59 -0
- package/tests/tests/routing/next-existent-optimized-route.js +29 -0
- package/tests/tests/routing/next-existent-route.js +29 -0
- package/tests/tests/routing/next-nonexistent-optimized-route.js +19 -0
- package/tests/tests/routing/next-nonexistent-route.js +19 -0
- package/tests/tests/routing/next-special-cases.js +54 -0
- package/tests/tests/routing/next-unoptimized.js +39 -0
- package/tests/tests/routing/no-path-use.js +29 -0
- package/tests/tests/routing/non-string-routes.js +27 -0
- package/tests/tests/routing/req-multiple-mountpaths.js +34 -0
- package/tests/tests/routing/simple-routes.js +29 -0
- package/tests/tests/routing/simple-use.js +52 -0
- package/tests/tests/routing/some-middlewares.js +27 -0
- package/tests/tests/routing/special-characters.js +28 -0
- package/tests/tests/routing/star.js +31 -0
- package/tests/tests/routing/sub-apps.js +32 -0
- package/tests/tests/routing/trailing-slash.js +37 -0
- package/tests/tests/routing/weird-route-start.js +18 -0
- package/tests/tests/send-file/accept-ranges.js +26 -0
- package/tests/tests/send-file/callback.js +23 -0
- package/tests/tests/send-file/default-error-routing.js +21 -0
- package/tests/tests/send-file/dotfiles.js +24 -0
- package/tests/tests/send-file/etag.js +19 -0
- package/tests/tests/send-file/fs-threads.js +39 -0
- package/tests/tests/send-file/head.js +49 -0
- package/tests/tests/send-file/headers.js +23 -0
- package/tests/tests/send-file/if-match.js +31 -0
- package/tests/tests/send-file/if-range.js +37 -0
- package/tests/tests/send-file/if-unmodified-since.js +32 -0
- package/tests/tests/send-file/immutable.js +22 -0
- package/tests/tests/send-file/large-file.js +19 -0
- package/tests/tests/send-file/last-modified.js +21 -0
- package/tests/tests/send-file/max-age.js +21 -0
- package/tests/tests/send-file/path-traversal.js +35 -0
- package/tests/tests/send-file/range.js +57 -0
- package/tests/tests/send-file/simple.js +18 -0
- package/tests/tests/settings/case-sensitive-routing.js +48 -0
- package/tests/tests/settings/env-errors.js +38 -0
- package/tests/tests/settings/etag.js +36 -0
- package/tests/tests/settings/json-escape.js +45 -0
- package/tests/tests/settings/json-replacer.js +50 -0
- package/tests/tests/settings/json-spaces.js +44 -0
- package/tests/tests/settings/query-parser.js +45 -0
- package/tests/tests/settings/strict-routing.js +64 -0
- package/tests/tests/settings/subdomain-offset.js +38 -0
- package/tests/tests/settings/trust-proxy-host.js +58 -0
- package/tests/tests/settings/trust-proxy-ip.js +58 -0
- package/tests/tests/settings/trust-proxy-ips.js +58 -0
- package/tests/tests/settings/trust-proxy-protocol.js +46 -0
- package/tests/tests/settings/x-powered-by.js +32 -0
- package/tests/uws.js +14 -0
package/src/router.js
ADDED
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
const { patternToRegex, needsConversionToRegex, deprecated, findIndexStartingFrom } = require("./utils.js");
|
|
2
|
+
const Response = require("./response.js");
|
|
3
|
+
const Request = require("./request.js");
|
|
4
|
+
const { EventEmitter } = require("tseep");
|
|
5
|
+
|
|
6
|
+
let routeKey = 0;
|
|
7
|
+
|
|
8
|
+
const methods = [
|
|
9
|
+
'all',
|
|
10
|
+
'post', 'put', 'delete', 'patch', 'options', 'head', 'trace', 'connect',
|
|
11
|
+
'checkout', 'copy', 'lock', 'mkcol', 'move', 'purge', 'propfind', 'proppatch',
|
|
12
|
+
'search', 'subscribe', 'unsubscribe', 'report', 'mkactivity', 'mkcalendar',
|
|
13
|
+
'checkout', 'merge', 'm-search', 'notify', 'subscribe', 'unsubscribe', 'search'
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
module.exports = class Router extends EventEmitter {
|
|
17
|
+
#paramCallbacks = new Map();
|
|
18
|
+
#mountpathCache = new Map();
|
|
19
|
+
#paramFunction;
|
|
20
|
+
constructor(settings = {}) {
|
|
21
|
+
super();
|
|
22
|
+
|
|
23
|
+
this._routes = [];
|
|
24
|
+
this.errorRoute = undefined;
|
|
25
|
+
this.mountpath = '/';
|
|
26
|
+
this.settings = settings;
|
|
27
|
+
this._request = Request;
|
|
28
|
+
this._response = Response;
|
|
29
|
+
this.request = this._request.prototype;
|
|
30
|
+
this.response = this._response.prototype;
|
|
31
|
+
|
|
32
|
+
if(typeof settings.caseSensitive !== 'undefined') {
|
|
33
|
+
this.settings['case sensitive routing'] = settings.caseSensitive;
|
|
34
|
+
delete this.settings.caseSensitive;
|
|
35
|
+
}
|
|
36
|
+
if(typeof settings.strict !== 'undefined') {
|
|
37
|
+
this.settings['strict routing'] = settings.strict;
|
|
38
|
+
delete this.settings.strict;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if(typeof this.settings['case sensitive routing'] === 'undefined') {
|
|
42
|
+
this.settings['case sensitive routing'] = true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for(let method of methods) {
|
|
46
|
+
this[method] = (path, ...callbacks) => {
|
|
47
|
+
this.#createRoute(method.toUpperCase(), path, this, ...callbacks);
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get(path, ...callbacks) {
|
|
53
|
+
if(typeof path === 'string' && callbacks.length === 0) {
|
|
54
|
+
const key = path;
|
|
55
|
+
if(typeof this.settings[key] === 'undefined' && this.parent) {
|
|
56
|
+
return this.parent.get(key);
|
|
57
|
+
} else {
|
|
58
|
+
return this.settings[key];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return this.#createRoute('GET', path, this, ...callbacks);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
del(path, ...callbacks) {
|
|
65
|
+
deprecated('app.del', 'app.delete');
|
|
66
|
+
return this.#createRoute('DELETE', path, this, ...callbacks);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
getFullMountpath(req) {
|
|
70
|
+
let fullStack = req._stack.join("");
|
|
71
|
+
let fullMountpath = this.#mountpathCache.get(fullStack);
|
|
72
|
+
if(!fullMountpath) {
|
|
73
|
+
fullMountpath = patternToRegex(fullStack, true);
|
|
74
|
+
this.#mountpathCache.set(fullStack, fullMountpath);
|
|
75
|
+
}
|
|
76
|
+
return fullMountpath;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
#pathMatches(route, req) {
|
|
80
|
+
let path = req._opPath;
|
|
81
|
+
let pattern = route.pattern;
|
|
82
|
+
|
|
83
|
+
if (typeof pattern === 'string') {
|
|
84
|
+
if(pattern === '/*') {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
if(path === '') {
|
|
88
|
+
path = '/';
|
|
89
|
+
}
|
|
90
|
+
if(!this.get('case sensitive routing')) {
|
|
91
|
+
path = path.toLowerCase();
|
|
92
|
+
pattern = pattern.toLowerCase();
|
|
93
|
+
}
|
|
94
|
+
return pattern === path;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return pattern.test(path);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#createRoute(method, path, parent = this, ...callbacks) {
|
|
101
|
+
callbacks = callbacks.flat();
|
|
102
|
+
const paths = Array.isArray(path) ? path : [path];
|
|
103
|
+
const routes = [];
|
|
104
|
+
for(let path of paths) {
|
|
105
|
+
if(!this.get('strict routing') && typeof path === 'string' && path.endsWith('/') && path !== '/') {
|
|
106
|
+
path = path.slice(0, -1);
|
|
107
|
+
}
|
|
108
|
+
if(path === '*') {
|
|
109
|
+
path = '/*';
|
|
110
|
+
}
|
|
111
|
+
const route = {
|
|
112
|
+
method: method === 'USE' ? 'ALL' : method.toUpperCase(),
|
|
113
|
+
path,
|
|
114
|
+
pattern: method === 'USE' || needsConversionToRegex(path) ? patternToRegex(path, method === 'USE') : path,
|
|
115
|
+
callbacks,
|
|
116
|
+
routeKey: routeKey++,
|
|
117
|
+
use: method === 'USE',
|
|
118
|
+
all: method === 'ALL' || method === 'USE',
|
|
119
|
+
gettable: method === 'GET' || method === 'HEAD',
|
|
120
|
+
};
|
|
121
|
+
routes.push(route);
|
|
122
|
+
// normal routes optimization
|
|
123
|
+
if(typeof route.pattern === 'string' && route.pattern !== '/*' && !this.parent && this.get('case sensitive routing') && this.uwsApp) {
|
|
124
|
+
// the only methods that uWS supports natively
|
|
125
|
+
if(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'CONNECT', 'TRACE'].includes(method)) {
|
|
126
|
+
const optimizedPath = this.#optimizeRoute(route, this._routes);
|
|
127
|
+
if(optimizedPath) {
|
|
128
|
+
this.#registerUwsRoute(route, optimizedPath);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// router optimization
|
|
133
|
+
if(
|
|
134
|
+
route.use && !needsConversionToRegex(path) && path !== '/*' && // must be predictable path
|
|
135
|
+
this.get('case sensitive routing') && // uWS only supports case sensitive routing
|
|
136
|
+
callbacks.filter(c => c instanceof Router).length === 1 && // only 1 router can be optimized per route
|
|
137
|
+
callbacks[callbacks.length - 1] instanceof Router // the router must be the last callback
|
|
138
|
+
) {
|
|
139
|
+
let callbacksBeforeRouter = [];
|
|
140
|
+
for(let callback of callbacks) {
|
|
141
|
+
if(callback instanceof Router) {
|
|
142
|
+
// get optimized path to router
|
|
143
|
+
let optimizedPathToRouter = this.#optimizeRoute(route, this._routes);
|
|
144
|
+
if(!optimizedPathToRouter) {
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
optimizedPathToRouter = optimizedPathToRouter.slice(0, -1); // remove last element, which is the router itself
|
|
148
|
+
if(optimizedPathToRouter) {
|
|
149
|
+
// wait for routes in router to be registered
|
|
150
|
+
setTimeout(() => {
|
|
151
|
+
if(!this.listenCalled) {
|
|
152
|
+
return; // can only optimize router whos parent is listening
|
|
153
|
+
}
|
|
154
|
+
for(let cbroute of callback._routes) {
|
|
155
|
+
if(!needsConversionToRegex(cbroute.path) && cbroute.path !== '/*') {
|
|
156
|
+
let optimizedRouterPath = this.#optimizeRoute(cbroute, callback._routes);
|
|
157
|
+
if(optimizedRouterPath) {
|
|
158
|
+
optimizedRouterPath = optimizedRouterPath.slice(0, -1);
|
|
159
|
+
const optimizedPath = [...optimizedPathToRouter, {
|
|
160
|
+
...route,
|
|
161
|
+
// fake route to update req._opPath and req.url
|
|
162
|
+
callbacks: [
|
|
163
|
+
(req, res, next) => {
|
|
164
|
+
next('skipPop');
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
}, ...optimizedRouterPath];
|
|
168
|
+
this.#registerUwsRoute({
|
|
169
|
+
...cbroute,
|
|
170
|
+
path: route.path + cbroute.path,
|
|
171
|
+
pattern: route.path + cbroute.path
|
|
172
|
+
}, optimizedPath);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}, 100);
|
|
177
|
+
}
|
|
178
|
+
// only 1 router can be optimized per route
|
|
179
|
+
break;
|
|
180
|
+
} else {
|
|
181
|
+
callbacksBeforeRouter.push(route);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
this._routes.push(...routes);
|
|
187
|
+
|
|
188
|
+
return parent;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// if route is a simple string, its possible to pre-calculate its path
|
|
192
|
+
// and then create a native uWS route for it, which is much faster
|
|
193
|
+
#optimizeRoute(route, routes) {
|
|
194
|
+
const optimizedPath = [];
|
|
195
|
+
|
|
196
|
+
for(let i = 0; i < routes.length; i++) {
|
|
197
|
+
const r = routes[i];
|
|
198
|
+
if(r.routeKey > route.routeKey) {
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
// if the methods are not the same, and its not an all method, skip it
|
|
202
|
+
if(!r.all && r.method !== route.method) {
|
|
203
|
+
// check if the methods are compatible (GET and HEAD)
|
|
204
|
+
if(!(r.method === 'HEAD' && route.method === 'GET')) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// check if the paths match
|
|
210
|
+
if(
|
|
211
|
+
(r.pattern instanceof RegExp && r.pattern.test(route.path)) ||
|
|
212
|
+
(typeof r.pattern === 'string' && (r.pattern === route.path || r.pattern === '/*'))
|
|
213
|
+
) {
|
|
214
|
+
if(r.callbacks.some(c => c instanceof Router)) {
|
|
215
|
+
return false; // cant optimize nested routers with matches
|
|
216
|
+
}
|
|
217
|
+
optimizedPath.push(r);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
optimizedPath.push(route);
|
|
221
|
+
|
|
222
|
+
return optimizedPath;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#registerUwsRoute(route, optimizedPath) {
|
|
226
|
+
let method = route.method.toLowerCase();
|
|
227
|
+
if(method === 'all') {
|
|
228
|
+
method = 'any';
|
|
229
|
+
} else if(method === 'delete') {
|
|
230
|
+
method = 'del';
|
|
231
|
+
}
|
|
232
|
+
const fn = async (res, req) => {
|
|
233
|
+
const request = new this._request(req, res, this);
|
|
234
|
+
const response = new this._response(res, request, this);
|
|
235
|
+
request.res = response;
|
|
236
|
+
response.req = request;
|
|
237
|
+
res.onAborted(() => {
|
|
238
|
+
const err = new Error('Connection closed');
|
|
239
|
+
err.code = 'ECONNRESET';
|
|
240
|
+
response.aborted = true;
|
|
241
|
+
response.socket.emit('error', err);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
const routed = await this._routeRequest(request, response, 0, optimizedPath, true, route);
|
|
245
|
+
if(routed) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
response.status(404);
|
|
249
|
+
response.send(this._generateErrorPage(`Cannot ${request.method} ${request.path}`, false));
|
|
250
|
+
};
|
|
251
|
+
route.optimizedPath = optimizedPath;
|
|
252
|
+
this.uwsApp[method](route.path, fn);
|
|
253
|
+
if(!this.get('strict routing') && route.path[route.path.length - 1] !== '/') {
|
|
254
|
+
this.uwsApp[method](route.path + '/', fn);
|
|
255
|
+
if(method === 'get') {
|
|
256
|
+
this.uwsApp.head(route.path + '/', fn);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if(method === 'get') {
|
|
260
|
+
this.uwsApp.head(route.path, fn);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
#handleError(err, request, response) {
|
|
265
|
+
if(this.errorRoute) {
|
|
266
|
+
return this.errorRoute(err, request, response, () => {
|
|
267
|
+
if(!response.headersSent) {
|
|
268
|
+
if(response.statusCode === 200) {
|
|
269
|
+
response.statusCode = 500;
|
|
270
|
+
}
|
|
271
|
+
response.send(this._generateErrorPage(err, true));
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
console.error(err);
|
|
276
|
+
if(response.statusCode === 200) {
|
|
277
|
+
response.statusCode = 500;
|
|
278
|
+
}
|
|
279
|
+
response.send(this._generateErrorPage(err, true));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
#extractParams(pattern, path) {
|
|
283
|
+
let match = pattern.exec(path);
|
|
284
|
+
return match?.groups ?? {};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
#preprocessRequest(req, res, route) {
|
|
288
|
+
return new Promise(async resolve => {
|
|
289
|
+
req.route = route;
|
|
290
|
+
if(typeof route.path === 'string' && route.path.includes(':') && route.pattern instanceof RegExp) {
|
|
291
|
+
let path = req.path;
|
|
292
|
+
if(req._stack.length > 0) {
|
|
293
|
+
path = path.replace(this.getFullMountpath(req), '');
|
|
294
|
+
}
|
|
295
|
+
req.params = this.#extractParams(route.pattern, path);
|
|
296
|
+
if(req._paramStack.length > 0) {
|
|
297
|
+
for(let params of req._paramStack) {
|
|
298
|
+
req.params = {...params, ...req.params};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
for(let param in req.params) {
|
|
303
|
+
if(this.#paramCallbacks.has(param) && !req._gotParams.has(param)) {
|
|
304
|
+
req._gotParams.add(param);
|
|
305
|
+
const pcs = this.#paramCallbacks.get(param);
|
|
306
|
+
for(let i = 0; i < pcs.length; i++) {
|
|
307
|
+
const fn = pcs[i];
|
|
308
|
+
await new Promise(resolveRoute => {
|
|
309
|
+
const next = (thingamabob) => {
|
|
310
|
+
if(thingamabob) {
|
|
311
|
+
if(thingamabob === 'route') {
|
|
312
|
+
return resolve('route');
|
|
313
|
+
} else {
|
|
314
|
+
this.#handleError(thingamabob, req, res);
|
|
315
|
+
return resolve(false);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return resolveRoute();
|
|
319
|
+
};
|
|
320
|
+
req.next = next;
|
|
321
|
+
fn(req, res, next, req.params[param], param);
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
req.params = {};
|
|
328
|
+
if(req._paramStack.length > 0) {
|
|
329
|
+
for(let params of req._paramStack) {
|
|
330
|
+
req.params = {...params, ...req.params};
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
resolve(true);
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
param(name, fn) {
|
|
339
|
+
if(typeof name === 'function') {
|
|
340
|
+
deprecated('app.param(callback)', 'app.param(name, callback)', true);
|
|
341
|
+
this.#paramFunction = name;
|
|
342
|
+
} else {
|
|
343
|
+
if(this.#paramFunction) {
|
|
344
|
+
if(!this.#paramCallbacks.has(name)) {
|
|
345
|
+
this.#paramCallbacks.set(name, []);
|
|
346
|
+
}
|
|
347
|
+
this.#paramCallbacks.get(name).push(this.#paramFunction(name, fn));
|
|
348
|
+
} else {
|
|
349
|
+
let names = Array.isArray(name) ? name : [name];
|
|
350
|
+
for(let name of names) {
|
|
351
|
+
if(!this.#paramCallbacks.has(name)) {
|
|
352
|
+
this.#paramCallbacks.set(name, []);
|
|
353
|
+
}
|
|
354
|
+
this.#paramCallbacks.get(name).push(fn);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
async _routeRequest(req, res, startIndex = 0, routes = this._routes, skipCheck = false, skipUntil) {
|
|
361
|
+
return new Promise(async (resolve) => {
|
|
362
|
+
let routeIndex = skipCheck ? startIndex : findIndexStartingFrom(routes, r => (r.all || r.method === req.method || (r.gettable && req.method === 'HEAD')) && this.#pathMatches(r, req), startIndex);
|
|
363
|
+
const route = routes[routeIndex];
|
|
364
|
+
if(!route) {
|
|
365
|
+
if(!skipCheck) {
|
|
366
|
+
// on normal unoptimized routes, if theres no match then there is no route
|
|
367
|
+
return resolve(false);
|
|
368
|
+
}
|
|
369
|
+
// on optimized routes, there can be more routes, so we have to use unoptimized routing and skip until we find route we stopped at
|
|
370
|
+
return resolve(this._routeRequest(req, res, 0, this._routes, false, skipUntil));
|
|
371
|
+
}
|
|
372
|
+
let callbackindex = 0;
|
|
373
|
+
const continueRoute = await this.#preprocessRequest(req, res, route);
|
|
374
|
+
if(route.use) {
|
|
375
|
+
req._stack.push(route.path);
|
|
376
|
+
req._opPath =
|
|
377
|
+
req.path.replace(this.getFullMountpath(req), '') +
|
|
378
|
+
(req.endsWithSlash && req.path !== '/' && this.get('strict routing') ? '/' : '');
|
|
379
|
+
req.url = req._opPath + req.urlQuery;
|
|
380
|
+
if(req.url === '') req.url = '/';
|
|
381
|
+
}
|
|
382
|
+
const next = async (thingamabob) => {
|
|
383
|
+
if(thingamabob) {
|
|
384
|
+
if(thingamabob === 'route' || thingamabob === 'skipPop') {
|
|
385
|
+
if(route.use && thingamabob !== 'skipPop') {
|
|
386
|
+
req._stack.pop();
|
|
387
|
+
req._opPath =
|
|
388
|
+
(req._stack.length > 0 ? req.path.replace(this.getFullMountpath(req), '') : req.path) +
|
|
389
|
+
(req.endsWithSlash && req.path !== '/' && this.get('strict routing') ? '/' : '');
|
|
390
|
+
req.url = req._opPath + req.urlQuery;
|
|
391
|
+
if(req.url === '') req.url = '/';
|
|
392
|
+
if(req.app.parent && route.callback.constructor.name === 'Application') {
|
|
393
|
+
req.app = req.app.parent;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return resolve(this._routeRequest(req, res, routeIndex + 1, routes, skipCheck, skipUntil));
|
|
397
|
+
} else {
|
|
398
|
+
this.#handleError(thingamabob, req, res);
|
|
399
|
+
return resolve(true);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const callback = route.callbacks[callbackindex++];
|
|
403
|
+
if(!callback) {
|
|
404
|
+
return next('route');
|
|
405
|
+
}
|
|
406
|
+
if(callback instanceof Router) {
|
|
407
|
+
if(callback.constructor.name === 'Application') {
|
|
408
|
+
req.app = callback;
|
|
409
|
+
}
|
|
410
|
+
if(callback.settings.mergeParams) {
|
|
411
|
+
req._paramStack.push(req.params);
|
|
412
|
+
}
|
|
413
|
+
const routed = await callback._routeRequest(req, res, 0);
|
|
414
|
+
if(routed) return resolve(true);
|
|
415
|
+
next();
|
|
416
|
+
} else {
|
|
417
|
+
try {
|
|
418
|
+
// skipping routes we already went through via optimized path
|
|
419
|
+
if(!skipCheck && skipUntil && skipUntil.routeKey >= route.routeKey) {
|
|
420
|
+
return next();
|
|
421
|
+
}
|
|
422
|
+
await callback(req, res, next);
|
|
423
|
+
} catch(err) {
|
|
424
|
+
this.#handleError(err, req, res);
|
|
425
|
+
return resolve(true);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
req.next = next;
|
|
430
|
+
if(continueRoute === 'route') {
|
|
431
|
+
next('route');
|
|
432
|
+
} else if(continueRoute) {
|
|
433
|
+
next();
|
|
434
|
+
} else {
|
|
435
|
+
resolve(true);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
use(path, ...callbacks) {
|
|
441
|
+
if(typeof path === 'function' || path instanceof Router || (Array.isArray(path) && path.every(p => typeof p === 'function' || p instanceof Router))) {
|
|
442
|
+
if(callbacks.length === 0 && typeof path === 'function' && path.length === 4) {
|
|
443
|
+
this.errorRoute = path;
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
callbacks.unshift(path);
|
|
447
|
+
path = '';
|
|
448
|
+
}
|
|
449
|
+
if(path === '/') {
|
|
450
|
+
path = '';
|
|
451
|
+
}
|
|
452
|
+
for(let callback of callbacks) {
|
|
453
|
+
if(callback instanceof Router) {
|
|
454
|
+
callback.mountpath = path;
|
|
455
|
+
callback.parent = this;
|
|
456
|
+
callback.emit('mount', this);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
this.#createRoute('USE', path, this, ...callbacks);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
route(path) {
|
|
463
|
+
let fns = {};
|
|
464
|
+
for(let method of methods) {
|
|
465
|
+
fns[method] = (...callbacks) => {
|
|
466
|
+
return this.#createRoute(method.toUpperCase(), path, fns, ...callbacks);
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
fns.get = (...callbacks) => {
|
|
470
|
+
return this.#createRoute('GET', path, fns, ...callbacks);
|
|
471
|
+
};
|
|
472
|
+
return fns;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
_generateErrorPage(err, checkEnv = false) {
|
|
476
|
+
if(checkEnv && this.get('env') === 'production') {
|
|
477
|
+
err = 'Internal Server Error';
|
|
478
|
+
}
|
|
479
|
+
return `<!DOCTYPE html>\n` +
|
|
480
|
+
`<html lang="en">\n` +
|
|
481
|
+
`<head>\n` +
|
|
482
|
+
`<meta charset="utf-8">\n` +
|
|
483
|
+
`<title>Error</title>\n` +
|
|
484
|
+
`</head>\n` +
|
|
485
|
+
`<body>\n` +
|
|
486
|
+
`<pre>${err?.stack ?? err}</pre>\n` +
|
|
487
|
+
`</body>\n` +
|
|
488
|
+
`</html>\n`;
|
|
489
|
+
}
|
|
490
|
+
}
|