From a31e04d1e5dc8bffe959806ebd6b6d9317e2e0c5 Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Sun, 5 Jan 2025 09:07:59 +0100 Subject: [PATCH] Replace get-port (#2242) --- lib/core/engine/index.js | 12 +++--------- lib/support/getPort.js | 25 +++++++++++++++++++++++++ package-lock.json | 17 ----------------- package.json | 1 - 4 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 lib/support/getPort.js diff --git a/lib/core/engine/index.js b/lib/core/engine/index.js index 9ad1bd9e7..d46a7c5c4 100644 --- a/lib/core/engine/index.js +++ b/lib/core/engine/index.js @@ -1,7 +1,6 @@ import { arch as _arch } from 'node:os'; import intel from 'intel'; import merge from 'lodash.merge'; -import getPort, { portNumbers } from 'get-port'; import { execaCommand as command } from 'execa'; import get from 'lodash.get'; import { StorageManager } from '../../support/storageManager.js'; @@ -31,6 +30,7 @@ import { loadScript, loadPageCompleteScript } from '../../support/engineUtils.js'; +import { getAvailablePort } from '../../support/getPort.js'; const log = intel.getLogger('browsertime'); const defaults = { scripts: [], @@ -67,19 +67,13 @@ export class Engine { async start() { const options = this.options; - options.devToolsPort = await getPort({ - port: portNumbers(9222, 9350), - host: '127.0.0.1' - }); + options.devToolsPort = await getAvailablePort([9222, 9350]); log.debug( `Preparing port ${options.devToolsPort} for devtools on Chrome/Edge` ); - options.safariDriverPort = await getPort({ - port: portNumbers(1234, 2000), - host: '127.0.0.1' - }); + options.safariDriverPort = await getAvailablePort([1234, 2000]); if (options.browser === 'safari' && options.safari.ios && options.video) { await IOSRecorder.activate(); diff --git a/lib/support/getPort.js b/lib/support/getPort.js new file mode 100644 index 000000000..d5a6c1326 --- /dev/null +++ b/lib/support/getPort.js @@ -0,0 +1,25 @@ +import { createServer } from 'node:net'; + +export async function getAvailablePort(portRange, host = '127.0.0.1') { + const startPort = portRange[0]; + const endPort = portRange[1]; + + for (let port = startPort; port <= endPort; port++) { + if (await isPortAvailable(port, host)) { + return port; + } + } + + throw new Error(`No available ports found in range ${startPort}-${endPort}`); +} + +function isPortAvailable(port, host) { + return new Promise(resolve => { + const server = createServer(); + server.unref(); + server.on('error', () => resolve(false)); + server.listen(port, host, () => { + server.close(() => resolve(true)); + }); + }); +} diff --git a/package-lock.json b/package-lock.json index 13c5b61f4..1a17841df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,6 @@ "execa": "9.5.2", "fast-stats": "0.0.7", "ff-test-bidi-har-export": "0.0.17", - "get-port": "7.1.0", "intel": "1.2.0", "lodash.get": "4.4.2", "lodash.merge": "4.6.2", @@ -3887,17 +3886,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/get-port": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/get-port/-/get-port-7.1.0.tgz", - "integrity": "sha512-QB9NKEeDg3xxVwCCwJQ9+xycaz6pBB6iQ76wiWMl1927n0Kir6alPiP+yuiICLLU4jpMe08dXfpebuQppFA2zw==", - "engines": { - "node": ">=16" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/get-stream": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz", @@ -10243,11 +10231,6 @@ "integrity": "sha512-2nk+7SIVb14QrgXFHcm84tD4bKQz0RxPuMT8Ag5KPOq7J5fEmAg0UbXdTOSHqNuHSU28k55qnceesxXRZGzKWA==", "dev": true }, - "get-port": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/get-port/-/get-port-7.1.0.tgz", - "integrity": "sha512-QB9NKEeDg3xxVwCCwJQ9+xycaz6pBB6iQ76wiWMl1927n0Kir6alPiP+yuiICLLU4jpMe08dXfpebuQppFA2zw==" - }, "get-stream": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz", diff --git a/package.json b/package.json index 288fee851..b09177480 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "execa": "9.5.2", "fast-stats": "0.0.7", "ff-test-bidi-har-export": "0.0.17", - "get-port": "7.1.0", "intel": "1.2.0", "lodash.get": "4.4.2", "lodash.merge": "4.6.2",