Skip to content

Latest commit

 

History

History

portz

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

portz npm

Service port registry and dependency queue manager.

Allows to run services concurrently respecting cross-dependencies by waiting for TCP port to become in use on 0.0.0.0 interface.

Install

$ yarn add portz

Usage

API

type TStartServerOptions = {
  fromPort: number,
  toPort: number
}

const startServer: (options: TStartServerOptions) => Promise<() => Promise<void>>
type TRegisterServiceOptions = {
  name: string,
  deps?: string[]
}

type TRegisterServiceResult = {
  port: number,
  deps?: {
    [k: string]: number
  },
}

const registerService: (options: TRegisterServiceOptions) => Promise<TRegisterServiceResult>
const unregisterService: (name: string) => Promise<void>
import { startServer, registerService } from 'portz'

const stopServer = await startServer({
  fromPort: 3000,
  toPort: 4000
})

console.log(
  await Promise.all([
    registerService({ name: 'foo', deps: ['bar'] }),
    registerService({ name: 'bar' }),
    registerService({ name: 'baz', deps: ['foo', 'bar'] }
  ])
)
/*
[
  { port: 3001, deps: { bar: 3000 } },
  { port: 3000 },
  { port: 3002, deps: { foo: 3001, bar: 3000 } }
]
*/

await stopServer()

CLI

{
  "scripts": {
    "foo": "echo $PORT",
    "bar": "echo $PORT $PORT_FOO"
  }
}
portz start --range 3000:4000
portz register --name foo -- npm run foo
portz register --name bar --deps foo -- npm run bar