Spaces:
Running
Running
Add local test server
Browse files- scripts/serve.mjs +16 -0
scripts/serve.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Local test server for dist/ with the cross-origin isolation headers.
|
| 2 |
+
import { createServer } from 'node:http';
|
| 3 |
+
import { readFile } from 'node:fs/promises';
|
| 4 |
+
import { join, extname } from 'node:path';
|
| 5 |
+
const dist = new URL('../dist/', import.meta.url).pathname;
|
| 6 |
+
const types = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.wasm': 'application/wasm', '.png': 'image/png', '.webmanifest': 'application/manifest+json' };
|
| 7 |
+
createServer(async (req, res) => {
|
| 8 |
+
let p = decodeURIComponent(new URL(req.url, 'http://x').pathname);
|
| 9 |
+
if (p.endsWith('/')) p += 'index.html';
|
| 10 |
+
try {
|
| 11 |
+
const body = await readFile(join(dist, p));
|
| 12 |
+
res.writeHead(200, { 'Content-Type': types[extname(p)] || 'application/octet-stream',
|
| 13 |
+
'Cross-Origin-Opener-Policy': 'same-origin', 'Cross-Origin-Embedder-Policy': 'require-corp' });
|
| 14 |
+
res.end(body);
|
| 15 |
+
} catch { res.writeHead(404); res.end(); }
|
| 16 |
+
}).listen(process.env.PORT || 4173, () => console.log('serving dist on', process.env.PORT || 4173));
|