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

Use next available port #69

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Use next available port #69

wants to merge 4 commits into from

Conversation

ralphtheninja
Copy link
Member

Closes #68

@@ -14,11 +15,12 @@ export type Inject = {

async function actualRun(appInfo: AppInfo, options: Options, inject: Inject): Promise<void> {
const { injectFrontend, injectSim, getIndexHtml } = inject;
options.basePort = await detectPort(options.basePort);
Copy link
Member Author

Choose a reason for hiding this comment

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

Before this line options.basePort is either 7000 (the default) or the value provided by --port and detectPort() then tries that port or finds the next available in sequence.

this.currentPort++;
const port = this.currentPort;
async add(): Promise<Instance> {
const port = await detectPort(this.currentPort + 1);
Copy link
Member Author

@ralphtheninja ralphtheninja Feb 28, 2025

Choose a reason for hiding this comment

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

This works quite nicely actually, so if you start e.g. webxdc-dev run /path/to/some.xdc in one console (would pick port 7000 and also have port 7001 and 7002 for the instances) and then do webxdc-dev run --port 7002 /path/to/some.xdc then the backend would start at 7003 (since 7002 was already taken by one of the instances in the first window) with two instances at 7004 and 7005.

If you then click Add Instance in the first window, then the next instance will get port 7006:

image

@@ -18,4 +18,4 @@ const program = createProgram({
},
});

program.parse();
program.parseAsync();
Copy link
Member Author

Choose a reason for hiding this comment

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

Need to call .parseAsync() if the action callback is async.

this.currentPort++;
const port = this.currentPort;
async add(): Promise<Instance> {
const port = await detectPort(this.currentPort + 1);
if (this.instances.has(port)) {
throw new Error(`Already have Webxdc instance at port: ${port}`);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this if-statement is redundant now since detectPort() will return a valid port that's not taken. But I'm fine with leaving it here for good measure.

@ralphtheninja
Copy link
Member Author

@Simon-Laux Mind having a quick look at this?

@@ -83,6 +83,7 @@
"adm-zip": "^0.5.9",
"body-parser": "^1.20.0",
"commander": "^9.3.0",
"detect-port": "^2.1.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe dumb question: couldn't you just try to listen on the port and catch the error if the address is already in use? instead of adding a new dependency.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not a dumb question. That's basically what the dependency does.

image

Copy link
Member Author

Choose a reason for hiding this comment

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

I can swap the dependency out for a simpler version of this for sure, no problem. I just wanted to solve the problem first 😄

@Simon-Laux
Copy link
Contributor

@Simon-Laux Mind having a quick look at this?

I'm actually not very familiar with this codebase, so from my perspective everything is fine as long as it works in the end.
maybe ask @Septias or @adbenitez or @WofWca, they did a bit more than me https://github.com/webxdc/webxdc-dev/graphs/contributors

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.

Use next available port instead of hardcoded as default when using run command
2 participants