Skip to content

Commit 5d8903e

Browse files
committed
fix(lib-storage): addressing comments
1 parent 7101875 commit 5d8903e

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

lib/lib-storage/src/Upload.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ describe(Upload.name, () => {
808808
(upload as any).uploadedParts = [{ PartNumber: 1, ETag: "etag1" }];
809809
(upload as any).isMultiPart = true;
810810

811-
await expect(upload.done()).rejects.toThrow("Expected 3 number of parts but uploaded only 1 part");
811+
await expect(upload.done()).rejects.toThrow("Expected 3 part(s) but uploaded 1 part(s).");
812812
});
813813

814814
it("should throw error when part size doesn't match expected size except for laast part", () => {
@@ -859,7 +859,7 @@ describe(Upload.name, () => {
859859

860860
expect(() => {
861861
(upload as any).__validateUploadPart(emptyPart, MOCK_PART_SIZE);
862-
}).toThrow("Content length is missing on the data for part number 1");
862+
}).toThrow("A dataPart was generated without a measurable data chunk size for part number 1");
863863
});
864864

865865
it("should skip validation for single-part uploads", () => {

lib/lib-storage/src/Upload.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class Upload extends EventEmitter {
6666

6767
private uploadedParts: CompletedPart[] = [];
6868
private uploadEnqueuedPartsCount = 0;
69-
private expectedPartsCount = 0;
69+
private expectedPartsCount?: number;
7070
/**
7171
* Last UploadId if the upload was done with MultipartUpload and not PutObject.
7272
*/
@@ -93,8 +93,8 @@ export class Upload extends EventEmitter {
9393
this.bytesUploadedSoFar = 0;
9494
this.abortController = options.abortController ?? new AbortController();
9595

96-
this.partSize = this.__calculatePartSize(this.totalBytes ?? 0, Upload.MIN_PART_SIZE);
97-
this.expectedPartsCount = this.totalBytes ? Math.ceil(this.totalBytes / this.partSize) : 1;
96+
this.partSize = Math.max(Upload.MIN_PART_SIZE, Math.floor((this.totalBytes || 0) / this.MAX_PARTS));
97+
this.expectedPartsCount = this.totalBytes !== undefined ? Math.ceil(this.totalBytes / this.partSize) : undefined;
9898

9999
this.__validateInput();
100100
}
@@ -237,8 +237,6 @@ export class Upload extends EventEmitter {
237237
return;
238238
}
239239

240-
this.__validateUploadPart(dataPart, this.partSize);
241-
242240
// Use put instead of multipart for one chunk uploads.
243241
if (dataPart.partNumber === 1 && dataPart.lastPart) {
244242
return await this.__uploadUsingPut(dataPart);
@@ -287,6 +285,8 @@ export class Upload extends EventEmitter {
287285

288286
this.uploadEnqueuedPartsCount += 1;
289287

288+
this.__validateUploadPart(dataPart);
289+
290290
const partResult = await this.client.send(
291291
new UploadPartCommand({
292292
...this.params,
@@ -369,10 +369,9 @@ export class Upload extends EventEmitter {
369369

370370
let result;
371371
if (this.isMultiPart) {
372-
if (this.totalBytes && this.uploadedParts.length !== this.expectedPartsCount) {
373-
throw new Error(
374-
`Expected ${this.expectedPartsCount} number of parts but uploaded only ${this.uploadedParts.length} parts`
375-
);
372+
const { expectedPartsCount, uploadedParts } = this;
373+
if (expectedPartsCount !== undefined && uploadedParts.length !== expectedPartsCount) {
374+
throw new Error(`Expected ${expectedPartsCount} part(s) but uploaded ${uploadedParts.length} part(s).`);
376375
}
377376

378377
this.uploadedParts.sort((a, b) => a.PartNumber! - b.PartNumber!);
@@ -438,27 +437,24 @@ export class Upload extends EventEmitter {
438437
});
439438
}
440439

441-
private __calculatePartSize(targetPartSizeBytes: number, minPartSize: number): number {
442-
const calculatedPartSize = Math.floor(targetPartSizeBytes / this.MAX_PARTS);
443-
return Math.max(minPartSize, calculatedPartSize);
444-
}
445-
446-
private __validateUploadPart(dataPart: RawDataPart, partSize: number): void {
447-
const actualPartSize = byteLength(dataPart.data) || 0;
440+
private __validateUploadPart(dataPart: RawDataPart): void {
441+
const actualPartSize = byteLength(dataPart.data) || undefined;
448442

449443
// Skip validation for single-part uploads (PUT operations)
450444
if (dataPart.partNumber === 1 && dataPart.lastPart) {
451445
return;
452446
}
453447

454-
if (actualPartSize === 0) {
455-
throw new Error(`Content length is missing on the data for part number ${dataPart.partNumber}`);
448+
if (actualPartSize === undefined) {
449+
throw new Error(
450+
`A dataPart was generated without a measurable data chunk size for part number ${dataPart.partNumber}`
451+
);
456452
}
457453

458454
// Validate part size (last part may be smaller)
459-
if (!dataPart.lastPart && actualPartSize !== partSize) {
455+
if (!dataPart.lastPart && actualPartSize !== this.partSize) {
460456
throw new Error(
461-
`The Part size for part number ${dataPart.partNumber}, size ${actualPartSize} does not match expected size ${partSize}`
457+
`The Part size for part number ${dataPart.partNumber}, size ${actualPartSize} does not match expected size ${this.partSize}`
462458
);
463459
}
464460
}

0 commit comments

Comments
 (0)