-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
383 lines (327 loc) · 9.15 KB
/
Copy pathindex.ts
File metadata and controls
383 lines (327 loc) · 9.15 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
*
* @Name: bun-microservice-gateways
* @Author: Max Base
* @Date: 05/20/2025
* @Repository: https://github.com/basemax/bun-microservice-gateways
*
*/
// --- TYPES ---
type HttpMethod =
| "GET"
| "POST"
| "PUT"
| "DELETE"
| "PATCH"
| "OPTIONS"
| "HEAD";
type HttpMethodValue = HttpMethod | HttpMethod[] | "*";
const VALID_METHODS: HttpMethod[] = [
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"OPTIONS",
"HEAD",
];
interface ServiceConfig {
prefix?: string;
path?: string;
method?: HttpMethodValue;
host: string;
port: number;
active?: boolean;
}
interface Config {
host: string;
port: number;
services: ServiceConfig[];
debug?: boolean;
}
// --- LOGGING ---
enum LogLevel {
DEBUG = 1,
INFO = 2,
ERROR = 3,
}
let currentLogLevel = LogLevel.INFO;
function formatLog(level: string, emoji: string, message: string) {
return `${emoji} [${level.padEnd(5)} ${new Date().toISOString()}] ${message}`;
}
function log(level: LogLevel, emoji: string, message: string) {
if (level >= currentLogLevel) {
const levelStr = LogLevel[level];
console.log(formatLog(levelStr, emoji, message));
}
}
function logDebug(message: string) {
log(LogLevel.DEBUG, "🐛", message);
}
function logInfo(message: string) {
log(LogLevel.INFO, "📡", message);
}
function logError(message: string) {
log(LogLevel.ERROR, "❌", message);
}
// --- CONFIG LOADING ---
let config: Config;
let DEBUG = false;
function validateServiceConfig(
svc: ServiceConfig,
index: number,
): ServiceConfig {
const errors: string[] = [];
if (typeof svc.host !== "string" || !svc.host) {
errors.push(`Service ${index + 1}: "host" must be a non-empty string`);
}
if (typeof svc.port !== "number" || svc.port <= 0) {
errors.push(`Service ${index + 1}: "port" must be a positive number`);
}
let method: HttpMethodValue | undefined = svc.method;
if (typeof method === "string") {
if (method === "*") {
method = "*";
} else if (VALID_METHODS.includes(method.toUpperCase() as HttpMethod)) {
method = method.toUpperCase() as HttpMethod;
} else {
errors.push(`Service ${index + 1}: Invalid method "${method}"`);
}
} else if (Array.isArray(method)) {
const upperMethods = method.map((m) => m.toUpperCase());
const invalids = upperMethods.filter(
(m) => !VALID_METHODS.includes(m as HttpMethod),
);
if (invalids.length > 0) {
errors.push(
`Service ${index + 1}: Invalid method(s) ${invalids.join(", ")}`,
);
} else {
method = upperMethods as HttpMethod[];
}
} else if (method !== undefined && method !== "*") {
errors.push(
`Service ${index + 1}: Unsupported method format: ${JSON.stringify(method)}`,
);
}
if (errors.length > 0) {
throw new Error(errors.join("; "));
}
return {
...svc,
method,
active: svc.active !== false,
};
}
async function loadConfig(): Promise<Config> {
const cfg = await Bun.file("config.json").json();
const topErrors: string[] = [];
if (typeof cfg.host !== "string" || !cfg.host) {
topErrors.push(`"host" must be a non-empty string`);
}
if (typeof cfg.port !== "number" || cfg.port <= 0) {
topErrors.push(`"port" must be a positive number`);
}
if (!Array.isArray(cfg.services)) {
topErrors.push(`"services" must be an array`);
}
if (topErrors.length > 0) {
throw new Error(topErrors.join("; "));
}
try {
cfg.services = cfg.services.map((svc: ServiceConfig, i: number) =>
validateServiceConfig(svc, i),
);
} catch (validationError) {
logError(
`❌ Configuration validation error: ${(validationError as Error).message}`,
);
throw validationError;
}
DEBUG = cfg.debug ?? false;
currentLogLevel = DEBUG ? LogLevel.DEBUG : LogLevel.INFO;
console.clear();
logInfo("🔁 Configuration loaded");
logInfo(`🚀 Starting Gateway on http://${cfg.host}:${cfg.port}`);
logInfo(`🔧 Debug mode: ${DEBUG ? "ON" : "OFF"}`);
logInfo(`📦 Loaded ${cfg.services.length} services:`);
cfg.services.forEach((svc: ServiceConfig, i: number) => {
const label = svc.path
? `path: ${Array.isArray(svc.method) ? svc.method.join(",") : svc.method} ${svc.path}`
: svc.prefix
? `prefix: ${svc.prefix}`
: "<no path or prefix>";
logInfo(` ${i + 1}. ${label} => ${svc.host}:${svc.port}`);
});
return cfg;
}
// --- UTILITIES ---
const hopByHopHeaders = [
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailers",
"transfer-encoding",
"upgrade",
];
function sanitizeHeaders(headers: Headers): Headers {
const sanitized = new Headers();
headers.forEach((value, key) => {
if (!hopByHopHeaders.includes(key.toLowerCase())) {
sanitized.append(key, value);
}
});
return sanitized;
}
function buildTargetUrl(req: Request, svc: ServiceConfig): string {
const reqUrl = new URL(req.url);
return `http://${svc.host}:${svc.port}${reqUrl.pathname}${reqUrl.search}`;
}
function prepareForwardedRequest(
req: Request,
svc: ServiceConfig,
targetUrl: string,
): Request {
const headers = sanitizeHeaders(req.headers);
if (!headers.has("host")) {
headers.set("host", `${svc.host}:${svc.port}`);
}
const origin = req.headers.get("origin");
if (origin) {
headers.set("origin", origin);
}
const originalXFF = req.headers.get("x-forwarded-for");
const clientIp = req.headers.get("cf-connecting-ip") || "unknown";
const xff = originalXFF ? `${originalXFF}, ${clientIp}` : clientIp;
headers.set("x-forwarded-for", xff);
return new Request(targetUrl, {
method: req.method,
headers,
body: req.body,
redirect: "manual",
});
}
async function forwardRequest(
req: Request,
svc: ServiceConfig,
): Promise<Response> {
const targetUrl = buildTargetUrl(req, svc);
logDebug(`➡️ Forwarding request to: ${targetUrl}`);
try {
const forwardedReq = prepareForwardedRequest(req, svc, targetUrl);
const response = await fetch(forwardedReq);
logDebug(`⬅️ Received ${response.status} from ${svc.host}:${svc.port}`);
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
} catch (error: unknown) {
const errMsg =
error instanceof Error ? error.stack || error.message : String(error);
if (DEBUG) {
logError(`💥 Failed to forward to ${targetUrl}: ${errMsg}`);
} else {
logError(
`💥 Failed to forward to ${targetUrl}: ${error instanceof Error ? error.message : String(error)}`,
);
}
return new Response("Bad Gateway", { status: 502 });
}
}
function matchService(req: Request): ServiceConfig | undefined {
const reqUrl = new URL(req.url);
const pathname =
reqUrl.pathname === "/" ? "/" : reqUrl.pathname.replace(/\/+$/, "");
const method = req.method.toUpperCase() as HttpMethod;
function methodMatches(
svcMethod: HttpMethodValue | undefined,
reqMethod: HttpMethod,
): boolean {
if (!svcMethod) return true;
if (svcMethod === "*") return true;
if (typeof svcMethod === "string") return svcMethod === reqMethod;
if (Array.isArray(svcMethod)) return svcMethod.includes(reqMethod);
return false;
}
const exactMatch = config.services.find(
(svc) =>
svc.active && svc.path === pathname && methodMatches(svc.method, method),
);
if (exactMatch) {
logDebug(`✅ Exact match: ${method} ${pathname}`);
return exactMatch;
}
const prefixMatch = config.services.find(
(svc) =>
svc.active &&
svc.prefix &&
pathname.startsWith(svc.prefix) &&
methodMatches(svc.method, method),
);
if (prefixMatch) {
logDebug(`🔎 Prefix match: ${pathname} starts with ${prefixMatch.prefix}`);
return prefixMatch;
}
logDebug(`❓ No match found for ${method} ${pathname}`);
return undefined;
}
// --- SERVER SETUP ---
async function startServer() {
try {
config = await loadConfig();
} catch (err) {
logError(`❌ Failed to load config: ${(err as Error).message}`);
process.exit(1);
}
const server = Bun.serve({
hostname: config.host,
port: config.port,
async fetch(req: Request) {
const url = new URL(req.url);
const pathname = url.pathname;
logDebug(`📥 Incoming: ${req.method} ${pathname}`);
if (pathname === "/__health") {
return new Response("✅ Gateway is healthy", { status: 200 });
}
const svc = matchService(req);
if (svc) return forwardRequest(req, svc);
return new Response("❌ Not Found", { status: 404 });
},
});
logInfo(`🚀 Gateway listening on http://${config.host}:${config.port}`);
process.on("SIGINT", async () => {
logInfo("🛑 Received SIGINT. Shutting down...");
await server.stop();
process.exit(0);
});
readStdin();
}
// --- STDIN COMMAND HANDLER ---
async function readStdin() {
const reader = Bun.stdin.stream().getReader();
try {
while (true) {
const { value, done } = await reader.read();
if (done) break;
const command = new TextDecoder().decode(value).trim();
logInfo(`📥 [STDIN] Received command: ${command}`);
if (command === "r") {
logInfo("🔄 Reloading configuration on command...");
config = await loadConfig();
logInfo("🔁 Configuration reloaded");
} else {
logError(`❌ Unknown command: ${command}`);
}
}
} catch (err: unknown) {
logError(`❌ Error reading stdin: ${(err as Error).message}`);
}
}
// --- START ---
startServer().catch((err) => {
logError(`Failed to start server: ${(err as Error).stack || err}`);
process.exit(1);
});