encapsulate details in happy-eyeballs
This commit is contained in:
parent
c29a5562e0
commit
5d4f149c64
|
|
@ -30,7 +30,7 @@ import { HttpsProxyAgent, SocksProxyAgent } from '../utilsBundle';
|
||||||
import { BrowserContext, verifyClientCertificates } from './browserContext';
|
import { BrowserContext, verifyClientCertificates } from './browserContext';
|
||||||
import { CookieStore, domainMatches } from './cookieStore';
|
import { CookieStore, domainMatches } from './cookieStore';
|
||||||
import { MultipartFormData } from './formData';
|
import { MultipartFormData } from './formData';
|
||||||
import { httpHappyEyeballsAgent, httpsHappyEyeballsAgent } from '../utils/happy-eyeballs';
|
import { httpHappyEyeballsAgent, httpsHappyEyeballsAgent, timingForSocket } from '../utils/happy-eyeballs';
|
||||||
import type { CallMetadata } from './instrumentation';
|
import type { CallMetadata } from './instrumentation';
|
||||||
import { SdkObject } from './instrumentation';
|
import { SdkObject } from './instrumentation';
|
||||||
import type { Playwright } from './playwright';
|
import type { Playwright } from './playwright';
|
||||||
|
|
@ -488,8 +488,9 @@ 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
|
||||||
dnsLookupAt = (socket as any).dnsLookupAt;
|
const happyEyeBallsTimings = timingForSocket(socket);
|
||||||
tcpConnectionAt = (socket as any).tcpConnectionAt;
|
dnsLookupAt = happyEyeBallsTimings.dnsLookupAt;
|
||||||
|
tcpConnectionAt = happyEyeBallsTimings.tcpConnectionAt;
|
||||||
|
|
||||||
// non-happy-eyeballs sockets
|
// non-happy-eyeballs sockets
|
||||||
socket.on('lookup', () => { dnsLookupAt = monotonicTime(); });
|
socket.on('lookup', () => { dnsLookupAt = monotonicTime(); });
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,9 @@ import { monotonicTime } from './time';
|
||||||
// Same as in Chromium (https://source.chromium.org/chromium/chromium/src/+/5666ff4f5077a7e2f72902f3a95f5d553ea0d88d:net/socket/transport_connect_job.cc;l=102)
|
// Same as in Chromium (https://source.chromium.org/chromium/chromium/src/+/5666ff4f5077a7e2f72902f3a95f5d553ea0d88d:net/socket/transport_connect_job.cc;l=102)
|
||||||
const connectionAttemptDelayMs = 300;
|
const connectionAttemptDelayMs = 300;
|
||||||
|
|
||||||
|
const kDNSLookupAt = Symbol('kDNSLookupAt')
|
||||||
|
const kTCPConnectionAt = Symbol('kTCPConnectionAt')
|
||||||
|
|
||||||
class HttpHappyEyeballsAgent extends http.Agent {
|
class HttpHappyEyeballsAgent extends http.Agent {
|
||||||
createConnection(options: http.ClientRequestArgs, oncreate?: (err: Error | null, socket?: net.Socket) => void): net.Socket | undefined {
|
createConnection(options: http.ClientRequestArgs, oncreate?: (err: Error | null, socket?: net.Socket) => void): net.Socket | undefined {
|
||||||
// There is no ambiguity in case of IP address.
|
// There is no ambiguity in case of IP address.
|
||||||
|
|
@ -134,12 +137,12 @@ export async function createConnectionAsync(
|
||||||
port: options.port as number,
|
port: options.port as number,
|
||||||
host: address });
|
host: address });
|
||||||
|
|
||||||
(socket as any).dnsLookupAt = dnsLookupAt;
|
(socket as any)[kDNSLookupAt] = dnsLookupAt;
|
||||||
|
|
||||||
// Each socket may fire only one of 'connect', 'timeout' or 'error' events.
|
// Each socket may fire only one of 'connect', 'timeout' or 'error' events.
|
||||||
// None of these events are fired after socket.destroy() is called.
|
// None of these events are fired after socket.destroy() is called.
|
||||||
socket.on('connect', () => {
|
socket.on('connect', () => {
|
||||||
(socket as any).tcpConnectionAt = monotonicTime();
|
(socket as any)[kTCPConnectionAt] = monotonicTime();
|
||||||
|
|
||||||
connected.resolve();
|
connected.resolve();
|
||||||
oncreate?.(null, socket);
|
oncreate?.(null, socket);
|
||||||
|
|
@ -195,3 +198,9 @@ function clientRequestArgsToHostName(options: http.ClientRequestArgs): string {
|
||||||
throw new Error('Either options.hostname or options.host must be provided');
|
throw new Error('Either options.hostname or options.host must be provided');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function timingForSocket(socket: net.Socket | tls.TLSSocket) {
|
||||||
|
return {
|
||||||
|
dnsLookupAt: (socket as any)[kDNSLookupAt] as number | undefined,
|
||||||
|
tcpConnectionAt: (socket as any)[kTCPConnectionAt] as number | undefined,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue