Skip to content

Commit

Permalink
chore(middleware-flexible-checksums): add config resolver (#6470)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Sep 13, 2024
1 parent eeffdc0 commit 8c6a2fb
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/middleware-flexible-checksums/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@smithy/node-config-provider": "^3.1.5",
"@smithy/protocol-http": "^4.1.1",
"@smithy/types": "^3.4.0",
"@smithy/util-middleware": "^3.0.4",
"@smithy/util-utf8": "^3.0.0",
"tslib": "^2.6.2"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { LoadedConfigSelectors } from "@smithy/node-config-provider";

import { RequestChecksumCalculation } from "./constants";
import { DEFAULT_REQUEST_CHECKSUM_CALCULATION, RequestChecksumCalculation } from "./constants";
import { SelectorType, stringUnionSelector } from "./stringUnionSelector";

export const ENV_REQUEST_CHECKSUM_CALCULATION = "AWS_REQUEST_CHECKSUM_CALCULATION";
export const CONFIG_REQUEST_CHECKSUM_CALCULATION = "request_checksum_calculation";
export const DEFAULT_REQUEST_CHECKSUM_CALCULATION = RequestChecksumCalculation.WHEN_SUPPORTED;

export const NODE_REQUEST_CHECKSUM_CALCULATION_CONFIG_OPTIONS: LoadedConfigSelectors<string> = {
environmentVariableSelector: (env) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { LoadedConfigSelectors } from "@smithy/node-config-provider";

import { RequestChecksumCalculation } from "./constants";
import { DEFAULT_RESPONSE_CHECKSUM_VALIDATION, RequestChecksumCalculation } from "./constants";
import { SelectorType, stringUnionSelector } from "./stringUnionSelector";

export const ENV_RESPONSE_CHECKSUM_VALIDATION = "AWS_RESPONSE_CHECKSUM_VALIDATION";
export const CONFIG_RESPONSE_CHECKSUM_VALIDATION = "response_checksum_validation";
export const DEFAULT_RESPONSE_CHECKSUM_VALIDATION = RequestChecksumCalculation.WHEN_SUPPORTED;

export const NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS: LoadedConfigSelectors<string> = {
environmentVariableSelector: (env) =>
Expand Down
4 changes: 4 additions & 0 deletions packages/middleware-flexible-checksums/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const RequestChecksumCalculation = {

export type RequestChecksumCalculation = (typeof RequestChecksumCalculation)[keyof typeof RequestChecksumCalculation];

export const DEFAULT_REQUEST_CHECKSUM_CALCULATION = RequestChecksumCalculation.WHEN_SUPPORTED;

/**
* Determines when checksum validation will be performed on response payloads.
*/
Expand All @@ -44,6 +46,8 @@ export const ResponseChecksumValidation = {

export type ResponseChecksumValidation = (typeof ResponseChecksumValidation)[keyof typeof ResponseChecksumValidation];

export const DEFAULT_RESPONSE_CHECKSUM_VALIDATION = RequestChecksumCalculation.WHEN_SUPPORTED;

/**
* Checksum Algorithms supported by the SDK.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/middleware-flexible-checksums/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./NODE_RESPONSE_CHECKSUM_VALIDATION_CONFIG_OPTIONS";
export * from "./constants";
export * from "./flexibleChecksumsMiddleware";
export * from "./getFlexibleChecksumsPlugin";
export * from "./resolveFlexibleChecksumsConfig";
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { normalizeProvider } from "@smithy/util-middleware";

import { DEFAULT_REQUEST_CHECKSUM_CALCULATION, DEFAULT_RESPONSE_CHECKSUM_VALIDATION } from "./constants";
import { resolveFlexibleChecksumsConfig } from "./resolveFlexibleChecksumsConfig";

jest.mock("@smithy/util-middleware");

describe(resolveFlexibleChecksumsConfig.name, () => {
beforeEach(() => {
(normalizeProvider as jest.Mock).mockImplementation((input) => input);
});

afterEach(() => {
jest.clearAllMocks();
});

it("returns default client checksums configuration, if not provided", () => {
const resolvedConfig = resolveFlexibleChecksumsConfig({});
expect(resolvedConfig).toEqual({
requestChecksumCalculation: DEFAULT_REQUEST_CHECKSUM_CALCULATION,
responseChecksumValidation: DEFAULT_RESPONSE_CHECKSUM_VALIDATION,
});
expect(normalizeProvider).toHaveBeenCalledTimes(2);
});

it("normalizes client checksums configuration", () => {
const mockInput = {
requestChecksumCalculation: "WHEN_REQUIRED",
responseChecksumValidation: "WHEN_REQUIRED",
};
const resolvedConfig = resolveFlexibleChecksumsConfig(mockInput);
expect(resolvedConfig).toEqual(mockInput);
expect(normalizeProvider).toHaveBeenCalledTimes(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Provider } from "@smithy/types";
import { normalizeProvider } from "@smithy/util-middleware";

import { DEFAULT_REQUEST_CHECKSUM_CALCULATION, DEFAULT_RESPONSE_CHECKSUM_VALIDATION } from "./constants";

export interface FlexibleChecksumsInputConfig {
/**
* Determines when a checksum will be calculated for request payloads.
*/
requestChecksumCalculation?: string | Provider<string>;

/**
* Determines when checksum validation will be performed on response payloads.
*/
responseChecksumValidation?: string | Provider<string>;
}

export interface FlexibleChecksumsResolvedConfig {
requestChecksumCalculation: Provider<string>;
responseChecksumValidation: Provider<string>;
}

export const resolveFlexibleChecksumsConfig = <T>(
input: T & FlexibleChecksumsInputConfig
): T & FlexibleChecksumsResolvedConfig => ({
...input,
requestChecksumCalculation: normalizeProvider(
input.requestChecksumCalculation ?? DEFAULT_REQUEST_CHECKSUM_CALCULATION
),
responseChecksumValidation: normalizeProvider(
input.responseChecksumValidation ?? DEFAULT_RESPONSE_CHECKSUM_VALIDATION
),
});

0 comments on commit 8c6a2fb

Please sign in to comment.