npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

port-used

v2.0.8

Published

A Node.js module to check if a TCP port is already bound.

Downloads

20,646

Readme

port-used

npm Build Status Code Climate Test Coverage Dependencies Greenkeeper badge

A Node.js module to check if a TCP port is currently in use. It returns a promise. Based on tcp-port-used

Installation

npm install port-used

Examples

To check a port's state:

var portUsed = require('port-used');

portUsed.check(44201, '127.0.0.1')
.then((inUse) => {
  console.log('Port 44201 usage: '+inUse);
}, (err) => {
  console.error('Error on check:', err.message);
});

To wait until a port on a host is available:

portUsed.waitUntilFree({
  port: 44203,
  host: 'some.host.com',
  retryTime: 500,
  timeout: 4000,
}).then(() => {
  console.log('Port 44203 on some.host.com is now free.');
}, (err) => {
  console.log('Error:', err.message);
});

To wait until a port on a host is accepting connections:

portUsed.waitUntilUsed({
  port: 44204,
  host: 'some.host.com',
  retryTime: 500,
  timeout: 4000,
}).then(() => {
  console.log('Port 44204 on some.host.com is now in use.');
}, (err) => {
  console.log('Error:', err.message);
});

To wait until a port on a host is in specific state:

portUsed.waitForStatus({
  port: 44204,
  host: 'some.host.com',
  inUse: true,
  retryTime: 500,
  timeout: 4000,
}).then(() => {
  console.log('Port 44204 on some.host.com is now in use.');
}, (err) => {
  console.log('Error:', err.message);
});

API

check(port [, host])

Checks if a TCP port is in use by attempting to connect to the port on host. If no host is specified, the module uses '127.0.0.1' (localhost). When the promise is resolved, there is a parameter inUse, when true means the port is in use and false means the port is free.

Parameters:

  • Number|Object port The port you are curious to see if available. If an object, must contain all the parameters as properties.
  • String [host] The host name or IP address of the host. Default is '127.0.0.1'.

Returns:

Promise A promise.

waitUntilFree(options)

Returns a promise and fulfills it only when the host's socket is free. Will retry on an interval specified in retryTime until the timeout. If not defined the retryTime is 250 ms and the timeout is 2000 ms. If the host is not defined, the modules uses the default '127.0.0.1'.

Parameters:

  • Object options an object of the following:
    • Number port a valid TCP port number.
    • String [host] The host name or IP address of the host. Default, if not defined: '127.0.0.1'
    • Number [retryTime] the retry interval in ms. Default is 250ms.
    • Number [timeout] the amount of time to wait until port is free. Default is 2000ms.

Returns:

Promise A promise.

waitUntilUsed(options)

Returns a promise and fulfills it only when the socket is accepting connections. Will retry on an interval specified in retryTime until the timeout. If not defined, the retryTime is 250 ms and the timeout is 2000 ms. If the host is not defined the module uses the default '127.0.0.1'.

Parameters:

  • Object options an object of the following:
    • Number port a valid TCP port number.
    • String [host] The host name or IP address of the host. Default is '127.0.0.1'
    • Number [retryTime] the retry interval in ms. Default is 250ms.
    • Number [timeout] the amount of time to wait until port is free. Default is 2000ms.

Returns:

Promise A promise.

waitForStatus(options)

Waits until the port on host matches the boolean status in terms of use. If the status is true, the promise defers until the port is in use. If the status is false the promise defers until the port is free. If the host is undefined or null, the module uses the default '127.0.0.1'. Also, if not defined the retryTime is 250 ms and the timeout is 2000 ms.

Parameters:

  • Object options an object of the following:
    • Number port a valid TCP port number.
    • String [host] The host name or IP address of the host. Default is '127.0.0.1'
    • Boolean inUse A boolean describing the condition to wait for in terms of "in use."
      True indicates wait until the port is in use. False indicates wait until the port is free.
    • Number [retryTime] the retry interval in ms. Default is 250ms.
    • Number [timeout] the amount of time to wait until port is free. Default is 2000ms.

Returns:

Promise A promise.

License

The MIT License (MIT)

Copyright (c) 2018 oculus42
Copyright (c) 2013 jut-io (stdarg)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.