@@ -2,74 +2,66 @@ import { Elysia, t } from "elysia";
22import * as db from "./postgres" ;
33import { 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
99function 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 as string ) || 1 , 500 ) ;
2412}
2513
2614export 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+ } ) ;
0 commit comments