-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.js
More file actions
69 lines (59 loc) · 2.3 KB
/
Copy pathserve.js
File metadata and controls
69 lines (59 loc) · 2.3 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
// serve.js - MagicWeb v3.2 Hypervisor Server
// Enforces 2026 WebAssembly Cross-Origin Isolation (COOP, COEP, CORP)
import http from "http";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const PORT = 8080;
const MIME_TYPES = {
".html": "text/html; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".mjs": "application/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".css": "text/css; charset=utf-8",
".wasm": "application/wasm",
".png": "image/png",
".svg": "image/svg+xml"
};
const server = http.createServer((req, res) => {
// Mandatory 2026 WASM Cross-Origin Isolation Headers
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "credentialless");
res.setHeader("Cross-Origin-Resource-Policy", "cross-origin");
res.setHeader("Access-Control-Allow-Origin", "*");
const cleanUrl = req.url.split("?")[0];
let relativePath = cleanUrl === "/" ? "index.html" : cleanUrl.replace(/^\//, "");
let filePath = path.join(__dirname, relativePath);
// Service Worker Root Registration Scope
if (cleanUrl.endsWith("sw.js")) {
res.setHeader("Service-Worker-Allowed", "/");
}
fs.readFile(filePath, (err, data) => {
if (err) {
// Fallback for virtual port paths before SW claims
if (cleanUrl.startsWith("/service/")) {
fs.readFile(path.join(__dirname, "index.html"), (errHtml, dataHtml) => {
if (!errHtml) {
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(dataHtml);
return;
}
});
return;
}
res.writeHead(404, { "Content-Type": "text/plain" });
res.end(`404 Not Found: ${cleanUrl}`);
return;
}
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME_TYPES[ext] || "application/octet-stream";
res.writeHead(200, { "Content-Type": contentType });
res.end(data);
});
});
server.listen(PORT, "127.0.0.1", () => {
console.log(`[MagicWeb v3.2] Hypervisor server running on http://localhost:${PORT}/`);
console.log(`Cross-Origin Isolation: ENABLED (COOP: same-origin, COEP: credentialless)`);
});