refactor
This commit is contained in:
parent
1d061f8fcb
commit
e21822891d
|
|
@ -311,29 +311,26 @@ export abstract class APIRequestContext extends SdkObject {
|
||||||
const requestOptions = { ...options, agent };
|
const requestOptions = { ...options, agent };
|
||||||
|
|
||||||
const startAt = monotonicTime();
|
const startAt = monotonicTime();
|
||||||
const timings: Record<'startAt' | 'requestFinishAt' | 'dnsLookupAt' | 'tcpConnectionAt' | 'tlsHandshakeAt' | 'firstByteAt' | 'endAt', number | undefined> = {
|
let dnsLookupAt: number | undefined;
|
||||||
startAt,
|
let tcpConnectionAt: number | undefined;
|
||||||
dnsLookupAt: undefined,
|
let tlsHandshakeAt: number | undefined;
|
||||||
tcpConnectionAt: undefined,
|
let requestFinishAt: number | undefined;
|
||||||
tlsHandshakeAt: undefined,
|
let firstByteAt: number | undefined;
|
||||||
requestFinishAt: undefined,
|
let endAt: number | undefined;
|
||||||
firstByteAt: undefined,
|
|
||||||
endAt: undefined
|
|
||||||
};
|
|
||||||
|
|
||||||
const request = requestConstructor(url, requestOptions as any, async response => {
|
const request = requestConstructor(url, requestOptions as any, async response => {
|
||||||
response.once('readable', () => { timings.firstByteAt = monotonicTime(); });
|
response.once('readable', () => { firstByteAt = monotonicTime(); });
|
||||||
response.once('end', () => { timings.endAt = monotonicTime(); });
|
response.once('end', () => { endAt = monotonicTime(); });
|
||||||
|
|
||||||
const notifyRequestFinished = (body?: Buffer) => {
|
const notifyRequestFinished = (body?: Buffer) => {
|
||||||
const harTimings: har.Timings = {
|
const timings: har.Timings = {
|
||||||
send: timings.requestFinishAt! - startAt,
|
send: requestFinishAt! - startAt,
|
||||||
wait: timings.firstByteAt! - timings.requestFinishAt!,
|
wait: firstByteAt! - requestFinishAt!,
|
||||||
receive: timings.endAt! - timings.firstByteAt!,
|
receive: endAt! - firstByteAt!,
|
||||||
dns: timings.dnsLookupAt ? timings.dnsLookupAt - startAt : -1,
|
dns: dnsLookupAt ? dnsLookupAt - startAt : -1,
|
||||||
connect: (timings.tlsHandshakeAt ?? timings.tcpConnectionAt!) - startAt,
|
connect: (tlsHandshakeAt ?? tcpConnectionAt!) - startAt,
|
||||||
ssl: timings.tlsHandshakeAt ? timings.tlsHandshakeAt - timings.tcpConnectionAt! : -1,
|
ssl: tlsHandshakeAt ? tlsHandshakeAt - tcpConnectionAt! : -1,
|
||||||
blocked: -1, // TODO: time spent in queue waiting for a network connection
|
blocked: -1,
|
||||||
};
|
};
|
||||||
|
|
||||||
const requestFinishedEvent: APIRequestFinishedEvent = {
|
const requestFinishedEvent: APIRequestFinishedEvent = {
|
||||||
|
|
@ -345,7 +342,7 @@ export abstract class APIRequestContext extends SdkObject {
|
||||||
rawHeaders: response.rawHeaders,
|
rawHeaders: response.rawHeaders,
|
||||||
cookies,
|
cookies,
|
||||||
body,
|
body,
|
||||||
timings: harTimings,
|
timings,
|
||||||
};
|
};
|
||||||
this.emit(APIRequestContext.Events.RequestFinished, requestFinishedEvent);
|
this.emit(APIRequestContext.Events.RequestFinished, requestFinishedEvent);
|
||||||
};
|
};
|
||||||
|
|
@ -493,15 +490,15 @@ export abstract class APIRequestContext extends SdkObject {
|
||||||
|
|
||||||
request.on('socket', socket => {
|
request.on('socket', socket => {
|
||||||
// happy eyeballs don't emit lookup and connect events, so we use our custom ones
|
// happy eyeballs don't emit lookup and connect events, so we use our custom ones
|
||||||
timings.dnsLookupAt = (socket as any).dnsLookupAt;
|
dnsLookupAt = (socket as any).dnsLookupAt;
|
||||||
timings.tcpConnectionAt = (socket as any).tcpConnectionAt;
|
tcpConnectionAt = (socket as any).tcpConnectionAt;
|
||||||
|
|
||||||
// standard case
|
// standard case
|
||||||
socket.on('lookup', () => { timings.dnsLookupAt = monotonicTime(); });
|
socket.on('lookup', () => { dnsLookupAt = monotonicTime(); });
|
||||||
socket.on('connect', () => { timings.tcpConnectionAt = monotonicTime(); });
|
socket.on('connect', () => { tcpConnectionAt = monotonicTime(); });
|
||||||
socket.on('secureConnect', () => { timings.tlsHandshakeAt = monotonicTime(); });
|
socket.on('secureConnect', () => { tlsHandshakeAt = monotonicTime(); });
|
||||||
});
|
});
|
||||||
request.on('finish', () => { timings.requestFinishAt = monotonicTime(); });
|
request.on('finish', () => { requestFinishAt = monotonicTime(); });
|
||||||
|
|
||||||
progress.log(`→ ${options.method} ${url.toString()}`);
|
progress.log(`→ ${options.method} ${url.toString()}`);
|
||||||
if (options.headers) {
|
if (options.headers) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue