Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Setup Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: latest

- name: Install dependencies
run: bun install

- name: Linting
run: bun x standard
run: bun run format

- name: Run tests
run: bun test
run: bun test
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"trailingComma": "all",
"semi": false,
"bracketSpacing": false
}
60 changes: 30 additions & 30 deletions bench.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
import { run, bench, group } from "mitata";
import httpNext from "./index";
import httpPrevious from "0http-bun";
import { run, bench, group } from "mitata"
import httpNext from "./index"
import httpPrevious from "0http-bun"

function setupRouter(router) {
router.use((req, next) => {
return next();
});
return next()
})

router.get("/", () => {
return new Response();
});
return new Response()
})
router.get("/:id", async (req) => {
return new Response(req.params.id);
});
return new Response(req.params.id)
})
router.get("/:id/error", () => {
throw new Error("Error");
});
throw new Error("Error")
})
}

const { router } = httpNext();
setupRouter(router);
const { router } = httpNext()
setupRouter(router)

const { router: routerPrevious } = httpPrevious();
setupRouter(routerPrevious);
const { router: routerPrevious } = httpPrevious()
setupRouter(routerPrevious)

group("Next Router", () => {
bench("Parameter URL", () => {
router.fetch(new Request(new URL("http://localhost/0")));
});
router.fetch(new Request(new URL("http://localhost/0")))
}).gc("inner")
bench("Not Found URL", () => {
router.fetch(new Request(new URL("http://localhost/0/404")));
});
router.fetch(new Request(new URL("http://localhost/0/404")))
}).gc("inner")
bench("Error URL", () => {
router.fetch(new Request(new URL("http://localhost/0/error")));
});
});
router.fetch(new Request(new URL("http://localhost/0/error")))
}).gc("inner")
})

group("Previous Router", () => {
bench("Parameter URL", () => {
routerPrevious.fetch(new Request(new URL("http://localhost/0")));
});
routerPrevious.fetch(new Request(new URL("http://localhost/0")))
}).gc("inner")
bench("Not Found URL", () => {
routerPrevious.fetch(new Request(new URL("http://localhost/0/404")));
});
routerPrevious.fetch(new Request(new URL("http://localhost/0/404")))
}).gc("inner")
bench("Error URL", () => {
routerPrevious.fetch(new Request(new URL("http://localhost/0/error")));
});
});
routerPrevious.fetch(new Request(new URL("http://localhost/0/error")))
}).gc("inner")
})

await run({
colors: true,
});
})
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module.exports = (config = {}) => {
const router = require('./lib/router/sequential')(config)

return {
router
router,
}
}
36 changes: 23 additions & 13 deletions lib/router/sequential.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Trouter = require('trouter')
const qs = require('fast-querystring')
const next = require('./../next')
const { Trouter } = require("trouter")
const qs = require("fast-querystring")
const next = require("./../next")

const STATUS_404 = {
status: 404
status: 404,
}
const STATUS_500 = {
status: 500
status: 500,
}

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

router.use = (prefix, ...middlewares) => {
if (typeof prefix === 'function') {
if (typeof prefix === "function") {
middlewares = [prefix, ...middlewares]
prefix = '/'
prefix = "/"
}
_use.call(router, prefix, middlewares)

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

router.fetch = (req) => {
const url = req.url
const startIndex = url.indexOf('/', 11)
const queryIndex = url.indexOf('?', startIndex + 1)
const path = queryIndex === -1 ? url.substring(startIndex) : url.substring(startIndex, queryIndex)
const startIndex = url.indexOf("/", 11)
const queryIndex = url.indexOf("?", startIndex + 1)
const path =
queryIndex === -1
? url.substring(startIndex)
: url.substring(startIndex, queryIndex)

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

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

return next(match.handlers, req, 0, config.defaultRoute, config.errorHandler)
return next(
match.handlers,
req,
0,
config.defaultRoute,
config.errorHandler
)
} else {
return config.defaultRoute(req)
}
}

router.on = (method, pattern, ...handlers) => router.add(method, pattern, handlers)
router.on = (method, pattern, ...handlers) =>
router.add(method, pattern, handlers)

return router
}
49 changes: 38 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"main": "index.js",
"scripts": {
"lint": "bun x standard",
"format": "bun x standard --fix",
"test": "bun test"
"test": "bun test",
"bench": "bun run bench.js",
"format": "prettier --write *.js"
},
"dependencies": {
"fast-querystring": "^1.1.2",
"trouter": "^3.2.1"
"trouter": "^4.0.0"
},
"repository": {
"type": "git",
Expand All @@ -25,9 +26,10 @@
"lib/"
],
"devDependencies": {
"0http-bun": "^1.0.3",
"bun-types": "^1.1.8",
"mitata": "^1.0.32"
"0http-bun": "^1.1.0",
"bun-types": "^1.2.5",
"mitata": "^1.0.34",
"prettier": "^3.5.3"
},
"keywords": [
"http",
Expand Down