Skip to content

Commit

Permalink
feat(simulator): support custom the address for the simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
jianzs committed Aug 23, 2024
1 parent 03f1602 commit abc80a8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilly-crabs-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@plutolang/simulator-adapter": patch
"@plutolang/cli": patch
---

feat(simulator): support custom address configuration
1 change: 1 addition & 0 deletions apps/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ async function executeOnce(project: config.Project, stack: config.Stack, entrypo
archRef: archRef,
entrypoint: "",
stateDir: stateDir,
extraConfigs: project.configs,
});
await deployWithAdapter(adapter, stack);
}
Expand Down
7 changes: 6 additions & 1 deletion components/adapters/simulator/src/simAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export class SimulatorAdapter extends core.Adapter {
WORK_DIR: this.stateDir,
};

this.simulator = new Simulator(this.rootpath);
let address: string | undefined;
if (this.extraConfigs?.simulator) {
address = this.extraConfigs.simulator.address;
}

this.simulator = new Simulator(this.rootpath, address);
await this.simulator.start();
envs.PLUTO_SIMULATOR_URL = this.simulator.serverUrl;

Expand Down
40 changes: 28 additions & 12 deletions components/adapters/simulator/src/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { ComputeClosure, AnyFunction, createClosure } from "@plutolang/base/clos
import { MethodNotFound, ResourceNotFound } from "./errors";

export class Simulator {
private readonly projectRoot: string;

private resources: Map<string, simulator.IResourceInstance>;
private closures: Map<string, ComputeClosure<AnyFunction>>;

Expand All @@ -18,8 +16,10 @@ export class Simulator {

private readonly exitHandler = async () => {};

constructor(projectRoot: string) {
this.projectRoot = projectRoot;
constructor(
private readonly projectRoot: string,
private readonly address?: string
) {
this.resources = new Map();
this.closures = new Map();

Expand Down Expand Up @@ -212,19 +212,35 @@ export class Simulator {

public async start(): Promise<void> {
const expressApp = this.createExpress();
for (let port = 9001; ; port++) {
const server = await tryListen(expressApp, port);

if (this.address) {
const [host, port] = this.address.split(":");
const server = await tryListen(expressApp, parseInt(port), host);
if (server === undefined) {
continue;
throw new Error(`Failed to listen on ${this.address}`);
}

const addr = server.address();
if (addr && typeof addr === "object" && addr.port) {
this._serverUrl = `http://${addr.address}:${addr.port}`;
}
this._serverUrl = `http://${host}:${port}`;
this._server = server;
} else {
if (process.env.DEBUG) {
console.log("Starting simulator on a random port...");
}

for (let port = 9001; ; port++) {
const server = await tryListen(expressApp, port);
if (server === undefined) {
continue;
}

break;
const addr = server.address();
if (addr && typeof addr === "object" && addr.port) {
this._serverUrl = `http://localhost:${addr.port}`;
}
this._server = server;

break;
}
}
}

Expand Down

0 comments on commit abc80a8

Please sign in to comment.