https://github.com/checkly/tcp-ping
https://github.com/checkly/tcp-ping
Last synced: 9 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/checkly/tcp-ping
- Owner: checkly
- License: apache-2.0
- Created: 2022-04-04T16:54:33.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-18T19:23:23.000Z (over 3 years ago)
- Last Synced: 2026-04-01T08:44:03.083Z (3 months ago)
- Language: TypeScript
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TCP ping
- Typescript first
- zero dependencies
- has some tests
- modern codebase
## Usage
```ts
import { TcpPing, TcpPingOptions } from '@checkly/clients/tcp-ping'
const options: TcpPingOptions = { host: 'localhost', port: 8080 }
const tcpPingResult = await new TcpPing().ping(options)
console.log(tcpPingResult)
/** prints
{
"state": "SUCCESS",
"timings": {
"start": 1668546586515,
"lookup": 1668546586572,
"connect": 1668546586599,
"error": 0,
"end": 1668546586600,
"phases":{
"dns": 57.748715,
"connect": 27.112156000000006,
"total": 84.916527
}
**/
```
# Adding payloads
You can write to the socket by adding a payload as a `Buffer`. This will also record the `data` time mark and the
`firstByte` phase, as well as a `reponse` in the result. This way you can ping Redis for instance:
```ts
import { TcpPing, TcpPingOptions } from '@checkly/clients/tcp-ping'
const options = { host: 'localhost', port: 6379, payload: new Buffer('*1\r\n$4\r\nPING\r\n', 'utf8') }
const tcpPingResult = await new TcpPing().ping(options)
console.log(tcpPingResult)
/** prints
{
state: 'SUCCESS',
timings: {
start: 1668799114636,
lookup: 1668799114644,
connect: 1668799114647,
data: 1668799114647,
error: 0,
end: 1668799114647,
phases: {
dns: 7.833849,
connect: 3.4006320000000008,
firstByte: 3.571518,
total: 14.805999
}
},
response:
}
**/
```
Converting the `Buffer` to a `string` yields:
```ts
console.log(tcpPingResult.response.toString())
// prints '+PONG\r\n'
```
## Inspiration
https://github.com/zulhilmizainuddin/nodejs-traceroute
https://github.com/apaszke/tcp-ping/blob/master/ping.js