-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (89 loc) · 3.45 KB
/
server.js
File metadata and controls
106 lines (89 loc) · 3.45 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
const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const PORT = 8081;
const CDN_BASE = 'https://cdn.zetaoffice.net';
const MIME_TYPES = {
'.html': 'text/html',
'.js': 'application/javascript',
'.wasm': 'application/wasm',
'.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) => {
console.log(`${req.method} ${req.url}`);
// Set COOP/COEP headers for cross-origin isolation
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
// Proxy CDN requests
if (req.url.startsWith('/cdn/')) {
const cdnPath = req.url.replace('/cdn/', '/');
const cdnUrl = CDN_BASE + cdnPath;
console.log(`Proxying to: ${cdnUrl}`);
https.get(cdnUrl, (proxyRes) => {
// Add CORP header to allow embedding
res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
res.setHeader('Access-Control-Allow-Origin', '*');
// Copy content type
const contentType = proxyRes.headers['content-type'];
if (contentType) {
res.setHeader('Content-Type', contentType);
}
// Copy caching headers
const cacheControl = proxyRes.headers['cache-control'];
if (cacheControl) {
res.setHeader('Cache-Control', cacheControl);
}
const encoding = proxyRes.headers['content-encoding'];
console.log(`Response encoding: ${encoding}, status: ${proxyRes.statusCode}`);
// Handle compressed responses
let stream = proxyRes;
if (encoding === 'gzip') {
stream = proxyRes.pipe(zlib.createGunzip());
} else if (encoding === 'deflate') {
stream = proxyRes.pipe(zlib.createInflate());
} else if (encoding === 'br') {
stream = proxyRes.pipe(zlib.createBrotliDecompress());
}
res.writeHead(proxyRes.statusCode);
stream.pipe(res);
}).on('error', (err) => {
console.error('Proxy error:', err);
res.writeHead(500);
res.end('Proxy error');
});
return;
}
// Serve local files
let filePath = req.url === '/' ? '/index.html' : req.url;
filePath = path.join(__dirname, filePath.split('?')[0]);
const ext = path.extname(filePath);
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404);
res.end('Not found');
} else {
res.writeHead(500);
res.end('Server error');
}
return;
}
res.setHeader('Content-Type', contentType);
res.setHeader('Cross-Origin-Resource-Policy', 'same-origin');
res.writeHead(200);
res.end(content);
});
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
console.log('CDN proxy available at /cdn/...');
});