From 33bfa8bbfe2e75ac527b7ad9f6b9e0923c37427c Mon Sep 17 00:00:00 2001 From: Filip Joelsson Date: Wed, 8 Nov 2023 11:26:19 +0100 Subject: [PATCH 1/4] fix: ranges now work even if port is unset in options --- src/get-port.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/get-port.ts b/src/get-port.ts index 81b4f86..16182f9 100644 --- a/src/get-port.ts +++ b/src/get-port.ts @@ -26,7 +26,8 @@ export async function getPort( _userOptions = { port: Number.parseInt(_userOptions + "") || 0 }; } - const _port = Number(_userOptions.port ?? process.env.PORT ?? 3000); + const defaultPort = 3000; + const _port = Number(_userOptions.port ?? process.env.PORT); const options = { name: "default", @@ -53,6 +54,7 @@ export async function getPort( options.port, ...options.ports, ..._generateRange(...options.portRange), + defaultPort, ].filter((port) => { if (!port) { return false; From 87b4dbe6756b9254719b6ce31d5f10478be9cbf2 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 9 Jan 2024 12:41:00 +0100 Subject: [PATCH 2/4] update test --- test/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.ts b/test/index.test.ts index 3560d7c..02292bd 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -117,7 +117,7 @@ describe("errors", () => { random: false, }).catch((error) => error); expect(error.toString()).toMatchInlineSnapshot( - `"GetPortError: Unable to find an available port on host "192.168.1.999" (tried 3000, 3000-3100)"`, + `"GetPortError: Unable to find an available port on host "192.168.1.999" (tried 3000-3100)"`, ); }); }); From bd44b040ed73e454a87a36b62861d28b0d5155ee Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 9 Jan 2024 12:42:20 +0100 Subject: [PATCH 3/4] fix: only append default port if no other ports specified --- src/get-port.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/get-port.ts b/src/get-port.ts index 44b659d..8a2348c 100644 --- a/src/get-port.ts +++ b/src/get-port.ts @@ -26,7 +26,6 @@ export async function getPort( _userOptions = { port: Number.parseInt(_userOptions + "") || 0 }; } - const defaultPort = 3000; const _port = Number(_userOptions.port ?? process.env.PORT); const options = { @@ -54,7 +53,6 @@ export async function getPort( options.port, ...options.ports, ..._generateRange(...options.portRange), - defaultPort, ].filter((port) => { if (!port) { return false; @@ -65,6 +63,9 @@ export async function getPort( } return true; }); + if (portsToCheck.length === 0) { + portsToCheck.push(3000); + } // Try to find a port let availablePort = await _findPort(portsToCheck, options.host); From 4cd5123c1946c5a0b55613b234b5086ef47497dc Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 9 Jan 2024 12:46:10 +0100 Subject: [PATCH 4/4] add test --- test/index.test.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/index.test.ts b/test/index.test.ts index 02292bd..3ea2d9b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -13,7 +13,7 @@ describe("getPort", () => { portBlocker?.close(); }); - describe("default host`", () => { + describe("default host", () => { test("default port is not in use", async () => { const port = await getPort(); expect(port).toEqual(3000); @@ -26,6 +26,13 @@ describe("getPort", () => { }); }); + describe("order", () => { + test("`ports` is preferred", async () => { + const port = await getPort({ ports: [8080] }); + expect(port).toEqual(8080); + }); + }); + describe("localhost", () => { test("default port is not in use", async () => { const port = await getPort({ host: "localhost" });