router-http 0.1.1 → 0.1.2

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.
Files changed (3) hide show
  1. package/README.md +1 -4
  2. package/package.json +1 -1
  3. package/src/index.js +17 -13
package/README.md CHANGED
@@ -13,10 +13,7 @@ An HTTP router focused in only that, similar to [express@router](https://github.
13
13
 
14
14
  Don't get me wrong: The original Express router is a piece of art. I used it for years and I just considered create this library after experienced a bug that never was addressed in the stable version due to the [lack of maintenance](https://github.com/pillarjs/router/pull/60).
15
15
 
16
- While I was evaluating the market for finding an alternative I found [polka](https://github.com/lukeed/polka/tree/master/packages/polka) was a good starting point for creating a replacement. This module is different than polka in some aspects:
17
-
18
- - This module doesn't take care about the http.Server.
19
- - This module doesn't use any of the Node.js built-in module, so it can be used in Vercel Edge Functions, Deno or CF Workers.
16
+ While I was evaluating the market for finding an alternative, I found [polka](https://github.com/lukeed/polka/tree/master/packages/polka) was a good starting point for creating a replacement. This module is different from polka in it isn't taking care about http.Server, it just acts as an isolated module.
20
17
 
21
18
  ## Install
22
19
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "router-http",
3
3
  "description": "Simple HTTP router compatible with Express",
4
4
  "homepage": "https://nicedoc.io/Kikobeats/router-http",
5
- "version": "0.1.1",
5
+ "version": "0.1.2",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "josefrancisco.verdu@gmail.com",
package/src/index.js CHANGED
@@ -89,26 +89,30 @@ class Router extends Trouter {
89
89
  req.search = req.query ?? info.search
90
90
  req.query = req.query ?? info.query
91
91
  // Exit if only a single function
92
- let i = 0
93
- let len = middlewares.length
92
+ let index = 0
93
+ let size = middlewares.length
94
94
  const num = fns.length
95
- if (len === i && num === 1) return fns[0](undefined, req, res)
95
+ if (size === index && num === 1) return fns[0](undefined, req, res)
96
96
 
97
97
  // Otherwise loop thru all middlware
98
98
  const next = err => (err ? this.unhandler(err, req, res, next) : loop())
99
99
 
100
- const loop = () => {
101
- if (res.writableEnded) return
102
- if (i >= len) return
103
- try {
104
- return middlewares[i++](req, res, next)
105
- } catch (err) {
106
- return next(err)
107
- }
108
- }
100
+ const loop = () =>
101
+ res.writableEnded ||
102
+ (index < size &&
103
+ (() => {
104
+ try {
105
+ const mware = middlewares[index++]
106
+ return index === size
107
+ ? mware(undefined, req, res, next)
108
+ : mware(req, res, next)
109
+ } catch (err) {
110
+ return this.unhandler(err, req, res, next)
111
+ }
112
+ })())
109
113
 
110
114
  middlewares = middlewares.concat(fns)
111
- len += num
115
+ size += num
112
116
  loop() // init
113
117
  }
114
118
  }