Skip to content

Commit 46c4acc

Browse files
committed
cleanup
1 parent 3795243 commit 46c4acc

File tree

6 files changed

+39
-78
lines changed

6 files changed

+39
-78
lines changed

demos/basic.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,4 @@ router.delete('/:id', async (req) => {
2121
})
2222
})
2323

24-
module.exports = {
25-
port: 3000,
26-
fetch: (request) => router.lookup(request)
27-
}
24+
module.exports = router

demos/minimal.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ router.get('/hi', async (req) => {
77
return new Response('Hello World!')
88
})
99

10-
module.exports = {
11-
port: 3000,
12-
fetch: (request) => router.lookup(request)
13-
}
10+
module.exports = router

lib/next.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
1-
module.exports = function next (middlewares, req, index, routers = {}, defaultRoute, errorHandler) {
1+
module.exports = function next(middlewares, req, index, defaultRoute, errorHandler) {
22
const middleware = middlewares[index]
33
if (!middleware) {
44
return defaultRoute(req)
55
}
66

7-
function step (err) {
7+
function step(err) {
88
if (err) {
99
return errorHandler(err, req)
1010
} else {
11-
return next(middlewares, req, index + 1, routers, defaultRoute, errorHandler)
11+
return next(middlewares, req, index + 1, defaultRoute, errorHandler)
1212
}
1313
}
1414

1515
try {
16-
if (middleware.id) {
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)
3217
} catch (err) {
3318
return errorHandler(err, req)
3419
}

lib/router/sequential.js

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
const Trouter = require('trouter')
44
const next = require('./../next')
5-
const LRU = require('lru-cache')
6-
const { parse } = require('regexparam')
75

86
module.exports = (config = {}) => {
9-
if (config.defaultRoute === undefined) {
7+
if (!config.defaultRoute) {
108
config.defaultRoute = (req) => {
119
const res = new Response(null, {
1210
status: 404
@@ -15,7 +13,7 @@ module.exports = (config = {}) => {
1513
return res
1614
}
1715
}
18-
if (config.errorHandler === undefined) {
16+
if (!config.errorHandler) {
1917
config.errorHandler = (err, req) => {
2018
const res = new Response(err.message, {
2119
status: 500
@@ -24,18 +22,9 @@ module.exports = (config = {}) => {
2422
return res
2523
}
2624
}
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-
}
3325

34-
const routers = {}
35-
const isCacheEnabled = config.cacheSize > 0
36-
const cache = isCacheEnabled ? new LRU({ max: config.cacheSize }) : null
3726
const router = new Trouter()
38-
router.id = config.id
27+
router.port = config.port || 3000
3928

4029
const _use = router.use
4130

@@ -46,53 +35,24 @@ module.exports = (config = {}) => {
4635
}
4736
_use.call(router, prefix, middlewares)
4837

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-
5538
return this
5639
}
5740

58-
router.lookup = (req, step) => {
41+
router.fetch = (req, step) => {
5942
const url = new URL(req.url)
6043
req.path = url.pathname || '/'
6144
req.query = url.queryparams
6245
req.search = url.search
6346
req.hostname = url.hostname
6447

65-
let match
66-
if (isCacheEnabled) {
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-
48+
const match = router.find(req.method, req.path)
49+
if (match.handlers.length > 0) {
9050
if (!req.params) {
9151
req.params = {}
9252
}
9353
Object.assign(req.params, match.params)
9454

95-
return next(middlewares, req, 0, routers, config.defaultRoute, config.errorHandler)
55+
return next(match.handlers, req, 0, config.defaultRoute, config.errorHandler)
9656
} else {
9757
return config.defaultRoute(req)
9858
}

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
"description": "0http alternative for Bun",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"lint": "bun x rome ci . --semicolons=as-needed",
8+
"format": "bun x rome format . --semicolons=as-needed --write"
89
},
910
"dependencies": {
10-
"lru-cache": "^7.10.1",
11-
"regexparam": "^2.0.0",
12-
"trouter": "^3.2.0"
11+
"trouter": "^3.2.1"
1312
},
1413
"repository": {
1514
"type": "git",

rome.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"formatter": {
3+
"enabled": true,
4+
"indentStyle": "space",
5+
"indentSize": 2,
6+
"lineWidth": 120,
7+
"ignore": [
8+
"dist"
9+
]
10+
},
11+
"javascript": {
12+
"formatter": {
13+
"quoteStyle": "single",
14+
"trailingComma": "none"
15+
}
16+
},
17+
"linter": {
18+
"enabled": true,
19+
"ignore": [
20+
"dist"
21+
]
22+
}
23+
}

0 commit comments

Comments
 (0)