From c45d4f303f5371487649072e9134f49ddb85acff Mon Sep 17 00:00:00 2001 From: Andriy Horak Date: Fri, 22 Nov 2024 00:24:01 +0200 Subject: [PATCH] ability to async read chunk data , fix leak data error for react native (#TODO) --- packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts | 4 ++-- packages/@uppy/aws-s3/src/MultipartUploader.ts | 10 ++++++---- packages/@uppy/aws-s3/src/index.ts | 7 ++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts b/packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts index c015633ad9..d349fc6823 100644 --- a/packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts +++ b/packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts @@ -253,7 +253,7 @@ export class HTTPCommunicationQueue { }).abortOn(signal) let body: FormData | Blob - const data = chunk.getData() + const data = await chunk.getData() if (method.toUpperCase() === 'POST') { const formData = new FormData() Object.entries(fields!).forEach(([key, value]) => @@ -387,7 +387,7 @@ export class HTTPCommunicationQueue { for (;;) { throwIfAborted(signal) - const chunkData = chunk.getData() + const chunkData = await chunk.getData() const { onProgress, onComplete } = chunk let signature diff --git a/packages/@uppy/aws-s3/src/MultipartUploader.ts b/packages/@uppy/aws-s3/src/MultipartUploader.ts index 6f5397a7fd..5dad800c0e 100644 --- a/packages/@uppy/aws-s3/src/MultipartUploader.ts +++ b/packages/@uppy/aws-s3/src/MultipartUploader.ts @@ -7,6 +7,7 @@ const MB = 1024 * 1024 interface MultipartUploaderOptions { getChunkSize?: (file: { size: number }) => number + getChunkData?: (start: number, end: number, opts: { chunkSize: number }) => Promise onProgress?: (bytesUploaded: number, bytesTotal: number) => void onPartComplete?: (part: { PartNumber: number; ETag: string }) => void shouldUseMultipart?: boolean | ((file: UppyFile) => boolean) @@ -33,7 +34,7 @@ const defaultOptions = { } satisfies Partial> export interface Chunk { - getData: () => Blob + getData: () => Promise onProgress: (ev: ProgressEvent) => void onComplete: (etag: string) => void shouldUseMultipart: boolean @@ -146,9 +147,10 @@ class MultipartUploader { const end = Math.min(fileSize, offset + chunkSize) // Defer data fetching/slicing until we actually need the data, because it's slow if we have a lot of files - const getData = () => { + const getData = async () => { const i2 = offset - return this.#data.slice(i2, end) + return this.options.getChunkData ? this.options.getChunkData(i2, end, { chunkSize }) : + this.#data.slice(i2, end) } this.#chunks[j] = { @@ -171,7 +173,7 @@ class MultipartUploader { } else { this.#chunks = [ { - getData: () => this.#data, + getData: async () => this.#data, onProgress: this.#onPartProgress(0), onComplete: this.#onPartComplete(0), shouldUseMultipart, diff --git a/packages/@uppy/aws-s3/src/index.ts b/packages/@uppy/aws-s3/src/index.ts index 153a7399f1..e67537c83f 100644 --- a/packages/@uppy/aws-s3/src/index.ts +++ b/packages/@uppy/aws-s3/src/index.ts @@ -204,6 +204,7 @@ type AWSS3MultipartWithoutCompanionMandatorySignPart< } type AWSS3MultipartWithoutCompanionMandatory = { getChunkSize?: (file: { size: number }) => number + getChunkData?: (start: number, end: number, opts: { chunkSize: number }) => Promise createMultipartUpload: (file: UppyFile) => MaybePromise listParts: ( file: UppyFile, @@ -301,6 +302,7 @@ export default class AwsS3Multipart< Pick< AWSS3MultipartWithoutCompanionMandatory, | 'getChunkSize' + | 'getChunkData' | 'createMultipartUpload' | 'listParts' | 'abortMultipartUpload' @@ -867,7 +869,10 @@ export default class AwsS3Multipart< this.opts.getChunkSize ? this.opts.getChunkSize.bind(this) : undefined, - + getChunkData: + this.opts.getChunkData ? + this.opts.getChunkData.bind(this) + : undefined, onProgress, onError, onSuccess,