@@ -21,6 +21,40 @@ function mapFile(obj, prefix) {
2121 } ;
2222}
2323
24+ export async function retryWithBackoff (
25+ operation ,
26+ maxAttempts = 3 ,
27+ initialDelay = 1000 ,
28+ maxDelay = 10000 ,
29+ backoffFactor = 2 ,
30+ ) {
31+ let lastError = null ;
32+
33+ for ( let attempt = 1 ; attempt <= maxAttempts ; attempt ++ ) {
34+ try {
35+ return await operation ( ) ;
36+ } catch ( error ) {
37+ lastError = error instanceof Error ? error : new Error ( String ( error ) ) ;
38+
39+ if ( attempt === maxAttempts ) {
40+ throw lastError ;
41+ }
42+
43+ // Calculate delay with exponential backoff
44+ const delay = Math . min (
45+ initialDelay * Math . pow ( backoffFactor , attempt - 1 ) ,
46+ maxDelay ,
47+ ) ;
48+
49+ // Wait before next attempt
50+ await new Promise ( ( resolve ) => setTimeout ( resolve , delay ) ) ;
51+ }
52+ }
53+
54+ // This line should never be reached due to the throw above
55+ throw lastError || new Error ( "Unexpected error in retryWithBackoff" ) ;
56+ }
57+
2458export const timeSince = ( date ) => {
2559 const seconds = Math . floor ( ( new Date ( ) - date ) / 1000 ) ;
2660
@@ -180,22 +214,29 @@ export const apiHandler = {
180214 } ,
181215 } ) ;
182216 } ,
183- uploadObjects : ( file , key , bucket , callback ) => {
184- // console.log(key)
185- return api . post ( `/buckets/${ bucket } /upload` , file , {
186- params : {
187- key : encode ( key ) ,
188- httpMetadata : encode (
189- JSON . stringify ( {
190- contentType : file . type ,
191- } ) ,
192- ) ,
217+ uploadObjects : async ( file , key , bucket , callback ) => {
218+ return await retryWithBackoff (
219+ async ( ) => {
220+ return await api . post ( `/buckets/${ bucket } /upload` , file , {
221+ params : {
222+ key : encode ( key ) ,
223+ httpMetadata : encode (
224+ JSON . stringify ( {
225+ contentType : file . type ,
226+ } ) ,
227+ ) ,
228+ } ,
229+ headers : {
230+ "Content-Type" : "multipart/form-data" ,
231+ } ,
232+ onUploadProgress : callback ,
233+ } ) ;
193234 } ,
194- headers : {
195- "Content-Type" : "multipart/form-data" ,
196- } ,
197- onUploadProgress : callback ,
198- } ) ;
235+ 5 ,
236+ 1000 ,
237+ 10000 ,
238+ 2 ,
239+ ) ;
199240 } ,
200241 listObjects : async ( bucket , prefix , delimiter = "/" , cursor = null ) => {
201242 return await api . get (
0 commit comments