alchemy 0.5.1 → 0.5.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/cloudflare/api.js +14 -3
- package/lib/cloudflare/worker.js +3 -0
- package/lib/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/cloudflare/api.ts +22 -5
- package/src/cloudflare/worker.ts +5 -0
- package/src/index.ts +1 -0
package/lib/cloudflare/api.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { withExponentialBackoff } from "../util/retry";
|
|
1
2
|
import { getCloudflareHeaders } from "./auth";
|
|
2
3
|
/**
|
|
3
4
|
* Custom error class for Cloudflare API errors
|
|
@@ -151,11 +152,21 @@ export class CloudflareApi {
|
|
|
151
152
|
delete combinedHeaders["Content-Type"];
|
|
152
153
|
}
|
|
153
154
|
const url = `${this.baseUrl}${path}`;
|
|
154
|
-
//
|
|
155
|
-
return fetch(url, {
|
|
155
|
+
// Use withExponentialBackoff for automatic retry on network errors
|
|
156
|
+
return withExponentialBackoff(() => fetch(url, {
|
|
156
157
|
...init,
|
|
157
158
|
headers: combinedHeaders,
|
|
158
|
-
})
|
|
159
|
+
}), (error) => {
|
|
160
|
+
// Only retry on network-related errors
|
|
161
|
+
const errorMsg = error.message || "";
|
|
162
|
+
const isNetworkError = errorMsg.includes("socket connection was closed") ||
|
|
163
|
+
errorMsg.includes("ECONNRESET") ||
|
|
164
|
+
errorMsg.includes("ETIMEDOUT") ||
|
|
165
|
+
errorMsg.includes("ECONNREFUSED");
|
|
166
|
+
return isNetworkError;
|
|
167
|
+
}, 5, // Maximum 5 attempts (1 initial + 4 retries)
|
|
168
|
+
1000 // Start with 1s delay, will exponentially increase
|
|
169
|
+
);
|
|
159
170
|
}
|
|
160
171
|
/**
|
|
161
172
|
* Helper for GET requests
|
package/lib/cloudflare/worker.js
CHANGED
|
@@ -557,6 +557,7 @@ async function uploadAssets(api, workerName, assets) {
|
|
|
557
557
|
// Process each bucket of files
|
|
558
558
|
for (const bucket of buckets) {
|
|
559
559
|
const formData = new FormData();
|
|
560
|
+
let totalBytes = 0;
|
|
560
561
|
// Add each file in the bucket to the form
|
|
561
562
|
for (const fileHash of bucket) {
|
|
562
563
|
// Find the file with this hash
|
|
@@ -575,8 +576,10 @@ async function uploadAssets(api, workerName, assets) {
|
|
|
575
576
|
const blob = new Blob([base64Content], {
|
|
576
577
|
type: getContentType(file.filePath),
|
|
577
578
|
});
|
|
579
|
+
totalBytes += blob.size;
|
|
578
580
|
formData.append(fileHash, blob, fileHash);
|
|
579
581
|
}
|
|
582
|
+
console.log(`Uploading ${totalBytes} bytes of assets`);
|
|
580
583
|
// Upload this batch of files
|
|
581
584
|
const uploadResponse = await api.post(`/accounts/${api.accountId}/workers/assets/upload?base64=true`, formData, {
|
|
582
585
|
headers: {
|
package/lib/index.d.ts
CHANGED
package/package.json
CHANGED
package/src/cloudflare/api.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Secret } from "../secret";
|
|
2
|
+
import { withExponentialBackoff } from "../util/retry";
|
|
2
3
|
import { type CloudflareAuthOptions, getCloudflareHeaders } from "./auth";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -240,11 +241,27 @@ export class CloudflareApi {
|
|
|
240
241
|
|
|
241
242
|
const url = `${this.baseUrl}${path}`;
|
|
242
243
|
|
|
243
|
-
//
|
|
244
|
-
return
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
244
|
+
// Use withExponentialBackoff for automatic retry on network errors
|
|
245
|
+
return withExponentialBackoff(
|
|
246
|
+
() =>
|
|
247
|
+
fetch(url, {
|
|
248
|
+
...init,
|
|
249
|
+
headers: combinedHeaders,
|
|
250
|
+
}),
|
|
251
|
+
(error) => {
|
|
252
|
+
// Only retry on network-related errors
|
|
253
|
+
const errorMsg = (error as Error).message || "";
|
|
254
|
+
const isNetworkError =
|
|
255
|
+
errorMsg.includes("socket connection was closed") ||
|
|
256
|
+
errorMsg.includes("ECONNRESET") ||
|
|
257
|
+
errorMsg.includes("ETIMEDOUT") ||
|
|
258
|
+
errorMsg.includes("ECONNREFUSED");
|
|
259
|
+
|
|
260
|
+
return isNetworkError;
|
|
261
|
+
},
|
|
262
|
+
5, // Maximum 5 attempts (1 initial + 4 retries)
|
|
263
|
+
1000 // Start with 1s delay, will exponentially increase
|
|
264
|
+
);
|
|
248
265
|
}
|
|
249
266
|
|
|
250
267
|
/**
|
package/src/cloudflare/worker.ts
CHANGED
|
@@ -978,6 +978,8 @@ async function uploadAssets(
|
|
|
978
978
|
for (const bucket of buckets) {
|
|
979
979
|
const formData = new FormData();
|
|
980
980
|
|
|
981
|
+
let totalBytes = 0;
|
|
982
|
+
|
|
981
983
|
// Add each file in the bucket to the form
|
|
982
984
|
for (const fileHash of bucket) {
|
|
983
985
|
// Find the file with this hash
|
|
@@ -1000,9 +1002,12 @@ async function uploadAssets(
|
|
|
1000
1002
|
const blob = new Blob([base64Content], {
|
|
1001
1003
|
type: getContentType(file.filePath),
|
|
1002
1004
|
});
|
|
1005
|
+
totalBytes += blob.size;
|
|
1003
1006
|
formData.append(fileHash, blob, fileHash);
|
|
1004
1007
|
}
|
|
1005
1008
|
|
|
1009
|
+
console.log(`Uploading ${totalBytes} bytes of assets`);
|
|
1010
|
+
|
|
1006
1011
|
// Upload this batch of files
|
|
1007
1012
|
const uploadResponse = await api.post(
|
|
1008
1013
|
`/accounts/${api.accountId}/workers/assets/upload?base64=true`,
|