Skip to content

Commit

Permalink
fix: 413 Request Entity Too Large handling (#375)
Browse files Browse the repository at this point in the history
* fix: `413 Request Entity Too Large` handling

Some proxies may return a 413 error if dynamic chunk size is too large

* test: add 413 response test
  • Loading branch information
kukhariev committed May 10, 2022
1 parent 75d1fba commit e6b62d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/uploadx/lib/uploader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Ajax, AjaxRequestConfig } from './ajax';
import { DynamicChunk } from './dynamic-chunk';
import { UploaderOptions } from './interfaces';
import { Uploader } from './uploader';

Expand Down Expand Up @@ -27,6 +28,11 @@ const file = getFile();

const snip = { file, size: 10, name: 'filename.mp4' };

const chunkInit = {
maxSize: DynamicChunk.maxSize,
size: DynamicChunk.size
};

let uploader: MockUploader;

export class MockUploader extends Uploader {
Expand Down Expand Up @@ -81,10 +87,14 @@ describe('Uploader', () => {
});

describe('chunkSize', () => {
afterEach(() => {
DynamicChunk.maxSize = chunkInit.maxSize;
DynamicChunk.size = chunkInit.size;
});
it('should use adaptive chunkSize if not specified', async () => {
uploader = new MockUploader(getFile(), {});
(uploader as any).getChunk();
expect(uploader.chunkSize).toEqual(4096 * 256);
expect(uploader.chunkSize).toEqual(chunkInit.size);
});
it('should set fixed chunkSize', async () => {
uploader = new MockUploader(file, { chunkSize: 4_194_304 });
Expand All @@ -96,13 +106,19 @@ describe('Uploader', () => {
(uploader as any).getChunk();
expect(uploader.chunkSize).toEqual(10);
});
it('should limit on 413', async () => {
uploader = new MockUploader(file, {});
uploader.responseStatus = 413;
(uploader as any).getChunk();
expect(uploader.chunkSize).toEqual(chunkInit.size / 2);
expect(DynamicChunk.maxSize).toEqual(chunkInit.size / 2);
});
});

describe('upload()', () => {
beforeEach(() => {
uploader = new MockUploader(file, { retryConfig: { maxAttempts: 3 }, token: 'token' });
});

it('should retry on 0', async () => {
const retry = spyOn<any>(uploader.retry, 'wait');
serverStatus = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/uploadx/lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ export abstract class Uploader implements UploadState {
}

protected getChunk(): { start: number; end: number; body: Blob } {
if (this.responseStatus === 413) {
DynamicChunk.maxSize = DynamicChunk.size = Math.floor(DynamicChunk.size / 2);
}
this.chunkSize =
this.options.chunkSize === 0 ? this.size : this.options.chunkSize || DynamicChunk.size;
const start = this.offset || 0;
Expand Down

0 comments on commit e6b62d3

Please sign in to comment.