Skip to content
/ deno-port Public

Deno module to get an available TCP port for your Deno server

License

Notifications You must be signed in to change notification settings

adoi/deno-port

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

deno-port

Build Status

Get an available TCP port for your Deno server

Basic Usage

  import { serve } from "https://deno.land/std/http/server.ts";
  import { getPort, portRange, portSelection } from "https://deno.land/x/port/mod.ts";

  const s = serve({ port: portSelection([3011, 8080], { log: true }) });
  for await (const req of s) {
    req.respond({ body: "Response from server\n" });
  }

The module works with different frameworks of Deno. You can check an example with OAK in the examples/ folder. It is inspired by get-port, find-free-port or other related packages for nodejs.


API

getPort()

getPort(port?: number, options?: { log?: boolean }): number

If port was specified it returns that port, if it was not, it returns a random port assigned from the Operating System.

Optional parameters:

  1. { port?: number }
  2. { log?: boolean }

Example usage:

getPort(3000, { log: true }); 

getPort(3000); // by default log is false

portSelection([1024, 1025, ..., n])

portSelection(ports: Array<number>, options?: { log?: boolean}): number

Returns a port from the specified selection of ports in the ports array.

Required parameters:

  1. ports: Array<numbers>

Optional parameters:

  1. { log?: boolean }

Example usage:

portSelection([8080, 3001], { log: true }); 

portSelection([3000, 3011]); // by default log is false

portRange(min_port, max_port)

portRange(min_port: number, max_port: number, options?: { log?: boolean}): number

Returns a port in between min_port and max_port.

Required parameters:

  1. min_port: number
  2. max_port: number

Optional parameters:

  1. { log?: boolean }

Example usage:

portRange(3001, 8080, { log: true }); 

portRange(3000, 3011); // by default log is false

Other

There are no race conditions when the ports are assigned.

About

Deno module to get an available TCP port for your Deno server

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published