Skip to content

Commit f997449

Browse files
committed
Experiment with integrity checks
Change-type: minor
1 parent 7ec249f commit f997449

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

lib/index.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
HeadObjectCommand,
1111
AbortMultipartUploadCommand,
1212
type StorageClass,
13+
type CompleteMultipartUploadCommandInput,
1314
} from '@aws-sdk/client-s3';
1415
import { Upload } from '@aws-sdk/lib-storage';
1516
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
@@ -36,6 +37,9 @@ const normalizeHref = (href: string) => {
3637
return href.split('?')[0];
3738
};
3839

40+
type ChecksumParamName =
41+
`Checksum${webResourceHandler.SupportedChecksumAlgorithm}`;
42+
3943
export class S3Handler implements webResourceHandler.WebResourceHandler {
4044
private readonly config: S3ClientConfig;
4145
private readonly bucket: string;
@@ -95,6 +99,11 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
9599
ContentDisposition: `inline; filename="${resource.originalname}"`,
96100
StorageClass: this.storageClass,
97101
};
102+
103+
if (resource.checksumAlgorithm != null) {
104+
const headerValue = this.getS3ChecksumHeader(resource.checksumAlgorithm);
105+
params[headerValue] = resource.checksum;
106+
}
98107
const upload = new Upload({ client: this.client, params });
99108

100109
upload.on('httpUploadProgress', async (ev) => {
@@ -175,15 +184,22 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
175184
uploadId,
176185
filename,
177186
providerCommitData,
187+
checksumAlgorithm,
188+
checksum,
178189
}: webResourceHandler.CommitMultipartUploadPayload): Promise<WebResource> => {
179-
await this.client.send(
180-
new CompleteMultipartUploadCommand({
181-
Bucket: this.bucket,
182-
Key: fileKey,
183-
UploadId: uploadId,
184-
MultipartUpload: providerCommitData,
185-
}),
186-
);
190+
const params: CompleteMultipartUploadCommandInput = {
191+
Bucket: this.bucket,
192+
Key: fileKey,
193+
UploadId: uploadId,
194+
MultipartUpload: providerCommitData,
195+
};
196+
197+
if (checksumAlgorithm != null) {
198+
const headerValue = this.getS3ChecksumHeader(checksumAlgorithm);
199+
params[headerValue] = checksum;
200+
}
201+
202+
await this.client.send(new CompleteMultipartUploadCommand(params));
187203

188204
const headResult = await this.client.send(
189205
new HeadObjectCommand({
@@ -244,6 +260,12 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
244260
return `${fieldName}_${randomUUID()}`;
245261
}
246262

263+
private getS3ChecksumHeader(
264+
algorithm: webResourceHandler.SupportedChecksumAlgorithm,
265+
): ChecksumParamName {
266+
return `Checksum${algorithm}`;
267+
}
268+
247269
private async getUploadParts(
248270
fileKey: string,
249271
uploadId: string,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"license": "Apache-2.0",
2020
"devDependencies": {
2121
"@balena/lint": "^9.1.3",
22-
"@balena/pinejs": "^21.3.0",
22+
"@balena/pinejs": "21.6.2-build-add-integrity-check-to-webresources-0e71fb0fd93cfe4380cc23c4e8302debda126cbf-1",
2323
"@balena/sbvr-types": "^9.1.0",
2424
"@types/chai": "^5.0.1",
2525
"@types/chai-as-promised": "^8.0.1",
@@ -42,7 +42,7 @@
4242
"memoizee": "^0.4.17"
4343
},
4444
"peerDependencies": {
45-
"@balena/pinejs": "^21.3.0"
45+
"@balena/pinejs": "21.6.2-build-add-integrity-check-to-webresources-0e71fb0fd93cfe4380cc23c4e8302debda126cbf-1"
4646
},
4747
"versionist": {
4848
"publishedAt": "2025-05-26T01:49:22.588Z"

0 commit comments

Comments
 (0)