Skip to content

Commit

Permalink
feat: option to keep partial chunks (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Aug 4, 2022
1 parent 0684cea commit 555b80b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class AppHomeComponent {
- `maxDelay` Maximum retry interval. Default value: `50_000`
- `onBusyDelay` Delay used between retries for non-error responses with missing range/offset. Default value: `1000`
- `timeout` Time interval after which unfinished requests must be retried
- `keepPartial` Determines whether partial chunks should be kept
- `token` Authorization token as a `string` or function returning a `string` or `Promise<string>`
Expand Down
8 changes: 7 additions & 1 deletion src/uploadx/lib/retry-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum ErrorType {
}

export type ShouldRetryFunction = (code: number, attempts: number) => boolean;
export type KeepPartialFunction = (code: number) => boolean;

export interface RetryConfig {
/** Maximum number of retry attempts */
Expand All @@ -26,6 +27,8 @@ export interface RetryConfig {
onBusyDelay?: number;
/** Time interval after which hanged requests must be retried */
timeout?: number;
/** Determines whether partial chunks should be kept */
keepPartial?: boolean | KeepPartialFunction;
}

const defaultRetryConfig: Required<RetryConfig> = {
Expand All @@ -39,7 +42,10 @@ const defaultRetryConfig: Required<RetryConfig> = {
minDelay: 500,
maxDelay: 50000,
onBusyDelay: 1000,
timeout: 0
timeout: 0,
keepPartial(statusCode): boolean {
return statusCode >= 400;
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/uploadx/lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export abstract class Uploader implements UploadState {
this._token = '';
break;
default:
if (this.responseStatus >= 400 || this.chunkSize > DynamicChunk.size) {
if (unfunc(this.retry.config.keepPartial, this.responseStatus)) {
this.offset = undefined;
}
this.status = 'retry';
Expand Down

0 comments on commit 555b80b

Please sign in to comment.