npm

portz
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i portz

Weekly Downloads

3

Version

0.1.0

License

MIT

Unpacked Size

16 kB

Total Files

17

Last publish

Collaborators

  • deepsweet