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

tcp-ping-port

v1.0.2

Published

A simple TCP ping utility to ping a port of an IP or domain

Downloads

1,026

Readme

tcp-ping-port

A simple TCP ping utility to ping a port of an IP or domain.

NPM version Release Status Build Status Coverage Status

Table of contents


Benefits


Following are the pros and cons of this package compared to the regular ping

Pros

  • Can be used with Amazon Lambda functions and Aazure functions.
    Regular ping uses ICMP protocol which is not permitted by AWS/Azure functions
  • TCP packets have higher priorty than ICMP packets
  • It can ping a particular port
  • It can work even if ICMP is disabled

Cons

  • Higher data usage

    | Protocol | Send (B)) | Receive (B) | Total (B) | Data saving | | -------- | --------- | ------------ | ---------- | ----------- | | TCP | 228 | 126 | 354 | 0% | | ICMP | 98 | 98 | 196 | 45% |

    The numbers are based on data reported by Wireshark

    TCP Ping

    | No. | Time | Source | Destination | Protocol | Length | Info | | --- | ---- | ------ | ----------- | -------- | ------ | ---- |
    | 1 | 0.000000 | x.x.x.x | y.y.y.y | DNS | 70 | Standard query 0xfe55 A google.com | | 2 | 0.000393 | y.y.y.y | x.x.x.x | DNS | 86 | Standard query response 0xfe55 A google.com A 172.217.165.14 | | | | | | | | |
    | 3 | 0.001364 | x.x.x.x | 172.217.165.14 | TCP | 66 | 49985 → 80 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 WS=256 SACK_PERM=1 | | 4 | 0.012339 | 172.217.165.14 | x.x.x.x | TCP | 66 | 80 → 49985 [SYN, ACK] Seq=0 Ack=1 Win=65535 Len=0 MSS=1430 SACK_PERM=1 WS=256 | | 5 | 0.012412 | x.x.x.x | 172.217.165.14 | TCP | 54 | 49985 → 80 [ACK] Seq=1 Ack=1 Win=262912 Len=0 | | 6 | 0.013110 | x.x.x.x | 172.217.165.14 | TCP | 54 | 49985 → 80 [FIN, ACK] Seq=1 Ack=1 Win=262912 Len=0 | | 7 | 0.024927 | 172.217.165.14 | x.x.x.x | TCP | 60 | 80 → 49985 [FIN, ACK] Seq=1 Ack=2 Win=65536 Len=0 | | 8 | 0.024987 | x.x.x.x | 172.217.165.14 | TCP | 54 | 49985 → 80 [ACK] Seq=2 Ack=2 Win=262912 Len=0 |

    ICMP ping

    | No. | Time | Source | Destination | Protocol | Length | Info | | --- | ---- | ------ | ----------- | -------- | ------ | ---- |
    | 1 | 0.000000 | x.x.x.x | y.y.y.y | DNS | 70 | Standard query 0xff9e A google.com| | 2 | 0.003286 | y.y.y.y | x.x.x.x | DNS | 86 | Standard query response 0xff9e A google.com A 172.217.165.14| | 3 | 0.040291 | x.x.x.x | 172.217.165.14| ICMP | 98 | Echo (ping) request id=0x0279, seq=1/256, ttl=128 (reply in 6)| | 4 | 0.064258 | 172.217.165.14 | x.x.x.x | ICMP | 98 | Echo (ping) reply id=0x0279, seq=1/256, ttl=112 (request in 5)|

    NOTE: The first 2 DNS resolution calls are same in both cases for IPv4 and are not included in the total data usagage.

back to top

Installation


$ npm install tcp-ping-port --save

back to top

How to use


Simple examples

const { tcpPingPort } = require("tcp-ping-port")

tcpPingPort("google.com").then(({online}) => {
    // ....
})

With additional options

const { tcpPingPort } = require("tcp-ping-port")
const dns = require('dns')
const options = { 
    socketTimeout: 11000, 
    dnsTimeout: 10000,
    dnsServers: '8.8.8.8',  // google DNS
}
tcpPingPort("google.com", 80, options).then(({online, ping}) => {
    // ....
})

Check if host is offline

const { tcpPingPort } = require("tcp-ping-port")

tcpPingPort("google.com").then(result => {
    if (!result.online && result.error.code === 'TCPPINGTIMEOUT') {
        // your code when host is not accessible....
    }
})

back to top

Dependencies

  • dns-fast-resolver - to resolve multiple domains at the same time and to handle any errors while resolving the domain name. Also, to skip the whole dns resolution if host name is an IP address.
  • lodash
  • maketype

back to top

API

tcpPingPort(hostname[, port, options, resolver])

Attemps to open and close TCP connection

  • hostname <string> Host name or an IP address to try to connect to
  • port <number> Port number
  • options <Object> Socket and Resolver options
    • socketTimeout <integer> Number of milliseconds to wait before the resolving request is canceled. Default value 4000 (4s).
    • dnsTimeout <integer> Number of milliseconds to wait before the resolving request is canceled. Default value 4000 (4s).
      Usefull for rare cases when hostnames may need more than default 4 seconds to be resolved.
    • dnsServers <string[]> Array of RFC 5952 formatted addresses. (Example: ['4.4.4.4', '[2001:4860:4860::8888]', '4.4.4.4:1053', '[2001:4860:4860::8888]:1053'])
  • returns <Promise<Result>> Promise that will return online status of host.
    • Result properties
      • online - indicates if host is online
        • True - when host is online and TCP connection is successfully opened and closed
        • False - when host name is not resolved or it failed to open a TCP connection. Check Result.error for details.
      • ping - returns duration (latency) of the ping in milliseconds.
      • error - returns error causing the host to be flagged as offline. Useful in verifying the host is offline.
      • host - hostname
      • port - port
      • ip - DNS Resolved IP address of the hostname

back to top

Testing

Run all tests

$ npm test

Run individual test ping "google.com:80"

$ npm run unit-test -- --grep "google.com:80"

back to top