Skip to content

Commit

Permalink
@uppy/aws-s3-multipart: add types to internal fields (#4535)
Browse files Browse the repository at this point in the history
Co-authored-by: Merlijn Vos <merlijn@soverin.net>
  • Loading branch information
aduh95 and Murderlon authored Jun 28, 2023
1 parent c9e1dfb commit 4ee5184
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
27 changes: 23 additions & 4 deletions packages/@uppy/aws-s3-multipart/src/MultipartUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,41 @@ function ensureInt (value) {

export const pausingUploadReason = Symbol('pausing upload, not an actual error')

/**
* A MultipartUploader instance is used per file upload to determine whether a
* upload should be done as multipart or as a regular S3 upload
* (based on the user-provided `shouldUseMultipart` option value) and to manage
* the chunk splitting.
*/
class MultipartUploader {
#abortController = new AbortController()

/** @type {import("../types/chunk").Chunk[]} */
#chunks

/** @type {{ uploaded: number, etag?: string, done?: boolean }[]} */
#chunkState

/**
* The (un-chunked) data to upload.
*
* @type {Blob}
*/
#data

/** @type {import("@uppy/core").UppyFile} */
#file

#uploadPromise
/** @type {boolean} */
#uploadHasStarted = false

/** @type {(err?: Error | any) => void} */
#onError

/** @type {() => void} */
#onSuccess

/** @type {typeof import('../types/index').AwsS3MultipartOptions["shouldUseMultipart"]} */
#shouldUseMultipart

#onReject = (err) => (err?.cause === pausingUploadReason ? null : this.#onError(err))
Expand Down Expand Up @@ -119,13 +137,14 @@ class MultipartUploader {
}

#createUpload () {
this.#uploadPromise = this
this
.options.companionComm.uploadFile(this.#file, this.#chunks, this.#abortController.signal)
.then(this.#onSuccess, this.#onReject)
this.#uploadHasStarted = true
}

#resumeUpload () {
this.#uploadPromise = this
this
.options.companionComm.resumeUploadFile(this.#file, this.#chunks, this.#abortController.signal)
.then(this.#onSuccess, this.#onReject)
}
Expand Down Expand Up @@ -158,7 +177,7 @@ class MultipartUploader {
}

start () {
if (this.#uploadPromise) {
if (this.#uploadHasStarted) {
if (!this.#abortController.signal.aborted) this.#abortController.abort(pausingUploadReason)
this.#abortController = new AbortController()
this.#resumeUpload()
Expand Down
14 changes: 14 additions & 0 deletions packages/@uppy/aws-s3-multipart/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ class HTTPCommunicationQueue {
}).abortOn(signal)
}

/**
* @param {import("@uppy/core").UppyFile} file
* @param {import("../types/chunk").Chunk[]} chunks
* @param {AbortSignal} signal
* @returns {Promise<void>}
*/
async uploadFile (file, chunks, signal) {
throwIfAborted(signal)
if (chunks.length === 1 && !chunks[0].shouldUseMultipart) {
Expand Down Expand Up @@ -281,6 +287,14 @@ class HTTPCommunicationQueue {
return this.#sendCompletionRequest(file, { key, uploadId, parts, signal }).abortOn(signal)
}

/**
*
* @param {import("@uppy/core").UppyFile} file
* @param {number} partNumber
* @param {import("../types/chunk").Chunk} chunk
* @param {AbortSignal} signal
* @returns {Promise<object>}
*/
async uploadChunk (file, partNumber, chunk, signal) {
throwIfAborted(signal)
const { uploadId, key } = await this.getUploadId(file, signal)
Expand Down
6 changes: 6 additions & 0 deletions packages/@uppy/aws-s3-multipart/types/chunk.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Chunk {
getData: () => Blob
onProgress: (ev: ProgressEvent) => void
onComplete: (etag: string) => void
shouldUseMultipart: boolean
}

0 comments on commit 4ee5184

Please sign in to comment.