-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevserver.js
52 lines (44 loc) · 1.61 KB
/
devserver.js
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
import * as esbuild from "esbuild";
import http from "node:http";
// Start esbuild's server on a random local port
let ctx = await esbuild.context({
bundle: true,
outdir: "www",
entryPoints: ["src/main.tsx"],
});
// The return value tells us where esbuild's local server is
let { host, port } = await ctx.serve({ servedir: "www" });
// Then start a proxy server on port 3000
http
.createServer((req, res) => {
const options = {
hostname: host,
port: port,
path: req.url,
method: req.method,
headers: req.headers,
};
// per https://webcontainers.io/guides/quickstart#cross-origin-isolation
// need these:
// Cross-Origin-Embedder-Policy: require-corp
// Cross-Origin-Opener-Policy: same-origin
res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
res.setHeader("Cross-Origin-Resource-Policy", "cross-origin");
// Forward each incoming request to esbuild
const proxyReq = http.request(options, (proxyRes) => {
// If esbuild returns "not found", send a custom 404 page
if (proxyRes.statusCode === 404) {
res.writeHead(404, { "Content-Type": "text/html" });
res.end("<h1>A custom 404 page</h1>");
return;
}
// Otherwise, forward the response from esbuild to the client
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res, { end: true });
});
// Forward the body of the request to esbuild
req.pipe(proxyReq, { end: true });
})
.listen(3000);
console.log("listening on http://localhost:3000");