-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcustom_request.ts
101 lines (85 loc) · 2.36 KB
/
custom_request.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import https from 'https';
import http from 'http';
export type Stats = {
body: string;
statusCode: number;
trtMs: number;
ttfbMs: number;
ttlbMs: number;
};
export function customRequest(url: string): Promise<Stats> {
const protocol = url.startsWith('https') ? https : http;
const stats: Stats = {
body: '',
statusCode: 0,
trtMs: 0,
ttfbMs: 0,
ttlbMs: 0
};
const processTimes = {
dnsLookup: BigInt(0),
tcpConnection: BigInt(0),
tlsHandshake: BigInt(0),
responseBodyStart: BigInt(0),
responseBodyEnd: BigInt(0)
};
// Ensure connections are not reused
const agent = new protocol.Agent({
keepAlive: false
});
return new Promise<Stats>((resolve, reject) => {
// Close the connection after the request
const options = {
agent,
headers: {
Connection: 'close'
}
};
const req = protocol.get(url, options, (res) => {
stats.statusCode = res.statusCode ?? -1;
// Handle redirects
if (res.statusCode === 301 && res.headers.location) {
const redirectUrl = res.headers.location;
req.destroy();
customRequest(redirectUrl)
.then((redirectStats) => resolve(redirectStats))
.catch((err) => reject(err));
return;
}
res.once('data', () => {
processTimes.responseBodyStart = process.hrtime.bigint();
stats.ttfbMs =
Number(processTimes.responseBodyStart - processTimes.tlsHandshake) /
1000_000;
});
res.on('data', (d) => {
stats.body += d;
});
res.on('end', () => {
processTimes.responseBodyEnd = process.hrtime.bigint();
stats.ttlbMs =
Number(processTimes.responseBodyEnd - processTimes.tlsHandshake) /
1000_000;
stats.trtMs =
Number(processTimes.responseBodyEnd - processTimes.dnsLookup) /
1000_000;
resolve(stats);
});
});
req.on('error', (error) => {
reject(error);
});
req.on('socket', (socket) => {
socket.on('lookup', () => {
processTimes.dnsLookup = process.hrtime.bigint();
});
socket.on('connect', () => {
processTimes.tcpConnection = process.hrtime.bigint();
});
socket.on('secureConnect', () => {
processTimes.tlsHandshake = process.hrtime.bigint();
});
});
req.end();
});
}