-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (53 loc) · 1.74 KB
/
index.js
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
import nodefetch from 'node-fetch';
import https from 'https';
const nativeFetch = async (url) => {
try {
const resp = await fetch(url, { method: 'GET' });
const t = await resp.text();
console.log(`${resp.statusText} (${resp.status}) ${resp.url} - ${JSON.stringify(resp.headers)} - Content-Len: ${t.length}`);
} catch (error) {
console.error(error.message);
}
}
const nodeFetch = async (url) => {
try {
const resp = await nodefetch(url, { method: 'GET' });
const t = await resp.text();
console.log(`${resp.statusText} (${resp.status}) - ${resp.headers} - Content-Len: ${t.length}`);
} catch (error) {
console.error(error.message);
}
}
const httpsGet = async (url) => {
await new Promise((resolve) => {
let data = '';
https.get(url, (resp) => {
resp.on('data', (chuck) => {
data += chuck;
});
resp.on('end', () => {
console.log(`Successfully fetched ${url} - Content-Len ${data.length}`);
resolve();
});
})
.on('error', (err) => {
console.log(`Query failed: ${err.message}`);
resolve();
});
});
}
(async () => {
console.log(`OPENSSL_CONFIG set to: ${process.env.OPENSSL_CONF}`);
console.log('\nUsing native fetch');
await nativeFetch('https://www.google.com');
await nativeFetch('https://cloud.robocorp.com/');
await nativeFetch('https://self-signed.badssl.com/');
console.log('\nUsing node-fetch');
await nodeFetch('https://www.google.com');
await nodeFetch('https://cloud.robocorp.com/');
await nodeFetch('https://self-signed.badssl.com/');
console.log('\nUsing https.get');
await httpsGet('https://www.google.com');
await httpsGet('https://cloud.robocorp.com/');
await httpsGet('https://self-signed.badssl.com/');
})();