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

Add proxy support #32

merged 8 commits into from
Sep 9, 2024

Conversation

rusty-key
Copy link
Collaborator

@rusty-key rusty-key commented Sep 5, 2024

This PR adds basic support for proxying by putting node http server in front of esbuild

@rusty-key rusty-key force-pushed the esbuild-proxy branch 2 times, most recently from 4f96371 to 4bc559e Compare September 5, 2024 13:54
Copy link
Member

@jchavarri jchavarri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

const port = getPort();
const durationLabel = "[Reshowcase] Watch and serve started. Duration";
console.time(durationLabel);
const clientPort = getPort();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clientPort is a bit weird naming as a port is always on the server. What about userFacingPort? Or userPort. Or just port as before (the esbuild one is an internal detail really)

.then((_serveResult) => {
console.timeEnd(durationLabel);
console.error("[Reshowcase] Watch mode started on port:", port);
.then((s) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd name s -> esbuildServer

@rusty-key
Copy link
Collaborator Author

I also realized that we need socket support for this. I'll add it before merging

@@ -225,11 +226,55 @@ if (isBuild) {
process.exit(1);
})
.then((ctx) => {
return ctx.serve({ port: port, servedir: outputPath });
return ctx.serve({ servedir: outputPath })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind of minor: as esbuild will pick up a random port if none is defined, and the esbuild server is created before the proxy one, maybe we could set esbuild port as port + 1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To ensure that esbuild doesn't accidentally take useful port?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah to make sure that esbuild picks port 3000 and user passes 3000 as well from command line. I think it's very unlikely though if esbuild picks the first available port... feel free to ignore.

Copy link
Collaborator Author

@rusty-key rusty-key left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging should be improved, and script itself should be more robust, but we can improve it step-by-step

...options.headers,
...(rewrite.changeOrigin ? { host: `${options.host}:${options.port}` } : {})
};
console.info(`forwarding ${req.url} to ${options.host}:${options.port}`);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Member

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?

Comment on lines 269 to 273
proxyReq.on("error", err => {
console.error("Proxy request error:", err);
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should show less details here as well

Copy link
Member

Choose a reason for hiding this comment

The 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 [Reshowcase] prefix applies).

Copy link
Member

@jchavarri jchavarri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, live reload doesn't work for me. I have tried this branch, esbuild branch, working locally, on nspawn, with async/await... nothing.

It's strange because i use the same mechanism for react-alicante workshop and over there it works great.

...options.headers,
...(rewrite.changeOrigin ? { host: `${options.host}:${options.port}` } : {})
};
console.info(`forwarding ${req.url} to ${options.host}:${options.port}`);
Copy link
Member

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?

if (rewrite) {
if (rewrite.socketPath) {
options.socketPath = rewrite.socketPath;
console.info(`forwarding ${req.url} to ${options.socketPath}`)
Copy link
Member

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?

Comment on lines 269 to 273
proxyReq.on("error", err => {
console.error("Proxy request error:", err);
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
});
Copy link
Member

Choose a reason for hiding this comment

The 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 [Reshowcase] prefix applies).


server.listen(port, error => {
if (error) {
return console.error(error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Reshowcase] prefix.

return console.error(error)
}

console.log(`[Reshowcase] Server listening on port ${port}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mention something about "watch mode"? The previous message was Watch and serve started.. Also the timings were useful (imo).

Copy link
Member

Choose a reason for hiding this comment

The 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.

@rusty-key
Copy link
Collaborator Author

For some reason, live reload doesn't work for me. I have tried this branch, esbuild branch, working locally, on nspawn, with async/await... nothing.

After looking at it, I am not sure I understand at all how it supposed to work. Script is completely dependent on dune, and it fully restarts after every build. So, is watch doing anything? We discard it as soon as rebuild happens.

@jchavarri
Copy link
Member

Ah, great point! That seems to be the issue. If I run in separate terminals:

  • dune build @example -w
  • ./commands/reshowcase start --entry=_build/default/example/example/example/Demo.js --output=_build/default/build

Then it works fine. I think this means we have to keep the esbuild command running, and we have to move away from the "Dune does everything" approach we were following.

Let me know if I can help somehow!

@jchavarri
Copy link
Member

I added a comment for the live reload in the upstream PR because it's probably out of the scope in this one.

@rusty-key
Copy link
Collaborator Author

I'll merge this to upstream PR and continue with fixes there to avoid conflicts

@rusty-key rusty-key merged commit 4d6625e into esbuild Sep 9, 2024
@rusty-key rusty-key deleted the esbuild-proxy branch September 9, 2024 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants