-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 5 commits
a13cb07
9ff7335
215425a
b27ba4c
9e3debe
6121837
92874ad
f638cb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 || [])], | ||
}; | ||
|
@@ -212,8 +215,6 @@ if (isBuild) { | |
}); | ||
} else { | ||
const port = getPort(); | ||
const durationLabel = "[Reshowcase] Watch and serve started. Duration"; | ||
console.time(durationLabel); | ||
|
||
esbuild | ||
.context(config) | ||
|
@@ -225,11 +226,62 @@ if (isBuild) { | |
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(`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(`forwarding ${req.url} to ${options.host}:${options.port}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added these logs, but maybe it's too verbose. Probably should hide behind some flag There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should add |
||
} | ||
} 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("Proxy request error:", err); | ||
res.writeHead(500, { "Content-Type": "text/plain" }); | ||
res.end("Internal Server Error"); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably should show less details here as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in this case we want to know as much as possible. Would definitely keep them (the same comment about |
||
|
||
req.pipe(proxyReq, { end: true }) | ||
}) | ||
|
||
server.listen(port, error => { | ||
if (error) { | ||
return console.error(error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
console.log(`[Reshowcase] Server listening on port ${port}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we mention something about "watch mode"? The previous message was There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, would be useful to print full URL so that one can cmd-click from the terminal and open the browser tab directly, if desired. |
||
}) | ||
}) | ||
.catch((error) => { | ||
console.error("[Reshowcase] Esbuild serve start failed:", error); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add
[Reshowcase]
as the other logs?