router-http 0.1.1 → 0.1.3
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 +1 -4
- package/package.json +1 -1
- package/src/index.js +21 -17
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
|
|
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.
|
|
5
|
+
"version": "0.1.3",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "josefrancisco.verdu@gmail.com",
|
package/src/index.js
CHANGED
|
@@ -56,13 +56,13 @@ class Router extends Trouter {
|
|
|
56
56
|
* .use('/v2', two)
|
|
57
57
|
*/
|
|
58
58
|
use = (base = '/', ...fns) => {
|
|
59
|
-
if (typeof base === 'function') {
|
|
60
|
-
this.#middlewares = this.#middlewares.concat(base, fns)
|
|
59
|
+
if (typeof base === 'function' || typeof base === 'boolean') {
|
|
60
|
+
this.#middlewares = this.#middlewares.concat(base, fns).filter(Boolean)
|
|
61
61
|
} else if (base === '/') {
|
|
62
|
-
this.#middlewares = this.#middlewares.concat(fns)
|
|
62
|
+
this.#middlewares = this.#middlewares.concat(fns).filter(Boolean)
|
|
63
63
|
} else {
|
|
64
64
|
base = lead(base)
|
|
65
|
-
fns.forEach(fn => {
|
|
65
|
+
fns.filter(Boolean).forEach(fn => {
|
|
66
66
|
const array = this.#middlewaresBy[base] ?? []
|
|
67
67
|
// eslint-disable-next-line no-sequences
|
|
68
68
|
array.length > 0 || array.push((r, _, nxt) => (mutate(base, r), nxt()))
|
|
@@ -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
|
|
93
|
-
let
|
|
92
|
+
let index = 0
|
|
93
|
+
let size = middlewares.length
|
|
94
94
|
const num = fns.length
|
|
95
|
-
if (
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
115
|
+
size += num
|
|
112
116
|
loop() // init
|
|
113
117
|
}
|
|
114
118
|
}
|