@dongdev/fca-unofficial 2.0.32 → 3.0.0

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.
@@ -17,21 +17,39 @@ const client = wrapper(axios.create({
17
17
  jar,
18
18
  withCredentials: true,
19
19
  timeout: 60000,
20
- validateStatus: s => s >= 200 && s < 600
20
+ validateStatus: s => s >= 200 && s < 600,
21
+ maxRedirects: 5,
22
+ maxContentLength: Infinity,
23
+ maxBodyLength: Infinity
21
24
  }));
22
25
 
23
26
  const delay = ms => new Promise(r => setTimeout(r, ms));
24
27
 
25
- async function requestWithRetry(fn, retries = 3) {
26
- let err;
28
+ async function requestWithRetry(fn, retries = 3, baseDelay = 1000) {
29
+ let lastError;
27
30
  for (let i = 0; i < retries; i++) {
28
- try { return await fn(); } catch (e) {
29
- err = e;
30
- if (i === retries - 1) return e.response ? e.response : Promise.reject(e);
31
- await delay((1 << i) * 1000 + Math.floor(Math.random() * 200));
31
+ try {
32
+ return await fn();
33
+ } catch (e) {
34
+ lastError = e;
35
+ // Don't retry on client errors (4xx) except 429 (rate limit)
36
+ const status = e?.response?.status || e?.statusCode || 0;
37
+ if (status >= 400 && status < 500 && status !== 429) {
38
+ return e.response || Promise.reject(e);
39
+ }
40
+ // Don't retry on last attempt
41
+ if (i === retries - 1) {
42
+ return e.response || Promise.reject(e);
43
+ }
44
+ // Exponential backoff with jitter
45
+ const backoffDelay = Math.min(
46
+ baseDelay * Math.pow(2, i) + Math.floor(Math.random() * 200),
47
+ 30000 // Max 30 seconds
48
+ );
49
+ await delay(backoffDelay);
32
50
  }
33
51
  }
34
- throw err;
52
+ throw lastError || new Error("Request failed after retries");
35
53
  }
36
54
 
37
55
  function cfg(base = {}) {
@@ -69,12 +87,12 @@ function isPairArrayList(arr) {
69
87
  }
70
88
 
71
89
  function cleanGet(url) {
72
- return requestWithRetry(() => client.get(url, cfg()));
90
+ return requestWithRetry(() => client.get(url, cfg()), 3, 1000);
73
91
  }
74
92
 
75
93
  function get(url, reqJar, qs, options, ctx, customHeader) {
76
94
  const headers = getHeaders(url, options, ctx, customHeader);
77
- return requestWithRetry(() => client.get(url, cfg({ reqJar, headers, params: qs })));
95
+ return requestWithRetry(() => client.get(url, cfg({ reqJar, headers, params: qs })), 3, 1000);
78
96
  }
79
97
 
80
98
  function post(url, reqJar, form, options, ctx, customHeader) {
@@ -107,7 +125,7 @@ function post(url, reqJar, form, options, ctx, customHeader) {
107
125
  data = p.toString();
108
126
  headers["Content-Type"] = "application/x-www-form-urlencoded";
109
127
  }
110
- return requestWithRetry(() => client.post(url, data, cfg({ reqJar, headers })));
128
+ return requestWithRetry(() => client.post(url, data, cfg({ reqJar, headers })), 3, 1000);
111
129
  }
112
130
 
113
131
  async function postFormData(url, reqJar, form, qs, options, ctx) {
@@ -160,7 +178,7 @@ async function postFormData(url, reqJar, form, qs, options, ctx) {
160
178
  }
161
179
  }
162
180
  const headers = { ...getHeaders(url, options, ctx), ...fd.getHeaders() };
163
- return requestWithRetry(() => client.post(url, fd, cfg({ reqJar, headers, params: qs })));
181
+ return requestWithRetry(() => client.post(url, fd, cfg({ reqJar, headers, params: qs })), 3, 1000);
164
182
  }
165
183
 
166
184
  function makeDefaults(html, userID, ctx) {