-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprod-server.ts
More file actions
66 lines (55 loc) · 2.08 KB
/
Copy pathprod-server.ts
File metadata and controls
66 lines (55 loc) · 2.08 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
import path from 'node:path';
import { handleApiRequest } from './api-router';
import { resetOctokit } from './github/client';
import { addSecurityHeaders } from './utils/http';
import { logInfo } from './utils/logger';
const distDir = path.resolve(import.meta.dir, '../dist');
const indexFile = path.join(distDir, 'index.html');
const port = Number(process.env.PORT ?? 3000);
const host = process.env.HOST ?? '0.0.0.0';
resetOctokit();
Bun.serve({
port,
hostname: host,
async fetch(req: Request): Promise<Response> {
const url = new URL(req.url);
const pathname = url.pathname;
if (pathname.startsWith('/api/')) {
const response = await handleApiRequest(req);
if (response) return response;
return new Response(JSON.stringify({ error: 'Not found' }), {
status: 404,
headers: { 'Content-Type': 'application/json' },
});
}
// Serve static files
let decodedPath: string;
try {
decodedPath = decodeURIComponent(pathname);
} catch {
return new Response('Bad Request', { status: 400 });
}
const filePath = safeJoin(distDir, decodedPath);
const file = Bun.file(filePath);
if (await file.exists()) {
return addSecurityHeaders(new Response(file));
}
// SPA fallback
return addSecurityHeaders(new Response(Bun.file(indexFile)));
},
});
const hasToken = Boolean(process.env.GITHUB_TOKEN?.trim());
logInfo(`[gitSdm] listening on http://${host}:${port}`);
logInfo(`[gitSdm] GitHub API: ${hasToken ? 'authenticated' : 'unauthenticated'}`);
function safeJoin(root: string, pathname: string): string {
const normalized = pathname.replace(/^[/\\]+/, '');
const resolved = path.resolve(root, normalized);
// Ensure the resolved path strictly resides within the root directory
// by appending the path separator to avoid sibling directory traversal
// (e.g. preventing /app/dist-server from bypassing /app/dist check).
const rootPrefix = root.endsWith(path.sep) ? root : root + path.sep;
if (resolved === root || resolved.startsWith(rootPrefix)) {
return resolved;
}
return indexFile;
}