Skip to content

Commit e61c20f

Browse files
authored
refactor: apply consistent formatting and update dependencies (#7)
* refactor: apply consistent formatting and update dependencies * ci: update GitHub Actions workflow for linting and formatting
1 parent e0d307b commit e61c20f

File tree

7 files changed

+112
-67
lines changed

7 files changed

+112
-67
lines changed

.github/workflows/tests.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ jobs:
66
runs-on: ubuntu-latest
77
steps:
88
- uses: actions/checkout@v2
9-
9+
1010
- name: Setup Bun
1111
uses: oven-sh/setup-bun@v1
1212
with:
1313
bun-version: latest
14-
14+
1515
- name: Install dependencies
1616
run: bun install
17-
17+
1818
- name: Linting
19-
run: bun x standard
20-
19+
run: bun run format
20+
2121
- name: Run tests
22-
run: bun test
22+
run: bun test

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"semi": false,
5+
"bracketSpacing": false
6+
}

bench.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
import { run, bench, group } from "mitata";
2-
import httpNext from "./index";
3-
import httpPrevious from "0http-bun";
1+
import { run, bench, group } from "mitata"
2+
import httpNext from "./index"
3+
import httpPrevious from "0http-bun"
44

55
function setupRouter(router) {
66
router.use((req, next) => {
7-
return next();
8-
});
7+
return next()
8+
})
99

1010
router.get("/", () => {
11-
return new Response();
12-
});
11+
return new Response()
12+
})
1313
router.get("/:id", async (req) => {
14-
return new Response(req.params.id);
15-
});
14+
return new Response(req.params.id)
15+
})
1616
router.get("/:id/error", () => {
17-
throw new Error("Error");
18-
});
17+
throw new Error("Error")
18+
})
1919
}
2020

21-
const { router } = httpNext();
22-
setupRouter(router);
21+
const { router } = httpNext()
22+
setupRouter(router)
2323

24-
const { router: routerPrevious } = httpPrevious();
25-
setupRouter(routerPrevious);
24+
const { router: routerPrevious } = httpPrevious()
25+
setupRouter(routerPrevious)
2626

2727
group("Next Router", () => {
2828
bench("Parameter URL", () => {
29-
router.fetch(new Request(new URL("http://localhost/0")));
30-
});
29+
router.fetch(new Request(new URL("http://localhost/0")))
30+
}).gc("inner")
3131
bench("Not Found URL", () => {
32-
router.fetch(new Request(new URL("http://localhost/0/404")));
33-
});
32+
router.fetch(new Request(new URL("http://localhost/0/404")))
33+
}).gc("inner")
3434
bench("Error URL", () => {
35-
router.fetch(new Request(new URL("http://localhost/0/error")));
36-
});
37-
});
35+
router.fetch(new Request(new URL("http://localhost/0/error")))
36+
}).gc("inner")
37+
})
3838

3939
group("Previous Router", () => {
4040
bench("Parameter URL", () => {
41-
routerPrevious.fetch(new Request(new URL("http://localhost/0")));
42-
});
41+
routerPrevious.fetch(new Request(new URL("http://localhost/0")))
42+
}).gc("inner")
4343
bench("Not Found URL", () => {
44-
routerPrevious.fetch(new Request(new URL("http://localhost/0/404")));
45-
});
44+
routerPrevious.fetch(new Request(new URL("http://localhost/0/404")))
45+
}).gc("inner")
4646
bench("Error URL", () => {
47-
routerPrevious.fetch(new Request(new URL("http://localhost/0/error")));
48-
});
49-
});
47+
routerPrevious.fetch(new Request(new URL("http://localhost/0/error")))
48+
}).gc("inner")
49+
})
5050

5151
await run({
5252
colors: true,
53-
});
53+
})

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module.exports = (config = {}) => {
33
const router = require('./lib/router/sequential')(config)
44

55
return {
6-
router
6+
router,
77
}
88
}

lib/router/sequential.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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")
44

55
const STATUS_404 = {
6-
status: 404
6+
status: 404,
77
}
88
const STATUS_500 = {
9-
status: 500
9+
status: 500,
1010
}
1111

1212
module.exports = (config = {}) => {
@@ -29,9 +29,9 @@ module.exports = (config = {}) => {
2929
const _use = router.use
3030

3131
router.use = (prefix, ...middlewares) => {
32-
if (typeof prefix === 'function') {
32+
if (typeof prefix === "function") {
3333
middlewares = [prefix, ...middlewares]
34-
prefix = '/'
34+
prefix = "/"
3535
}
3636
_use.call(router, prefix, middlewares)
3737

@@ -40,11 +40,14 @@ module.exports = (config = {}) => {
4040

4141
router.fetch = (req) => {
4242
const url = req.url
43-
const startIndex = url.indexOf('/', 11)
44-
const queryIndex = url.indexOf('?', startIndex + 1)
45-
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)
4649

47-
req.path = path || '/'
50+
req.path = path || "/"
4851
req.query = queryIndex > 0 ? qs.parse(url.substring(queryIndex + 1)) : {}
4952

5053
const cacheKey = `${req.method}:${req.path}`
@@ -62,13 +65,20 @@ module.exports = (config = {}) => {
6265
}
6366
Object.assign(req.params, match.params)
6467

65-
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+
)
6675
} else {
6776
return config.defaultRoute(req)
6877
}
6978
}
7079

71-
router.on = (method, pattern, ...handlers) => router.add(method, pattern, handlers)
80+
router.on = (method, pattern, ...handlers) =>
81+
router.add(method, pattern, handlers)
7282

7383
return router
7484
}

package-lock.json

Lines changed: 38 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
"main": "index.js",
66
"scripts": {
77
"lint": "bun x standard",
8-
"format": "bun x standard --fix",
9-
"test": "bun test"
8+
"test": "bun test",
9+
"bench": "bun run bench.js",
10+
"format": "prettier --write *.js"
1011
},
1112
"dependencies": {
1213
"fast-querystring": "^1.1.2",
13-
"trouter": "^3.2.1"
14+
"trouter": "^4.0.0"
1415
},
1516
"repository": {
1617
"type": "git",
@@ -25,9 +26,10 @@
2526
"lib/"
2627
],
2728
"devDependencies": {
28-
"0http-bun": "^1.0.3",
29-
"bun-types": "^1.1.8",
30-
"mitata": "^1.0.32"
29+
"0http-bun": "^1.1.0",
30+
"bun-types": "^1.2.5",
31+
"mitata": "^1.0.34",
32+
"prettier": "^3.5.3"
3133
},
3234
"keywords": [
3335
"http",

0 commit comments

Comments
 (0)