-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: main
Are you sure you want to change the base?
Conversation
@@ -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); |
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.
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); |
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.
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:
@@ -18,4 +18,4 @@ const program = createProgram({ | |||
}, | |||
}); | |||
|
|||
program.parse(); | |||
program.parseAsync(); |
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.
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}`); | ||
} |
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.
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.
@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", |
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 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.
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.
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.
I can swap the dependency out for a simpler version of this for sure, no problem. I just wanted to solve the problem first 😄
I'm actually not very familiar with this codebase, so from my perspective everything is fine as long as it works in the end. |
Closes #68