Skip to content

Commit

Permalink
fix(waitForPort): check for all available hosts by default
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 28, 2022
1 parent 780e2cd commit ba5f3b0
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ export async function getRandomPort (host?: HostAddress) {
return port
}

export interface WaitForPortOptions {
host?: HostAddress
delay?: number
retries?: number
}
export async function waitForPort (port: PortNumber, opts: WaitForPortOptions = {}) {
const delay = opts.delay || 500
const retries = opts.retries || 4
for (let i = retries; i > 0; i--) {
if (await checkPort(port, opts.host) === false) {
return
}
await new Promise(resolve => setTimeout(resolve, delay))
}
throw new Error(`Timeout waiting for port ${port} after ${retries} retries with ${delay}ms interval.`)
}

export async function checkPort (port: PortNumber, host: HostAddress | HostAddress[] = process.env.HOST): Promise<PortNumber|false> {
if (!host) {
host = getLocalHosts([undefined /* default */, '0.0.0.0'])
Expand All @@ -86,23 +103,6 @@ export async function checkPort (port: PortNumber, host: HostAddress | HostAddre
return port
}

export interface WaitForPortOptions {
host?: HostAddress
delay?: number
retries?: number
}
export async function waitForPort (port: PortNumber, opts: WaitForPortOptions = {}) {
const delay = opts.delay || 500
const retries = opts.retries || 4
for (let i = retries; i > 0; i--) {
if (await _checkPort(port, opts.host) === false) {
return
}
await new Promise(resolve => setTimeout(resolve, delay))
}
throw new Error(`Timeout waiting for port ${port} after ${retries} retries with ${delay}ms interval.`)
}

// ----- Internal -----

function _checkPort (port: PortNumber, host: HostAddress): Promise<PortNumber|false> {
Expand Down

0 comments on commit ba5f3b0

Please sign in to comment.