Archived
1
0

feat: add getAvailablePort helper function

This commit is contained in:
Joe Previte
2021-07-27 14:22:25 -07:00
parent 42cfa4a7ca
commit 9137816e33
3 changed files with 42 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import { promises as fs } from "fs"
import * as os from "os"
import * as path from "path"
import * as net from "net"
/**
* Return a mock of @coder/logger.
@ -61,3 +62,23 @@ export function useEnv(key: string): [(nextValue: string | undefined) => string
return [setValue, resetValue]
}
/**
* Helper function to get a random port.
*
* Source: https://github.com/sindresorhus/get-port/blob/main/index.js#L23-L33
*/
export const getAvailablePort = (options?: net.ListenOptions): Promise<number> =>
new Promise((resolve, reject) => {
const server = net.createServer()
server.unref()
server.on("error", reject)
server.listen(options, () => {
// NOTE@jsjoeio: not a huge fan of the type assertion
// but it works for now.
const { port } = server.address() as net.AddressInfo
server.close(() => {
resolve(port)
})
})
})