MCPcopy
hub / github.com/vercel/next.js / getFreePort

Function getFreePort

packages/next/src/server/lib/worker-utils.ts:3–17  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1import http from 'http'
2
3export const getFreePort = async (): Promise<number> => {
4 return new Promise((resolve, reject) => {
5 const server = http.createServer(() => {})
6 server.listen(0, () => {
7 const address = server.address()
8 server.close()
9
10 if (address && typeof address === 'object') {
11 resolve(address.port)
12 } else {
13 reject(new Error('invalid address from server: ' + address?.toString()))
14 }
15 })
16 })
17}

Callers

nothing calls this directly

Calls 6

createServerMethod · 0.80
addressMethod · 0.80
closeMethod · 0.65
resolveFunction · 0.50
rejectFunction · 0.50
toStringMethod · 0.45

Tested by

no test coverage detected