Skip to content

Commit

Permalink
chore: remove case where input[requestAlgorithmMember] is not set
Browse files Browse the repository at this point in the history
Removes the case from flexibleChecksumsMiddleware where the
input[requestAlgorithmMember] is not set, as that case is now
checked in flexibleChecksumsInputMiddleware.

Also removes check for isS3Express and it's default algorithm.
  • Loading branch information
trivikr committed Nov 19, 2024
1 parent 317926c commit dc5b5ae
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 92 deletions.
5 changes: 0 additions & 5 deletions packages/middleware-flexible-checksums/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,3 @@ export enum ChecksumLocation {
* @internal
*/
export const DEFAULT_CHECKSUM_ALGORITHM = ChecksumAlgorithm.CRC32;

/**
* @internal
*/
export const S3_EXPRESS_DEFAULT_CHECKSUM_ALGORITHM = ChecksumAlgorithm.CRC32;
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,4 @@ describe(flexibleChecksumsMiddleware.name, () => {
expect(mockBase64Encoder).toHaveBeenCalledWith(mockRawChecksum);
});
});

describe("set feature", () => {
it.each([
["FLEXIBLE_CHECKSUMS_REQ_WHEN_REQUIRED", "a", RequestChecksumCalculation.WHEN_REQUIRED],
["FLEXIBLE_CHECKSUMS_REQ_WHEN_SUPPORTED", "Z", RequestChecksumCalculation.WHEN_SUPPORTED],
])("logs %s and %s when RequestChecksumCalculation=%s", async (feature, value, requestChecksumCalculation) => {
const mockConfig = {
requestChecksumCalculation: () => Promise.resolve(requestChecksumCalculation),
} as PreviouslyResolved;
const handler = flexibleChecksumsMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, {});
await handler(mockArgs);
expect(setFeature).toHaveBeenCalledWith(expect.anything(), feature, value);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,11 @@ export const flexibleChecksumsMiddleware =
const requestChecksumCalculation = await config.requestChecksumCalculation();
const { input, requestChecksumRequired, requestAlgorithmMember } = middlewareConfig;

if (requestChecksumCalculation === RequestChecksumCalculation.WHEN_REQUIRED) {
setFeature(context, "FLEXIBLE_CHECKSUMS_REQ_WHEN_REQUIRED", "a");
} else {
setFeature(context, "FLEXIBLE_CHECKSUMS_REQ_WHEN_SUPPORTED", "Z");
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,6 @@ describe(getChecksumAlgorithmForRequest.name, () => {
});
});

describe("when requestAlgorithmMember is not set in input", () => {
const mockOptionsWithAlgoMember = { requestAlgorithmMember: mockRequestAlgorithmMember };

describe(`when requestChecksumCalculation is '${RequestChecksumCalculation.WHEN_REQUIRED}'`, () => {
const mockOptions = {
...mockOptionsWithAlgoMember,
requestChecksumCalculation: RequestChecksumCalculation.WHEN_REQUIRED,
};

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

it("returns undefined if requestChecksumRequired is false", () => {
expect(getChecksumAlgorithmForRequest({}, { ...mockOptions, requestChecksumRequired: false })).toBeUndefined();
});
});

describe(`when requestChecksumCalculation is '${RequestChecksumCalculation.WHEN_SUPPORTED}'`, () => {
const mockOptions = {
...mockOptionsWithAlgoMember,
requestChecksumCalculation: RequestChecksumCalculation.WHEN_SUPPORTED,
};

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

it(`returns ${DEFAULT_CHECKSUM_ALGORITHM} if requestChecksumRequired is false`, () => {
expect(getChecksumAlgorithmForRequest({}, { ...mockOptions, requestChecksumRequired: false })).toEqual(
DEFAULT_CHECKSUM_ALGORITHM
);
});
});
});

it("throws error if input[requestAlgorithmMember] if not supported by client", () => {
const unsupportedAlgo = "unsupportedAlgo";
const mockInput = { [mockRequestAlgorithmMember]: unsupportedAlgo };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
ChecksumAlgorithm,
DEFAULT_CHECKSUM_ALGORITHM,
RequestChecksumCalculation,
S3_EXPRESS_DEFAULT_CHECKSUM_ALGORITHM,
} from "./constants";
import { ChecksumAlgorithm, DEFAULT_CHECKSUM_ALGORITHM, RequestChecksumCalculation } from "./constants";
import { CLIENT_SUPPORTED_ALGORITHMS } from "./types";

export interface GetChecksumAlgorithmForRequestOptions {
Expand All @@ -30,22 +25,14 @@ export interface GetChecksumAlgorithmForRequestOptions {
*/
export const getChecksumAlgorithmForRequest = (
input: any,
{
requestChecksumRequired,
requestAlgorithmMember,
requestChecksumCalculation,
}: GetChecksumAlgorithmForRequestOptions,
isS3Express?: boolean
{ requestChecksumRequired, requestAlgorithmMember, requestChecksumCalculation }: 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]) {
// The Operation input member that is used to configure request checksum behavior is not set.
if (!requestAlgorithmMember) {
// Select an algorithm only if request checksum calculation is supported
// or request checksum is required.
return requestChecksumCalculation === RequestChecksumCalculation.WHEN_SUPPORTED || requestChecksumRequired
? defaultAlgorithm
? DEFAULT_CHECKSUM_ALGORITHM
: undefined;
}

Expand Down

0 comments on commit dc5b5ae

Please sign in to comment.