router-http 0.1.3 → 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/LICENSE.md CHANGED
File without changes
package/README.md CHANGED
@@ -4,16 +4,15 @@
4
4
  [![Coverage Status](https://img.shields.io/coveralls/Kikobeats/router-http.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/router-http)
5
5
  [![NPM Status](https://img.shields.io/npm/dm/router-http.svg?style=flat-square)](https://www.npmjs.org/package/router-http)
6
6
 
7
- An HTTP router focused in only that, similar to [express@router](https://github.com/pillarjs/router), but:
7
+ A middleware style router, similar to [express@router](https://github.com/pillarjs/router), plus:
8
8
 
9
- - Focused in just one thing.
9
+ - Faster (x3 compared with Express).
10
10
  - Maintained and well tested.
11
- - Smaller and portable (1.4 kB).
12
- - Most of router API is supported.
11
+ - Smaller (1.4 kB).
13
12
 
14
13
  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
14
 
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.
15
+ 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 library is different from polka in that it only contains the code that is strictly necessary for routing, nothing else.
17
16
 
18
17
  ## Install
19
18
 
@@ -30,17 +29,17 @@ const createRouter = require('router-http')
30
29
 
31
30
  const router = createRouter((error, req, res) => {
32
31
  const hasError = error !== undefined
33
- res.statusCode = hasError ? 500 : 404
34
- res.end(hasError ? err.message : 'Not Found')
32
+ res.statusCode = hasError ? error.statusCode ?? 500 : 404
33
+ res.end(hasError ? error.message ?? 'Internal Server Error' : 'Not Found')
35
34
  })
36
35
  ```
37
36
 
38
37
  The router requires a final handler that will be called if an error occurred or none of the routes match.
39
38
 
40
- After that, you can declare any HTTP verb route:
41
-
42
39
  ### Declaring routes
43
40
 
41
+ The routes are declared using HTTP verbs:
42
+
44
43
  ```js
45
44
  /**
46
45
  * Declaring multiple routes based on the HTTP verb.
@@ -97,6 +96,18 @@ router
97
96
  })
98
97
  ```
99
98
 
99
+ Also, you can declare conditional middlewares:
100
+
101
+ ```js
102
+ /**
103
+ * Just add the middleware if it's production environment
104
+ */
105
+ router
106
+ .use(process.env.NODE_ENV === 'production' && authentication())
107
+ ```
108
+
109
+ They are only will add if the condition is satisfied.
110
+
100
111
  ### Prefixing routes
101
112
 
102
113
  In case you need you can prefix all the routes:
@@ -107,7 +118,7 @@ routes.get('/', (req, res) => res.end('Welcome to my API!'))
107
118
  /**
108
119
  * Prefix all routes with the API version
109
120
  */
110
- const router = Router(final)
121
+ const router = createRouter(final)
111
122
  router
112
123
  .use('/latest', routes)
113
124
  .use('/v1', routes)
@@ -123,19 +134,44 @@ const server = http.createServer(router)
123
134
 
124
135
  ## Benchmark
125
136
 
126
- The performance is essentially the same than polka, and that is almost x3 faster than express.
137
+ **express@4.18.2**
127
138
 
128
- See [benchmark](/benchmark) sections.
139
+ ```
140
+ Running 30s test @ http://localhost:3000/user/123
141
+ 8 threads and 100 connections
142
+ Thread Stats Avg Stdev Max +/- Stdev
143
+ Latency 4.12ms 653.26us 21.71ms 89.35%
144
+ Req/Sec 2.93k 159.60 5.99k 84.75%
145
+ 700421 requests in 30.06s, 102.87MB read
146
+ Requests/sec: 23304.22
147
+ Transfer/sec: 3.42MB
148
+ ```
149
+
150
+ **router-http@1.0.0**
151
+
152
+ ```
153
+ Running 30s test @ http://localhost:3000/user/123
154
+ 8 threads and 100 connections
155
+ Thread Stats Avg Stdev Max +/- Stdev
156
+ Latency 1.33ms 690.36us 30.28ms 97.16%
157
+ Req/Sec 9.27k 1.09k 11.76k 89.58%
158
+ 2214097 requests in 30.02s, 276.61MB read
159
+ Requests/sec: 73754.53
160
+ Transfer/sec: 9.21MB
161
+ ```
162
+
163
+ See more details, check [benchmark](/benchmark) section.
129
164
 
130
165
  ## Related
131
166
 
132
167
  - [send-http](https://github.com/Kikobeats/send-http) – A `res.end` with data type detection.
133
168
  - [http-body](https://github.com/Kikobeats/http-body) – Parse the http.IncomingMessage body into text/json/buffer.
134
- - [http-compression](https://github.com/Kikobeats/http-compression) – Adding compression (gzip/brotli) for your HTTP server in Node.js
135
- - [to-query](https://github.com/Kikobeats/to-query) Get query object from a request url.
169
+ - [http-compression](https://github.com/Kikobeats/http-compression) – Adding compression (gzip/brotli) for your HTTP server in Node.js.
136
170
 
137
171
  ## License
138
172
 
173
+ Full credits to [Luke Edwards](https://github.com/lukeed) for writing [Polka](https://github.com/lukeed/polka) and inspired this project.
174
+
139
175
  **router-http** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/router-http/blob/master/LICENSE.md) License.<br>
140
176
  Authored and maintained by [Kiko Beats](https://kikobeats.com) with help from [contributors](https://github.com/Kikobeats/router-http/contributors).
141
177
 
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.3",
5
+ "version": "1.0.0",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "josefrancisco.verdu@gmail.com",
@@ -28,13 +28,14 @@
28
28
  "routes"
29
29
  ],
30
30
  "dependencies": {
31
- "trouter": "~3.2.0"
31
+ "trouter": "~3.2.1"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@commitlint/cli": "latest",
35
35
  "@commitlint/config-conventional": "latest",
36
36
  "ava": "latest",
37
37
  "c8": "latest",
38
+ "ci-publish": "latest",
38
39
  "conventional-github-releaser": "latest",
39
40
  "finepack": "latest",
40
41
  "git-authors-cli": "latest",
@@ -53,6 +54,21 @@
53
54
  "files": [
54
55
  "src"
55
56
  ],
57
+ "scripts": {
58
+ "clean": "rm -rf node_modules",
59
+ "contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
60
+ "coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
61
+ "lint": "standard-markdown README.md && standard",
62
+ "postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
63
+ "prerelease": "npm run update:check && npm run contributors",
64
+ "pretest": "npm run lint",
65
+ "release": "standard-version -a",
66
+ "release:github": "conventional-github-releaser -p angular",
67
+ "release:tags": "git push --follow-tags origin HEAD:master",
68
+ "test": "c8 ava",
69
+ "update": "ncu -u",
70
+ "update:check": "ncu -- --error-level 2"
71
+ },
56
72
  "license": "MIT",
57
73
  "commitlint": {
58
74
  "extends": [
@@ -73,20 +89,5 @@
73
89
  "simple-git-hooks": {
74
90
  "commit-msg": "npx commitlint --edit",
75
91
  "pre-commit": "npx nano-staged"
76
- },
77
- "scripts": {
78
- "clean": "rm -rf node_modules",
79
- "contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
80
- "coverage": "c8 report --reporter=text-lcov > coverage/lcov.info",
81
- "lint": "standard-markdown README.md && standard",
82
- "postrelease": "npm run release:tags && npm run release:github && npm publish",
83
- "prerelease": "npm run update:check",
84
- "pretest": "npm run lint",
85
- "release": "standard-version -a",
86
- "release:github": "conventional-github-releaser -p angular",
87
- "release:tags": "git push --follow-tags origin HEAD:master",
88
- "test": "c8 ava",
89
- "update": "ncu -u",
90
- "update:check": "ncu -- --error-level 2"
91
92
  }
92
- }
93
+ }
package/src/index.js CHANGED
@@ -49,24 +49,24 @@ class Router extends Trouter {
49
49
  #middlewaresBy = []
50
50
 
51
51
  /**
52
- * Middleware declaration, where the base is optional
52
+ * Middleware declaration, where the page is optional
53
53
  * .use(one)
54
54
  * .use('/v1', one)
55
55
  * .use(one, two)
56
56
  * .use('/v2', two)
57
57
  */
58
- use = (base = '/', ...fns) => {
59
- if (typeof base === 'function' || typeof base === 'boolean') {
60
- this.#middlewares = this.#middlewares.concat(base, fns).filter(Boolean)
61
- } else if (base === '/') {
58
+ use = (page = '/', ...fns) => {
59
+ if (typeof page === 'function' || typeof page === 'boolean') {
60
+ this.#middlewares = this.#middlewares.concat(page, fns).filter(Boolean)
61
+ } else if (page === '/') {
62
62
  this.#middlewares = this.#middlewares.concat(fns).filter(Boolean)
63
63
  } else {
64
- base = lead(base)
64
+ page = lead(page)
65
65
  fns.filter(Boolean).forEach(fn => {
66
- const array = this.#middlewaresBy[base] ?? []
66
+ const array = this.#middlewaresBy[page] ?? []
67
67
  // eslint-disable-next-line no-sequences
68
- array.length > 0 || array.push((r, _, nxt) => (mutate(base, r), nxt()))
69
- this.#middlewaresBy[base] = array.concat(fn)
68
+ array.length > 0 || array.push((r, _, nxt) => (mutate(page, r), nxt()))
69
+ this.#middlewaresBy[page] = array.concat(fn)
70
70
  })
71
71
  }
72
72
  return this
@@ -77,9 +77,9 @@ class Router extends Trouter {
77
77
  let fns = []
78
78
  let middlewares = this.#middlewares
79
79
  const route = this.find(req.method, info.pathname)
80
- const base = value((req.path = info.pathname))
81
- if (this.#middlewaresBy[base] !== undefined) {
82
- middlewares = middlewares.concat(this.#middlewaresBy[base])
80
+ const page = value((req.path = info.pathname))
81
+ if (this.#middlewaresBy[page] !== undefined) {
82
+ middlewares = middlewares.concat(this.#middlewaresBy[page])
83
83
  }
84
84
  if (route) {
85
85
  fns = route.handlers