-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
100 lines (88 loc) · 2.97 KB
/
Copy pathserver.js
File metadata and controls
100 lines (88 loc) · 2.97 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
import http from 'http';
import fs from 'fs';
import path from 'path';
import { transformSync } from 'esbuild';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = process.env.PORT || 8080;
const MIME_TYPES = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
};
const server = http.createServer((req, res) => {
// Normalize path
let filePath = '.' + req.url.split('?')[0];
if (filePath === './') filePath = './index.html';
// Handle extensionless imports common in TS (e.g., import ... from './types')
// Browser requests /types, we need to serve /types.ts
let extname = path.extname(filePath);
// Try to resolve file if extension is missing
if (!fs.existsSync(filePath)) {
const extensions = ['.ts', '.tsx', '.js', '.jsx'];
for (const ext of extensions) {
if (fs.existsSync(filePath + ext)) {
filePath += ext;
extname = ext;
break;
}
}
}
try {
if (!fs.existsSync(filePath)) {
res.writeHead(404);
res.end(`File not found: ${req.url}`);
return;
}
// Special handling for index.html to inject API_KEY
if (filePath === './index.html') {
let content = fs.readFileSync(filePath, 'utf-8');
const apiKey = process.env.API_KEY || '';
// Inject a shim for process.env so the frontend code works
const shim = `<script>window.process = { env: { API_KEY: "${apiKey}" } };</script>`;
// Insert before head or body
content = content.replace('<head>', `<head>${shim}`);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content);
return;
}
// On-the-fly Transpilation for TS/TSX
if (extname === '.ts' || extname === '.tsx') {
const rawContent = fs.readFileSync(filePath, 'utf-8');
try {
const result = transformSync(rawContent, {
loader: extname.slice(1), // 'ts' or 'tsx'
format: 'esm', // Browser expects ES modules
target: 'es2020',
sourcemap: 'inline'
});
res.writeHead(200, { 'Content-Type': 'application/javascript' });
res.end(result.code);
} catch (e) {
console.error("Transpile error:", e);
res.writeHead(500);
res.end(`Transpile Error: ${e.message}`);
}
return;
}
// Serve Static Files
const contentType = MIME_TYPES[extname] || 'application/octet-stream';
const content = fs.readFileSync(filePath);
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
} catch (error) {
console.error(`Server error for ${filePath}:`, error);
res.writeHead(500);
res.end(`Server Error: ${error.code}`);
}
});
server.listen(PORT, () => {
console.log(`Zero-Chat Server running on port ${PORT}`);
});