-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (30 loc) · 892 Bytes
/
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
const https = require('https');
const http = require('http');
module.exports = async (url, options = {}) => {
return new Promise((resolve, reject) => {
try {
const tracer = (urls, protocol = https) => {
if (urls.slice(0, 5) === 'http:') {
protocol = http;
}
protocol
.get(urls, options, ({ statusCode, headers: { location }, socket: {_host: host} }) => {
if ([301, 302, 303, 307, 308].includes(statusCode)) {
if (location) {
if (location.startsWith('/')) {
location = `https://${host}${location}`;
}
tracer(location);
return;
}
}
resolve(urls);
})
.on('error', (err) => reject(err));
};
tracer(url);
} catch (err) {
reject(err);
}
});
};