Skip to content

Commit 2ed435c

Browse files
authored
[Elysia] Update Elysia to 1.2 (#9481)
* fix(elysia): native static response not setting headers conflict with recent Bun version * fix(elysia): remove lockfile * fix(elysia): update lockfile * fix(elysia): update elysia version * fix(elysia): update lockfile * 🧹 chore(elysia) update Elysia to 1.2 * 🧹 chore(elysia) update Elysia to 1.2 * 🧹 chore(elysia) update Elysia to 1.2 * fix(elysia): incorrect /update * fix(elysia): incorrect /update * fix(elysia): incorrect /update * fix(elysia): update lock file
1 parent ef93537 commit 2ed435c

File tree

4 files changed

+87
-85
lines changed

4 files changed

+87
-85
lines changed
2.1 KB
Binary file not shown.

frameworks/TypeScript/elysia/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.0.1",
44
"module": "src/index.js",
55
"devDependencies": {
6-
"typescript": "^5.5.4"
6+
"@types/bun": "^1.1.14",
7+
"typescript": "^5.7.2"
78
},
89
"scripts": {
910
"dev": "bun run --watch src/index.ts",
@@ -12,7 +13,7 @@
1213
"compile": "bun build --compile --minify --target bun --outfile server src/index.ts"
1314
},
1415
"dependencies": {
15-
"elysia": "^1.1.16",
16-
"postgres": "^3.4.4"
16+
"elysia": "^1.2.9",
17+
"postgres": "^3.4.5"
1718
}
1819
}

frameworks/TypeScript/elysia/src/db-handlers.ts

Lines changed: 57 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,66 @@ import { Elysia, t } from "elysia";
22
import * as db from "./postgres";
33
import { Fortune } from "./types";
44

5-
function rand() {
6-
return Math.ceil(Math.random() * 10000);
5+
export function rand() {
6+
return Math.ceil(Math.random() * 10000);
77
}
88

99
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>`;
10+
// NaN is falsy, fallback to one.
11+
return Math.min(+q! || 1, 500);
2412
}
2513

2614
export const dbHandlers = new Elysia()
27-
.headers({
28-
server: "Elysia",
29-
})
30-
.get("/db", () => db.find(rand()))
31-
.get("/fortunes", async (c) => {
32-
const fortunes = await db.fortunes();
33-
34-
fortunes.push({
35-
id: 0,
36-
message: "Additional fortune added at request time.",
37-
});
38-
39-
fortunes.sort((a, b) => {
40-
if (a.message < b.message) return -1;
41-
42-
return 1;
43-
});
44-
45-
c.set.headers["content-type"] = "text/html; charset=utf-8";
46-
47-
return renderTemplate(fortunes);
48-
})
49-
.get("/queries", (c) => {
50-
const num = parseQueriesNumber(c.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 Promise.all(worldPromises);
58-
})
59-
.get("/updates", async (c) => {
60-
const num = parseQueriesNumber(c.query.queries);
61-
const worldPromises = new Array(num);
62-
63-
for (let i = 0; i < num; i++) {
64-
worldPromises[i] = db.find(rand());
65-
}
66-
67-
const worlds = await Promise.all(worldPromises);
68-
69-
for (let i = 0; i < num; i++) {
70-
worlds[i].randomNumber = rand();
71-
}
72-
73-
await db.bulkUpdate(worlds);
74-
return worlds;
75-
});
15+
.headers({
16+
server: "Elysia",
17+
})
18+
// ? Mark as async for Promise result to prevent double Elysia's mapResponse execution
19+
.get("/db", async () => db.find(rand()))
20+
.get("/fortunes", async (c) => {
21+
const fortunes = await db.fortunes();
22+
23+
fortunes.push({
24+
id: 0,
25+
message: "Additional fortune added at request time.",
26+
});
27+
28+
fortunes.sort((a, b) => {
29+
if (a.message < b.message) return -1;
30+
31+
return 1;
32+
});
33+
34+
c.set.headers["content-type"] = "text/html; charset=utf-8";
35+
36+
const n = fortunes.length;
37+
38+
let html = "";
39+
for (let i = 0; i < n; i++) {
40+
html += `<tr><td>${fortunes[i].id}</td><td>${Bun.escapeHTML(
41+
fortunes[i].message,
42+
)}</td></tr>`;
43+
}
44+
45+
return `<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>${html}</table></body></html>`;
46+
})
47+
// ? Mark as async for Promise result to prevent double Elysia's mapResponse execution
48+
.get("/queries", async (c) => {
49+
const num = parseQueriesNumber(c.query.queries);
50+
const worldPromises = new Array(num);
51+
52+
for (let i = 0; i < num; i++) worldPromises[i] = db.find(rand());
53+
54+
return Promise.all(worldPromises);
55+
})
56+
.get("/updates", async (c) => {
57+
const num = parseQueriesNumber(c.query.queries);
58+
const worldPromises = new Array(num);
59+
60+
for (let i = 0; i < num; i++)
61+
worldPromises[i] = db.findThenRand(rand());
62+
63+
const worlds = await Promise.all(worldPromises);
64+
65+
await db.bulkUpdate(worlds);
66+
return worlds;
67+
});
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
import postgres from "postgres";
2-
import { Fortune, World } from "./types";
2+
import { rand } from "./db-handlers";
3+
import type { Fortune, World } from "./types";
34

45
const sql = postgres({
5-
host: "tfb-database",
6-
user: "benchmarkdbuser",
7-
password: "benchmarkdbpass",
8-
database: "hello_world",
9-
max: 1,
6+
host: "tfb-database",
7+
user: "benchmarkdbuser",
8+
password: "benchmarkdbpass",
9+
database: "hello_world",
10+
max: 1,
1011
});
1112

1213
export const fortunes = () => sql<Fortune[]>`SELECT id, message FROM fortune`;
1314

1415
export const find = (id: number) =>
15-
sql<World[]>`SELECT id, randomNumber FROM world WHERE id = ${id}`.then(
16-
(arr) => arr[0],
17-
);
16+
sql<World[]>`SELECT id, randomNumber FROM world WHERE id = ${id}`.then(
17+
(arr) => arr[0],
18+
);
19+
20+
export const findThenRand = (id: number) =>
21+
sql<World[]>`SELECT id, randomNumber FROM world WHERE id = ${id}`.then(
22+
(arr) => {
23+
arr[0].randomNumber = rand();
24+
return arr[0];
25+
},
26+
);
1827

1928
export const bulkUpdate = (worlds: World[]) => {
20-
worlds = worlds.toSorted((a, b) => a.id - b.id);
29+
worlds = worlds.toSorted((a, b) => a.id - b.id);
2130

22-
const values = new Array(worlds.length);
23-
for (let i = 0; i < worlds.length; i++) {
24-
values[i] = [worlds[i].id, worlds[i].randomNumber];
25-
}
31+
const values = new Array(worlds.length);
32+
for (let i = 0; i < worlds.length; i++) {
33+
values[i] = [worlds[i].id, worlds[i].randomNumber];
34+
}
2635

27-
return sql`UPDATE world SET randomNumber = (update_data.randomNumber)::int
28-
FROM (VALUES ${sql(values)}) AS update_data (id, randomNumber)
29-
WHERE world.id = (update_data.id)::int`;
36+
return sql`UPDATE world SET randomNumber = (update_data.randomNumber)::int
37+
FROM (VALUES ${sql(values)}) AS update_data (id, randomNumber)
38+
WHERE world.id = (update_data.id)::int`;
3039
};

0 commit comments

Comments
 (0)