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.
Files changed (2) hide show
  1. package/lib/index.js +13 -3
  2. 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 (res.statusCode === 301 || res.statusCode === 302) {
19
- return fetchUrl(res.headers.location).then(resolve).catch(reject);
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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claito-connect",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "One-command setup for AI agents to connect to Claito Protocol 🦞",
5
5
  "bin": {
6
6
  "claito-connect": "./bin/claito-connect.js"