0http-bun 0.0.2 → 0.0.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 +4 -5
- package/demos/basic.js +1 -4
- package/demos/minimal.js +1 -4
- package/lib/next.js +3 -18
- package/lib/router/sequential.js +8 -47
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -8,7 +8,9 @@ Experimental, bun-based HTTP framework inspired by [0http](https://0http.21no.de
|
|
|
8
8
|
```js
|
|
9
9
|
const http = require('0http-bun')
|
|
10
10
|
|
|
11
|
-
const { router } = http({
|
|
11
|
+
const { router } = http({
|
|
12
|
+
port: 3000
|
|
13
|
+
})
|
|
12
14
|
router.use((req, next) => {
|
|
13
15
|
req.ctx = {
|
|
14
16
|
engine: 'bun'
|
|
@@ -28,10 +30,7 @@ router.delete('/:id', async (req) => {
|
|
|
28
30
|
})
|
|
29
31
|
})
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
port: 3000,
|
|
33
|
-
fetch: (request) => router.lookup(request)
|
|
34
|
-
}
|
|
33
|
+
export default router
|
|
35
34
|
```
|
|
36
35
|
# Benchmarks
|
|
37
36
|
## 0http-bun (bun v0.2.2)
|
package/demos/basic.js
CHANGED
package/demos/minimal.js
CHANGED
package/lib/next.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
module.exports = function next (middlewares, req, index,
|
|
1
|
+
module.exports = function next (middlewares, req, index, defaultRoute, errorHandler) {
|
|
2
2
|
const middleware = middlewares[index]
|
|
3
3
|
if (!middleware) {
|
|
4
4
|
return defaultRoute(req)
|
|
@@ -8,27 +8,12 @@ module.exports = function next (middlewares, req, index, routers = {}, defaultRo
|
|
|
8
8
|
if (err) {
|
|
9
9
|
return errorHandler(err, req)
|
|
10
10
|
} else {
|
|
11
|
-
return next(middlewares, req, index + 1,
|
|
11
|
+
return next(middlewares, req, index + 1, defaultRoute, errorHandler)
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
try {
|
|
16
|
-
|
|
17
|
-
// nested routes support
|
|
18
|
-
const pattern = routers[middleware.id]
|
|
19
|
-
if (pattern) {
|
|
20
|
-
req.preRouterPath = req.path
|
|
21
|
-
|
|
22
|
-
req.path = req.path.replace(pattern, '')
|
|
23
|
-
if (!req.path.startsWith('/')) {
|
|
24
|
-
req.path = '/' + req.path
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return middleware.lookup(req, step)
|
|
29
|
-
} else {
|
|
30
|
-
return middleware(req, step)
|
|
31
|
-
}
|
|
16
|
+
return middleware(req, step)
|
|
32
17
|
} catch (err) {
|
|
33
18
|
return errorHandler(err, req)
|
|
34
19
|
}
|
package/lib/router/sequential.js
CHANGED
|
@@ -2,11 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const Trouter = require('trouter')
|
|
4
4
|
const next = require('./../next')
|
|
5
|
-
const LRU = require('lru-cache')
|
|
6
|
-
const { parse } = require('regexparam')
|
|
7
5
|
|
|
8
6
|
module.exports = (config = {}) => {
|
|
9
|
-
if (config.defaultRoute
|
|
7
|
+
if (!config.defaultRoute) {
|
|
10
8
|
config.defaultRoute = (req) => {
|
|
11
9
|
const res = new Response(null, {
|
|
12
10
|
status: 404
|
|
@@ -15,7 +13,7 @@ module.exports = (config = {}) => {
|
|
|
15
13
|
return res
|
|
16
14
|
}
|
|
17
15
|
}
|
|
18
|
-
if (config.errorHandler
|
|
16
|
+
if (!config.errorHandler) {
|
|
19
17
|
config.errorHandler = (err, req) => {
|
|
20
18
|
const res = new Response(err.message, {
|
|
21
19
|
status: 500
|
|
@@ -24,18 +22,9 @@ module.exports = (config = {}) => {
|
|
|
24
22
|
return res
|
|
25
23
|
}
|
|
26
24
|
}
|
|
27
|
-
if (config.cacheSize === undefined) {
|
|
28
|
-
config.cacheSize = 1000
|
|
29
|
-
}
|
|
30
|
-
if (config.id === undefined) {
|
|
31
|
-
config.id = (Date.now().toString(36) + Math.random().toString(36).substring(2, 5)).toUpperCase()
|
|
32
|
-
}
|
|
33
25
|
|
|
34
|
-
const routers = {}
|
|
35
|
-
const isCacheEnabled = config.cacheSize > 0
|
|
36
|
-
const cache = isCacheEnabled ? new LRU({ max: config.cacheSize }) : null
|
|
37
26
|
const router = new Trouter()
|
|
38
|
-
router.
|
|
27
|
+
router.port = config.port || 3000
|
|
39
28
|
|
|
40
29
|
const _use = router.use
|
|
41
30
|
|
|
@@ -46,53 +35,25 @@ module.exports = (config = {}) => {
|
|
|
46
35
|
}
|
|
47
36
|
_use.call(router, prefix, middlewares)
|
|
48
37
|
|
|
49
|
-
if (middlewares[0].id) {
|
|
50
|
-
// caching router -> pattern relation for urls pattern replacement
|
|
51
|
-
const { pattern } = parse(prefix, true)
|
|
52
|
-
routers[middlewares[0].id] = pattern
|
|
53
|
-
}
|
|
54
|
-
|
|
55
38
|
return this
|
|
56
39
|
}
|
|
57
40
|
|
|
58
|
-
router.
|
|
41
|
+
router.fetch = (req, step) => {
|
|
59
42
|
const url = new URL(req.url)
|
|
43
|
+
|
|
60
44
|
req.path = url.pathname || '/'
|
|
61
45
|
req.query = url.queryparams
|
|
62
46
|
req.search = url.search
|
|
63
47
|
req.hostname = url.hostname
|
|
64
48
|
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
const reqCacheKey = req.method + req.path
|
|
68
|
-
match = cache.get(reqCacheKey)
|
|
69
|
-
if (!match) {
|
|
70
|
-
match = router.find(req.method, req.path)
|
|
71
|
-
cache.set(reqCacheKey, match)
|
|
72
|
-
}
|
|
73
|
-
} else {
|
|
74
|
-
match = router.find(req.method, req.path)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (match.handlers.length !== 0) {
|
|
78
|
-
const middlewares = [...match.handlers]
|
|
79
|
-
if (step !== undefined) {
|
|
80
|
-
// router is being used as a nested router
|
|
81
|
-
middlewares.push((req, next) => {
|
|
82
|
-
req.path = req.preRouterPath
|
|
83
|
-
|
|
84
|
-
delete req.preRouterPath
|
|
85
|
-
|
|
86
|
-
return step()
|
|
87
|
-
})
|
|
88
|
-
}
|
|
89
|
-
|
|
49
|
+
const match = router.find(req.method, req.path)
|
|
50
|
+
if (match.handlers.length > 0) {
|
|
90
51
|
if (!req.params) {
|
|
91
52
|
req.params = {}
|
|
92
53
|
}
|
|
93
54
|
Object.assign(req.params, match.params)
|
|
94
55
|
|
|
95
|
-
return next(
|
|
56
|
+
return next(match.handlers, req, 0, config.defaultRoute, config.errorHandler)
|
|
96
57
|
} else {
|
|
97
58
|
return config.defaultRoute(req)
|
|
98
59
|
}
|
package/package.json
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "0http-bun",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "0http alternative for Bun",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"
|
|
7
|
+
"lint": "bun x standard",
|
|
8
|
+
"format": "bun x standard --fix"
|
|
8
9
|
},
|
|
9
10
|
"dependencies": {
|
|
10
|
-
"
|
|
11
|
-
"regexparam": "^2.0.0",
|
|
12
|
-
"trouter": "^3.2.0"
|
|
11
|
+
"trouter": "^3.2.1"
|
|
13
12
|
},
|
|
14
13
|
"repository": {
|
|
15
14
|
"type": "git",
|