Skip to content

Commit

Permalink
fix: http improvements (#56)
Browse files Browse the repository at this point in the history
* ref: replace timings values

* fix: download limit error + error code mapping

* ref: error

* fix: test cases

* fix: http2 doesnt return tls

* fix: misleading comment

* fix: timings: no total/download values on error
  • Loading branch information
patrykcieszkowski authored Jun 9, 2022
1 parent c2a93c3 commit cedde96
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
38 changes: 26 additions & 12 deletions src/command/http-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export type HttpOptions = {
};
};

export type Timings = {
[k: string]: number | Record<string, unknown>;
phases: Record<string, number>;
};

const allowedHttpProtocols = ['http', 'https', 'http2'];
const allowedHttpMethods = ['get', 'head'];
export const httpOptionsSchema = Joi.object<HttpOptions>({
Expand Down Expand Up @@ -67,7 +72,7 @@ export const httpCmd = (options: HttpOptions, resolverFn?: ResolverType): Reques
return got.stream(url, options_);
};

const isTlsSocket = (socket: unknown): socket is TLSSocket => 'getPeerCertificate' in (socket as {getPeerCertificate?: unknown});
const isTlsSocket = (socket: unknown): socket is TLSSocket => Boolean((socket as {getPeerCertificate?: unknown}).getPeerCertificate);

export class HttpCommand implements CommandInterface<HttpOptions> {
constructor(private readonly cmd: typeof httpCmd) {}
Expand All @@ -94,10 +99,15 @@ export class HttpCommand implements CommandInterface<HttpOptions> {
};

const respond = () => {
const timings = (stream.timings ?? {}) as Timings;
if (!timings['end']) {
timings['end'] = Date.now();
}

result.timings = {
...result.timings,
total: stream.response?.timings.phases.total,
download: stream.response?.timings.phases.download,
total: timings.phases['total'] ?? Number(timings['end']) - Number(timings['start']),
download: timings.phases['download'] ?? Number(timings['end']) - Number(timings['response']),
};

const rawOutput = options.query.method === 'head'
Expand Down Expand Up @@ -154,16 +164,16 @@ export class HttpCommand implements CommandInterface<HttpOptions> {
result.timings = {
firstByte: resp.timings.phases.firstByte,
dns: resp.timings.phases.dns,
response: Number(resp.timings?.response) - Number(resp.timings?.start),
connect: Number(resp.timings?.connect) - Number(resp.timings?.start),
tls: resp.timings.phases.tls,
tcp: resp.timings.phases.tcp,
};

const socket = resp.socket;
if (isTlsSocket(socket)) {
const cert = socket.getPeerCertificate();
const rSocket = resp.socket;
if (isTlsSocket(rSocket)) {
const cert = rSocket.getPeerCertificate();
result.tls = {
authorized: socket.authorized,
...(socket.authorizationError ? {error: socket.authorizationError} : {}),
authorized: rSocket.authorized,
...(rSocket.authorizationError ? {error: rSocket.authorizationError} : {}),
createdAt: cert.valid_from,
expireAt: cert.valid_to,
issuer: {...cert.issuer},
Expand All @@ -175,8 +185,12 @@ export class HttpCommand implements CommandInterface<HttpOptions> {
}
});

stream.on('error', (error: Error) => {
result.error = error.message;
stream.on('error', (error: Error & {code: string}) => {
// Skip error mapping on download limit
if (error.message !== 'Exceeded the download.') {
result.error = `${error.message} - ${error.code}`;
}

respond();
});

Expand Down
45 changes: 27 additions & 18 deletions test/unit/cmd/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as sinon from 'sinon';
import {expect} from 'chai';
import {Socket} from 'socket.io-client';
import {HttpCommand} from '../../../src/command/http-command.js';
import type {Timings} from '../../../src/command/http-command.js';

type StreamCert = {
valid_from: number;
Expand All @@ -20,10 +21,7 @@ type StreamCert = {
};

type StreamResponse = {
timings: {
[k: string]: number | Record<string, number>;
phases?: Record<string, number>;
};
timings: Timings;
socket: {
authorized?: boolean;
authorizationError?: string;
Expand All @@ -32,15 +30,26 @@ type StreamResponse = {
};
};

class HttpError extends Error {
code: string;

constructor(message: string, code: string) {
super(message);
this.code = code;
}
}

class Stream {
response: StreamResponse | undefined;
timings: Timings | undefined;
stream: PassThrough;

constructor(
response: StreamResponse,
) {
this.stream = new PassThrough();
this.response = response;
this.timings = response?.timings;
}

on(key: string, fn: (..._args: any[]) => void) {
Expand Down Expand Up @@ -78,9 +87,9 @@ describe('http command', () => {
httpVersion: '1.1',
timings: {
start: 0,
connect: 1,
response: 11,
phases: {
tls: 2,
tcp: 2,
dns: 5,
download: 10,
total: 11,
Expand All @@ -105,8 +114,8 @@ describe('http command', () => {
},
timings: {
dns: 5,
connect: 1,
response: 11,
tls: 2,
tcp: 2,
download: 10,
firstByte: 1,
total: 11,
Expand Down Expand Up @@ -157,12 +166,12 @@ describe('http command', () => {
statusCode: 200,
timings: {
start: 0,
response: 200,
connect: 100,
phases: {
download: 10,
total: 11,
dns: 5,
tls: 5,
tcp: 2,
firstByte: 1,
},
},
Expand All @@ -183,10 +192,10 @@ describe('http command', () => {
test: 'abc',
},
timings: {
connect: 100,
response: 200,
dns: 5,
total: 11,
tls: 5,
tcp: 2,
firstByte: 1,
download: 10,
},
Expand Down Expand Up @@ -246,11 +255,11 @@ describe('http command', () => {
statusCode: 200,
timings: {
start: 0,
response: 200,
connect: 100,
phases: {
download: 10,
total: 11,
tls: 2,
tcp: 2,
dns: 5,
firstByte: 1,
},
Expand All @@ -272,8 +281,8 @@ describe('http command', () => {
test: 'abc',
},
timings: {
connect: 100,
response: 200,
tls: 2,
tcp: 2,
total: 11,
dns: 5,
firstByte: 1,
Expand Down Expand Up @@ -333,7 +342,7 @@ describe('http command', () => {
},
},
},
error: new Error('ENODATA google.com'),
error: new HttpError('ENODATA google.com', 'abc'),
};

const response = {
Expand All @@ -351,7 +360,7 @@ describe('http command', () => {
total: 11,
},
tls: {},
rawOutput: 'ENODATA google.com',
rawOutput: 'ENODATA google.com - abc',
statusCode: 0,
},
testId: 'test',
Expand Down

0 comments on commit cedde96

Please sign in to comment.