Skip to content

Commit

Permalink
feat(client-sqs): handle md5=false in sqs middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Apr 2, 2024
1 parent d6bda9d commit d382a08
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 6 deletions.
5 changes: 2 additions & 3 deletions clients/client-sqs/src/SQSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,9 @@ export interface ClientDefaults extends Partial<__SmithyConfiguration<__HttpHand

/**
* A constructor for a class implementing the {@link __Checksum} interface
* that computes MD5 hashes.
* @internal
* that computes MD5 hashes, or false to prevent MD5 computation.
*/
md5?: __ChecksumConstructor | __HashConstructor;
md5?: __ChecksumConstructor | __HashConstructor | false;

/**
* The provider populating default tracking information to be sent with `user-agent`, `x-amz-user-agent` header
Expand Down
2 changes: 1 addition & 1 deletion packages/middleware-sdk-sqs/src/configurations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChecksumConstructor, HashConstructor } from "@smithy/types";

export interface PreviouslyResolved {
md5: ChecksumConstructor | HashConstructor;
md5: ChecksumConstructor | HashConstructor | false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ const handlerResponse = (body: string) => {
};

describe("middleware-sdk-sqs", () => {
// TODO: check in CI
xdescribe(SQS.name + ` w/ useAwsQuery: ${useAwsQuery}`, () => {
describe(SQS.name + ` w/ useAwsQuery: ${useAwsQuery}`, () => {
describe("correct md5 hashes", () => {
beforeEach(() => {
hashError = "";
Expand Down
3 changes: 3 additions & 0 deletions packages/middleware-sdk-sqs/src/receive-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export function receiveMessageMiddleware(options: PreviouslyResolved): Initializ
return <Output extends MetadataBearer>(next: InitializeHandler<any, Output>): InitializeHandler<any, Output> =>
async (args: InitializeHandlerArguments<any>): Promise<InitializeHandlerOutput<Output>> => {
const resp = await next({ ...args });
if (options.md5 === false) {
return resp;
}
const output = resp.output as unknown as ReceiveMessageResult;
const messageIds = [];
if (output.Messages !== undefined) {
Expand Down
19 changes: 19 additions & 0 deletions packages/middleware-sdk-sqs/src/receive-messages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,23 @@ describe("receiveMessageMiddleware", () => {
expect(mockHashUpdate.mock.calls.length).toBe(2);
expect(mockHashDigest.mock.calls.length).toBe(2);
});

it("ignores checksum if md5=false in config", async () => {
const next = jest.fn().mockReturnValue({
output: {
Messages: [
{ Body: "foo", MD5OfBody: "XXYYZZ", MessageId: "fooMessage" },
{ Body: "bar", MD5OfBody: "XXYYZZ", MessageId: "barMessage" },
],
},
});
const handler = receiveMessageMiddleware({
md5: false,
})(next, {} as any);

await handler({ input: {} });

expect(mockHashUpdate.mock.calls.length).toBe(0);
expect(mockHashDigest.mock.calls.length).toBe(0);
});
});
3 changes: 3 additions & 0 deletions packages/middleware-sdk-sqs/src/send-message-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export const sendMessageBatchMiddleware =
<Output extends MetadataBearer>(next: InitializeHandler<any, Output>): InitializeHandler<any, Output> =>
async (args: InitializeHandlerArguments<any>): Promise<InitializeHandlerOutput<Output>> => {
const resp = await next({ ...args });
if (options.md5 === false) {
return resp;
}
const output = resp.output as unknown as SendMessageBatchResult;
const messageIds = [];
const entries: Record<string, SendMessageBatchResultEntry> = {};
Expand Down
3 changes: 3 additions & 0 deletions packages/middleware-sdk-sqs/src/send-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const sendMessageMiddleware =
<Output extends MetadataBearer>(next: InitializeHandler<any, Output>): InitializeHandler<any, Output> =>
async (args: InitializeHandlerArguments<any>): Promise<InitializeHandlerOutput<Output>> => {
const resp = await next({ ...args });
if (options.md5 === false) {
return resp;
}
const output = resp.output as SendMessageResult;
const hash = new options.md5();
hash.update(toUint8Array(args.input.MessageBody || ""));
Expand Down

0 comments on commit d382a08

Please sign in to comment.