@@ -58,20 +58,8 @@ export class ReCaptchaV3Provider implements AppCheckProvider {
58
58
* @internal
59
59
*/
60
60
async getToken ( ) : Promise < AppCheckTokenInternal > {
61
- if ( this . _throttleData ) {
62
- if ( Date . now ( ) - this . _throttleData . allowRequestsAfter > 0 ) {
63
- // If after throttle timestamp, clear throttle data.
64
- this . _throttleData = null ;
65
- } else {
66
- // If before, throw.
67
- throw ERROR_FACTORY . create ( AppCheckError . THROTTLED , {
68
- time : new Date (
69
- this . _throttleData . allowRequestsAfter
70
- ) . toLocaleString ( ) ,
71
- httpStatus : this . _throttleData . httpStatus
72
- } ) ;
73
- }
74
- }
61
+ throwIfThrottled ( this . _throttleData ) ;
62
+
75
63
if ( ! this . _app || ! this . _platformLoggerProvider ) {
76
64
// This should only occur if user has not called initializeAppCheck().
77
65
// We don't have an appName to provide if so.
@@ -92,59 +80,25 @@ export class ReCaptchaV3Provider implements AppCheckProvider {
92
80
) ;
93
81
} catch ( e ) {
94
82
if ( ( e as FirebaseError ) . code === AppCheckError . FETCH_STATUS_ERROR ) {
95
- const throttleData = this . _setBackoff (
96
- Number ( ( e as FirebaseError ) . customData ?. httpStatus )
83
+ this . _throttleData = setBackoff (
84
+ Number ( ( e as FirebaseError ) . customData ?. httpStatus ) ,
85
+ this . _throttleData
97
86
) ;
98
87
throw ERROR_FACTORY . create ( AppCheckError . THROTTLED , {
99
- time : getDurationString ( throttleData . allowRequestsAfter - Date . now ( ) ) ,
100
- httpStatus : throttleData . httpStatus
88
+ time : getDurationString (
89
+ this . _throttleData . allowRequestsAfter - Date . now ( )
90
+ ) ,
91
+ httpStatus : this . _throttleData . httpStatus
101
92
} ) ;
102
93
} else {
103
94
throw e ;
104
95
}
105
96
}
97
+ // If successful, clear throttle data.
98
+ this . _throttleData = null ;
106
99
return result ;
107
100
}
108
101
109
- /**
110
- * Set throttle data to block requests until after a certain time
111
- * depending on the failed request's status code.
112
- * @param httpStatus - Status code of failed request.
113
- * @returns Data about current throttle state and expiration time.
114
- */
115
- private _setBackoff ( httpStatus : number ) : ThrottleData {
116
- /**
117
- * Block retries for 1 day for the following error codes:
118
- *
119
- * 404: Likely malformed URL.
120
- *
121
- * 403:
122
- * - Attestation failed
123
- * - Wrong API key
124
- * - Project deleted
125
- */
126
- if ( httpStatus === 404 || httpStatus === 403 ) {
127
- this . _throttleData = {
128
- backoffCount : 1 ,
129
- allowRequestsAfter : Date . now ( ) + ONE_DAY ,
130
- httpStatus
131
- } ;
132
- } else {
133
- /**
134
- * For all other error codes, the time when it is ok to retry again
135
- * is based on exponential backoff.
136
- */
137
- const backoffCount = this . _throttleData ? this . _throttleData . backoffCount : 0 ;
138
- const backoffMillis = calculateBackoffMillis ( backoffCount , 1000 , 2 ) ;
139
- this . _throttleData = {
140
- backoffCount : backoffCount + 1 ,
141
- allowRequestsAfter : Date . now ( ) + backoffMillis ,
142
- httpStatus
143
- } ;
144
- }
145
- return this . _throttleData ;
146
- }
147
-
148
102
/**
149
103
* @internal
150
104
*/
@@ -227,3 +181,56 @@ export class CustomProvider implements AppCheckProvider {
227
181
}
228
182
}
229
183
}
184
+
185
+ /**
186
+ * Set throttle data to block requests until after a certain time
187
+ * depending on the failed request's status code.
188
+ * @param httpStatus - Status code of failed request.
189
+ * @returns Data about current throttle state and expiration time.
190
+ */
191
+ function setBackoff (
192
+ httpStatus : number ,
193
+ throttleData : ThrottleData | null
194
+ ) : ThrottleData {
195
+ /**
196
+ * Block retries for 1 day for the following error codes:
197
+ *
198
+ * 404: Likely malformed URL.
199
+ *
200
+ * 403:
201
+ * - Attestation failed
202
+ * - Wrong API key
203
+ * - Project deleted
204
+ */
205
+ if ( httpStatus === 404 || httpStatus === 403 ) {
206
+ return {
207
+ backoffCount : 1 ,
208
+ allowRequestsAfter : Date . now ( ) + ONE_DAY ,
209
+ httpStatus
210
+ } ;
211
+ } else {
212
+ /**
213
+ * For all other error codes, the time when it is ok to retry again
214
+ * is based on exponential backoff.
215
+ */
216
+ const backoffCount = throttleData ? throttleData . backoffCount : 0 ;
217
+ const backoffMillis = calculateBackoffMillis ( backoffCount , 1000 , 2 ) ;
218
+ return {
219
+ backoffCount : backoffCount + 1 ,
220
+ allowRequestsAfter : Date . now ( ) + backoffMillis ,
221
+ httpStatus
222
+ } ;
223
+ }
224
+ }
225
+
226
+ function throwIfThrottled ( throttleData : ThrottleData | null ) : void {
227
+ if ( throttleData ) {
228
+ if ( Date . now ( ) - throttleData . allowRequestsAfter <= 0 ) {
229
+ // If before, throw.
230
+ throw ERROR_FACTORY . create ( AppCheckError . THROTTLED , {
231
+ time : getDurationString ( throttleData . allowRequestsAfter - Date . now ( ) ) ,
232
+ httpStatus : throttleData . httpStatus
233
+ } ) ;
234
+ }
235
+ }
236
+ }
0 commit comments