ultimate-express 1.4.5 → 1.4.6

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/README.md CHANGED
@@ -323,6 +323,9 @@ Almost all middlewares that are compatible with Express are compatible with µEx
323
323
  - ✅ [vhost](https://npmjs.com/package/vhost)
324
324
  - ✅ [tsoa](https://github.com/lukeautry/tsoa)
325
325
  - ✅ [express-mongo-sanitize](https://www.npmjs.com/package/express-mongo-sanitize)
326
+ - ✅ [helmet](https://www.npmjs.com/package/helmet)
327
+ - ✅ [passport](https://www.npmjs.com/package/passport)
328
+ - ✅ [morgan](https://www.npmjs.com/package/morgan)
326
329
 
327
330
  Middlewares and modules that are confirmed to not work:
328
331
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultimate-express",
3
- "version": "1.4.5",
3
+ "version": "1.4.6",
4
4
  "description": "The Ultimate Express. Fastest http server with full Express compatibility, based on uWebSockets.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -86,11 +86,15 @@
86
86
  "express-session": "^1.18.0",
87
87
  "express-subdomain": "^1.0.6",
88
88
  "formdata-node": "^6.0.3",
89
+ "helmet": "^8.1.0",
89
90
  "method-override": "^3.0.0",
91
+ "morgan": "^1.10.0",
90
92
  "multer": "^1.4.5-lts.1",
91
93
  "mustache-express": "^1.3.2",
92
94
  "nyc": "^17.1.0",
93
95
  "pako": "^2.1.0",
96
+ "passport": "^0.7.0",
97
+ "passport-local": "^1.0.0",
94
98
  "pkg-pr-new": "^0.0.29",
95
99
  "pug": "^3.0.3",
96
100
  "response-time": "^2.3.3",
@@ -188,6 +188,9 @@ class Application extends Router {
188
188
 
189
189
  const matchedRoute = await this._routeRequest(request, response);
190
190
  if(!matchedRoute && !response.headersSent && !response.aborted) {
191
+ if(request._error) {
192
+ return this._handleError(request._error, null, request, response);
193
+ }
191
194
  response.status(404);
192
195
  this._sendErrorPage(request, response, `Cannot ${request.method} ${request.path}`, false);
193
196
  }
package/src/router.js CHANGED
@@ -46,7 +46,6 @@ module.exports = class Router extends EventEmitter {
46
46
  this._paramCallbacks = new Map();
47
47
  this._mountpathCache = new Map();
48
48
  this._routes = [];
49
- this.errorRoute = undefined;
50
49
  this.mountpath = '/';
51
50
  this.settings = settings;
52
51
  this._request = Request;
@@ -293,6 +292,9 @@ module.exports = class Router extends EventEmitter {
293
292
  }
294
293
  const matchedRoute = await this._routeRequest(request, response, 0, optimizedPath, true, route);
295
294
  if(!matchedRoute && !response.headersSent && !response.aborted) {
295
+ if(request._error) {
296
+ return this._handleError(request._error, null, request, response);
297
+ }
296
298
  response.status(404);
297
299
  request.noEtag = true;
298
300
  this._sendErrorPage(request, response, `Cannot ${request.method} ${request._originalPath}`, false);
@@ -332,20 +334,12 @@ module.exports = class Router extends EventEmitter {
332
334
  }
333
335
  }
334
336
 
335
- _handleError(err, request, response) {
336
- let errorRoute = this.errorRoute, parent = this.parent;
337
- while(!errorRoute && parent) {
338
- errorRoute = parent.errorRoute;
339
- parent = parent.parent;
340
- }
341
- if(errorRoute) {
342
- return errorRoute(err, request, response, () => {
343
- if(!response.headersSent) {
344
- if(response.statusCode === 200) {
345
- response.statusCode = 500;
346
- }
347
- this._sendErrorPage(request, response, err, true);
348
- }
337
+ _handleError(err, handler, request, response) {
338
+ if(handler) {
339
+ return handler(err, request, response, () => {
340
+ delete request._error;
341
+ delete request._errorKey;
342
+ return request.next();
349
343
  });
350
344
  }
351
345
  console.error(err);
@@ -405,8 +399,8 @@ module.exports = class Router extends EventEmitter {
405
399
  if(thingamabob === 'route') {
406
400
  return resolve('route');
407
401
  } else {
408
- this._handleError(thingamabob, req, res);
409
- return resolve(false);
402
+ req._error = thingamabob;
403
+ req._errorKey = route.routeKey;
410
404
  }
411
405
  }
412
406
  return resolveRoute();
@@ -460,7 +454,7 @@ module.exports = class Router extends EventEmitter {
460
454
  let callbackindex = 0;
461
455
 
462
456
  // avoid calling _preprocessRequest as async function as its slower
463
- // but it seems like calling it as async has unintended consequence of resetting max call stack size
457
+ // but it seems like calling it as async has unintended, but useful consequence of resetting max call stack size
464
458
  // so call it as async when the request has been through every 300 routes to reset it
465
459
  const continueRoute = this._paramCallbacks.size === 0 && req.routeCount % 300 !== 0 ?
466
460
  this._preprocessRequest(req, res, route) : await this._preprocessRequest(req, res, route);
@@ -513,8 +507,8 @@ module.exports = class Router extends EventEmitter {
513
507
  req.routeCount++;
514
508
  return resolve(this._routeRequest(req, res, routeIndex + 1, routes, skipCheck, skipUntil));
515
509
  } else {
516
- this._handleError(thingamabob, req, res);
517
- return resolve(true);
510
+ req._error = thingamabob;
511
+ req._errorKey = route.routeKey;
518
512
  }
519
513
  }
520
514
  const callback = route.callbacks[callbackindex++];
@@ -535,6 +529,15 @@ module.exports = class Router extends EventEmitter {
535
529
  if(routed) return resolve(true);
536
530
  next();
537
531
  } else {
532
+ // handle errors and error handlers
533
+ if(req._error || callback.length === 4) {
534
+ if(req._error && callback.length === 4 && route.routeKey >= req._errorKey) {
535
+ return this._handleError(req._error, callback, req, res);
536
+ } else {
537
+ return next();
538
+ }
539
+ }
540
+
538
541
  try {
539
542
  // skipping routes we already went through via optimized path
540
543
  if(!skipCheck && skipUntil && skipUntil.routeKey >= route.routeKey) {
@@ -544,16 +547,18 @@ module.exports = class Router extends EventEmitter {
544
547
  if(out instanceof Promise) {
545
548
  out.catch(err => {
546
549
  if(this.get("catch async errors")) {
547
- this._handleError(err, req, res);
548
- return resolve(true);
550
+ req._error = err;
551
+ req._errorKey = route.routeKey;
552
+ return next();
549
553
  } else {
550
554
  throw err;
551
555
  }
552
556
  });
553
557
  }
554
558
  } catch(err) {
555
- this._handleError(err, req, res);
556
- return resolve(true);
559
+ req._error = err;
560
+ req._errorKey = route.routeKey;
561
+ return next();
557
562
  }
558
563
  }
559
564
  }
@@ -570,10 +575,6 @@ module.exports = class Router extends EventEmitter {
570
575
 
571
576
  use(path, ...callbacks) {
572
577
  if(typeof path === 'function' || path instanceof Router || (Array.isArray(path) && path.every(p => typeof p === 'function' || p instanceof Router))) {
573
- if(callbacks.length === 0 && typeof path === 'function' && path.length === 4) {
574
- this.errorRoute = path;
575
- return;
576
- }
577
578
  callbacks.unshift(path);
578
579
  path = '';
579
580
  }