From a13cb077748cb56c469d170bddcb3b4bf8d9bdad Mon Sep 17 00:00:00 2001 From: Rusty Key Date: Thu, 5 Sep 2024 12:57:36 +0200 Subject: [PATCH 1/8] try out proxy possibility --- commands/reshowcase | 59 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/commands/reshowcase b/commands/reshowcase index 331af47..abe79ea 100755 --- a/commands/reshowcase +++ b/commands/reshowcase @@ -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) => { @@ -190,9 +191,11 @@ const getPort = () => { } }; +const {pathRewrites, ...esCustomConfig} = customConfig; + const config = { ...defaultConfig, - ...customConfig, + ...esCustomConfig, define: { ...defaultConfig.define, ...(customConfig.define || {}) }, plugins: [...defaultConfig.plugins, ...(customConfig.plugins || [])], }; @@ -211,9 +214,7 @@ if (isBuild) { process.exit(1); }); } else { - const port = getPort(); - const durationLabel = "[Reshowcase] Watch and serve started. Duration"; - console.time(durationLabel); + const clientPort = getPort(); esbuild .context(config) @@ -225,11 +226,53 @@ if (isBuild) { process.exit(1); }) .then((ctx) => { - return ctx.serve({ port: port, servedir: outputPath }); + return ctx.serve({ servedir: outputPath }) }) - .then((_serveResult) => { - console.timeEnd(durationLabel); - console.error("[Reshowcase] Watch mode started on port:", port); + .then((s) => { + const server = http.createServer((req, res) => { + let changeOrigin = false; + let host = s.host; + let port = s.port; + + pathRewrites?.forEach(rewrite => { + if (req.url.startsWith(rewrite.context)) { + const url = new URL(rewrite.target); + host = url.hostname; + port = url.port; + changeOrigin = rewrite.changeOrigin; + } + }); + + const proxyReq = http.request({ + hostname: host, + port: port, + path: req.url, + method: req.method, + headers: { + ...req.headers, + ...(changeOrigin ? { host: `${host}:${port}` } : {}) + }, + }, proxyRes => { + res.writeHead(proxyRes.statusCode, proxyRes.headers) + proxyRes.pipe(res, { end: true }) + }) + + proxyReq.on("error", err => { + console.error("Proxy request error:", err); + res.writeHead(500, { "Content-Type": "text/plain" }); + res.end("Internal Server Error"); + }); + + req.pipe(proxyReq, { end: true }) + }) + + server.listen(clientPort, error => { + if (error) { + return console.error(error) + } + + console.log(`[Reshowcase] Server listening on port ${clientPort}`) + }) }) .catch((error) => { console.error("[Reshowcase] Esbuild serve start failed:", error); From 9ff733571e40f5372a882d5702e08bb4b78fda45 Mon Sep 17 00:00:00 2001 From: Rusty Key Date: Thu, 5 Sep 2024 16:21:33 +0200 Subject: [PATCH 2/8] add socket support --- commands/reshowcase | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/commands/reshowcase b/commands/reshowcase index abe79ea..d99e64a 100755 --- a/commands/reshowcase +++ b/commands/reshowcase @@ -230,29 +230,31 @@ if (isBuild) { }) .then((s) => { const server = http.createServer((req, res) => { - let changeOrigin = false; - let host = s.host; - let port = s.port; + let options = { + path: req.url, + method: req.method, + }; - pathRewrites?.forEach(rewrite => { - if (req.url.startsWith(rewrite.context)) { + const rewrite = pathRewrites?.find(rewrite => req.url.startsWith(rewrite.context)) + + if (rewrite) { + if (rewrite.socketPath) { + options.socketPath = rewrite.socketPath; + } else { const url = new URL(rewrite.target); - host = url.hostname; - port = url.port; - changeOrigin = rewrite.changeOrigin; + options.host = url.hostname; + options.port = url.port; + options.headers = { + ...req.headers, + ...(rewrite.changeOrigin ? { host: `${options.host}:${options.port}` } : {}) + }; } - }); + } else { + options.host = s.host; + options.port = s.port; + } - const proxyReq = http.request({ - hostname: host, - port: port, - path: req.url, - method: req.method, - headers: { - ...req.headers, - ...(changeOrigin ? { host: `${host}:${port}` } : {}) - }, - }, proxyRes => { + const proxyReq = http.request(options, proxyRes => { res.writeHead(proxyRes.statusCode, proxyRes.headers) proxyRes.pipe(res, { end: true }) }) From 215425a0eda832b76566162d433b43e428e37d7c Mon Sep 17 00:00:00 2001 From: Rusty Key Date: Thu, 5 Sep 2024 16:22:37 +0200 Subject: [PATCH 3/8] clarify variable names --- commands/reshowcase | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/commands/reshowcase b/commands/reshowcase index d99e64a..b8e36fa 100755 --- a/commands/reshowcase +++ b/commands/reshowcase @@ -214,7 +214,7 @@ if (isBuild) { process.exit(1); }); } else { - const clientPort = getPort(); + const port = getPort(); esbuild .context(config) @@ -228,7 +228,7 @@ if (isBuild) { .then((ctx) => { return ctx.serve({ servedir: outputPath }) }) - .then((s) => { + .then(esbuildServer => { const server = http.createServer((req, res) => { let options = { path: req.url, @@ -250,8 +250,8 @@ if (isBuild) { }; } } else { - options.host = s.host; - options.port = s.port; + options.host = esbuildServer.host; + options.port = esbuildServer.port; } const proxyReq = http.request(options, proxyRes => { @@ -268,12 +268,12 @@ if (isBuild) { req.pipe(proxyReq, { end: true }) }) - server.listen(clientPort, error => { + server.listen(port, error => { if (error) { return console.error(error) } - console.log(`[Reshowcase] Server listening on port ${clientPort}`) + console.log(`[Reshowcase] Server listening on port ${port}`) }) }) .catch((error) => { From b27ba4cbb02d965b41dbf29bafc66677ca4a0d71 Mon Sep 17 00:00:00 2001 From: Rusty Key Date: Thu, 5 Sep 2024 17:04:43 +0200 Subject: [PATCH 4/8] do not omit headers --- commands/reshowcase | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/commands/reshowcase b/commands/reshowcase index b8e36fa..7316b2d 100755 --- a/commands/reshowcase +++ b/commands/reshowcase @@ -233,6 +233,7 @@ if (isBuild) { let options = { path: req.url, method: req.method, + headers: req.headers, }; const rewrite = pathRewrites?.find(rewrite => req.url.startsWith(rewrite.context)) @@ -240,14 +241,16 @@ if (isBuild) { if (rewrite) { if (rewrite.socketPath) { options.socketPath = rewrite.socketPath; + console.info(`forwarding ${req.url} to ${options.socketPath}`) } else { const url = new URL(rewrite.target); options.host = url.hostname; options.port = url.port; options.headers = { - ...req.headers, + ...options.headers, ...(rewrite.changeOrigin ? { host: `${options.host}:${options.port}` } : {}) }; + console.info(`forwarding ${req.url} to ${options.host}:${options.port}`); } } else { options.host = esbuildServer.host; From 9e3debec19d2a194de751ce5d084224f77370bef Mon Sep 17 00:00:00 2001 From: Rusty Key Date: Thu, 5 Sep 2024 17:18:52 +0200 Subject: [PATCH 5/8] =?UTF-8?q?ensure=20that=20esbuild=20doesn=E2=80=99t?= =?UTF-8?q?=20take=20server=E2=80=99s=20port?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/reshowcase | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/commands/reshowcase b/commands/reshowcase index 7316b2d..8ebfbd4 100755 --- a/commands/reshowcase +++ b/commands/reshowcase @@ -226,7 +226,11 @@ if (isBuild) { process.exit(1); }) .then((ctx) => { - return ctx.serve({ servedir: outputPath }) + return ctx.serve({ + servedir: outputPath, + // ensure that esbuild doesn't take server's port + port: port + 1, + }) }) .then(esbuildServer => { const server = http.createServer((req, res) => { From 612183752d7bab565a5f3bf8f33750932a7e2c00 Mon Sep 17 00:00:00 2001 From: Rusty Key Date: Mon, 9 Sep 2024 11:26:24 +0100 Subject: [PATCH 6/8] add [reshowcase] labels to logs --- commands/reshowcase | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/commands/reshowcase b/commands/reshowcase index 8ebfbd4..c4f9be0 100755 --- a/commands/reshowcase +++ b/commands/reshowcase @@ -201,7 +201,7 @@ const config = { }; if (isBuild) { - const durationLabel = "[Reshowcase] Build finished. Duration"; + const durationLabel = "[reshowcase] Build finished. Duration"; console.time(durationLabel); esbuild @@ -210,7 +210,7 @@ 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 { @@ -222,7 +222,7 @@ if (isBuild) { 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) => { @@ -245,7 +245,7 @@ if (isBuild) { if (rewrite) { if (rewrite.socketPath) { options.socketPath = rewrite.socketPath; - console.info(`forwarding ${req.url} to ${options.socketPath}`) + console.info(`[reshowcase] forwarding ${req.url} to ${options.socketPath}`) } else { const url = new URL(rewrite.target); options.host = url.hostname; @@ -267,7 +267,7 @@ if (isBuild) { }) proxyReq.on("error", err => { - console.error("Proxy request error:", err); + console.error("[reshowcase] Proxy request error:", err); res.writeHead(500, { "Content-Type": "text/plain" }); res.end("Internal Server Error"); }); @@ -277,14 +277,14 @@ if (isBuild) { server.listen(port, error => { if (error) { - return console.error(error) + return console.error(`[reshowcase] ${error}`) } - console.log(`[Reshowcase] Server listening on port ${port}`) + console.log(`[reshowcase] Server listening on port ${port}`) }) }) .catch((error) => { - console.error("[Reshowcase] Esbuild serve start failed:", error); + console.error("[reshowcase] Esbuild serve start failed:", error); process.exit(1); }); } From 92874ad9fdc82b939546b89f7bc6041576027330 Mon Sep 17 00:00:00 2001 From: Rusty Key Date: Mon, 9 Sep 2024 11:34:47 +0100 Subject: [PATCH 7/8] add timings to logs --- commands/reshowcase | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/commands/reshowcase b/commands/reshowcase index c4f9be0..a83c96e 100755 --- a/commands/reshowcase +++ b/commands/reshowcase @@ -103,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!")); }, }; @@ -214,6 +214,8 @@ if (isBuild) { process.exit(1); }); } else { + const durationLabel = "[reshowcase] Watch and serve started. Duration"; + console.time(durationLabel); const port = getPort(); esbuild @@ -278,9 +280,10 @@ if (isBuild) { server.listen(port, error => { if (error) { return console.error(`[reshowcase] ${error}`) + } else { + console.timeEnd(durationLabel); + console.log(`[reshowcase] http://localhost:${port}`) } - - console.log(`[reshowcase] Server listening on port ${port}`) }) }) .catch((error) => { From f638cb26256ead64a3b2b5bd0472d090748046d0 Mon Sep 17 00:00:00 2001 From: Rusty Key Date: Mon, 9 Sep 2024 14:18:30 +0100 Subject: [PATCH 8/8] add more labels to logs --- commands/reshowcase | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/reshowcase b/commands/reshowcase index a83c96e..50c4cd8 100755 --- a/commands/reshowcase +++ b/commands/reshowcase @@ -256,7 +256,7 @@ if (isBuild) { ...options.headers, ...(rewrite.changeOrigin ? { host: `${options.host}:${options.port}` } : {}) }; - console.info(`forwarding ${req.url} to ${options.host}:${options.port}`); + console.info(`[reshowcase] forwarding ${req.url} to ${options.host}:${options.port}`); } } else { options.host = esbuildServer.host;