Skip to content

Commit 94c1e1b

Browse files
committed
feat: addresses change requests for range and part validation
1 parent 879000e commit 94c1e1b

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

lib/lib-storage/src/s3-transfer-manager/S3TransferManager.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@ export class S3TransferManager implements IS3TransferManager {
392392
if (initialPart.PartsCount! > 1) {
393393
for (let part = 2; part <= initialPart.PartsCount!; part++) {
394394
this.checkAborted(transferOptions);
395-
396395
const getObjectRequest = {
397396
...request,
398397
PartNumber: part,
@@ -401,7 +400,7 @@ export class S3TransferManager implements IS3TransferManager {
401400
const getObject = this.s3ClientInstance
402401
.send(new GetObjectCommand(getObjectRequest), transferOptions)
403402
.then((response) => {
404-
this.validatePartDownload(part, response.ContentRange, partSize ?? 0);
403+
this.validatePartDownload(response.ContentRange, part, partSize ?? 0);
405404
if (response.Body && typeof (response.Body as any).getReader === "function") {
406405
const reader = (response.Body as any).getReader();
407406
(response.Body as any).getReader = function () {
@@ -415,12 +414,11 @@ export class S3TransferManager implements IS3TransferManager {
415414
requests.push(getObjectRequest);
416415
partCount++;
417416
}
418-
}
419-
420-
if (partCount !== initialPart.PartsCount) {
421-
throw new Error(
422-
`The number of parts downloaded (${partCount}) does not match the expected number (${initialPart.PartsCount})`
423-
);
417+
if (partCount !== initialPart.PartsCount) {
418+
throw new Error(
419+
`The number of parts downloaded (${partCount}) does not match the expected number (${initialPart.PartsCount})`
420+
);
421+
}
424422
}
425423
} else {
426424
this.checkAborted(transferOptions);
@@ -630,18 +628,20 @@ export class S3TransferManager implements IS3TransferManager {
630628
}
631629
}
632630

633-
private validatePartDownload(partNumber: number, contentRange: string | undefined, partSize: number) {
634-
if (!contentRange) return;
631+
private validatePartDownload(contentRange: string | undefined, partNumber: number, partSize: number) {
632+
if (!contentRange) {
633+
throw new Error(`Missing ContentRange for part ${partNumber}.`);
634+
}
635635

636636
const match = contentRange.match(/bytes (\d+)-(\d+)\/(\d+)/);
637637
if (!match) throw new Error(`Invalid ContentRange format: ${contentRange}`);
638638

639639
const start = Number.parseInt(match[1]);
640640
const end = Number.parseInt(match[2]);
641-
const total = Number.parseInt(match[3]);
641+
const total = Number.parseInt(match[3]) - 1;
642642

643643
const expectedStart = (partNumber - 1) * partSize;
644-
const expectedEnd = Math.min(expectedStart + partSize - 1, total - 1);
644+
const expectedEnd = Math.min(expectedStart + partSize - 1, total);
645645

646646
if (start !== expectedStart) {
647647
throw new Error(`Expected part ${partNumber} to start at ${expectedStart} but got ${start}`);
@@ -653,14 +653,16 @@ export class S3TransferManager implements IS3TransferManager {
653653
}
654654

655655
private validateRangeDownload(requestRange: string, responseRange: string | undefined) {
656-
if (!responseRange) return;
656+
if (!responseRange) {
657+
throw new Error(`Missing ContentRange for range ${requestRange}.`);
658+
}
657659

658660
const match = responseRange.match(/bytes (\d+)-(\d+)\/(\d+)/);
659661
if (!match) throw new Error(`Invalid ContentRange format: ${responseRange}`);
660662

661663
const start = Number.parseInt(match[1]);
662664
const end = Number.parseInt(match[2]);
663-
const total = Number.parseInt(match[3]);
665+
const total = Number.parseInt(match[3]) - 1;
664666

665667
const rangeMatch = requestRange.match(/bytes=(\d+)-(\d+)/);
666668
if (!rangeMatch) throw new Error(`Invalid Range format: ${requestRange}`);
@@ -672,10 +674,14 @@ export class S3TransferManager implements IS3TransferManager {
672674
throw new Error(`Expected range to start at ${expectedStart} but got ${start}`);
673675
}
674676

675-
const isFinalPart = end + 1 === total;
677+
if (end === expectedEnd) {
678+
return;
679+
}
676680

677-
if (end !== expectedEnd && !(isFinalPart && end < expectedEnd)) {
678-
throw new Error(`Expected range to end at ${expectedEnd} but got ${end}`);
681+
if (end === total) {
682+
return;
679683
}
684+
685+
throw new Error(`Expected range to end at ${expectedEnd} but got ${end}`);
680686
}
681687
}

0 commit comments

Comments
 (0)