| 1 |
import {EventEmitter} from 'events'; |
| 2 |
import {Socket} from 'net'; |
| 3 |
import {ClientRequest, IncomingMessage} from 'http'; |
| 4 |
import deferToConnect from 'defer-to-connect'; |
| 5 |
import {types} from 'util'; |
| 6 |
|
| 7 |
export interface 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 interface ClientRequestWithTimings extends ClientRequest { |
| 31 |
timings?: Timings; |
| 32 |
} |
| 33 |
|
| 34 |
export interface IncomingMessageWithTimings extends IncomingMessage { |
| 35 |
timings?: Timings; |
| 36 |
} |
| 37 |
|
| 38 |
const nodejsMajorVersion = Number(process.versions.node.split('.')[0]); |
| 39 |
|
| 40 |
const timer = (request: ClientRequestWithTimings): Timings => { |
| 41 |
if (request.timings) { |
| 42 |
return request.timings; |
| 43 |
} |
| 44 |
|
| 45 |
const timings: Timings = { |
| 46 |
start: Date.now(), |
| 47 |
socket: undefined, |
| 48 |
lookup: undefined, |
| 49 |
connect: undefined, |
| 50 |
secureConnect: undefined, |
| 51 |
upload: undefined, |
| 52 |
response: undefined, |
| 53 |
end: undefined, |
| 54 |
error: undefined, |
| 55 |
abort: undefined, |
| 56 |
phases: { |
| 57 |
wait: undefined, |
| 58 |
dns: undefined, |
| 59 |
tcp: undefined, |
| 60 |
tls: undefined, |
| 61 |
request: undefined, |
| 62 |
firstByte: undefined, |
| 63 |
download: undefined, |
| 64 |
total: undefined |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
request.timings = timings; |
| 69 |
|
| 70 |
const handleError = (origin: EventEmitter): void => { |
| 71 |
const emit = origin.emit.bind(origin); |
| 72 |
origin.emit = (event, ...args: unknown[]) => { |
| 73 |
// Catches the `error` event |
| 74 |
if (event === 'error') { |
| 75 |
timings.error = Date.now(); |
| 76 |
timings.phases.total = timings.error - timings.start; |
| 77 |
|
| 78 |
origin.emit = emit; |
| 79 |
} |
| 80 |
|
| 81 |
// Saves the original behavior |
| 82 |
return emit(event, ...args); |
| 83 |
}; |
| 84 |
}; |
| 85 |
|
| 86 |
handleError(request); |
| 87 |
|
| 88 |
const onAbort = (): void => { |
| 89 |
timings.abort = Date.now(); |
| 90 |
|
| 91 |
// Let the `end` response event be responsible for setting the total phase, |
| 92 |
// unless the Node.js major version is >= 13. |
| 93 |
if (!timings.response || nodejsMajorVersion >= 13) { |
| 94 |
timings.phases.total = Date.now() - timings.start; |
| 95 |
} |
| 96 |
}; |
| 97 |
|
| 98 |
request.prependOnceListener('abort', onAbort); |
| 99 |
|
| 100 |
const onSocket = (socket: Socket): void => { |
| 101 |
timings.socket = Date.now(); |
| 102 |
timings.phases.wait = timings.socket - timings.start; |
| 103 |
|
| 104 |
if (types.isProxy(socket)) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
const lookupListener = (): void => { |
| 109 |
timings.lookup = Date.now(); |
| 110 |
timings.phases.dns = timings.lookup - timings.socket!; |
| 111 |
}; |
| 112 |
|
| 113 |
socket.prependOnceListener('lookup', lookupListener); |
| 114 |
|
| 115 |
deferToConnect(socket, { |
| 116 |
connect: () => { |
| 117 |
timings.connect = Date.now(); |
| 118 |
|
| 119 |
if (timings.lookup === undefined) { |
| 120 |
socket.removeListener('lookup', lookupListener); |
| 121 |
timings.lookup = timings.connect; |
| 122 |
timings.phases.dns = timings.lookup - timings.socket!; |
| 123 |
} |
| 124 |
|
| 125 |
timings.phases.tcp = timings.connect - timings.lookup; |
| 126 |
|
| 127 |
// This callback is called before flushing any data, |
| 128 |
// so we don't need to set `timings.phases.request` here. |
| 129 |
}, |
| 130 |
secureConnect: () => { |
| 131 |
timings.secureConnect = Date.now(); |
| 132 |
timings.phases.tls = timings.secureConnect - timings.connect!; |
| 133 |
} |
| 134 |
}); |
| 135 |
}; |
| 136 |
|
| 137 |
if (request.socket) { |
| 138 |
onSocket(request.socket); |
| 139 |
} else { |
| 140 |
request.prependOnceListener('socket', onSocket); |
| 141 |
} |
| 142 |
|
| 143 |
const onUpload = (): void => { |
| 144 |
timings.upload = Date.now(); |
| 145 |
timings.phases.request = timings.upload - (timings.secureConnect ?? timings.connect!); |
| 146 |
}; |
| 147 |
|
| 148 |
const writableFinished = (): boolean => { |
| 149 |
if (typeof request.writableFinished === 'boolean') { |
| 150 |
return request.writableFinished; |
| 151 |
} |
| 152 |
|
| 153 |
// Node.js doesn't have `request.writableFinished` property |
| 154 |
return request.finished && (request as any).outputSize === 0 && (!request.socket || request.socket.writableLength === 0); |
| 155 |
}; |
| 156 |
|
| 157 |
if (writableFinished()) { |
| 158 |
onUpload(); |
| 159 |
} else { |
| 160 |
request.prependOnceListener('finish', onUpload); |
| 161 |
} |
| 162 |
|
| 163 |
request.prependOnceListener('response', (response: IncomingMessageWithTimings): void => { |
| 164 |
timings.response = Date.now(); |
| 165 |
timings.phases.firstByte = timings.response - timings.upload!; |
| 166 |
|
| 167 |
response.timings = timings; |
| 168 |
|
| 169 |
handleError(response); |
| 170 |
|
| 171 |
response.prependOnceListener('end', () => { |
| 172 |
timings.end = Date.now(); |
| 173 |
timings.phases.download = timings.end - timings.response!; |
| 174 |
timings.phases.total = timings.end - timings.start; |
| 175 |
}); |
| 176 |
|
| 177 |
response.prependOnceListener('aborted', onAbort); |
| 178 |
}); |
| 179 |
|
| 180 |
return timings; |
| 181 |
}; |
| 182 |
|
| 183 |
export default timer; |
| 184 |
|
| 185 |
// For CommonJS default export support |
| 186 |
module.exports = timer; |
| 187 |
module.exports.default = timer; |