-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunity-server.js
More file actions
73 lines (60 loc) · 2.09 KB
/
unity-server.js
File metadata and controls
73 lines (60 loc) · 2.09 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
const PORT = 8000;
const mimeTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.ico': 'image/x-icon',
'.wasm': 'application/wasm'
};
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url);
let pathname = parsedUrl.pathname;
// Default to index.html for root requests
if (pathname === '/') {
pathname = '/index.html';
}
const filePath = path.join(__dirname, pathname);
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 Not Found');
return;
}
let contentType = 'application/octet-stream';
let contentEncoding = null;
// Handle Brotli compressed files
if (pathname.endsWith('.br')) {
contentEncoding = 'br';
if (pathname.endsWith('.js.br')) {
contentType = 'application/javascript';
} else if (pathname.endsWith('.wasm.br')) {
contentType = 'application/wasm';
} else if (pathname.endsWith('.data.br')) {
contentType = 'application/octet-stream';
}
} else {
// Regular files
const ext = path.extname(pathname);
contentType = mimeTypes[ext] || 'application/octet-stream';
}
const headers = {
'Content-Type': contentType,
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin'
};
if (contentEncoding) {
headers['Content-Encoding'] = contentEncoding;
}
res.writeHead(200, headers);
res.end(data);
});
});
server.listen(PORT, () => {
console.log(`Unity WebGL server running at http://localhost:${PORT}`);
console.log('Press Ctrl+C to stop the server');
});