Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(middleware-flexible-checksums): add config resolver #6470

Merged
merged 1 commit into from
Sep 13, 2024
Merged
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
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not something to address for this PR, but overall I think we should mutate directly instead of shallow-copy-and-write Object.assign.

This is a config resolver and not in a hot path so it's fine here.

requestChecksumCalculation: normalizeProvider(
input.requestChecksumCalculation ?? DEFAULT_REQUEST_CHECKSUM_CALCULATION
),
responseChecksumValidation: normalizeProvider(
input.responseChecksumValidation ?? DEFAULT_RESPONSE_CHECKSUM_VALIDATION
),
});
Loading