Skip to content

Commit

Permalink
ability to async read chunk data , fix leak data error for react nati…
Browse files Browse the repository at this point in the history
…ve (#TODO)
  • Loading branch information
Flexoman committed Nov 30, 2024
1 parent 44a378a commit c45d4f3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/@uppy/aws-s3/src/HTTPCommunicationQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export class HTTPCommunicationQueue<M extends Meta, B extends Body> {
}).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]) =>
Expand Down Expand Up @@ -387,7 +387,7 @@ export class HTTPCommunicationQueue<M extends Meta, B extends Body> {

for (;;) {
throwIfAborted(signal)
const chunkData = chunk.getData()
const chunkData = await chunk.getData()
const { onProgress, onComplete } = chunk
let signature

Expand Down
10 changes: 6 additions & 4 deletions packages/@uppy/aws-s3/src/MultipartUploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const MB = 1024 * 1024

interface MultipartUploaderOptions<M extends Meta, B extends Body> {
getChunkSize?: (file: { size: number }) => number
getChunkData?: (start: number, end: number, opts: { chunkSize: number }) => Promise<Blob>
onProgress?: (bytesUploaded: number, bytesTotal: number) => void
onPartComplete?: (part: { PartNumber: number; ETag: string }) => void
shouldUseMultipart?: boolean | ((file: UppyFile<M, B>) => boolean)
Expand All @@ -33,7 +34,7 @@ const defaultOptions = {
} satisfies Partial<MultipartUploaderOptions<any, any>>

export interface Chunk {
getData: () => Blob
getData: () => Promise<Blob>
onProgress: (ev: ProgressEvent) => void
onComplete: (etag: string) => void
shouldUseMultipart: boolean
Expand Down Expand Up @@ -146,9 +147,10 @@ class MultipartUploader<M extends Meta, B extends Body> {
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] = {
Expand All @@ -171,7 +173,7 @@ class MultipartUploader<M extends Meta, B extends Body> {
} else {
this.#chunks = [
{
getData: () => this.#data,
getData: async () => this.#data,
onProgress: this.#onPartProgress(0),
onComplete: this.#onPartComplete(0),
shouldUseMultipart,
Expand Down
7 changes: 6 additions & 1 deletion packages/@uppy/aws-s3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ type AWSS3MultipartWithoutCompanionMandatorySignPart<
}
type AWSS3MultipartWithoutCompanionMandatory<M extends Meta, B extends Body> = {
getChunkSize?: (file: { size: number }) => number
getChunkData?: (start: number, end: number, opts: { chunkSize: number }) => Promise<Blob>
createMultipartUpload: (file: UppyFile<M, B>) => MaybePromise<UploadResult>
listParts: (
file: UppyFile<M, B>,
Expand Down Expand Up @@ -301,6 +302,7 @@ export default class AwsS3Multipart<
Pick<
AWSS3MultipartWithoutCompanionMandatory<M, B>,
| 'getChunkSize'
| 'getChunkData'
| 'createMultipartUpload'
| 'listParts'
| 'abortMultipartUpload'
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c45d4f3

Please sign in to comment.