|
| 1 | +import {errorMonitor} from 'node:events'; |
| 2 | +import {types} from 'node:util'; |
| 3 | +import type {ClientRequest, IncomingMessage} from 'node:http'; |
| 4 | +import type {Socket} from 'node:net'; |
| 5 | +import deferToConnect from './defer-to-connect.js'; |
| 6 | + |
| 7 | +export type Timings = { |
| 8 | + start: number; |
| 9 | + socket?: number; |
| 10 | + lookup?: number; |
| 11 | + connect?: number; |
| 12 | + secureConnect?: number; |
| 13 | + upload?: number; |
| 14 | + response?: number; |
| 15 | + end?: number; |
| 16 | + error?: number; |
| 17 | + abort?: number; |
| 18 | + phases: { |
| 19 | + wait?: number; |
| 20 | + dns?: number; |
| 21 | + tcp?: number; |
| 22 | + tls?: number; |
| 23 | + request?: number; |
| 24 | + firstByte?: number; |
| 25 | + download?: number; |
| 26 | + total?: number; |
| 27 | + }; |
| 28 | +}; |
| 29 | + |
| 30 | +export type ClientRequestWithTimings = ClientRequest & { |
| 31 | + timings?: Timings; |
| 32 | +}; |
| 33 | + |
| 34 | +export type IncomingMessageWithTimings = IncomingMessage & { |
| 35 | + timings?: Timings; |
| 36 | +}; |
| 37 | + |
| 38 | +type SocketWithConnectionTimings = Socket & { |
| 39 | + __initial_connection_timings__?: { |
| 40 | + dnsPhase: number; |
| 41 | + tcpPhase: number; |
| 42 | + tlsPhase?: number; |
| 43 | + }; |
| 44 | +}; |
| 45 | + |
| 46 | +const timer = (request: ClientRequestWithTimings): Timings => { |
| 47 | + if (request.timings) { |
| 48 | + return request.timings; |
| 49 | + } |
| 50 | + |
| 51 | + const timings: Timings = { |
| 52 | + start: Date.now(), |
| 53 | + socket: undefined, |
| 54 | + lookup: undefined, |
| 55 | + connect: undefined, |
| 56 | + secureConnect: undefined, |
| 57 | + upload: undefined, |
| 58 | + response: undefined, |
| 59 | + end: undefined, |
| 60 | + error: undefined, |
| 61 | + abort: undefined, |
| 62 | + phases: { |
| 63 | + wait: undefined, |
| 64 | + dns: undefined, |
| 65 | + tcp: undefined, |
| 66 | + tls: undefined, |
| 67 | + request: undefined, |
| 68 | + firstByte: undefined, |
| 69 | + download: undefined, |
| 70 | + total: undefined, |
| 71 | + }, |
| 72 | + }; |
| 73 | + |
| 74 | + request.timings = timings; |
| 75 | + |
| 76 | + const handleError = (origin: ClientRequest | IncomingMessage) => { |
| 77 | + origin.once(errorMonitor, () => { |
| 78 | + timings.error = Date.now(); |
| 79 | + timings.phases.total = timings.error - timings.start; |
| 80 | + }); |
| 81 | + }; |
| 82 | + |
| 83 | + handleError(request); |
| 84 | + |
| 85 | + const onAbort = () => { |
| 86 | + timings.abort = Date.now(); |
| 87 | + timings.phases.total = timings.abort - timings.start; |
| 88 | + }; |
| 89 | + |
| 90 | + request.prependOnceListener('abort', onAbort); |
| 91 | + |
| 92 | + const onSocket = (socket: SocketWithConnectionTimings) => { |
| 93 | + timings.socket = Date.now(); |
| 94 | + timings.phases.wait = timings.socket - timings.start; |
| 95 | + |
| 96 | + if (types.isProxy(socket)) { |
| 97 | + // HTTP/2: The socket is a proxy, so connection events won't fire. |
| 98 | + // We can't measure connection timings, so leave them undefined. |
| 99 | + // This prevents NaN in phases.request calculation. |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + // Check if socket is already connected (reused from connection pool) |
| 104 | + const socketAlreadyConnected = socket.writable && !socket.connecting; |
| 105 | + |
| 106 | + if (socketAlreadyConnected) { |
| 107 | + // Socket reuse detected: the socket was already connected from a previous request. |
| 108 | + // For reused sockets, set all connection timestamps to socket time since no new |
| 109 | + // connection was made for THIS request. But preserve phase durations from the |
| 110 | + // original connection so they're not lost. |
| 111 | + timings.lookup = timings.socket; |
| 112 | + timings.connect = timings.socket; |
| 113 | + |
| 114 | + if (socket.__initial_connection_timings__) { |
| 115 | + // Restore the phase timings from the initial connection |
| 116 | + timings.phases.dns = socket.__initial_connection_timings__.dnsPhase; |
| 117 | + timings.phases.tcp = socket.__initial_connection_timings__.tcpPhase; |
| 118 | + timings.phases.tls = socket.__initial_connection_timings__.tlsPhase; |
| 119 | + |
| 120 | + // Set secureConnect timestamp if there was TLS |
| 121 | + if (timings.phases.tls !== undefined) { |
| 122 | + timings.secureConnect = timings.socket; |
| 123 | + } |
| 124 | + } else { |
| 125 | + // Socket reused but no initial timings stored (e.g., from external code) |
| 126 | + // Set phases to 0 |
| 127 | + timings.phases.dns = 0; |
| 128 | + timings.phases.tcp = 0; |
| 129 | + } |
| 130 | + |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + const lookupListener = () => { |
| 135 | + timings.lookup = Date.now(); |
| 136 | + timings.phases.dns = timings.lookup - timings.socket!; |
| 137 | + }; |
| 138 | + |
| 139 | + socket.prependOnceListener('lookup', lookupListener); |
| 140 | + |
| 141 | + deferToConnect(socket, { |
| 142 | + connect() { |
| 143 | + timings.connect = Date.now(); |
| 144 | + if (timings.lookup === undefined) { |
| 145 | + // No DNS lookup occurred (e.g., connecting to an IP address directly) |
| 146 | + // Set lookup to socket time (no time elapsed for DNS) |
| 147 | + socket.removeListener('lookup', lookupListener); |
| 148 | + timings.lookup = timings.socket!; |
| 149 | + timings.phases.dns = 0; |
| 150 | + } |
| 151 | + |
| 152 | + timings.phases.tcp = timings.connect - timings.lookup; |
| 153 | + |
| 154 | + // Store connection phase timings on socket for potential reuse |
| 155 | + if (!socket.__initial_connection_timings__) { |
| 156 | + socket.__initial_connection_timings__ = { |
| 157 | + dnsPhase: timings.phases.dns!, |
| 158 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion -- TypeScript can't prove this is defined due to callback structure |
| 159 | + tcpPhase: timings.phases.tcp!, |
| 160 | + }; |
| 161 | + } |
| 162 | + }, |
| 163 | + secureConnect() { |
| 164 | + timings.secureConnect = Date.now(); |
| 165 | + timings.phases.tls = timings.secureConnect - timings.connect!; |
| 166 | + |
| 167 | + // Update stored timings with TLS phase timing |
| 168 | + if (socket.__initial_connection_timings__) { |
| 169 | + socket.__initial_connection_timings__.tlsPhase = timings.phases.tls; |
| 170 | + } |
| 171 | + }, |
| 172 | + }); |
| 173 | + }; |
| 174 | + |
| 175 | + if (request.socket) { |
| 176 | + onSocket(request.socket as SocketWithConnectionTimings); |
| 177 | + } else { |
| 178 | + request.prependOnceListener('socket', onSocket as (socket: Socket) => void); |
| 179 | + } |
| 180 | + |
| 181 | + const onUpload = () => { |
| 182 | + timings.upload = Date.now(); |
| 183 | + |
| 184 | + // Calculate request phase if we have connection timings |
| 185 | + const secureOrConnect = timings.secureConnect ?? timings.connect; |
| 186 | + if (secureOrConnect !== undefined) { |
| 187 | + timings.phases.request = timings.upload - secureOrConnect; |
| 188 | + } |
| 189 | + // If both are undefined (HTTP/2), phases.request stays undefined (not NaN) |
| 190 | + }; |
| 191 | + |
| 192 | + if (request.writableFinished) { |
| 193 | + onUpload(); |
| 194 | + } else { |
| 195 | + request.prependOnceListener('finish', onUpload); |
| 196 | + } |
| 197 | + |
| 198 | + request.prependOnceListener('response', (response: IncomingMessageWithTimings) => { |
| 199 | + timings.response = Date.now(); |
| 200 | + timings.phases.firstByte = timings.response - timings.upload!; |
| 201 | + |
| 202 | + response.timings = timings; |
| 203 | + handleError(response); |
| 204 | + |
| 205 | + response.prependOnceListener('end', () => { |
| 206 | + request.off('abort', onAbort); |
| 207 | + response.off('aborted', onAbort); |
| 208 | + |
| 209 | + if (timings.phases.total !== undefined) { |
| 210 | + // Aborted or errored |
| 211 | + return; |
| 212 | + } |
| 213 | + |
| 214 | + timings.end = Date.now(); |
| 215 | + timings.phases.download = timings.end - timings.response!; |
| 216 | + timings.phases.total = timings.end - timings.start; |
| 217 | + }); |
| 218 | + |
| 219 | + response.prependOnceListener('aborted', onAbort); |
| 220 | + }); |
| 221 | + |
| 222 | + return timings; |
| 223 | +}; |
| 224 | + |
| 225 | +export default timer; |
0 commit comments