0http-bun 1.0.1 → 1.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "0http-bun",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "0http for Bun",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,6 +14,12 @@
14
14
  "type": "git",
15
15
  "url": "git+https://github.com/BackendStack21/0http-bun.git"
16
16
  },
17
+ "files": [
18
+ "LICENSE",
19
+ "README.md",
20
+ "index.js",
21
+ "lib/"
22
+ ],
17
23
  "devDependencies": {
18
24
  "0http-bun": "^1.0.0",
19
25
  "mitata": "^0.1.11"
Binary file
package/bench.ts DELETED
@@ -1,64 +0,0 @@
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 DELETED
@@ -1,19 +0,0 @@
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
- })
package/demos/basic.js DELETED
@@ -1,24 +0,0 @@
1
- /* global Response */
2
- const http = require('../index')
3
-
4
- const { router } = http({})
5
- router.use((req, next) => {
6
- req.ctx = {
7
- engine: 'bun'
8
- }
9
-
10
- return next()
11
- })
12
- router.get('/:id', async (req) => {
13
- return Response.json(req.params)
14
- })
15
- router.post('/', async (req) => {
16
- return new Response('POST')
17
- })
18
- router.delete('/:id', async (req) => {
19
- return Response.json(req.params, {
20
- status: 200
21
- })
22
- })
23
-
24
- export default router
package/demos/cluster.ts DELETED
@@ -1,9 +0,0 @@
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/demos/minimal.js DELETED
@@ -1,10 +0,0 @@
1
- /* global Response */
2
- const http = require('../index')
3
-
4
- const { router } = http({})
5
-
6
- router.get('/hi', async (req) => {
7
- return new Response('Hello World!')
8
- })
9
-
10
- export default router
@@ -1,44 +0,0 @@
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
- })
@@ -1,86 +0,0 @@
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
- })