Skip to content

Commit

Permalink
fix: maxChunkSize options (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev authored Jun 15, 2021
1 parent f8c809c commit 7d19e9f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/uploadx/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export interface UploaderOptions extends UploadItem {
* If not specified, the optimal size will be automatically adjusted based on the network speed.
*/
chunkSize?: number;
/** Adaptive chunk size limit */
maxChunkSize?: number;
withCredentials?: boolean;
/**
* Function called before every request
Expand Down
7 changes: 4 additions & 3 deletions src/uploadx/lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export abstract class Uploader implements UploadState {
/** Upload endpoint */
endpoint = '/upload';
/** Chunk size in bytes */
chunkSize: number;
chunkSize?: number;
/** Auth token/tokenGetter */
token: UploadxControlEvent['token'];
/** Byte offset within the whole file */
Expand Down Expand Up @@ -104,7 +104,7 @@ export abstract class Uploader implements UploadState {
lastModified:
file.lastModified || (file as File & { lastModifiedDate: Date }).lastModifiedDate.getTime()
};
this.chunkSize = options.chunkSize || this.size;
options.maxChunkSize && (DynamicChunk.maxSize = options.maxChunkSize);
this._prerequest = options.prerequest || (req => req);
this._authorize = options.authorize || (req => req);
this.configure(options);
Expand Down Expand Up @@ -244,7 +244,8 @@ export abstract class Uploader implements UploadState {
}

protected getChunk(): { start: number; end: number; body: Blob } {
this.chunkSize = isNumber(this.options.chunkSize) ? this.chunkSize : DynamicChunk.size;
this.chunkSize =
this.options.chunkSize === 0 ? this.size : this.options.chunkSize || DynamicChunk.size;
const start = this.offset || 0;
const end = Math.min(start + this.chunkSize, this.size);
const body = this.file.slice(this.offset, end);
Expand Down

0 comments on commit 7d19e9f

Please sign in to comment.