Skip to content

Commit 61ebc14

Browse files
author
Daniil Zotov
authored
Update Bun (#9211)
* update bun * use bun build --compile * kill children on parent exit
1 parent 1b2f23a commit 61ebc14

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
FROM oven/bun:1.0
1+
FROM oven/bun:1.1
22

33
EXPOSE 8080
44

55
WORKDIR /app
66

7-
USER bun
8-
97
COPY ./src .
108

119
ENV NODE_ENV=production
1210

11+
RUN bun build --compile --minify --outfile server .
12+
13+
USER bun
14+
1315
CMD ["bun", "spawn.ts"]
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const HELLO_WORLD_STR = "Hello, World!";
2-
const options: ResponseInit = { headers: { "Server": "Bun" } };
1+
const plainOptions: ResponseInit = { headers: { "Server": "Bun" } };
2+
const jsonOptions: ResponseInit = { headers: { "Server": "Bun", "Content-Type": "application/json" } };
33

44
const server = Bun.serve({
55
port: 8080,
66
reusePort: true,
77
fetch(req: Request) {
88
const pathname = req.url.slice(req.url.indexOf("/", 8));
99

10-
if (pathname === "/json") {
11-
return Response.json({ message: HELLO_WORLD_STR }, options);
10+
if (pathname == "/json") {
11+
return new Response(JSON.stringify({ message: "Hello, World!" }), jsonOptions);
1212
}
1313

14-
if (pathname === "/plaintext") {
15-
return new Response(HELLO_WORLD_STR, options);
14+
if (pathname == "/plaintext") {
15+
return new Response("Hello, World!", plainOptions);
1616
}
1717

1818
return new Response("", { status: 404 })
1919
},
2020
});
2121

22-
console.log(`Listening on localhost:${server.port}`);
22+
console.log(`Listening on ${server.url}\n`);
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
import os from "node:os";
1+
const cpus = navigator.hardwareConcurrency;
2+
const buns = new Array(cpus);
23

3-
const numCPUs = os.cpus().length;
4-
for (let i = 0; i < numCPUs; i++) {
5-
Bun.spawn(["bun", "index.ts"], {
4+
for (let i = 0; i < cpus; i++) {
5+
buns[i] = Bun.spawn(["./server"], {
66
stdio: ["inherit", "inherit", "inherit"],
77
env: { ...process.env },
88
});
99
}
10+
11+
function kill() {
12+
for (const bun of buns) {
13+
bun.kill();
14+
}
15+
}
16+
17+
process.on("SIGINT", kill);
18+
process.on("exit", kill);

0 commit comments

Comments
 (0)