Skip to content

Commit 728e334

Browse files
committed
Use detect-port for both frontend (base port) and instances
1 parent 97787cf commit 728e334

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

backend/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export function createFrontend(
5959
app.get<{}, Instance[]>("/instances", (req, res) => {
6060
res.json(instances.list());
6161
});
62-
app.post<{}, Instance>("/instances", (req, res) => {
63-
const instance = instances.add();
62+
app.post<{}, Instance>("/instances", async (req, res) => {
63+
const instance = await instances.add();
6464
instance.start();
6565
res.json({
6666
id: instance.id,

backend/instance.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import expressWs from "express-ws";
22
import { WebSocket, Server } from "ws";
3+
import detectPort from "detect-port";
34

45
import { ReceivedStatusUpdate } from "@webxdc/types";
56
import { createProcessor, IProcessor, WebXdcMulti, OnMessage } from "./message";
@@ -86,12 +87,12 @@ export class Instances {
8687
});
8788
}
8889

89-
add(): Instance {
90-
this.currentPort++;
91-
const port = this.currentPort;
90+
async add(): Promise<Instance> {
91+
const port = await detectPort(this.currentPort + 1);
9292
if (this.instances.has(port)) {
9393
throw new Error(`Already have Webxdc instance at port: ${port}`);
9494
}
95+
this.currentPort = port
9596

9697
const instanceUrl = getInstanceUrl(port);
9798

backend/run.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import detectPort from "detect-port";
12
import process from "process";
23
import open from "open";
34

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

1516
async function actualRun(appInfo: AppInfo, options: Options, inject: Inject): Promise<void> {
1617
const { injectFrontend, injectSim, getIndexHtml } = inject;
18+
options.basePort = await detectPort(options.basePort);
1719
const instances = new Instances(appInfo, injectSim, options);
1820

1921
const numberOfInstances = 2;
2022
for (let i = 0; i < numberOfInstances; i++) {
21-
instances.add();
23+
await instances.add();
2224
}
2325

2426
const frontend = createFrontend(

0 commit comments

Comments
 (0)