Skip to content

Commit

Permalink
📦 NEW: Support options.timing = true
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 5, 2022
1 parent 70a75c6 commit f07c9f8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export class HttpClient extends EventEmitter {
keepAliveSocket: true,
requestUrls: [],
timing: {
waiting: 0,
contentDownload: 0,
},
};
Expand Down Expand Up @@ -309,6 +310,10 @@ export class HttpClient extends EventEmitter {

requestOptions.headers = headers;
const response = await undiciRequest(requestUrl, requestOptions);
if (args.timing) {
res.timing.waiting = performanceTime(requestStartTime);
}

const context = response.context as { history: URL[] };
let lastUrl = '';
if (context?.history) {
Expand Down Expand Up @@ -378,7 +383,10 @@ export class HttpClient extends EventEmitter {
}
}
}
res.rt = res.timing.contentDownload = performanceTime(requestStartTime);
res.rt = performanceTime(requestStartTime);
if (args.timing) {
res.timing.contentDownload = res.rt;
}

const clientResponse: HttpClientResponse = {
data,
Expand Down Expand Up @@ -417,7 +425,10 @@ export class HttpClient extends EventEmitter {
if (res.requestUrls.length === 0) {
res.requestUrls.push(requestUrl.href);
}
res.rt = res.timing.contentDownload = performanceTime(requestStartTime);
res.rt = performanceTime(requestStartTime);
if (args.timing) {
res.timing.contentDownload = res.rt;
}
throw err;
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ export type RequestOptions = {
* Alias to compressed
* */
gzip?: boolean;
/** Enable timing or not, default is false. */
/**
* @deprecated
* Enable timing or not, default is false.
* */
timing?: boolean;
/**
* @deprecated
* Not support on HTTPS
* Custom DNS lookup function, default is dns.lookup.
* Require node >= 4.0.0(for http protocol) and node >=8(for https protocol)
*/
Expand Down
1 change: 1 addition & 0 deletions src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type HttpClientResponseMeta = {
*/
timing: {
contentDownload: number;
waiting: number;
};
// remoteAddress: remoteAddress,
// remotePort: remotePort,
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ export { HttpClient } from './HttpClient';
export default {
request,
};

54 changes: 54 additions & 0 deletions test/options.timing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { strict as assert } from 'assert';
import urllib from '../src';
import { HttpClientResponseMeta } from '../src/Response';
import { startServer } from './fixtures/server';

describe('options.timing.test.ts', () => {
let close: any;
let _url: string;
beforeAll(async () => {
const { closeServer, url } = await startServer();
close = closeServer;
_url = url;
});

afterAll(async () => {
await close();
});

it('should timing = true work', async () => {
const response = await urllib.request(`${_url}?content-encoding=gzip`, {
dataType: 'json',
timing: true,
});
assert.equal(response.status, 200);
const res = response.res as HttpClientResponseMeta;
assert(res.timing.waiting > 0);
assert(res.timing.contentDownload > 0);
assert(res.timing.contentDownload > res.timing.waiting);
assert.equal(res.timing.contentDownload, res.rt);
});

it('should timing = false work', async () => {
const response = await urllib.request(`${_url}?content-encoding=gzip`, {
dataType: 'json',
timing: false,
});
assert.equal(response.status, 200);
const res = response.res as HttpClientResponseMeta;
assert.equal(res.timing.waiting, 0);
assert.equal(res.timing.contentDownload, 0);
assert(res.rt > 0);
});

it('should timing default to false', async () => {
const response = await urllib.request(`${_url}?content-encoding=gzip`, {
dataType: 'json',
});
assert.equal(response.status, 200);
const res = response.res as HttpClientResponseMeta;
assert.equal(res.timing.waiting, 0);
assert.equal(res.timing.contentDownload, 0);
assert(res.rt > 0);
});
});

0 comments on commit f07c9f8

Please sign in to comment.