Skip to content

Commit

Permalink
chore(middleware-flexible-checksums): change default algorithm to CRC…
Browse files Browse the repository at this point in the history
…32 (#6749)
  • Loading branch information
trivikr authored Jan 14, 2025
1 parent e305b39 commit 2293f5a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
10 changes: 4 additions & 6 deletions packages/middleware-flexible-checksums/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export const DEFAULT_RESPONSE_CHECKSUM_VALIDATION = RequestChecksumCalculation.W
* Checksum Algorithms supported by the SDK.
*/
export enum ChecksumAlgorithm {
/**
* @deprecated Use {@link ChecksumAlgorithm.CRC32} instead.
*/
MD5 = "MD5",
CRC32 = "CRC32",
CRC32C = "CRC32C",
Expand All @@ -70,9 +73,4 @@ export enum ChecksumLocation {
/**
* @internal
*/
export const DEFAULT_CHECKSUM_ALGORITHM = ChecksumAlgorithm.MD5;

/**
* @internal
*/
export const S3_EXPRESS_DEFAULT_CHECKSUM_ALGORITHM = ChecksumAlgorithm.CRC32;
export const DEFAULT_CHECKSUM_ALGORITHM = ChecksumAlgorithm.CRC32;
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,10 @@ export const flexibleChecksumsMiddleware =
const { base64Encoder, streamHasher } = config;
const { requestChecksumRequired, requestAlgorithmMember } = middlewareConfig;

const checksumAlgorithm = getChecksumAlgorithmForRequest(
input,
{
requestChecksumRequired,
requestAlgorithmMember: requestAlgorithmMember?.name,
},
!!context.isS3ExpressBucket
);
const checksumAlgorithm = getChecksumAlgorithmForRequest(input, {
requestChecksumRequired,
requestAlgorithmMember: requestAlgorithmMember?.name,
});
let updatedBody = requestBody;
let updatedHeaders = headers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { describe, expect, test as it } from "vitest";

import { ChecksumAlgorithm } from "./constants";
import { ChecksumAlgorithm, DEFAULT_CHECKSUM_ALGORITHM } from "./constants";
import { getChecksumAlgorithmForRequest } from "./getChecksumAlgorithmForRequest";
import { CLIENT_SUPPORTED_ALGORITHMS } from "./types";

describe(getChecksumAlgorithmForRequest.name, () => {
const mockRequestAlgorithmMember = "mockRequestAlgorithmMember";

describe("when requestAlgorithmMember is not provided", () => {
it("returns MD5 if requestChecksumRequired is set", () => {
expect(getChecksumAlgorithmForRequest({}, { requestChecksumRequired: true })).toEqual(ChecksumAlgorithm.MD5);
it(`returns ${DEFAULT_CHECKSUM_ALGORITHM} if requestChecksumRequired is set`, () => {
expect(getChecksumAlgorithmForRequest({}, { requestChecksumRequired: true })).toEqual(DEFAULT_CHECKSUM_ALGORITHM);
});

it("returns undefined if requestChecksumRequired is false", () => {
Expand All @@ -20,9 +20,9 @@ describe(getChecksumAlgorithmForRequest.name, () => {
describe("when requestAlgorithmMember is not set in input", () => {
const mockOptions = { requestAlgorithmMember: mockRequestAlgorithmMember };

it("returns MD5 if requestChecksumRequired is set", () => {
it(`returns ${DEFAULT_CHECKSUM_ALGORITHM} if requestChecksumRequired is set`, () => {
expect(getChecksumAlgorithmForRequest({}, { ...mockOptions, requestChecksumRequired: true })).toEqual(
ChecksumAlgorithm.MD5
DEFAULT_CHECKSUM_ALGORITHM
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChecksumAlgorithm, DEFAULT_CHECKSUM_ALGORITHM, S3_EXPRESS_DEFAULT_CHECKSUM_ALGORITHM } from "./constants";
import { ChecksumAlgorithm, DEFAULT_CHECKSUM_ALGORITHM } from "./constants";
import { CLIENT_SUPPORTED_ALGORITHMS } from "./types";

export interface GetChecksumAlgorithmForRequestOptions {
Expand All @@ -20,16 +20,13 @@ export interface GetChecksumAlgorithmForRequestOptions {
*/
export const getChecksumAlgorithmForRequest = (
input: any,
{ requestChecksumRequired, requestAlgorithmMember }: GetChecksumAlgorithmForRequestOptions,
isS3Express?: boolean
{ requestChecksumRequired, requestAlgorithmMember }: GetChecksumAlgorithmForRequestOptions
): ChecksumAlgorithm | undefined => {
const defaultAlgorithm = isS3Express ? S3_EXPRESS_DEFAULT_CHECKSUM_ALGORITHM : DEFAULT_CHECKSUM_ALGORITHM;

// Either the Operation input member that is used to configure request checksum behavior is not set, or
// the value for input member to configure flexible checksum is not set.
if (!requestAlgorithmMember || !input[requestAlgorithmMember]) {
// Select an algorithm only if request checksum is required.
return requestChecksumRequired ? defaultAlgorithm : undefined;
return requestChecksumRequired ? DEFAULT_CHECKSUM_ALGORITHM : undefined;
}

const checksumAlgorithm = input[requestAlgorithmMember];
Expand Down
2 changes: 1 addition & 1 deletion private/aws-middleware-test/src/middleware-serde.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("middleware-serde", () => {
"x-amz-acl": "private",
"content-length": "509",
Expect: "100-continue",
"content-md5": "qpwmS0vhCISEXes008aoXA==",
"x-amz-checksum-crc32": "XnKFaw==",
host: "s3.us-west-2.amazonaws.com",
"x-amz-content-sha256": "c0a89780e1aac5dfa17604e9e25616e7babba0b655db189be49b4c352543bb22",
},
Expand Down

0 comments on commit 2293f5a

Please sign in to comment.