diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 8fd7c74..f31f47b 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -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 \ No newline at end of file + run: bun test diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..a5b861d --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "singleQuote": true, + "trailingComma": "all", + "semi": false, + "bracketSpacing": false + } \ No newline at end of file diff --git a/bench.ts b/bench.ts index ff19488..853ece7 100644 --- a/bench.ts +++ b/bench.ts @@ -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, -}); +}) diff --git a/index.js b/index.js index 507fbf2..3ab1578 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,6 @@ module.exports = (config = {}) => { const router = require('./lib/router/sequential')(config) return { - router + router, } } diff --git a/lib/router/sequential.js b/lib/router/sequential.js index f11a139..5d94240 100644 --- a/lib/router/sequential.js +++ b/lib/router/sequential.js @@ -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 = {}) => { @@ -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) @@ -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}` @@ -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 } diff --git a/package-lock.json b/package-lock.json index 82492f9..ba7e454 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,21 +1,21 @@ { "name": "0http-bun", - "version": "1.0.4", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "0http-bun", - "version": "1.0.4", + "version": "1.1.0", "license": "MIT", "dependencies": { "fast-querystring": "^1.1.2", - "trouter": "^3.2.1" + "trouter": "^4.0.0" }, "devDependencies": { "0http-bun": "^1.0.3", "bun-types": "^1.1.8", - "mitata": "^1.0.32" + "mitata": "^1.0.34" } }, "node_modules/@types/node": { @@ -46,6 +46,29 @@ "trouter": "^3.2.1" } }, + "node_modules/0http-bun/node_modules/regexparam": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/regexparam/-/regexparam-1.3.0.tgz", + "integrity": "sha512-6IQpFBv6e5vz1QAqI+V4k8P2e/3gRrqfCJ9FI+O1FLQTO+Uz6RXZEZOPmTJ6hlGj7gkERzY5BRCv09whKP96/g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/0http-bun/node_modules/trouter": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/trouter/-/trouter-3.2.1.tgz", + "integrity": "sha512-oY3CmIiEYOe1YMEzh++I67lrNOUldtCeuLL0vRPydvQLHZpSJ03B5dgDFlpFsiriMq6e//NDjjopjUzXOztHow==", + "dev": true, + "license": "MIT", + "dependencies": { + "regexparam": "^1.3.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/bun-types": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/bun-types/-/bun-types-1.1.8.tgz", @@ -70,24 +93,28 @@ } }, "node_modules/mitata": { - "version": "1.0.32", - "resolved": "https://registry.npmjs.org/mitata/-/mitata-1.0.32.tgz", - "integrity": "sha512-UioSpAqd+QRTzIjtudWKVar3LbNt0jKS6i7S12uvRxr+eiv77TK7oMi8gNRbdNPH1rKWxrA5M48/QqVKigSQ9Q==", + "version": "1.0.34", + "resolved": "https://registry.npmjs.org/mitata/-/mitata-1.0.34.tgz", + "integrity": "sha512-Mc3zrtNBKIMeHSCQ0XqRLo1vbdIx1wvFV9c8NJAiyho6AjNfMY8bVhbS12bwciUdd1t4rj8099CH3N3NFahaUA==", "dev": true, "license": "MIT" }, "node_modules/regexparam": { - "version": "1.3.0", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/regexparam/-/regexparam-3.0.0.tgz", + "integrity": "sha512-RSYAtP31mvYLkAHrOlh25pCNQ5hWnT106VukGaaFfuJrZFkGRX5GhUAdPqpSDXxOhA2c4akmRuplv1mRqnBn6Q==", "license": "MIT", "engines": { - "node": ">=6" + "node": ">=8" } }, "node_modules/trouter": { - "version": "3.2.1", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/trouter/-/trouter-4.0.0.tgz", + "integrity": "sha512-bwwr76BThfiVwAFZqks5cJ+VoKNM3/2Yg1ZwJslkdmAUQ6S0UNoCoGYFDxdw+u1skfexggdmD2p35kW5Td4Cug==", "license": "MIT", "dependencies": { - "regexparam": "^1.3.0" + "regexparam": "^3.0.0" }, "engines": { "node": ">=6" diff --git a/package.json b/package.json index 8d6809f..6794c26 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",