Skip to content

Commit

Permalink
@tus/s3-store: Fix incorrect part retreival procedure (#507)
Browse files Browse the repository at this point in the history
* @tus/s3-store: Fix incorrect part retreival procedure

* change that function always returns an array
  • Loading branch information
mitjap authored Nov 1, 2023
1 parent 9149b84 commit 7ae0bf7
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions packages/s3-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,38 +378,26 @@ export class S3Store extends DataStore {
private async retrieveParts(
id: string,
partNumberMarker?: string
): Promise<Array<AWS.Part> | undefined> {
): Promise<Array<AWS.Part>> {
const params: AWS.ListPartsCommandInput = {
Bucket: this.bucket,
Key: id,
UploadId: this.cache.get(id)?.['upload-id'],
}
if (partNumberMarker) {
params.PartNumberMarker = partNumberMarker
PartNumberMarker: partNumberMarker,
}

const data = await this.client.listParts(params)

// INFO: NextPartNumberMarker should be undefined when there are no more parts to retrieve,
// instead it keeps giving `0` so to prevent an infinite loop we check the number.
if (data.NextPartNumberMarker && Number(data.NextPartNumberMarker) > 0) {
return this.retrieveParts(id, data.NextPartNumberMarker).then((parts) => {
if (parts && data.Parts) {
return [...data.Parts, ...parts]
}
return data.Parts
})
}
let parts = data.Parts ?? []

const parts = data.Parts
if (data.IsTruncated) {
const rest = await this.retrieveParts(id, data.NextPartNumberMarker)
parts = [...parts, ...rest]
}

if (parts && !partNumberMarker) {
return (
parts
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.sort((a, b) => a.PartNumber! - b.PartNumber!)
.filter((value, index) => value.PartNumber === index + 1)
)
if (!partNumberMarker) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
parts.sort((a, b) => a.PartNumber! - b.PartNumber!)
}

return parts
Expand Down Expand Up @@ -490,7 +478,8 @@ export class S3Store extends DataStore {
// Metadata request needs to happen first
const metadata = await this.getMetadata(id)
const parts = await this.retrieveParts(id)
const partNumber = parts?.length ?? 0
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const partNumber: number = parts.length > 0 ? parts[parts.length - 1].PartNumber! : 0
const nextPartNumber = partNumber + 1

const bytesUploaded = await this.processUpload(
Expand All @@ -505,7 +494,7 @@ export class S3Store extends DataStore {
if (metadata.file.size === newOffset) {
try {
const parts = await this.retrieveParts(id)
await this.finishMultipartUpload(metadata, parts as Array<AWS.Part>)
await this.finishMultipartUpload(metadata, parts)
this.clearCache(id)
} catch (error) {
log(`[${id}] failed to finish upload`, error)
Expand Down

0 comments on commit 7ae0bf7

Please sign in to comment.