@@ -26,6 +26,7 @@ import { ErrorHandler, RequestHandler, RequestInfo } from './requestinfo';
26
26
import { isJustDef } from './type' ;
27
27
import { makeQueryString } from './url' ;
28
28
import { Connection , ErrorCode , Headers , ConnectionType } from './connection' ;
29
+ import { isRetryStatusCode } from './utils' ;
29
30
30
31
export interface Request < T > {
31
32
getPromise ( ) : Promise < T > ;
@@ -69,7 +70,8 @@ class NetworkRequest<I extends ConnectionType, O> implements Request<O> {
69
70
private errorCallback_ : ErrorHandler | null ,
70
71
private timeout_ : number ,
71
72
private progressCallback_ : ( ( p1 : number , p2 : number ) => void ) | null ,
72
- private connectionFactory_ : ( ) => Connection < I >
73
+ private connectionFactory_ : ( ) => Connection < I > ,
74
+ private retry = true
73
75
) {
74
76
this . promise_ = new Promise ( ( resolve , reject ) => {
75
77
this . resolve_ = resolve as ( value ?: O | PromiseLike < O > ) => void ;
@@ -93,16 +95,15 @@ class NetworkRequest<I extends ConnectionType, O> implements Request<O> {
93
95
const connection = this . connectionFactory_ ( ) ;
94
96
this . pendingConnection_ = connection ;
95
97
96
- const progressListener : ( progressEvent : ProgressEvent ) => void =
97
- progressEvent => {
98
- const loaded = progressEvent . loaded ;
99
- const total = progressEvent . lengthComputable
100
- ? progressEvent . total
101
- : - 1 ;
102
- if ( this . progressCallback_ !== null ) {
103
- this . progressCallback_ ( loaded , total ) ;
104
- }
105
- } ;
98
+ const progressListener : (
99
+ progressEvent : ProgressEvent
100
+ ) => void = progressEvent => {
101
+ const loaded = progressEvent . loaded ;
102
+ const total = progressEvent . lengthComputable ? progressEvent . total : - 1 ;
103
+ if ( this . progressCallback_ !== null ) {
104
+ this . progressCallback_ ( loaded , total ) ;
105
+ }
106
+ } ;
106
107
if ( this . progressCallback_ !== null ) {
107
108
connection . addUploadProgressListener ( progressListener ) ;
108
109
}
@@ -118,7 +119,11 @@ class NetworkRequest<I extends ConnectionType, O> implements Request<O> {
118
119
this . pendingConnection_ = null ;
119
120
const hitServer = connection . getErrorCode ( ) === ErrorCode . NO_ERROR ;
120
121
const status = connection . getStatus ( ) ;
121
- if ( ! hitServer || this . isRetryStatusCode_ ( status ) ) {
122
+ if (
123
+ ( ! hitServer ||
124
+ isRetryStatusCode ( status , this . additionalRetryCodes_ ) ) &&
125
+ this . retry
126
+ ) {
122
127
const wasCanceled = connection . getErrorCode ( ) === ErrorCode . ABORT ;
123
128
backoffCallback (
124
129
false ,
@@ -196,22 +201,6 @@ class NetworkRequest<I extends ConnectionType, O> implements Request<O> {
196
201
this . pendingConnection_ . abort ( ) ;
197
202
}
198
203
}
199
-
200
- private isRetryStatusCode_ ( status : number ) : boolean {
201
- // The codes for which to retry came from this page:
202
- // https://cloud.google.com/storage/docs/exponential-backoff
203
- const isFiveHundredCode = status >= 500 && status < 600 ;
204
- const extraRetryCodes = [
205
- // Request Timeout: web server didn't receive full request in time.
206
- 408 ,
207
- // Too Many Requests: you're getting rate-limited, basically.
208
- 429
209
- ] ;
210
- const isExtraRetryCode = extraRetryCodes . indexOf ( status ) !== - 1 ;
211
- const isRequestSpecificRetryCode =
212
- this . additionalRetryCodes_ . indexOf ( status ) !== - 1 ;
213
- return isFiveHundredCode || isExtraRetryCode || isRequestSpecificRetryCode ;
214
- }
215
204
}
216
205
217
206
/**
@@ -271,7 +260,8 @@ export function makeRequest<I extends ConnectionType, O>(
271
260
authToken : string | null ,
272
261
appCheckToken : string | null ,
273
262
requestFactory : ( ) => Connection < I > ,
274
- firebaseVersion ?: string
263
+ firebaseVersion ?: string ,
264
+ retry = true
275
265
) : Request < O > {
276
266
const queryPart = makeQueryString ( requestInfo . urlParams ) ;
277
267
const url = requestInfo . url + queryPart ;
@@ -291,6 +281,7 @@ export function makeRequest<I extends ConnectionType, O>(
291
281
requestInfo . errorHandler ,
292
282
requestInfo . timeout ,
293
283
requestInfo . progressCallback ,
294
- requestFactory
284
+ requestFactory ,
285
+ retry
295
286
) ;
296
287
}
0 commit comments