getport
Get an available TCP port
Usage
import { getPort, randomPort, makeRange } from "https://deno.land/x/getport/mod.ts";
deno run --allow-net script.ts
Example
Without params
import { getPort } from "https://deno.land/x/getport/mod.ts"; const port = getPort(); console.log(port); //=> Return first port available between 1024 and 65535
Pass in a preferred port:
import { getPort } from "https://deno.land/x/getport/mod.ts"; const port = await getPort(3000); console.log(port); //=> Will use 3000 if available, otherwise returns the port plus one
Pass in an array of preferred ports:
import { getPort } from "https://deno.land/x/getport/mod.ts"; const port = await getPort({ port: [3000, 3001, 3002] }); console.log(port); //=> Will use once element in the preferred ports array if available, otherwise returns the last port in array plus one
Use the
makeRange()
helper in case you need a port in a certain range:import { getPort, randomPort, makeRange } from "https://deno.land/x/getport/mod.ts"; const range = makeRange(3000, 3100); console.log(range); //=> Return [3000, 3002, ..., 3099, 3100] const port = await getPort(range); console.log(port); //=> Will use first available port between 3000 and 3100
API
getPort
makeRange
randomPort
Change Logs
See CHANGELOG.md