0http-bun 1.0.4 → 1.1.1

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/index.js CHANGED
@@ -3,6 +3,6 @@ module.exports = (config = {}) => {
3
3
  const router = require('./lib/router/sequential')(config)
4
4
 
5
5
  return {
6
- router
6
+ router,
7
7
  }
8
8
  }
@@ -1,15 +1,17 @@
1
- const Trouter = require('trouter')
2
- const qs = require('fast-querystring')
3
- const next = require('./../next')
1
+ const { Trouter } = require("trouter")
2
+ const qs = require("fast-querystring")
3
+ const next = require("./../next")
4
4
 
5
5
  const STATUS_404 = {
6
- status: 404
6
+ status: 404,
7
7
  }
8
8
  const STATUS_500 = {
9
- status: 500
9
+ status: 500,
10
10
  }
11
11
 
12
12
  module.exports = (config = {}) => {
13
+ const cache = new Map()
14
+
13
15
  if (!config.defaultRoute) {
14
16
  config.defaultRoute = () => {
15
17
  return new Response(null, STATUS_404)
@@ -27,9 +29,9 @@ module.exports = (config = {}) => {
27
29
  const _use = router.use
28
30
 
29
31
  router.use = (prefix, ...middlewares) => {
30
- if (typeof prefix === 'function') {
32
+ if (typeof prefix === "function") {
31
33
  middlewares = [prefix, ...middlewares]
32
- prefix = '/'
34
+ prefix = "/"
33
35
  }
34
36
  _use.call(router, prefix, middlewares)
35
37
 
@@ -38,27 +40,45 @@ module.exports = (config = {}) => {
38
40
 
39
41
  router.fetch = (req) => {
40
42
  const url = req.url
41
- const startIndex = url.indexOf('/', 11)
42
- const queryIndex = url.indexOf('?', startIndex + 1)
43
- const path = queryIndex === -1 ? url.substring(startIndex) : url.substring(startIndex, queryIndex)
43
+ const startIndex = url.indexOf("/", 11)
44
+ const queryIndex = url.indexOf("?", startIndex + 1)
45
+ const path =
46
+ queryIndex === -1
47
+ ? url.substring(startIndex)
48
+ : url.substring(startIndex, queryIndex)
44
49
 
45
- req.path = path || '/'
50
+ req.path = path || "/"
46
51
  req.query = queryIndex > 0 ? qs.parse(url.substring(queryIndex + 1)) : {}
47
52
 
48
- const match = router.find(req.method, req.path)
49
- if (match.handlers.length > 0) {
53
+ const cacheKey = `${req.method}:${req.path}`
54
+ let match = null
55
+ if (cache.has(cacheKey)) {
56
+ match = cache.get(cacheKey)
57
+ } else {
58
+ match = router.find(req.method, req.path)
59
+ cache.set(cacheKey, match)
60
+ }
61
+
62
+ if (match?.handlers?.length > 0) {
50
63
  if (!req.params) {
51
64
  req.params = {}
52
65
  }
53
66
  Object.assign(req.params, match.params)
54
67
 
55
- return next(match.handlers, req, 0, config.defaultRoute, config.errorHandler)
68
+ return next(
69
+ match.handlers,
70
+ req,
71
+ 0,
72
+ config.defaultRoute,
73
+ config.errorHandler
74
+ )
56
75
  } else {
57
76
  return config.defaultRoute(req)
58
77
  }
59
78
  }
60
79
 
61
- router.on = (method, pattern, ...handlers) => router.add(method, pattern, handlers)
80
+ router.on = (method, pattern, ...handlers) =>
81
+ router.add(method, pattern, handlers)
62
82
 
63
83
  return router
64
84
  }
package/package.json CHANGED
@@ -1,15 +1,17 @@
1
1
  {
2
2
  "name": "0http-bun",
3
- "version": "1.0.4",
3
+ "version": "1.1.1",
4
4
  "description": "0http for Bun",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "lint": "bun x standard",
8
- "format": "bun x standard --fix"
8
+ "test": "bun test",
9
+ "bench": "bun run bench.js",
10
+ "format": "prettier --write *.js"
9
11
  },
10
12
  "dependencies": {
11
13
  "fast-querystring": "^1.1.2",
12
- "trouter": "^3.2.1"
14
+ "trouter": "^4.0.0"
13
15
  },
14
16
  "repository": {
15
17
  "type": "git",
@@ -24,9 +26,10 @@
24
26
  "lib/"
25
27
  ],
26
28
  "devDependencies": {
27
- "0http-bun": "^1.0.3",
28
- "bun-types": "^1.1.8",
29
- "mitata": "^0.1.11"
29
+ "0http-bun": "^1.1.0",
30
+ "bun-types": "^1.2.5",
31
+ "mitata": "^1.0.34",
32
+ "prettier": "^3.5.3"
30
33
  },
31
34
  "keywords": [
32
35
  "http",