This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create http utils to use for free port detection and web site pings (#31
- Loading branch information
1 parent
6e9fbb5
commit 03a798a
Showing
1 changed file
with
89 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import * as http from 'http'; | ||
import * as https from 'https'; | ||
|
||
import { timeout } from './timer'; | ||
|
||
/** | ||
* Checks local server for a free port. | ||
* | ||
* @param port Port number to check. | ||
* | ||
* @returns True if port is free and false otherwise. | ||
*/ | ||
function isPortFree(port: number) { | ||
return new Promise((resolve) => { | ||
const server = http.createServer() | ||
.listen(port, () => { | ||
server.close(); | ||
resolve(true); | ||
}) | ||
.on('error', () => { | ||
resolve(false); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* Tries to find a free port recursively. | ||
* | ||
* @param port Starting port number | ||
* | ||
* @returns Available port number or the next port nubmer to try. | ||
*/ | ||
export async function tryPort(port = 3000): Promise<number> { | ||
if (await isPortFree(port)) { | ||
return port; | ||
} | ||
return tryPort(port + 1); | ||
} | ||
|
||
/** | ||
* Pings a url to check if it's up. | ||
* | ||
* @param url The url to ping. | ||
* @returns Url ping result promise. | ||
*/ | ||
export function ping(url: string) { | ||
const promise = new Promise<boolean>((resolve) => { | ||
const useHttps = url.indexOf('https') === 0; | ||
const request = useHttps ? https.request : http.request; | ||
|
||
const pingRequest = request(url, () => { | ||
resolve(true); | ||
pingRequest.destroy(); | ||
}); | ||
|
||
pingRequest.on('error', () => { | ||
resolve(false); | ||
pingRequest.destroy(); | ||
}); | ||
|
||
pingRequest.write(''); | ||
pingRequest.end(); | ||
}); | ||
|
||
return promise; | ||
} | ||
|
||
/** | ||
* Waits for a url to be pingable. | ||
* | ||
* @param url The url to ping. | ||
* @param interval The interval to wait between pings. | ||
* @param max The maximum amount of time to wait. | ||
* | ||
* @returns Url ping result promise. | ||
*/ | ||
export async function waitFor(url: string, interval = 200, max = 30_000) { | ||
let time = Math.ceil(max / interval); | ||
while (time > 0) { | ||
time -= 1; | ||
if (await ping(url)) { | ||
return true; | ||
} | ||
await timeout(interval); | ||
} | ||
|
||
return false; | ||
} |