| 1 |
import {EventEmitter} from 'events'; |
| 2 |
import http = require('http'); |
| 3 |
import {ClientRequest, IncomingMessage} from 'http'; |
| 4 |
import https = require('https'); |
| 5 |
import {AddressInfo} from 'net'; |
| 6 |
import util = require('util'); |
| 7 |
import pEvent from 'p-event'; |
| 8 |
import test from 'ava'; |
| 9 |
import timer, {Timings, ClientRequestWithTimings, IncomingMessageWithTimings} from '../source'; |
| 10 |
import http2 = require('http2-wrapper'); |
| 11 |
|
| 12 |
let server: http.Server & { |
| 13 |
url?: string; |
| 14 |
listenAsync?: any; |
| 15 |
closeAsync?: any; |
| 16 |
}; |
| 17 |
|
| 18 |
test.before('setup', async () => { |
| 19 |
server = http.createServer((_request, response) => { |
| 20 |
response.write('o'); |
| 21 |
|
| 22 |
setTimeout(() => { |
| 23 |
response.end('k'); |
| 24 |
}, 200); |
| 25 |
}); |
| 26 |
|
| 27 |
server.listenAsync = util.promisify(server.listen.bind(server)); |
| 28 |
server.closeAsync = util.promisify(server.close.bind(server)); |
| 29 |
|
| 30 |
await server.listenAsync(); |
| 31 |
server.url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`; |
| 32 |
}); |
| 33 |
|
| 34 |
test.after('cleanup', async () => { |
| 35 |
await server.closeAsync(); |
| 36 |
}); |
| 37 |
|
| 38 |
const nodejsMajorVersion = Number(process.versions.node.split('.')[0]); |
| 39 |
|
| 40 |
const error = 'Simple error'; |
| 41 |
|
| 42 |
const makeRequest = (url = 'https://httpbin.org/anything', options: http.RequestOptions = {}): {request: ClientRequest; timings: Timings} => { |
| 43 |
const {protocol} = new URL(url); |
| 44 |
const fn = protocol === 'http:' ? http : https; |
| 45 |
|
| 46 |
const request = fn.get(url, {agent: false, ...options}); |
| 47 |
const timings = timer(request); |
| 48 |
|
| 49 |
return {request, timings}; |
| 50 |
}; |
| 51 |
|
| 52 |
test('by default everything is set to undefined', t => { |
| 53 |
const {timings} = makeRequest(); |
| 54 |
|
| 55 |
t.is(typeof timings, 'object'); |
| 56 |
t.is(typeof timings.start, 'number'); |
| 57 |
t.is(timings.socket, undefined); |
| 58 |
t.is(timings.lookup, undefined); |
| 59 |
t.is(timings.connect, undefined); |
| 60 |
t.is(timings.secureConnect, undefined); |
| 61 |
t.is(timings.response, undefined); |
| 62 |
t.is(timings.end, undefined); |
| 63 |
t.is(timings.error, undefined); |
| 64 |
t.is(timings.abort, undefined); |
| 65 |
|
| 66 |
t.deepEqual(timings.phases, { |
| 67 |
wait: undefined, |
| 68 |
dns: undefined, |
| 69 |
tcp: undefined, |
| 70 |
tls: undefined, |
| 71 |
request: undefined, |
| 72 |
firstByte: undefined, |
| 73 |
download: undefined, |
| 74 |
total: undefined |
| 75 |
}); |
| 76 |
}); |
| 77 |
|
| 78 |
test('timings (socket reuse)', async t => { |
| 79 |
const agent = new http.Agent({keepAlive: true}); |
| 80 |
|
| 81 |
{ |
| 82 |
const {request} = makeRequest(server.url, {agent}); |
| 83 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 84 |
response.resume(); |
| 85 |
await pEvent(response, 'end'); |
| 86 |
} |
| 87 |
|
| 88 |
{ |
| 89 |
const {request} = makeRequest(server.url, {agent}); |
| 90 |
await pEvent(request, 'socket'); |
| 91 |
const timings = timer(request); |
| 92 |
|
| 93 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 94 |
response.resume(); |
| 95 |
await pEvent(response, 'end'); |
| 96 |
|
| 97 |
t.true(timings.phases.wait! <= 1); |
| 98 |
t.true(timings.phases.dns! <= 1); |
| 99 |
t.true(timings.phases.tcp! <= 1); |
| 100 |
t.true(timings.phases.request! <= 1); |
| 101 |
} |
| 102 |
|
| 103 |
agent.destroy(); |
| 104 |
|
| 105 |
t.pass(); |
| 106 |
}); |
| 107 |
|
| 108 |
test('timings', async t => { |
| 109 |
const {request, timings} = makeRequest(); |
| 110 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 111 |
response.resume(); |
| 112 |
await pEvent(response, 'end'); |
| 113 |
|
| 114 |
t.is(typeof timings, 'object'); |
| 115 |
t.is(typeof timings.start, 'number'); |
| 116 |
t.is(typeof timings.socket, 'number'); |
| 117 |
t.is(typeof timings.lookup, 'number'); |
| 118 |
t.is(typeof timings.connect, 'number'); |
| 119 |
t.is(typeof timings.secureConnect, 'number'); |
| 120 |
t.is(typeof timings.upload, 'number'); |
| 121 |
t.is(typeof timings.response, 'number'); |
| 122 |
t.is(typeof timings.end, 'number'); |
| 123 |
}); |
| 124 |
|
| 125 |
test('phases', async t => { |
| 126 |
const {request, timings} = makeRequest(); |
| 127 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 128 |
response.resume(); |
| 129 |
await pEvent(response, 'end'); |
| 130 |
|
| 131 |
t.is(typeof timings.phases, 'object'); |
| 132 |
t.is(typeof timings.phases.wait, 'number'); |
| 133 |
t.is(typeof timings.phases.dns, 'number'); |
| 134 |
t.is(typeof timings.phases.tcp, 'number'); |
| 135 |
t.is(typeof timings.phases.tls, 'number'); |
| 136 |
t.is(typeof timings.phases.firstByte, 'number'); |
| 137 |
t.is(typeof timings.phases.download, 'number'); |
| 138 |
t.is(typeof timings.phases.total, 'number'); |
| 139 |
|
| 140 |
t.is(timings.phases.wait, timings.socket! - timings.start); |
| 141 |
t.is(timings.phases.dns, timings.lookup! - timings.socket!); |
| 142 |
t.is(timings.phases.tcp, timings.connect! - timings.lookup!); |
| 143 |
t.is(timings.phases.tls, timings.secureConnect! - timings.connect!); |
| 144 |
t.is(timings.phases.request, timings.upload! - timings.secureConnect!); |
| 145 |
t.is(timings.phases.firstByte, timings.response! - timings.upload!); |
| 146 |
t.is(timings.phases.download, timings.end! - timings.response!); |
| 147 |
t.is(timings.phases.total, timings.end! - timings.start); |
| 148 |
}); |
| 149 |
|
| 150 |
test('no memory leak (`lookup` event)', async t => { |
| 151 |
const {request} = makeRequest(); |
| 152 |
|
| 153 |
await pEvent(request, 'finish'); |
| 154 |
|
| 155 |
t.is(request.socket!.listenerCount('lookup'), 0); |
| 156 |
}); |
| 157 |
|
| 158 |
test('sets `total` on request error', async t => { |
| 159 |
const request = http.get(server.url!, { |
| 160 |
timeout: 1 |
| 161 |
}); |
| 162 |
request.on('timeout', () => { |
| 163 |
request.abort(); |
| 164 |
}); |
| 165 |
|
| 166 |
const timings = timer(request); |
| 167 |
|
| 168 |
const error: Error = await pEvent(request, 'error'); |
| 169 |
t.is(error.message, 'socket hang up'); |
| 170 |
|
| 171 |
t.is(typeof timings.error, 'number'); |
| 172 |
t.is(timings.phases.total, timings.error! - timings.start); |
| 173 |
}); |
| 174 |
|
| 175 |
test('sets `total` on response error', async t => { |
| 176 |
const request = http.get(server.url!, (response: IncomingMessage) => { |
| 177 |
setImmediate(() => { |
| 178 |
response.emit('error', new Error(error)); |
| 179 |
}); |
| 180 |
}); |
| 181 |
const timings = timer(request); |
| 182 |
|
| 183 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 184 |
const error_: Error = await pEvent(response, 'error'); |
| 185 |
|
| 186 |
t.is(error_.message, error); |
| 187 |
t.is(typeof timings.error, 'number'); |
| 188 |
t.is(timings.phases.total, timings.error! - timings.start); |
| 189 |
}); |
| 190 |
|
| 191 |
test.cb('sets `total` on abort', t => { |
| 192 |
const request = http.get(server.url!); |
| 193 |
request.abort(); |
| 194 |
|
| 195 |
const timings = timer(request); |
| 196 |
|
| 197 |
process.nextTick(() => { |
| 198 |
t.is(typeof timings.abort, 'number'); |
| 199 |
t.is(timings.phases.total, timings.abort! - timings.start); |
| 200 |
t.falsy((request as any).res); |
| 201 |
|
| 202 |
t.end(); |
| 203 |
}); |
| 204 |
}); |
| 205 |
|
| 206 |
test.cb('sets `total` on abort - after `response` event', t => { |
| 207 |
const request = http.get(server.url!); |
| 208 |
const timings = timer(request); |
| 209 |
|
| 210 |
request.once('response', response => { |
| 211 |
request.abort(); |
| 212 |
|
| 213 |
if (nodejsMajorVersion >= 13) { |
| 214 |
process.nextTick(() => { |
| 215 |
t.is(typeof timings.abort, 'number'); |
| 216 |
t.is(timings.phases.total, timings.abort! - timings.start); |
| 217 |
t.truthy((request as any).res); |
| 218 |
|
| 219 |
t.end(); |
| 220 |
}); |
| 221 |
} else { |
| 222 |
response.once('end', () => { |
| 223 |
t.is(typeof timings.abort, 'number'); |
| 224 |
t.is(timings.phases.total, timings.end! - timings.start); |
| 225 |
t.truthy((request as any).res); |
| 226 |
|
| 227 |
t.end(); |
| 228 |
}); |
| 229 |
} |
| 230 |
}); |
| 231 |
}); |
| 232 |
|
| 233 |
test('doesn\'t throw when someone used `.prependOnceListener()`', t => { |
| 234 |
const emitter = new EventEmitter(); |
| 235 |
timer(emitter as ClientRequest); |
| 236 |
// eslint-disable-next-line @typescript-eslint/no-empty-function |
| 237 |
emitter.prependOnceListener('error', () => {}); |
| 238 |
|
| 239 |
t.notThrows(() => emitter.emit('error', new Error(error))); |
| 240 |
}); |
| 241 |
|
| 242 |
test('sensible timings', async t => { |
| 243 |
const {timings, request} = makeRequest('https://google.com'); |
| 244 |
const now = Date.now(); |
| 245 |
|
| 246 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 247 |
response.resume(); |
| 248 |
await pEvent(response, 'end'); |
| 249 |
|
| 250 |
t.true(timings.socket! >= now); |
| 251 |
t.true(timings.lookup! >= now); |
| 252 |
t.true(timings.connect! >= now); |
| 253 |
t.true(timings.secureConnect! >= now); |
| 254 |
t.true(timings.response! >= now); |
| 255 |
t.true(timings.end! >= now); |
| 256 |
t.is(timings.error, undefined); |
| 257 |
t.is(timings.abort, undefined); |
| 258 |
t.true(timings.phases.wait! < 1000); |
| 259 |
t.true(timings.phases.dns! < 1000); |
| 260 |
t.true(timings.phases.tcp! < 1000); |
| 261 |
t.true(timings.phases.tls! < 1000); |
| 262 |
t.true(timings.phases.request! < 1000); |
| 263 |
t.true(timings.phases.firstByte! < 1000); |
| 264 |
t.true(timings.phases.download! < 1000); |
| 265 |
t.true(timings.phases.total! < 1000); |
| 266 |
}); |
| 267 |
|
| 268 |
test('prepends once listeners', async t => { |
| 269 |
const request = https.get('https://google.com'); |
| 270 |
|
| 271 |
const promise = new Promise<void>(resolve => { |
| 272 |
request.once('response', () => { |
| 273 |
t.true(typeof timings.response === 'number'); |
| 274 |
|
| 275 |
resolve(); |
| 276 |
}); |
| 277 |
|
| 278 |
const timings = timer(request); |
| 279 |
}); |
| 280 |
|
| 281 |
await promise; |
| 282 |
request.abort(); |
| 283 |
}); |
| 284 |
|
| 285 |
test('`tls` phase for https requests', async t => { |
| 286 |
const {request, timings} = makeRequest('https://google.com'); |
| 287 |
|
| 288 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 289 |
response.resume(); |
| 290 |
await pEvent(response, 'end'); |
| 291 |
|
| 292 |
t.is(typeof timings.secureConnect, 'number'); |
| 293 |
t.is(typeof timings.phases.tls, 'number'); |
| 294 |
}); |
| 295 |
|
| 296 |
test('no `tls` phase for http requests', async t => { |
| 297 |
const {request, timings} = makeRequest(server.url); |
| 298 |
|
| 299 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 300 |
response.resume(); |
| 301 |
await pEvent(response, 'end'); |
| 302 |
|
| 303 |
t.is(timings.secureConnect, undefined); |
| 304 |
t.is(timings.phases.tls, undefined); |
| 305 |
}); |
| 306 |
|
| 307 |
test('timings are accessible via `request.timings`', t => { |
| 308 |
const {request, timings} = makeRequest('https://google.com'); |
| 309 |
request.abort(); |
| 310 |
|
| 311 |
const typedRequest = request as ClientRequestWithTimings; |
| 312 |
|
| 313 |
t.is(typedRequest.timings, timings); |
| 314 |
}); |
| 315 |
|
| 316 |
test('timings are accessible via `response.timings`', async t => { |
| 317 |
const {request, timings} = makeRequest('https://google.com'); |
| 318 |
|
| 319 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 320 |
const typedResponse = response as IncomingMessageWithTimings; |
| 321 |
|
| 322 |
t.is(typedResponse.timings, timings); |
| 323 |
|
| 324 |
response.resume(); |
| 325 |
await pEvent(response, 'end'); |
| 326 |
}); |
| 327 |
|
| 328 |
test('can extend `http.IncomingMessage`', t => { |
| 329 |
interface Response extends IncomingMessage { |
| 330 |
timings: boolean; |
| 331 |
} |
| 332 |
|
| 333 |
0 as unknown as Response; |
| 334 |
|
| 335 |
t.pass(); |
| 336 |
}); |
| 337 |
|
| 338 |
test('singleton', t => { |
| 339 |
const {request, timings} = makeRequest('https://google.com'); |
| 340 |
request.abort(); |
| 341 |
|
| 342 |
const secondTimings = timer(request); |
| 343 |
|
| 344 |
const typedRequest = request as ClientRequestWithTimings; |
| 345 |
|
| 346 |
t.is(typedRequest.timings, timings); |
| 347 |
t.is(timings, secondTimings); |
| 348 |
}); |
| 349 |
|
| 350 |
test('sets `abort` on response.destroy()', async t => { |
| 351 |
const {request, timings} = makeRequest(server.url); |
| 352 |
const response = await pEvent(request, 'response'); |
| 353 |
response.destroy(); |
| 354 |
|
| 355 |
await pEvent(request, 'close'); |
| 356 |
t.is(typeof timings.abort, 'number'); |
| 357 |
}); |
| 358 |
|
| 359 |
const version = process.versions.node.split('.').map(v => Number(v)) as [number, number, number]; |
| 360 |
if (version[0] >= 16) { |
| 361 |
test('skips proxy-wrapped sockets', async t => { |
| 362 |
const request = await http2.auto('https://httpbin.org/anything'); |
| 363 |
const timings = timer(request); |
| 364 |
|
| 365 |
request.end(); |
| 366 |
|
| 367 |
const response: IncomingMessage = await pEvent(request, 'response'); |
| 368 |
response.resume(); |
| 369 |
|
| 370 |
t.is(request.socket!.listenerCount('lookup'), 0); |
| 371 |
|
| 372 |
await pEvent(response, 'end'); |
| 373 |
|
| 374 |
t.is(typeof timings, 'object'); |
| 375 |
t.is(typeof timings.start, 'number'); |
| 376 |
t.is(typeof timings.socket, 'number'); |
| 377 |
t.is(timings.lookup, undefined); |
| 378 |
t.is(timings.connect, undefined); |
| 379 |
t.is(timings.secureConnect, undefined); |
| 380 |
t.is(typeof timings.response, 'number'); |
| 381 |
t.is(typeof timings.end, 'number'); |
| 382 |
t.is(timings.error, undefined); |
| 383 |
t.is(timings.abort, undefined); |
| 384 |
}); |
| 385 |
} |