Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
HeadObjectCommand,
AbortMultipartUploadCommand,
type StorageClass,
type CompleteMultipartUploadCommandInput,
} from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
Expand All @@ -36,6 +37,9 @@ const normalizeHref = (href: string) => {
return href.split('?')[0];
};

type ChecksumParamName =
`Checksum${webResourceHandler.SupportedChecksumAlgorithm}`;

export class S3Handler implements webResourceHandler.WebResourceHandler {
private readonly config: S3ClientConfig;
private readonly bucket: string;
Expand Down Expand Up @@ -95,6 +99,11 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
ContentDisposition: `inline; filename="${resource.originalname}"`,
StorageClass: this.storageClass,
};

if (resource.checksumAlgorithm != null) {
const headerValue = this.getS3ChecksumHeader(resource.checksumAlgorithm);
params[headerValue] = resource.checksum;
}
const upload = new Upload({ client: this.client, params });

upload.on('httpUploadProgress', async (ev) => {
Expand Down Expand Up @@ -175,15 +184,22 @@ export class S3Handler implements webResourceHandler.WebResourceHandler {
uploadId,
filename,
providerCommitData,
checksumAlgorithm,
checksum,
}: webResourceHandler.CommitMultipartUploadPayload): Promise<WebResource> => {
await this.client.send(
new CompleteMultipartUploadCommand({
Bucket: this.bucket,
Key: fileKey,
UploadId: uploadId,
MultipartUpload: providerCommitData,
}),
);
const params: CompleteMultipartUploadCommandInput = {
Bucket: this.bucket,
Key: fileKey,
UploadId: uploadId,
MultipartUpload: providerCommitData,
};

if (checksumAlgorithm != null) {
const headerValue = this.getS3ChecksumHeader(checksumAlgorithm);
params[headerValue] = checksum;
}

await this.client.send(new CompleteMultipartUploadCommand(params));

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

private getS3ChecksumHeader(
algorithm: webResourceHandler.SupportedChecksumAlgorithm,
): ChecksumParamName {
return `Checksum${algorithm}`;
}

private async getUploadParts(
fileKey: string,
uploadId: string,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"license": "Apache-2.0",
"devDependencies": {
"@balena/lint": "^9.1.3",
"@balena/pinejs": "^21.3.0",
"@balena/pinejs": "21.6.2-build-add-integrity-check-to-webresources-0e71fb0fd93cfe4380cc23c4e8302debda126cbf-1",
"@balena/sbvr-types": "^9.1.0",
"@types/chai": "^5.0.1",
"@types/chai-as-promised": "^8.0.1",
Expand All @@ -42,7 +42,7 @@
"memoizee": "^0.4.17"
},
"peerDependencies": {
"@balena/pinejs": "^21.3.0"
"@balena/pinejs": "*"
},
"versionist": {
"publishedAt": "2025-05-26T01:49:22.588Z"
Expand Down