-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
197 lines (172 loc) · 5.62 KB
/
server.ts
File metadata and controls
197 lines (172 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { Elysia, t } from "elysia";
import staticPlugin from "@elysiajs/static";
import { logger } from "@bogeychan/elysia-logger";
import { QueueService } from "./src/services/queue.service";
import { StopService } from "./src/services/stop.service";
import {
StopResponse,
QueueResponse,
LocationsResponse,
ErrorResponse,
} from "./src/models/response";
// Simple in-memory cache with TTL
interface CacheEntry {
data: unknown;
timestamp: number;
}
const cache = new Map<string, CacheEntry>();
const CACHE_TTL = 43200 * 1000; // 12 hours (Mapillary URLs expire in ~24h)
function getCacheKey(path: string, query?: Record<string, unknown>): string {
if (query && Object.keys(query).length > 0) {
const sortedParams = Object.keys(query)
.sort()
.map((k) => `${k}=${JSON.stringify(query[k])}`)
.join("&");
return `${path}?${sortedParams}`;
}
return path;
}
function getFromCache(key: string): unknown | null {
const entry = cache.get(key);
if (!entry) return null;
if (Date.now() - entry.timestamp > CACHE_TTL) {
cache.delete(key);
return null;
}
return entry.data;
}
function setCache(key: string, data: unknown): void {
cache.set(key, { data, timestamp: Date.now() });
}
export const app = new Elysia()
.use(
logger({
level: "info",
autoLogging: true,
})
)
.derive(({ request, set }) => {
// Check cache for applicable endpoints
const path = new URL(request.url).pathname;
const shouldCache = request.method === "GET" &&
(path === "/api/stops/locations" || path.startsWith("/api/stops/"));
let cachedData: unknown = null;
if (shouldCache) {
const cacheKey = getCacheKey(path);
cachedData = getFromCache(cacheKey);
if (cachedData) {
set.headers["X-Cache"] = "HIT";
} else {
set.headers["X-Cache"] = "MISS";
}
}
return {
cachedData,
shouldCache,
};
})
.onAfterHandle(({ request, response }) => {
// Only cache GET requests with successful responses
if (request.method !== "GET") return;
if (!response || typeof response !== "object") return;
const path = new URL(request.url).pathname;
if (path !== "/api/stops/locations" && !path.startsWith("/api/stops/")) {
return;
}
const cacheKey = getCacheKey(path);
setCache(cacheKey, response);
})
.mapResponse(({ responseValue, request, set }) => {
// Only compress object responses (JSON)
if (typeof responseValue !== "object" || responseValue === null) {
return;
}
const jsonString = JSON.stringify(responseValue);
if (jsonString.length < 1024) {
return; // Don't compress small responses
}
const acceptEncoding = request.headers.get("Accept-Encoding") || "";
// Try Gzip first (best compression: 77.1% reduction)
if (acceptEncoding.includes("gzip")) {
const compressed = Bun.gzipSync(jsonString);
set.headers["Content-Encoding"] = "gzip";
set.headers["Content-Length"] = compressed.length.toString();
set.headers["Content-Type"] = "application/json; charset=utf-8";
return new Response(compressed);
}
// Fallback to Deflate (tied with gzip for best compression)
if (acceptEncoding.includes("deflate")) {
const compressed = Bun.deflateSync(jsonString);
set.headers["Content-Encoding"] = "deflate";
set.headers["Content-Length"] = compressed.length.toString();
set.headers["Content-Type"] = "application/json; charset=utf-8";
return new Response(compressed);
}
// Fallback to Zstd (fastest compression, slightly worse ratio)
if (acceptEncoding.includes("zstd")) {
const compressed = Bun.zstdCompressSync(Buffer.from(jsonString));
set.headers["Content-Encoding"] = "zstd";
set.headers["Content-Length"] = compressed.length.toString();
set.headers["Content-Type"] = "application/json; charset=utf-8";
return new Response(new Uint8Array(compressed));
}
// No compression supported by client
return;
})
.onError(({ code, error: _error, status }) => {
if (code === "NOT_FOUND") {
return status(404, { error: "Resource not found" });
}
if (code === "VALIDATION") {
return status(400, { error: "Validation failed" });
}
return status(500, { error: "Internal server error" });
})
.use(staticPlugin({ assets: "public", prefix: "/" }))
.get("/", () => Bun.file("public/index.html"))
.get("/api/queue", async ({ query }) => {
const stops = await QueueService.generate(
query.lat,
query.lng,
query.radius_m,
query.min_tram_m ?? 0
);
return { stops };
}, {
query: t.Object({
lat: t.Numeric(),
lng: t.Numeric(),
radius_m: t.Numeric(),
min_tram_m: t.Optional(t.Numeric()),
}),
response: {
200: QueueResponse,
400: ErrorResponse,
},
})
.get("/api/stops/:id", async ({ cachedData, params, status, log }) => {
if (cachedData) return cachedData as typeof StopResponse.static;
const stop = await StopService.findById(params.id, log);
if (!stop) {
return status(404, { error: `Stop not found: ${params.id}` });
}
return stop;
}, {
params: t.Object({ id: t.Numeric() }),
response: {
200: StopResponse,
404: ErrorResponse,
},
})
.get("/api/stops/locations", async ({ cachedData }) => {
if (cachedData) return cachedData as typeof LocationsResponse.static;
const locations = await StopService.getAllLocations();
return { locations };
}, {
response: {
200: LocationsResponse,
},
})
.listen(3000);
export type App = typeof app;
console.log("Listening on http://localhost:3000");