Skip to content

Commit ef409d1

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

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

lib/index.ts

Lines changed: 37 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,16 @@ const normalizeHref = (href: string) => {
3637
return href.split('?')[0];
3738
};
3839

40+
export const supportedChecksumAlgorithms = [
41+
'CRC32',
42+
'CRC32C',
43+
'CRC64NVME',
44+
] as const;
45+
export type SupportedChecksumAlgorithm =
46+
(typeof supportedChecksumAlgorithms)[number];
47+
48+
type ChecksumParamName = `Checksum${SupportedChecksumAlgorithm}`;
49+
3950
export class S3Handler implements webResourceHandler.WebResourceHandler {
4051
private readonly config: S3ClientConfig;
4152
private readonly bucket: string;
@@ -95,6 +106,11 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
95106
ContentDisposition: `inline; filename="${resource.originalname}"`,
96107
StorageClass: this.storageClass,
97108
};
109+
110+
if (resource.checksumAlgorithm != null) {
111+
const headerValue = this.getS3ChecksumHeader(resource.checksumAlgorithm);
112+
params[headerValue] = resource.checksum;
113+
}
98114
const upload = new Upload({ client: this.client, params });
99115

100116
upload.on('httpUploadProgress', async (ev) => {
@@ -175,15 +191,22 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
175191
uploadId,
176192
filename,
177193
providerCommitData,
194+
checksumAlgorithm,
195+
checksum,
178196
}: 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-
);
197+
const params: CompleteMultipartUploadCommandInput = {
198+
Bucket: this.bucket,
199+
Key: fileKey,
200+
UploadId: uploadId,
201+
MultipartUpload: providerCommitData,
202+
};
203+
204+
if (checksumAlgorithm != null) {
205+
const headerValue = this.getS3ChecksumHeader(checksumAlgorithm);
206+
params[headerValue] = checksum;
207+
}
208+
209+
await this.client.send(new CompleteMultipartUploadCommand(params));
187210

188211
const headResult = await this.client.send(
189212
new HeadObjectCommand({
@@ -244,6 +267,12 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
244267
return `${fieldName}_${randomUUID()}`;
245268
}
246269

270+
private getS3ChecksumHeader(
271+
algorithm: SupportedChecksumAlgorithm,
272+
): ChecksumParamName {
273+
return `Checksum${algorithm}`;
274+
}
275+
247276
private async getUploadParts(
248277
fileKey: string,
249278
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)