Skip to content

Commit

Permalink
fix: do not emit progress on metadata send
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Apr 22, 2022
1 parent 14719e6 commit 96cb2b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/uploadx/lib/uploader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ describe('Uploader', () => {
});
});

describe('request', () => {
serverStatus = 200;

it('should report progress if content is uploading', async () => {
uploader = new MockUploader(file, {});
uploader.offset = 0;
const request = spyOn<any>(uploader.ajax, 'request').and.callThrough();
await uploader.request({ method: 'POST', body: new FormData() });
expect((request.calls.mostRecent().args[0] as any).onUploadProgress).toBeTruthy();
});

it('should not report progress if offset is undefined', async () => {
uploader = new MockUploader(file, {});
uploader.offset = undefined;
const request = spyOn<any>(uploader.ajax, 'request').and.callThrough();
await uploader.request({ method: 'POST', body: new FormData() });
expect((request.calls.mostRecent().args[0] as any).onUploadProgress).toBeUndefined();
});
});

describe('prerequest', () => {
const common = { common: 'true' };
const auth = { auth: 'token' };
Expand Down
5 changes: 3 additions & 2 deletions src/uploadx/lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ export abstract class Uploader implements UploadState {
validateStatus: () => true,
timeout: this.retry.config.timeout
};
if (body && typeof body !== 'string') {
//todo: more reliable detection
if (isNumber(this.offset) && body && typeof body === 'object') {
ajaxRequestConfig.onUploadProgress = this.onProgress();
}
const response = await this.ajax.request(ajaxRequestConfig);
Expand Down Expand Up @@ -277,7 +278,7 @@ export abstract class Uploader implements UploadState {
DynamicChunk.scale(this.speed);
if (!throttle) {
throttle = setTimeout(() => (throttle = undefined), 500);
const uploaded = (this.offset as number) + loaded;
const uploaded = (this.offset || 0) + loaded;
this.progress = +((uploaded / this.size) * 100).toFixed(2);
this.remaining = ~~((this.size - uploaded) / this.speed);
this.stateChange(this);
Expand Down

0 comments on commit 96cb2b6

Please sign in to comment.