Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@network-utils/tcp-ping

justintaddei11.2kMIT1.2.3TypeScript support: included

A simple TCP ping util, written in Typescript, to test the reachability and latency of a host.

tcp, probe, util, ping, network, socket, ip

readme

tcp-ping

checks code style: prettier

A simple promise-based TCP ping util, written in Typescript, to test the reachability and latency of a host.

Installation

$ npm install @network-utils/tcp-ping --save

Usage

ping(options?: Partial<IPingOptions>, progress?: (progress, total) => void): Promise<IPingResult>

Pings the given host and returns an object containing the latency of the connection and any errors that may have occured.

NOTE: Attempts are not concurrent. As such if a host is unreachable and you provide options { attempts: 60, timeout: 1000 } then ping will not resolve for a full minute!

import { ping } from '@network/tcp-ping'

ping({
  // The IP address or hostname of the host you want
  // to ping. Defaults to 127.0.0.1 (localhost)
  address: '192.168.1.47',
  // How many times do you want want to
  // attempt to reach the host? Default is 10
  attempts: 10,
  // What port do you want to connect on?
  // Default is 80
  port: 80,
  // How long do you want to wait (in milliseconds)
  // before assuming an attempt has failed?
  // Default is 3000 (3 seconds)
  timeout: 3000
}, update).then(result => {
  console.log('ping result:', result)


    // ping result:
    {
      averageLatency: 19.2753,
      errors: [
        {
          // Which attempt failed
          attempt: 3,
          error: Error('Request timeout')
        }
      ],
      maximumLatency: 35.1978,
      minimumLatency: 3.7716,
      options: {
        address: '192.168.1.47',
        attempts: 10,
        port: 80,
        timeout: 3000
      }
    }
})

function update(progress, total) {
  console.log(progress, '/', total)
  /*
    1 / 10
    2 / 10
    3 / 10
    ...
  */
}

probe(port: number, address?: string, timeout?: number): Promise<boolean>

Makes one attempt to reach the host and returns a boolean indicating whether or not it was successful.
If address is not provided it will default to '127.0.0.1'.
If timeout is not provided it will default to 3000.

import { probe } from '@network/tcp-ping'

probe(80, '192.168.1.47', 500).then(hostReachable => {
  if (hostReachable) console.log('The host is reachable 🙌')
  else console.log('The host is not reachable 🤐')
})

// Or

const hostReachable = await probe(80, '192.168.1.47', 500)

Errors

  • All methods will throw a "Negative port" error if port < 1.

Testing

$ git clone https://github.com/justintaddei/tcp-ping.git
$ cd tcp-ping
$ npm install
$ npm test

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.2.3 (2023-05-09)

1.2.2 (2023-05-09)

Bug Fixes

  • deps: bump actions/cache from 2.1.6 to 2.1.7 (349eaad)
  • deps: bump actions/cache from 2.1.7 to 3.0.1 (827b0d1)
  • deps: bump actions/cache from 3.0.1 to 3.0.2 (848168a)
  • deps: bump actions/cache from 3.0.2 to 3.0.3 (5f7bac5)
  • deps: bump actions/cache from 3.0.3 to 3.0.4 (a0f058b)
  • deps: bump actions/cache from 3.0.4 to 3.3.1 (afb6146)
  • deps: bump actions/setup-node from 2.2.0 to 2.3.0 (0caab2c)
  • deps: bump actions/setup-node from 2.3.0 to 2.4.0 (7c5baab)
  • deps: bump actions/setup-node from 2.4.0 to 2.4.1 (0c2e3da)
  • deps: bump actions/setup-node from 2.4.1 to 2.5.1 (891e0ba)
  • deps: bump actions/setup-node from 2.5.1 to 3 (7efe91f)
  • deps: bump ajv from 6.10.2 to 6.12.6 (0cb40cb)
  • deps: bump dependabot/fetch-metadata from 1.1.1 to 1.2.0 (9d7168a)
  • deps: bump dependabot/fetch-metadata from 1.2.0 to 1.2.1 (9d3de02)
  • deps: bump dependabot/fetch-metadata from 1.2.1 to 1.3.0 (0757cf3)
  • deps: bump dependabot/fetch-metadata from 1.3.0 to 1.3.1 (fc50474)
  • deps: bump dependabot/fetch-metadata from 1.3.1 to 1.3.2 (18f58e1)
  • deps: bump dependabot/fetch-metadata from 1.3.2 to 1.4.0 (380a8aa)
  • deps: bump ini from 1.3.5 to 1.3.8 (6632433)
  • deps: bump minimist from 1.2.5 to 1.2.6 (e09acbe)
  • deps: bump path-parse from 1.0.6 to 1.0.7 (34bbd84)
  • deps: bump tar from 4.4.13 to 4.4.19 (e8e5e47)
  • deps: bump tmpl from 1.0.4 to 1.0.5 (22c6f4c)
  • update tests to work with jest@29 (c62b3bc)

CHANGE LOG

v1.2.0

  • Remove IP limitation, so that hostnames can be used to probe/ping.