-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(processes):
waitForPort
helper (#452)
* feat(processes): waitForPort helper * ci: adjust timeout * ci: install lsof * chore: replace `lsof` by `node:net` * ci: fix delay * chore: remove lsof * chore: fix imports * docs: add `waitForPort` documentation and examples
- Loading branch information
1 parent
9110430
commit 01751f8
Showing
30 changed files
with
431 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export type WaitForPortOptions = { | ||
/** | ||
* Retry interval in milliseconds | ||
* | ||
* --- | ||
* | ||
* @default 100 | ||
*/ | ||
interval?: number; | ||
/** | ||
* Timeout in milliseconds | ||
* | ||
* --- | ||
* | ||
* @default 60000 | ||
*/ | ||
timeout?: number; | ||
/** | ||
* Delays both the start and end by the defined milliseconds. | ||
* | ||
* --- | ||
* | ||
* @default 0 | ||
*/ | ||
delay?: number; | ||
/** | ||
* Host to check the port on. | ||
* | ||
* --- | ||
* | ||
* @default "localhost" | ||
*/ | ||
host?: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* c8 ignore next */ | ||
import type { WaitForPortOptions } from '../@types/processes.js'; | ||
import { createConnection } from 'node:net'; | ||
|
||
const checkPort = (port: number, host: string): Promise<boolean> => | ||
new Promise((resolve) => { | ||
const client = createConnection(port, host); | ||
|
||
client.on('connect', () => { | ||
client.destroy(); | ||
resolve(true); | ||
}); | ||
|
||
/* c8 ignore start */ | ||
client.on('error', () => { | ||
resolve(false); | ||
}); | ||
/* c8 ignore stop */ | ||
}); | ||
|
||
/** | ||
* Wait until the defined milliseconds. | ||
*/ | ||
export const sleep = (milliseconds: number): Promise<void> => { | ||
/* c8 ignore start */ | ||
if (!Number.isInteger(milliseconds)) { | ||
throw new Error(`Milliseconds must be an integer.`); | ||
} | ||
/* c8 ignore stop */ | ||
|
||
return new Promise((resolve) => setTimeout(resolve, milliseconds)); | ||
}; | ||
|
||
/* c8 ignore start */ // c8 bug | ||
/** | ||
* Wait until the defined port is active. | ||
*/ | ||
export const waitForPort = async ( | ||
port: number, | ||
options?: WaitForPortOptions | ||
): Promise<void> => { | ||
/* c8 ignore stop */ | ||
const delay = options?.delay || 0; | ||
const interval = options?.interval || 100; | ||
const timeout = options?.timeout || 60000; | ||
const host = options?.host || 'localhost'; | ||
|
||
/* c8 ignore start */ | ||
if (!Number.isInteger(port)) { | ||
throw new Error('Port must be an integer.'); | ||
} | ||
|
||
if (!Number.isInteger(interval)) { | ||
throw new Error('Interval must be an integer.'); | ||
} | ||
|
||
if (!Number.isInteger(timeout)) { | ||
throw new Error('Timeout must be an integer.'); | ||
} | ||
|
||
if (!Number.isInteger(delay)) { | ||
throw new Error('Delay must be an integer.'); | ||
} | ||
/* c8 ignore stop */ | ||
|
||
await sleep(delay); | ||
|
||
const startTime = Date.now(); | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const hasPort = await checkPort(port, host); | ||
|
||
if (hasPort) break; | ||
|
||
/* c8 ignore start */ | ||
if (Date.now() - startTime >= timeout) { | ||
throw new Error(`Timeout waiting for port ${port} to become active`); | ||
} | ||
/* c8 ignore stop */ | ||
|
||
await sleep(interval); | ||
} | ||
|
||
await sleep(delay); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.