Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy support #32

Merged
merged 8 commits into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 67 additions & 12 deletions commands/reshowcase
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require("fs");
const path = require("path");
const os = require("os");
const esbuild = require("esbuild");
const http = require("http");
const { htmlPlugin } = require("@craftamap/esbuild-plugin-html");

const toAbsolutePath = (filepath) => {
Expand Down Expand Up @@ -102,7 +103,7 @@ const customConfig = (() => {
const watchPlugin = {
name: "watchPlugin",
setup: (build) => {
build.onEnd((_buildResult) => console.log("[Esbuild] Rebuild finished!"));
build.onEnd((_buildResult) => console.log("[esbuild] Rebuild finished!"));
},
};

Expand Down Expand Up @@ -190,15 +191,17 @@ const getPort = () => {
}
};

const {pathRewrites, ...esCustomConfig} = customConfig;

const config = {
...defaultConfig,
...customConfig,
...esCustomConfig,
define: { ...defaultConfig.define, ...(customConfig.define || {}) },
plugins: [...defaultConfig.plugins, ...(customConfig.plugins || [])],
};

if (isBuild) {
const durationLabel = "[Reshowcase] Build finished. Duration";
const durationLabel = "[reshowcase] Build finished. Duration";
console.time(durationLabel);

esbuild
Expand All @@ -207,32 +210,84 @@ if (isBuild) {
console.timeEnd(durationLabel);
})
.catch((error) => {
console.error("[Reshowcase] Esbuild build failed:", error);
console.error("[reshowcase] Esbuild build failed:", error);
process.exit(1);
});
} else {
const port = getPort();
const durationLabel = "[Reshowcase] Watch and serve started. Duration";
const durationLabel = "[reshowcase] Watch and serve started. Duration";
console.time(durationLabel);
const port = getPort();

esbuild
.context(config)
.then((ctx) => {
return ctx.watch().then(() => ctx);
})
.catch((error) => {
console.error("[Reshowcase] Esbuild watch start failed:", error);
console.error("[reshowcase] Esbuild watch start failed:", error);
process.exit(1);
})
.then((ctx) => {
return ctx.serve({ port: port, servedir: outputPath });
return ctx.serve({
servedir: outputPath,
// ensure that esbuild doesn't take server's port
port: port + 1,
})
})
.then((_serveResult) => {
console.timeEnd(durationLabel);
console.error("[Reshowcase] Watch mode started on port:", port);
.then(esbuildServer => {
const server = http.createServer((req, res) => {
let options = {
path: req.url,
method: req.method,
headers: req.headers,
};

const rewrite = pathRewrites?.find(rewrite => req.url.startsWith(rewrite.context))

if (rewrite) {
if (rewrite.socketPath) {
options.socketPath = rewrite.socketPath;
console.info(`[reshowcase] forwarding ${req.url} to ${options.socketPath}`)
} else {
const url = new URL(rewrite.target);
options.host = url.hostname;
options.port = url.port;
options.headers = {
...options.headers,
...(rewrite.changeOrigin ? { host: `${options.host}:${options.port}` } : {})
};
console.info(`[reshowcase] forwarding ${req.url} to ${options.host}:${options.port}`);
}
} else {
options.host = esbuildServer.host;
options.port = esbuildServer.port;
}

const proxyReq = http.request(options, proxyRes => {
res.writeHead(proxyRes.statusCode, proxyRes.headers)
proxyRes.pipe(res, { end: true })
})

proxyReq.on("error", err => {
console.error("[reshowcase] Proxy request error:", err);
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
});

req.pipe(proxyReq, { end: true })
})

server.listen(port, error => {
if (error) {
return console.error(`[reshowcase] ${error}`)
} else {
console.timeEnd(durationLabel);
console.log(`[reshowcase] http://localhost:${port}`)
}
})
})
.catch((error) => {
console.error("[Reshowcase] Esbuild serve start failed:", error);
console.error("[reshowcase] Esbuild serve start failed:", error);
process.exit(1);
});
}