@@ -8,7 +8,7 @@ import noop from 'lodash.noop';
88import Rusha from 'rusha' ;
99import Chunk from './Chunk' ;
1010import Base from './Base' ;
11- import type { StringAnyMap } from '../flowTypes' ;
11+ import type { BoxItem , StringAnyMap } from '../flowTypes' ;
1212import int32ArrayToBase64 from '../util/base64' ;
1313
1414const DEFAULT_RETRY_COMMIT_DELAY_MS = 3000 ; // Wait 3s before retrying a chunk
@@ -201,7 +201,7 @@ class ChunkedUpload extends Base {
201201 sha1 : int32ArrayToBase64 ( new Rusha ( ) . rawDigest ( buffer ) ) ,
202202 totalSize : this . file . size ,
203203 successCallback : this . handleChunkSuccess ,
204- errorCallback : this . handleChunkError ,
204+ errorCallback : this . handleUploadError ,
205205 progressCallback : ( progressEvent ) => this . handleChunkProgress ( chunk , progressEvent )
206206 } ) ;
207207
@@ -231,18 +231,6 @@ class ChunkedUpload extends Base {
231231 } ) ;
232232 } ;
233233
234- /**
235- * Handles upload error for a chunk.
236- *
237- * @private
238- * @param {Error } error - Progress error
239- * @return {void }
240- */
241- handleChunkError = ( error : Error ) => {
242- this . cancel ( ) ;
243- this . errorCallback ( error ) ;
244- } ;
245-
246234 /**
247235 * Handles upload progress event for a chunk.
248236 *
@@ -251,7 +239,7 @@ class ChunkedUpload extends Base {
251239 * @param {ProgressEvent } event - Progress event
252240 * @return {void }
253241 */
254- handleChunkProgress = ( chunk : Chunk , event : ProgressEvent ) => {
242+ handleChunkProgress = ( chunk : Chunk , event : ProgressEvent ) : void => {
255243 if ( ! event . lengthComputable ) {
256244 return ;
257245 }
@@ -306,24 +294,54 @@ class ChunkedUpload extends Base {
306294 Digest : `SHA=${ digest } `
307295 } ;
308296
309- this . xhr . post ( this . getUploadSessionUrl ( this . sessionId , 'commit' ) , postData , headers ) . then ( ( response ) => {
310- // Retry after a delay since server is still processing chunks
311- if ( response . status === 202 ) {
312- this . finished = false ;
313- const retryAfterSec = parseInt ( response . headers . get ( 'Retry-After' ) , 10 ) ;
314-
315- if ( isNaN ( retryAfterSec ) ) {
316- setTimeout ( ( ) => this . commitFile ( ) , DEFAULT_RETRY_COMMIT_DELAY_MS ) ;
317- } else {
318- setTimeout ( ( ) => this . commitFile ( ) , retryAfterSec * 1000 ) ;
319- }
320- } else {
321- this . successCallback ( response ) ;
322- }
323- } ) ;
297+ this . xhr
298+ . post ( this . getUploadSessionUrl ( this . sessionId , 'commit' ) , postData , headers )
299+ . then ( this . handleCommitSuccess )
300+ . catch ( this . handleUploadError ) ;
324301 } ) ;
325302 }
326303
304+ /**
305+ * Handles a successful commit file response.
306+ *
307+ * @param {Response } response - Fetch response object
308+ * @return {void }
309+ */
310+ handleCommitSuccess = ( {
311+ entries,
312+ headers,
313+ status
314+ } : {
315+ entries : BoxItem [ ] ,
316+ headers : Headers ,
317+ status : number
318+ } ) : void => {
319+ // Retry after a delay since server is still processing chunks
320+ if ( status === 202 ) {
321+ this . finished = false ;
322+ const retryAfterSec = parseInt ( headers . get ( 'Retry-After' ) , 10 ) ;
323+
324+ if ( isNaN ( retryAfterSec ) ) {
325+ setTimeout ( ( ) => this . commitFile ( ) , DEFAULT_RETRY_COMMIT_DELAY_MS ) ;
326+ } else {
327+ setTimeout ( ( ) => this . commitFile ( ) , retryAfterSec * 1000 ) ;
328+ }
329+ } else if ( entries ) {
330+ this . successCallback ( entries ) ;
331+ }
332+ } ;
333+
334+ /**
335+ * Handles an upload error. Cancels the pending upload and executes error callback.
336+ *
337+ * @param {Error } error - Error
338+ * @return {void }
339+ */
340+ handleUploadError = ( error : Error ) : void => {
341+ this . cancel ( ) ;
342+ this . errorCallback ( error ) ;
343+ } ;
344+
327345 /**
328346 * Updates SHA1 digest, ensuring that parts are added in the right order.
329347 *
0 commit comments