Skip to content

Commit b976fe8

Browse files
author
Daniil Zotov
authored
Update Elysia (#9212)
* fix spawn.ts, update bun * update elysia, refactor /json and /plaintext * refactor db handlers * use bun build --compile
1 parent a3ff927 commit b976fe8

10 files changed

+105
-98
lines changed

Diff for: frameworks/TypeScript/elysia/bun.lockb

-8 Bytes
Binary file not shown.

Diff for: frameworks/TypeScript/elysia/elysia-postgres.dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM oven/bun:1.0
1+
FROM oven/bun:1.1
22

33
EXPOSE 8080
44

Diff for: frameworks/TypeScript/elysia/elysia-smol-postgres.dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM oven/bun:1.0
1+
FROM oven/bun:1.1
22

33
EXPOSE 8080
44

Diff for: frameworks/TypeScript/elysia/elysia.dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM oven/bun:1.0
1+
FROM oven/bun:1.1
22

33
EXPOSE 8080
44

Diff for: frameworks/TypeScript/elysia/package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
"version": "0.0.1",
44
"module": "src/index.js",
55
"devDependencies": {
6-
"bun-types": "latest"
6+
"bun-types": "^1.1.23",
7+
"typescript": "^5.5.4"
78
},
89
"scripts": {
910
"dev": "bun run --watch src/index.ts",
1011
"start": "bun run src/index.ts",
11-
"build": "bun build --target=bun --minify src/index.ts --outdir=build"
12+
"build": "bun build --compile --minify --outfile server src/index.ts"
1213
},
1314
"dependencies": {
14-
"elysia": "^0.7.17",
15-
"postgres": "^3.4.1"
15+
"elysia": "^1.1.6",
16+
"postgres": "^3.4.4"
1617
}
17-
}
18+
}

Diff for: frameworks/TypeScript/elysia/spawn.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import os from 'node:os';
1+
const cpus = navigator.hardwareConcurrency;
2+
const buns = new Array(cpus);
23

3-
// @ts-ignore
4-
const numCPUs = os.availableParallelism();
5-
for (let i = 0; i < numCPUs; i++) {
6-
Bun.spawn(['bun', 'build/index.js'], {
4+
for (let i = 0; i < cpus; i++) {
5+
buns[i] = Bun.spawn(['./server'], {
76
stdio: ['inherit', 'inherit', 'inherit'],
87
env: { ...process.env },
98
});
109
}
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);

Diff for: frameworks/TypeScript/elysia/src/db-handlers.ts

+56-62
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,77 @@
11
import Elysia from 'elysia';
2-
import * as postgres from './postgres';
3-
import { Fortune, World } from './types';
2+
import * as db from './postgres';
3+
import { Fortune } from './types';
44

5-
const deps = new Elysia({
6-
name: 'deps',
7-
})
8-
.decorate('db', postgres)
9-
.decorate('generateRandomNumber', () => Math.ceil(Math.random() * 10000))
10-
.decorate('html', (fortunes: Fortune[]) => {
11-
const n = fortunes.length;
12-
13-
let html = '';
14-
for (let i = 0; i < n; i++) {
15-
html += `<tr><td>${fortunes[i].id}</td><td>${Bun.escapeHTML(
16-
fortunes[i].message
17-
)}</td></tr>`;
18-
}
5+
function rand () {
6+
return Math.ceil(Math.random() * 10000)
7+
}
198

20-
return `<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>${html}</table></body></html>`;
21-
});
9+
function parseQueriesNumber (q?: string) {
10+
return Math.min(parseInt(q || '1') || 1, 500)
11+
}
12+
13+
function renderTemplate (fortunes: Fortune[]) {
14+
const n = fortunes.length;
15+
16+
let html = '';
17+
for (let i = 0; i < n; i++) {
18+
html += `<tr><td>${fortunes[i].id}</td><td>${Bun.escapeHTML(
19+
fortunes[i].message
20+
)}</td></tr>`;
21+
}
22+
23+
return `<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>${html}</table></body></html>`;
24+
}
2225

2326
const dbHandlers = new Elysia({
2427
name: 'db-handlers',
2528
})
26-
.use(deps)
27-
.get(
28-
'/db',
29-
async ({ db, generateRandomNumber }) =>
30-
await db.find(generateRandomNumber())
31-
)
32-
.get(
33-
'/fortunes',
34-
async ({ db, html }) => {
35-
const fortunes = await db.fortunes();
36-
37-
fortunes.push({
38-
id: 0,
39-
message: 'Additional fortune added at request time.',
40-
});
41-
42-
fortunes.sort((a, b) => (a.message < b.message ? -1 : 1));
43-
44-
return html(fortunes);
45-
},
46-
{
47-
afterHandle({ set }) {
48-
set.headers['content-type'] = 'text/html; charset=utf-8';
49-
},
50-
}
51-
)
52-
.derive(({ query }) => ({
53-
numberOfObjects: Math.min(parseInt(query.queries || '1') || 1, 500),
54-
}))
55-
.get('/queries', async ({ db, generateRandomNumber, numberOfObjects }) => {
56-
const worldPromises = new Array<Promise<World>>(numberOfObjects);
57-
58-
for (let i = 0; i < numberOfObjects; i++) {
59-
worldPromises[i] = db.find(generateRandomNumber());
60-
}
29+
.onAfterHandle(({ set }) => {
30+
set.headers['server'] = 'Elysia';
31+
})
6132

62-
const worlds = await Promise.all(worldPromises);
33+
.get('/db', () => db.find(rand()))
6334

64-
return worlds;
35+
.get('/fortunes', async ({ set }) => {
36+
const fortunes = await db.fortunes();
37+
38+
fortunes.push({
39+
id: 0,
40+
message: 'Additional fortune added at request time.',
41+
});
42+
43+
fortunes.sort((a, b) => (a.message < b.message ? -1 : 1));
44+
45+
set.headers['content-type'] = 'text/html; charset=utf-8';
46+
return renderTemplate(fortunes);
47+
})
48+
49+
.get('/queries', async ({ query }) => {
50+
const num = parseQueriesNumber(query.queries)
51+
const worldPromises = new Array(num);
52+
53+
for (let i = 0; i < num; i++) {
54+
worldPromises[i] = db.find(rand());
55+
}
56+
57+
return await Promise.all(worldPromises);
6558
})
66-
.get('/updates', async ({ db, generateRandomNumber, numberOfObjects }) => {
67-
const worldPromises = new Array<Promise<World>>(numberOfObjects);
6859

69-
for (let i = 0; i < numberOfObjects; i++) {
70-
worldPromises[i] = db.find(generateRandomNumber());
60+
.get('/updates', async ({ query }) => {
61+
const num = parseQueriesNumber(query.queries)
62+
const worldPromises = new Array(num);
63+
64+
for (let i = 0; i < num; i++) {
65+
worldPromises[i] = db.find(rand());
7166
}
7267

7368
const worlds = await Promise.all(worldPromises);
7469

75-
for (let i = 0; i < numberOfObjects; i++) {
76-
worlds[i].randomNumber = generateRandomNumber();
70+
for (let i = 0; i < num; i++) {
71+
worlds[i].randomNumber = rand();
7772
}
7873

7974
await db.bulkUpdate(worlds);
80-
8175
return worlds;
8276
});
8377

Diff for: frameworks/TypeScript/elysia/src/index.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@ import dbHandlers from './db-handlers';
33

44
const app = new Elysia({
55
serve: {
6-
// @ts-ignore
76
reusePort: true,
87
},
98
})
10-
.onAfterHandle(({ set }) => {
9+
.get('/plaintext', ({ set }) => {
1110
set.headers['server'] = 'Elysia';
11+
return 'Hello, World!';
1212
})
13-
.decorate('HELLO_WORLD_STR', 'Hello, World!')
14-
.get('/plaintext', ({ HELLO_WORLD_STR }) => HELLO_WORLD_STR)
15-
.get('/json', ({ HELLO_WORLD_STR }) => ({ message: HELLO_WORLD_STR }));
1613

17-
if (process.env.DATABASE) {
14+
.get('/json', ({ set }) => {
15+
set.headers = {
16+
'content-type': 'application/json',
17+
'server': 'Elysia',
18+
};
19+
20+
return JSON.stringify({ message: 'Hello, World!' });
21+
});
22+
23+
if (Bun.env.DATABASE) {
1824
app.use(dbHandlers);
1925
}
2026

2127
app.listen(8080);
2228

23-
console.info(
24-
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
25-
);
29+
console.info(`🦊 Elysia is running at ${app.server!.url}`);

Diff for: frameworks/TypeScript/elysia/src/postgres.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ const sql = postgres({
99
max: 1,
1010
});
1111

12-
export const fortunes = async () =>
13-
await sql<Fortune[]>`SELECT id, message FROM fortune`;
12+
export const fortunes = () =>
13+
sql<Fortune[]>`SELECT id, message FROM fortune`;
1414

15-
export const find = async (id: number) =>
16-
await sql<World[]>`SELECT id, randomNumber FROM world WHERE id = ${id}`.then(
15+
export const find = (id: number) =>
16+
sql<World[]>`SELECT id, randomNumber FROM world WHERE id = ${id}`.then(
1717
(arr) => arr[0]
1818
);
1919

20-
export const bulkUpdate = async (worlds: World[]) =>
21-
await sql`UPDATE world SET randomNumber = (update_data.randomNumber)::int
20+
export const bulkUpdate = (worlds: World[]) =>
21+
sql`UPDATE world SET randomNumber = (update_data.randomNumber)::int
2222
FROM (VALUES ${sql(
2323
worlds
2424
.map((world) => [world.id, world.randomNumber])

Diff for: frameworks/TypeScript/elysia/src/types.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export type Fortune = {
2-
id: number;
3-
message: string;
4-
};
1+
export interface Fortune {
2+
id: number
3+
message: string
4+
}
55

6-
export type World = {
7-
id: number;
8-
randomNumber: number;
9-
};
6+
export interface World {
7+
id: number
8+
randomNumber: number
9+
}

0 commit comments

Comments
 (0)