-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
94 lines (79 loc) · 2.58 KB
/
Copy pathserver.js
File metadata and controls
94 lines (79 loc) · 2.58 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
#!/usr/bin/env node
/**
* Simple development server with COOP/COEP headers for SharedArrayBuffer support
* Required for multi-threaded WASM with Rayon
*
* Usage: node server.js [port]
* Default port: 3000
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = process.argv[2] || 3000;
const ROOT = __dirname;
// MIME types for common file extensions
const MIME_TYPES = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'text/javascript',
'.mjs': 'text/javascript',
'.json': 'application/json',
'.wasm': 'application/wasm',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.webp': 'image/webp',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.ttf': 'font/ttf',
};
const server = http.createServer((req, res) => {
// Parse URL and handle query strings
let urlPath = req.url.split('?')[0];
// Default to index.html
if (urlPath === '/') {
urlPath = '/index.html';
}
const filePath = path.join(ROOT, urlPath);
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
// Security: prevent directory traversal
if (!filePath.startsWith(ROOT)) {
res.writeHead(403);
res.end('Forbidden');
return;
}
fs.readFile(filePath, (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404);
res.end('Not Found');
} else {
res.writeHead(500);
res.end('Server Error');
}
return;
}
// Set headers for SharedArrayBuffer support (required for WASM threads)
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
res.setHeader('Content-Type', contentType);
res.setHeader('Cache-Control', 'no-cache');
res.writeHead(200);
res.end(data);
});
});
server.listen(PORT, () => {
console.log(`
🍺 Beercap Mosaic Generator - Development Server
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Local: http://localhost:${PORT}
✓ Cross-Origin-Opener-Policy: same-origin
✓ Cross-Origin-Embedder-Policy: require-corp
✓ SharedArrayBuffer enabled (WASM threads ready)
Press Ctrl+C to stop
`);
});