Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/command/handlers/ping/tcp-ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ type TcpPingStartData = {
port: number;
};

type TcpPingProbeData = {
type TcpPingBaseProbeData = {
type: 'probe';
address: string;
hostname: string;
port: number;
};

type TcpPingSuccessProbeData = TcpPingBaseProbeData & {
rtt: number;
success: boolean;
success: true;
};

type TcpPingFailProbeData = TcpPingBaseProbeData & {
success: false;
};

type TcpPingProbeData = TcpPingSuccessProbeData | TcpPingFailProbeData;

type TcpPingStatsData = {
type: 'statistics';
address: string;
Expand Down Expand Up @@ -74,12 +83,12 @@ export async function tcpPingSingle (hostname: string, address: string, port: nu
});

socket.on('error', () => {
resolve({ type: 'probe', address, hostname, port, rtt: -1, success: false });
resolve({ type: 'probe', address, hostname, port, success: false });
socket.destroy();
});

socket.on('timeout', () => {
resolve({ type: 'probe', address, hostname, port, rtt: -1, success: false });
resolve({ type: 'probe', address, hostname, port, success: false });
socket.destroy();
});

Expand All @@ -104,7 +113,7 @@ export async function tcpPing (
const { target, port, packets, timeout, interval, ipVersion } = options;
const startTime = performance.now();
const results: Array<TcpPingData> = [];
const successTimings: Array<TcpPingProbeData> = [];
const successTimings: Array<TcpPingSuccessProbeData> = [];
let address;

if (isIP(target)) {
Expand Down Expand Up @@ -233,7 +242,7 @@ export function formatTcpPingResult (lines: Array<TcpPingData>): PingParseOutput
};
}

const timings = probeData.map(probe => ({
const timings = probeData.filter(t => t.success === true).map(probe => ({
rtt: roundNumber(probe.rtt, 2),
})).filter(t => !Number.isNaN(t.rtt));

Expand Down
24 changes: 15 additions & 9 deletions test/unit/command/handlers/ping/tcp-ping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,13 @@ describe('tcp-ping', () => {

if (result.type === 'probe') {
expect(result.success).to.be.true;
expect(result.address).to.equal(HOST);
expect(result.hostname).to.equal(HOST);
expect(result.port).to.equal(openPort);
expect(result.rtt).to.be.a('number').within(0, 10);

if (result.success) {
expect(result.address).to.equal(HOST);
expect(result.hostname).to.equal(HOST);
expect(result.port).to.equal(openPort);
expect(result.rtt).to.be.a('number').within(0, 10);
}
}
});

Expand All @@ -171,10 +174,13 @@ describe('tcp-ping', () => {

if (result.type === 'probe') {
expect(result.success).to.be.true;
expect(result.address).to.equal(HOST);
expect(result.hostname).to.equal(HOST);
expect(result.port).to.equal(openPort);
expect(result.rtt).to.be.a('number').greaterThan(20);

if (result.success) {
expect(result.address).to.equal(HOST);
expect(result.hostname).to.equal(HOST);
expect(result.port).to.equal(openPort);
expect(result.rtt).to.be.a('number').greaterThan(20);
}
}
});

Expand All @@ -189,6 +195,7 @@ describe('tcp-ping', () => {
expect(result.address).to.equal(HOST);
expect(result.hostname).to.equal(HOST);
expect(result.port).to.equal(openPort);
expect(result).to.not.have.property('rtt');
}
});

Expand Down Expand Up @@ -504,7 +511,6 @@ describe('tcp-ping', () => {
address: '93.184.216.34',
hostname: 'example.com',
port: 80,
rtt: -1,
success: false,
},
];
Expand Down
2 changes: 1 addition & 1 deletion test/unit/command/ping-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('ping command executor', () => {
const mockedSocket = sandbox.createStubInstance(Socket);

const fakeTcpHandler = (emit: (cb: (chunk: unknown) => void) => Promise<void>) => {
return async (_options: never, onProgress?: (result: any) => void) => {
return async (_options: unknown, onProgress?: (result: any) => void) => {
const chunks = [];

await emit((chunk) => {
Expand Down
Loading