|
| 1 | +import { |
| 2 | + HandlerExecutionContext, |
| 3 | + MetadataBearer, |
| 4 | + RelativeMiddlewareOptions, |
| 5 | + SerializeHandler, |
| 6 | + SerializeHandlerArguments, |
| 7 | + SerializeHandlerOutput, |
| 8 | + SerializeMiddleware, |
| 9 | +} from "@smithy/types"; |
| 10 | + |
| 11 | +import { PreviouslyResolved } from "./configuration"; |
| 12 | +import { ResponseChecksumValidation } from "./constants"; |
| 13 | + |
| 14 | +export interface FlexibleChecksumsInterceptorMiddlewareConfig { |
| 15 | + /** |
| 16 | + * Defines a top-level operation input member used to opt-in to best-effort validation |
| 17 | + * of a checksum returned in the HTTP response of the operation. |
| 18 | + */ |
| 19 | + requestValidationModeMember?: string; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +export const flexibleChecksumsInterceptorMiddlewareOptions: RelativeMiddlewareOptions = { |
| 26 | + name: "flexibleChecksumsInterceptorMiddleware", |
| 27 | + toMiddleware: "serializerMiddleware", |
| 28 | + relation: "before", |
| 29 | + tags: ["BODY_CHECKSUM"], |
| 30 | + override: true, |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * @internal |
| 35 | + * |
| 36 | + * The interceptor counterpart to the flexibleChecksumsMiddleware. |
| 37 | + */ |
| 38 | +export const flexibleChecksumsInterceptorMiddleware = |
| 39 | + ( |
| 40 | + config: PreviouslyResolved, |
| 41 | + middlewareConfig: FlexibleChecksumsInterceptorMiddlewareConfig |
| 42 | + ): SerializeMiddleware<any, any> => |
| 43 | + <Output extends MetadataBearer>( |
| 44 | + next: SerializeHandler<any, Output>, |
| 45 | + context: HandlerExecutionContext |
| 46 | + ): SerializeHandler<any, Output> => |
| 47 | + async (args: SerializeHandlerArguments<any>): Promise<SerializeHandlerOutput<Output>> => { |
| 48 | + const input = args.input; |
| 49 | + const { requestValidationModeMember } = middlewareConfig; |
| 50 | + const responseChecksumValidation = await config.responseChecksumValidation(); |
| 51 | + |
| 52 | + const isResponseChecksumValidationNeeded = |
| 53 | + requestValidationModeMember && |
| 54 | + (input[requestValidationModeMember] === "ENABLED" || |
| 55 | + responseChecksumValidation === ResponseChecksumValidation.WHEN_SUPPORTED); |
| 56 | + |
| 57 | + if (isResponseChecksumValidationNeeded) { |
| 58 | + input[requestValidationModeMember] = "ENABLED"; |
| 59 | + } |
| 60 | + |
| 61 | + return next(args); |
| 62 | + }; |
0 commit comments