Skip to content

Commit d226f37

Browse files
authored
feat: support Bun built-in http server (#8292)
* feat: support Bun built-in http server * refactor(bun): listen hostname `0.0.0.0`
1 parent a0b6b61 commit d226f37

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

javascript/bun/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

javascript/bun/bun.lock

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/bun/cluster.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import cluster, { type Worker } from 'node:cluster';
2+
3+
function startServer() {
4+
const server = Bun.serve({
5+
hostname: '0.0.0.0',
6+
port: 3000,
7+
reusePort: true,
8+
development: false,
9+
routes: {
10+
'/': () => new Response(null, { status: 204 }),
11+
'/user': () => new Response(null, { status: 204 }),
12+
'/user/:id': ({ params }) => new Response(params.id, { status: 200 }),
13+
},
14+
});
15+
16+
console.debug(`Worker PID: ${process.pid} listening on ${server.url}`);
17+
}
18+
19+
function forkWorkers() {
20+
console.log(`Primary PID: ${process.pid}`);
21+
for (let i = 0; i < navigator.hardwareConcurrency; i++) cluster.fork();
22+
23+
cluster.on('exit', (worker, code, signal) => {
24+
console.error(`Worker PID: ${worker.process.pid} died with code ${code} and signal ${signal}`);
25+
});
26+
}
27+
28+
if (cluster.isPrimary) forkWorkers();
29+
else startServer();

javascript/bun/config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
framework:
2+
github: oven-sh/bun
3+
version: 1.2.5
4+
5+
engines:
6+
- bun

javascript/bun/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"@types/bun": "^1.2.5"
4+
}
5+
}

0 commit comments

Comments
 (0)