ultimate-express 1.0.9 → 1.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultimate-express",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
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": {
@@ -17,6 +17,7 @@ function static(root, options) {
17
17
  options.extensions = options.extensions.map(ext => ext.startsWith('.') ? ext.slice(1) : ext);
18
18
  }
19
19
  options.root = root;
20
+ options.skipEncodePath = true;
20
21
 
21
22
  return (req, res, next) => {
22
23
  const iq = req.url.indexOf('?');
package/src/response.js CHANGED
@@ -244,6 +244,9 @@ module.exports = class Response extends Writable {
244
244
  this.status(500);
245
245
  return done(new Error('path must be absolute or specify root to res.sendFile'));
246
246
  }
247
+ if(!options.skipEncodePath) {
248
+ path = encodeURI(path);
249
+ }
247
250
  path = decode(path);
248
251
  if(path === -1) {
249
252
  this.status(400);
@@ -349,27 +352,30 @@ module.exports = class Response extends Writable {
349
352
 
350
353
  // range requests
351
354
  let offset = 0, len = stat.size, ranged = false;
352
- if(options.acceptRanges && this.req.headers.range) {
353
- let ranges = this.req.range(stat.size, {
354
- combine: true
355
- });
356
-
357
- // if-range
358
- if(!isRangeFresh(this.req, this)) {
359
- ranges = -2;
360
- }
361
-
362
- if(ranges === -1) {
363
- this.status(416);
364
- this.set('Content-Range', `bytes */${stat.size}`);
365
- return done(new Error('Range Not Satisfiable'));
366
- }
367
- if(ranges !== -2 && ranges.length === 1) {
368
- this.status(206);
369
- this.set('Content-Range', `bytes ${ranges[0].start}-${ranges[0].end}/${stat.size}`);
370
- offset = ranges[0].start;
371
- len = ranges[0].end - ranges[0].start + 1;
372
- ranged = true;
355
+ if(options.acceptRanges) {
356
+ this.set('accept-ranges', 'bytes');
357
+ if(this.req.headers.range) {
358
+ let ranges = this.req.range(stat.size, {
359
+ combine: true
360
+ });
361
+
362
+ // if-range
363
+ if(!isRangeFresh(this.req, this)) {
364
+ ranges = -2;
365
+ }
366
+
367
+ if(ranges === -1) {
368
+ this.status(416);
369
+ this.set('Content-Range', `bytes */${stat.size}`);
370
+ return done(new Error('Range Not Satisfiable'));
371
+ }
372
+ if(ranges !== -2 && ranges.length === 1) {
373
+ this.status(206);
374
+ this.set('Content-Range', `bytes ${ranges[0].start}-${ranges[0].end}/${stat.size}`);
375
+ offset = ranges[0].start;
376
+ len = ranges[0].end - ranges[0].start + 1;
377
+ ranged = true;
378
+ }
373
379
  }
374
380
  }
375
381
 
package/src/router.js CHANGED
@@ -12,6 +12,7 @@ const methods = [
12
12
  'search', 'subscribe', 'unsubscribe', 'report', 'mkactivity', 'mkcalendar',
13
13
  'checkout', 'merge', 'm-search', 'notify', 'subscribe', 'unsubscribe', 'search'
14
14
  ];
15
+ const supportedUwsMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'CONNECT', 'TRACE'];
15
16
 
16
17
  module.exports = class Router extends EventEmitter {
17
18
  #paramCallbacks = new Map();
@@ -122,8 +123,7 @@ module.exports = class Router extends EventEmitter {
122
123
  routes.push(route);
123
124
  // normal routes optimization
124
125
  if(typeof route.pattern === 'string' && route.pattern !== '/*' && !this.parent && this.get('case sensitive routing') && this.uwsApp) {
125
- // the only methods that uWS supports natively
126
- if(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'CONNECT', 'TRACE'].includes(method)) {
126
+ if(supportedUwsMethods.includes(method)) {
127
127
  const optimizedPath = this.#optimizeRoute(route, this._routes);
128
128
  if(optimizedPath) {
129
129
  this.#registerUwsRoute(route, optimizedPath);
@@ -153,13 +153,13 @@ module.exports = class Router extends EventEmitter {
153
153
  return; // can only optimize router whos parent is listening
154
154
  }
155
155
  for(let cbroute of callback._routes) {
156
- if(!needsConversionToRegex(cbroute.path) && cbroute.path !== '/*') {
156
+ if(!needsConversionToRegex(cbroute.path) && cbroute.path !== '/*' && supportedUwsMethods.includes(cbroute.method)) {
157
157
  let optimizedRouterPath = this.#optimizeRoute(cbroute, callback._routes);
158
158
  if(optimizedRouterPath) {
159
159
  optimizedRouterPath = optimizedRouterPath.slice(0, -1);
160
160
  const optimizedPath = [...optimizedPathToRouter, {
161
- ...route,
162
161
  // fake route to update req._opPath and req.url
162
+ ...route,
163
163
  callbacks: [
164
164
  (req, res, next) => {
165
165
  next('skipPop');
@@ -263,8 +263,13 @@ module.exports = class Router extends EventEmitter {
263
263
  }
264
264
 
265
265
  #handleError(err, request, response) {
266
- if(this.errorRoute) {
267
- return this.errorRoute(err, request, response, () => {
266
+ let errorRoute = this.errorRoute, parent = this.parent;
267
+ while(!errorRoute && parent) {
268
+ errorRoute = parent.errorRoute;
269
+ parent = parent.parent;
270
+ }
271
+ if(errorRoute) {
272
+ return errorRoute(err, request, response, () => {
268
273
  if(!response.headersSent) {
269
274
  if(response.statusCode === 200) {
270
275
  response.statusCode = 500;