From cc8329697e5c10ebff3ff8fc4be4f4470b73ba64 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Mon, 27 Feb 2023 22:15:33 +1300 Subject: [PATCH] improve port handling --- executor/pkg/package.json | 2 +- packages/chopsticks/src/cli.ts | 5 +++-- packages/chopsticks/src/server.ts | 2 +- packages/chopsticks/src/setup-with-server.ts | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/executor/pkg/package.json b/executor/pkg/package.json index 5e1a5bb0..c857c1c6 100644 --- a/executor/pkg/package.json +++ b/executor/pkg/package.json @@ -14,4 +14,4 @@ ], "main": "chopsticks_executor.js", "types": "chopsticks_executor.d.ts" -} \ No newline at end of file +} diff --git a/packages/chopsticks/src/cli.ts b/packages/chopsticks/src/cli.ts index 0308eb1a..0ef50233 100644 --- a/packages/chopsticks/src/cli.ts +++ b/packages/chopsticks/src/cli.ts @@ -14,7 +14,7 @@ import { isUrl } from './utils' import { runBlock } from './run-block' const processConfig = async (path: string) => { - let file + let file: string if (isUrl(path)) { file = await axios.get(path).then((x) => x.data) } else { @@ -26,8 +26,9 @@ const processConfig = async (path: string) => { const processArgv = async (argv: any) => { if (argv.config) { - return { ...(await processConfig(argv.config)), ...argv } + argv = { ...(await processConfig(argv.config)), ...argv } } + argv.port = argv.port ?? (process.env.PORT ? Number(process.env.PORT) : 8000) return argv } diff --git a/packages/chopsticks/src/server.ts b/packages/chopsticks/src/server.ts index d15d77dc..a53081c8 100644 --- a/packages/chopsticks/src/server.ts +++ b/packages/chopsticks/src/server.ts @@ -38,7 +38,7 @@ export const createServer = async (handler: Handler, port?: number) => { let wss: WebSocketServer | undefined let listenPort: number | undefined for (let i = 0; i < 5; i++) { - const preferPort = (port || 0) + i + const preferPort = (port ?? 0) > 0 ? (port ?? 0) + i : 0 logger.debug('Try starting on port %d', preferPort) const [maybeWss, maybeListenPort] = await createWS(preferPort) if (maybeWss && maybeListenPort) { diff --git a/packages/chopsticks/src/setup-with-server.ts b/packages/chopsticks/src/setup-with-server.ts index fea9c9df..233726ab 100644 --- a/packages/chopsticks/src/setup-with-server.ts +++ b/packages/chopsticks/src/setup-with-server.ts @@ -6,7 +6,7 @@ import { setup } from './setup' export const setupWithServer = async (argv: Config) => { const context = await setup(argv) - const port = argv.port || Number(process.env.PORT) || 8000 + const port = argv.port ?? 8000 if (argv.genesis) { // mine 1st block when starting from genesis to set some mock validation data