zenflow / composite-service

Compose multiple services into one
MIT License
7 stars 1 forks source link

Add "Port Utilities" #16

Open zenflow opened 3 years ago

zenflow commented 3 years ago

The following interface would be useful for safely determining port numbers for your services.

type PortNumber = number | string

// Throw an error if the given `port` number is already in use
declare function assertPortFree(port: PortNumber): void

// Returns an array of port numbers that are not in use. 
declare function findPorts(numberOfPorts: number, options: { exclude: PortNumber | PortNumber[] }): number[]

// Example of using findPorts
const [apiPort, webPort] = findPorts(2, { exclude: PORT });

The function signatures above assume that we're only ever working with ports on localhost. Should we support working with ports on other hosts? Docker Machine runs on a different host than localhost on Windows, for example.

zenflow commented 3 years ago

Inspiration (or maybe good enough): https://github.com/http-party/node-portfinder