claito-connect 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +13 -3
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -10,13 +10,23 @@ const HEARTBEAT_FILENAME = 'HEARTBEAT.md';
|
|
|
10
10
|
/**
|
|
11
11
|
* Fetch content from URL
|
|
12
12
|
*/
|
|
13
|
-
function fetchUrl(url) {
|
|
13
|
+
function fetchUrl(url, redirectsLeft = 5) {
|
|
14
14
|
return new Promise((resolve, reject) => {
|
|
15
15
|
const client = url.startsWith('https') ? https : http;
|
|
16
16
|
|
|
17
17
|
client.get(url, (res) => {
|
|
18
|
-
if (
|
|
19
|
-
|
|
18
|
+
if ([301, 302, 307, 308].includes(res.statusCode)) {
|
|
19
|
+
if (redirectsLeft <= 0) {
|
|
20
|
+
reject(new Error(`Too many redirects: ${url}`));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const location = res.headers.location;
|
|
24
|
+
if (!location) {
|
|
25
|
+
reject(new Error(`Redirect with no location: ${url}`));
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const nextUrl = new URL(location, url).toString();
|
|
29
|
+
return fetchUrl(nextUrl, redirectsLeft - 1).then(resolve).catch(reject);
|
|
20
30
|
}
|
|
21
31
|
|
|
22
32
|
if (res.statusCode !== 200) {
|