Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to async read chunk data , fix leak data error for react native #5522

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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