0http-bun 1.0.0 → 1.0.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/bench.ts ADDED
@@ -0,0 +1,64 @@
1
+ import { run, bench, group, baseline } from 'mitata';
2
+ import httpNext from './index'
3
+ import httpPrevious from '0http-bun'
4
+
5
+ function setupRouter(router) {
6
+ router.use((req, next) => {
7
+ return next()
8
+ })
9
+
10
+ router.get('/', () => {
11
+ return new Response()
12
+ })
13
+ router.get('/:id', async (req) => {
14
+ return new Response(req.params.id)
15
+ })
16
+ router.get('/:id/error', () => {
17
+ throw new Error('Error')
18
+ })
19
+ }
20
+
21
+ const { router } = httpNext()
22
+ setupRouter(router)
23
+
24
+ const { router: routerPrevious } = httpPrevious()
25
+ setupRouter(routerPrevious)
26
+
27
+ group('Next Router', () => {
28
+ baseline('Base URL', () => {
29
+ router.fetch(new Request(new URL('http://localhost/')))
30
+ })
31
+ bench('Parameter URL', () => {
32
+ router.fetch(new Request(new URL('http://localhost/0')))
33
+ })
34
+ bench('Not Found URL', () => {
35
+ router.fetch(new Request(new URL('http://localhost/0/404')))
36
+ })
37
+ bench('Error URL', () => {
38
+ router.fetch(new Request(new URL('http://localhost/0/error')))
39
+ })
40
+ })
41
+
42
+ group('Previous Router', () => {
43
+ baseline('Base URL', () => {
44
+ routerPrevious.fetch(new Request(new URL('http://localhost/')))
45
+ })
46
+ bench('Parameter URL', () => {
47
+ routerPrevious.fetch(new Request(new URL('http://localhost/0')))
48
+ })
49
+ bench('Not Found URL', () => {
50
+ routerPrevious.fetch(new Request(new URL('http://localhost/0/404')))
51
+ })
52
+ bench('Error URL', () => {
53
+ routerPrevious.fetch(new Request(new URL('http://localhost/0/error')))
54
+ })
55
+ })
56
+
57
+ await run({
58
+ silent: false,
59
+ avg: true,
60
+ json: false,
61
+ colors: true,
62
+ min_max: false,
63
+ percentiles: false
64
+ })
package/demos/app.ts ADDED
@@ -0,0 +1,19 @@
1
+ import http from '../index'
2
+
3
+ const { router } = http()
4
+
5
+ router.get('/', () => {
6
+ return new Response()
7
+ })
8
+ router.get('/:id', (req) => {
9
+ return new Response(req.params.id)
10
+ })
11
+ router.post('/', () => {
12
+ return new Response()
13
+ })
14
+
15
+ Bun.serve({
16
+ port: 3000,
17
+ reusePort: true,
18
+ fetch: router.fetch
19
+ })
@@ -0,0 +1,9 @@
1
+ import os from "node:os";
2
+
3
+ const numCPUs = os.cpus().length;
4
+ for (let i = 0; i < numCPUs; i++) {
5
+ Bun.spawn(["bun", "app.ts"], {
6
+ stdio: ["inherit", "inherit", "inherit"],
7
+ env: { ...process.env },
8
+ });
9
+ }
package/lib/next.js CHANGED
@@ -1,19 +1,17 @@
1
1
  module.exports = function next (middlewares, req, index, defaultRoute, errorHandler) {
2
- const middleware = middlewares[index]
3
- if (!middleware) {
2
+ if (index >= middlewares.length) {
4
3
  return defaultRoute(req)
5
4
  }
6
5
 
7
- function step (err) {
8
- if (err) {
9
- return errorHandler(err, req)
10
- } else {
11
- return next(middlewares, req, index + 1, defaultRoute, errorHandler)
12
- }
13
- }
6
+ const middleware = middlewares[index++]
14
7
 
15
8
  try {
16
- return middleware(req, step)
9
+ return middleware(req, (err) => {
10
+ if (err) {
11
+ return errorHandler(err, req)
12
+ }
13
+ return next(middlewares, req, index, defaultRoute, errorHandler)
14
+ })
17
15
  } catch (err) {
18
16
  return errorHandler(err, req)
19
17
  }
@@ -1,25 +1,22 @@
1
- /* global Response */
2
-
3
1
  const Trouter = require('trouter')
4
2
  const next = require('./../next')
5
3
 
4
+ const status404 = {
5
+ status: 404
6
+ }
7
+ const status500 = {
8
+ status: 500
9
+ }
10
+
6
11
  module.exports = (config = {}) => {
7
12
  if (!config.defaultRoute) {
8
13
  config.defaultRoute = () => {
9
- const res = new Response(null, {
10
- status: 404
11
- })
12
-
13
- return res
14
+ return new Response(null, status404)
14
15
  }
15
16
  }
16
17
  if (!config.errorHandler) {
17
18
  config.errorHandler = (err) => {
18
- const res = new Response(err.message, {
19
- status: 500
20
- })
21
-
22
- return res
19
+ return new Response(err.message, status500)
23
20
  }
24
21
  }
25
22
 
@@ -43,8 +40,6 @@ module.exports = (config = {}) => {
43
40
 
44
41
  req.path = url.pathname || '/'
45
42
  req.query = url.queryparams
46
- req.search = url.search
47
- req.hostname = url.hostname
48
43
 
49
44
  const match = router.find(req.method, req.path)
50
45
  if (match.handlers.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "0http-bun",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "0http for Bun",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,6 +14,10 @@
14
14
  "type": "git",
15
15
  "url": "git+https://github.com/BackendStack21/0http-bun.git"
16
16
  },
17
+ "devDependencies": {
18
+ "0http-bun": "^1.0.0",
19
+ "mitata": "^0.1.11"
20
+ },
17
21
  "keywords": [
18
22
  "http",
19
23
  "server",