Skip to content

Commit c602275

Browse files
fix(upload-core): auto retry failed uploads (#73)
Fixes #71
1 parent 01ce510 commit c602275

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

packages/upload-core/src/tests/upload.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ const optionsWithMeta = {
2626
clientId: 'c',
2727
};
2828

29+
const optionsWithRetry = {
30+
bucketId: 'a',
31+
customerId: 'b',
32+
clientId: 'c',
33+
retryDelays: [1000, 2000],
34+
};
35+
2936
describe('upload.core', () => {
3037
it('should be defined', () => {
3138
expect(Upload).toBeTruthy();
@@ -43,6 +50,13 @@ describe('upload.core', () => {
4350
}).toThrow('[options.bucketId] must be defined');
4451
});
4552

53+
it('should allow override to defaults', () => {
54+
const file = Buffer.from('hello world'.split(''));
55+
file.name = 'fileName.png';
56+
const upload = new Upload(file, optionsWithRetry);
57+
expect(upload.options.retryDelays[0]).toBe(optionsWithRetry.retryDelays[0]);
58+
});
59+
4660
it('should allow single file as constructor argument', () => {
4761
const file = Buffer.from('hello world'.split(''));
4862
file.name = 'fileName.png';

packages/upload-core/src/upload.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const defaultOptions = {
1515
endpoint: '/ms/api/availity/internal/core/vault/upload/v1/resumable',
1616
chunkSize: 3e6, // 3MB
1717
removeFingerprintOnSuccess: true,
18+
retryDelays: [0, 1000, 3000, 5000],
19+
stripFileNamePathSegments: true,
1820
fingerprint(file, options = {}) {
1921
const attributes = [file.name, file.type, file.size, file.lastModified];
2022
let attributesKey = 'tus-';
@@ -58,7 +60,7 @@ class Upload {
5860
}
5961

6062
this.file = file;
61-
this.options = Object.assign(options, defaultOptions);
63+
this.options = Object.assign({}, defaultOptions, options);
6264
this.percentage = 0;
6365
this.onError = [];
6466
this.onSuccess = [];
@@ -182,6 +184,7 @@ class Upload {
182184
resume: true,
183185
endpoint: `${this.options.endpoint}/${this.options.bucketId}/`,
184186
chunkSize: this.options.chunkSize,
187+
retryDelays: this.options.retryDelays,
185188
removeFingerprintOnSuccess: this.options.removeFingerprintOnSuccess,
186189
fingerprint: this.options.fingerprint,
187190
metadata,
@@ -292,7 +295,7 @@ class Upload {
292295
}
293296

294297
trimFileName(fileName) {
295-
if (this.options.stripFileNamePathSegments !== false) {
298+
if (this.options.stripFileNamePathSegments) {
296299
fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
297300
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
298301
}

0 commit comments

Comments
 (0)