0http-bun 0.0.2 → 1.0.0

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/README.md CHANGED
@@ -8,7 +8,9 @@ Experimental, bun-based HTTP framework inspired by [0http](https://0http.21no.de
8
8
  ```js
9
9
  const http = require('0http-bun')
10
10
 
11
- const { router } = http({})
11
+ const { router } = http({
12
+ port: 3000
13
+ })
12
14
  router.use((req, next) => {
13
15
  req.ctx = {
14
16
  engine: 'bun'
@@ -28,10 +30,7 @@ router.delete('/:id', async (req) => {
28
30
  })
29
31
  })
30
32
 
31
- module.exports = {
32
- port: 3000,
33
- fetch: (request) => router.lookup(request)
34
- }
33
+ export default router
35
34
  ```
36
35
  # Benchmarks
37
36
  ## 0http-bun (bun v0.2.2)
package/demos/basic.js CHANGED
@@ -21,7 +21,4 @@ router.delete('/:id', async (req) => {
21
21
  })
22
22
  })
23
23
 
24
- module.exports = {
25
- port: 3000,
26
- fetch: (request) => router.lookup(request)
27
- }
24
+ export default router
package/demos/minimal.js CHANGED
@@ -7,7 +7,4 @@ router.get('/hi', async (req) => {
7
7
  return new Response('Hello World!')
8
8
  })
9
9
 
10
- module.exports = {
11
- port: 3000,
12
- fetch: (request) => router.lookup(request)
13
- }
10
+ export default router
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  module.exports = (config = {}) => {
2
- const router = config.router || require('./lib/router/sequential')(config)
2
+ // Sequential is default and only router implementation for now
3
+ const router = require('./lib/router/sequential')(config)
3
4
 
4
5
  return {
5
6
  router
package/lib/next.js CHANGED
@@ -1,4 +1,4 @@
1
- module.exports = function next (middlewares, req, index, routers = {}, defaultRoute, errorHandler) {
1
+ module.exports = function next (middlewares, req, index, defaultRoute, errorHandler) {
2
2
  const middleware = middlewares[index]
3
3
  if (!middleware) {
4
4
  return defaultRoute(req)
@@ -8,27 +8,12 @@ module.exports = function next (middlewares, req, index, routers = {}, defaultRo
8
8
  if (err) {
9
9
  return errorHandler(err, req)
10
10
  } else {
11
- return next(middlewares, req, index + 1, routers, defaultRoute, errorHandler)
11
+ return next(middlewares, req, index + 1, defaultRoute, errorHandler)
12
12
  }
13
13
  }
14
14
 
15
15
  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)
32
17
  } catch (err) {
33
18
  return errorHandler(err, req)
34
19
  }
@@ -2,12 +2,10 @@
2
2
 
3
3
  const Trouter = require('trouter')
4
4
  const next = require('./../next')
5
- const LRU = require('lru-cache')
6
- const { parse } = require('regexparam')
7
5
 
8
6
  module.exports = (config = {}) => {
9
- if (config.defaultRoute === undefined) {
10
- config.defaultRoute = (req) => {
7
+ if (!config.defaultRoute) {
8
+ config.defaultRoute = () => {
11
9
  const res = new Response(null, {
12
10
  status: 404
13
11
  })
@@ -15,8 +13,8 @@ module.exports = (config = {}) => {
15
13
  return res
16
14
  }
17
15
  }
18
- if (config.errorHandler === undefined) {
19
- config.errorHandler = (err, req) => {
16
+ if (!config.errorHandler) {
17
+ config.errorHandler = (err) => {
20
18
  const res = new Response(err.message, {
21
19
  status: 500
22
20
  })
@@ -24,18 +22,9 @@ module.exports = (config = {}) => {
24
22
  return res
25
23
  }
26
24
  }
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
- }
33
25
 
34
- const routers = {}
35
- const isCacheEnabled = config.cacheSize > 0
36
- const cache = isCacheEnabled ? new LRU({ max: config.cacheSize }) : null
37
26
  const router = new Trouter()
38
- router.id = config.id
27
+ router.port = config.port || 3000
39
28
 
40
29
  const _use = router.use
41
30
 
@@ -46,53 +35,25 @@ module.exports = (config = {}) => {
46
35
  }
47
36
  _use.call(router, prefix, middlewares)
48
37
 
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
-
55
38
  return this
56
39
  }
57
40
 
58
- router.lookup = (req, step) => {
41
+ router.fetch = (req) => {
59
42
  const url = new URL(req.url)
43
+
60
44
  req.path = url.pathname || '/'
61
45
  req.query = url.queryparams
62
46
  req.search = url.search
63
47
  req.hostname = url.hostname
64
48
 
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
-
49
+ const match = router.find(req.method, req.path)
50
+ if (match.handlers.length > 0) {
90
51
  if (!req.params) {
91
52
  req.params = {}
92
53
  }
93
54
  Object.assign(req.params, match.params)
94
55
 
95
- return next(middlewares, req, 0, routers, config.defaultRoute, config.errorHandler)
56
+ return next(match.handlers, req, 0, config.defaultRoute, config.errorHandler)
96
57
  } else {
97
58
  return config.defaultRoute(req)
98
59
  }
package/package.json CHANGED
@@ -1,15 +1,14 @@
1
1
  {
2
2
  "name": "0http-bun",
3
- "version": "0.0.2",
4
- "description": "0http alternative for Bun",
3
+ "version": "1.0.0",
4
+ "description": "0http for Bun",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "lint": "bun x standard",
8
+ "format": "bun x standard --fix"
8
9
  },
9
10
  "dependencies": {
10
- "lru-cache": "^7.10.1",
11
- "regexparam": "^2.0.0",
12
- "trouter": "^3.2.0"
11
+ "trouter": "^3.2.1"
13
12
  },
14
13
  "repository": {
15
14
  "type": "git",
@@ -0,0 +1,44 @@
1
+ /* global describe, it, expect, beforeAll */
2
+
3
+ const http = require('../index')
4
+ const { router } = http({
5
+ port: 3000,
6
+ defaultRoute: (req) => {
7
+ const res = new Response('Not Found!', {
8
+ status: 404
9
+ })
10
+
11
+ return res
12
+ },
13
+ errorHandler: (err) => {
14
+ const res = new Response('Error: ' + err.message, {
15
+ status: 500
16
+ })
17
+
18
+ return res
19
+ }
20
+ })
21
+
22
+ describe('Router Configuration', () => {
23
+ beforeAll(async () => {
24
+ router.get('/error', () => {
25
+ throw new Error('Unexpected error')
26
+ })
27
+ })
28
+
29
+ it('should return a 500 response for a route that throws an error', async () => {
30
+ const response = await router.fetch(new Request('http://localhost:3000/error', {
31
+ method: 'GET'
32
+ }))
33
+ expect(response.status).toBe(500)
34
+ expect(await response.text()).toEqual('Error: Unexpected error')
35
+ })
36
+
37
+ it('should return a 404 response for a route that does not exist', async () => {
38
+ const response = await router.fetch(new Request('http://localhost:3000/does-not-exist', {
39
+ method: 'GET'
40
+ }))
41
+ expect(response.status).toBe(404)
42
+ expect(await response.text()).toEqual('Not Found!')
43
+ })
44
+ })
@@ -0,0 +1,86 @@
1
+ /* global describe, it, expect, beforeAll */
2
+
3
+ const http = require('../index')
4
+ const { router } = http({ port: 3000 })
5
+
6
+ describe('Router', () => {
7
+ beforeAll(async () => {
8
+ router.use((req, next) => {
9
+ req.ctx = {
10
+ engine: 'bun'
11
+ }
12
+
13
+ return next()
14
+ })
15
+
16
+ router.get('/get-params/:id', (req) => {
17
+ return Response.json(req.params)
18
+ })
19
+
20
+ router.delete('/get-params/:id', () => {
21
+ return Response.json('OK')
22
+ })
23
+
24
+ router.get('/error', () => {
25
+ throw new Error('Unexpected error')
26
+ })
27
+
28
+ router.post('/create', async (req) => {
29
+ const body = await req.text()
30
+
31
+ return Response.json(JSON.parse(body))
32
+ })
33
+
34
+ router.get('/', (req) => {
35
+ return Response.json(req.ctx)
36
+ })
37
+ })
38
+
39
+ it('should return a JSON response with the request parameters for GET requests', async () => {
40
+ const response = await router.fetch(new Request('http://localhost:3000/get-params/123', {
41
+ method: 'GET'
42
+ }))
43
+ expect(response.status).toBe(200)
44
+ expect(await response.json()).toEqual({ id: '123' })
45
+ })
46
+
47
+ it('should return a JSON response with the request parameters for DELETE requests', async () => {
48
+ const response = await router.fetch(new Request('http://localhost:3000/get-params/123', {
49
+ method: 'DELETE'
50
+ }))
51
+ expect(response.status).toBe(200)
52
+ expect(await response.json()).toEqual('OK')
53
+ })
54
+
55
+ it('should return a JSON response with the request body for POST requests', async () => {
56
+ const response = await router.fetch(new Request('http://localhost:3000/create', {
57
+ method: 'POST',
58
+ body: JSON.stringify({ foo: 'bar' })
59
+ }))
60
+ expect(response.status).toBe(200)
61
+ expect(await response.json()).toEqual({ foo: 'bar' })
62
+ })
63
+
64
+ it('should return a 404 response for a non-existent route', async () => {
65
+ const response = await router.fetch(new Request('http://localhost:3000/non-existent', {
66
+ method: 'GET'
67
+ }))
68
+ expect(response.status).toBe(404)
69
+ })
70
+
71
+ it('should return a 500 response for a route that throws an error', async () => {
72
+ const response = await router.fetch(new Request('http://localhost:3000/error', {
73
+ method: 'GET'
74
+ }))
75
+ expect(response.status).toBe(500)
76
+ expect(await response.text()).toEqual('Unexpected error')
77
+ })
78
+
79
+ it('should return a 200 response for a route that returns a Response object', async () => {
80
+ const response = await router.fetch(new Request('http://localhost:3000/', {
81
+ method: 'GET'
82
+ }))
83
+ expect(response.status).toBe(200)
84
+ expect(await response.json()).toEqual({ engine: 'bun' })
85
+ })
86
+ })