-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps-native.ts
More file actions
50 lines (44 loc) · 1.3 KB
/
https-native.ts
File metadata and controls
50 lines (44 loc) · 1.3 KB
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
import https from 'node:https';
import { HardenedHttpsAgent, defaultAgentOptions } from '../dist';
async function main() {
// Customize standard agent options if required
const httpsAgentOptions: https.AgentOptions = {
keepAlive: true,
timeout: 55000,
maxSockets: 20,
maxFreeSockets: 5,
maxCachedSessions: 500,
};
// Merge standard agent options with hardened defaults
const agent = new HardenedHttpsAgent({
...httpsAgentOptions,
...defaultAgentOptions(),
loggerOptions: {
level: 'debug',
}
});
try {
console.log('\n> Performing request...');
await new Promise<void>((resolve, reject) => {
const req = https.request(
'https://example.com',
{ method: 'GET', agent, timeout: 15000 },
(res) => {
const status = res.statusCode ?? 0;
if (status >= 200 && status < 300) {
resolve();
} else {
reject(new Error(`Unexpected status ${status}`));
}
res.resume();
},
);
req.on('error', reject);
req.end();
});
console.log('> Congrats! You have successfully performed a more secure request with hardened-https-agent.');
} catch (error) {
console.error('> An error occurred while performing the request', error);
}
}
main();