Skip to content

Commit dc63e37

Browse files
authored
fix(middleware-sdk-sqs): call next() exactly once in sendMessageMiddleware (#1752)
1 parent 56c3639 commit dc63e37

File tree

4 files changed

+84
-52
lines changed

4 files changed

+84
-52
lines changed

packages/middleware-sdk-sqs/src/send-message-batch.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,31 @@ describe("sendMessageBatchMiddleware", () => {
1414
mockHashDigest.mockClear();
1515
});
1616

17+
it("should call next exactly once", async () => {
18+
const next = jest.fn().mockReturnValue({
19+
output: {
20+
Successful: [
21+
{ Id: "foo", MD5OfMessageBody: "00" },
22+
{ Id: "bar", MD5OfMessageBody: "00" },
23+
],
24+
},
25+
});
26+
const handler = sendMessageBatchMiddleware({
27+
md5: MockHash,
28+
})(next, {} as any);
29+
30+
await handler({
31+
input: {
32+
Entries: [
33+
{ Id: "foo", MessageBody: "0" },
34+
{ Id: "bar", MessageBody: "0" },
35+
],
36+
},
37+
});
38+
39+
expect(next).toBeCalledTimes(1);
40+
});
41+
1742
it("should do nothing if the checksums match", async () => {
1843
const next = jest.fn().mockReturnValue({
1944
output: {

packages/middleware-sdk-sqs/src/send-message-batch.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,40 @@ interface SendMessageBatchResultEntry {
2121
MessageId: string | undefined;
2222
}
2323

24-
export function sendMessageBatchMiddleware(options: PreviouslyResolved): InitializeMiddleware<any, any> {
25-
return <Output extends MetadataBearer>(
26-
next: InitializeHandler<any, Output>
27-
): InitializeHandler<any, Output> => async (
28-
args: InitializeHandlerArguments<any>
29-
): Promise<InitializeHandlerOutput<Output>> => {
30-
const resp = await next({ ...args });
31-
const output = (resp.output as unknown) as SendMessageBatchResult;
32-
const messageIds = [];
33-
const entries: { [index: string]: SendMessageBatchResultEntry } = {};
34-
if (output.Successful !== undefined) {
35-
for (const entry of output.Successful) {
36-
if (entry.Id !== undefined) {
37-
entries[entry.Id] = entry;
38-
}
24+
export const sendMessageBatchMiddleware = (options: PreviouslyResolved): InitializeMiddleware<any, any> => <
25+
Output extends MetadataBearer
26+
>(
27+
next: InitializeHandler<any, Output>
28+
): InitializeHandler<any, Output> => async (
29+
args: InitializeHandlerArguments<any>
30+
): Promise<InitializeHandlerOutput<Output>> => {
31+
const resp = await next({ ...args });
32+
const output = (resp.output as unknown) as SendMessageBatchResult;
33+
const messageIds = [];
34+
const entries: { [index: string]: SendMessageBatchResultEntry } = {};
35+
if (output.Successful !== undefined) {
36+
for (const entry of output.Successful) {
37+
if (entry.Id !== undefined) {
38+
entries[entry.Id] = entry;
3939
}
4040
}
41-
for (const entry of args.input.Entries) {
42-
if (entries[entry.Id]) {
43-
const md5 = entries[entry.Id].MD5OfMessageBody;
44-
const hash = new options.md5();
45-
hash.update(entry.MessageBody || "");
46-
if (md5 !== toHex(await hash.digest())) {
47-
messageIds.push(entries[entry.Id].MessageId);
48-
}
41+
}
42+
for (const entry of args.input.Entries) {
43+
if (entries[entry.Id]) {
44+
const md5 = entries[entry.Id].MD5OfMessageBody;
45+
const hash = new options.md5();
46+
hash.update(entry.MessageBody || "");
47+
if (md5 !== toHex(await hash.digest())) {
48+
messageIds.push(entries[entry.Id].MessageId);
4949
}
5050
}
51-
if (messageIds.length > 0) {
52-
throw new Error("Invalid MD5 checksum on messages: " + messageIds.join(", "));
53-
}
51+
}
52+
if (messageIds.length > 0) {
53+
throw new Error("Invalid MD5 checksum on messages: " + messageIds.join(", "));
54+
}
5455

55-
return next({
56-
...args,
57-
});
58-
};
59-
}
56+
return resp;
57+
};
6058

6159
export const sendMessageBatchMiddlewareOptions: InitializeHandlerOptions = {
6260
step: "initialize",

packages/middleware-sdk-sqs/src/send-message.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("sendMessageMiddleware", () => {
1414
mockHashDigest.mockClear();
1515
});
1616

17-
it("should do nothing if the checksum matches", async () => {
17+
it("should only call next once", async () => {
1818
const next = jest.fn().mockReturnValue({
1919
output: {
2020
MD5OfMessageBody: "00",
@@ -23,10 +23,21 @@ describe("sendMessageMiddleware", () => {
2323
const handler = sendMessageMiddleware({
2424
md5: MockHash,
2525
})(next, {} as any);
26+
await handler({ input: {} });
27+
expect(next).toBeCalledTimes(1);
28+
});
2629

27-
await handler({
28-
input: {},
30+
it("should do nothing if the checksum matches", async () => {
31+
const next = jest.fn().mockReturnValue({
32+
output: {
33+
MD5OfMessageBody: "00",
34+
},
2935
});
36+
const handler = sendMessageMiddleware({
37+
md5: MockHash,
38+
})(next, {} as any);
39+
40+
await handler({ input: {} });
3041

3142
expect(mockHashUpdate.mock.calls.length).toBe(1);
3243
expect(mockHashDigest.mock.calls.length).toBe(1);

packages/middleware-sdk-sqs/src/send-message.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@ interface SendMessageResult {
1515
MD5OfMessageBody?: string;
1616
}
1717

18-
export function sendMessageMiddleware(options: PreviouslyResolved): InitializeMiddleware<any, any> {
19-
return <Output extends MetadataBearer>(
20-
next: InitializeHandler<any, Output>
21-
): InitializeHandler<any, Output> => async (
22-
args: InitializeHandlerArguments<any>
23-
): Promise<InitializeHandlerOutput<Output>> => {
24-
const resp = await next({ ...args });
25-
const output = resp.output as SendMessageResult;
26-
const hash = new options.md5();
27-
hash.update(args.input.MessageBody || "");
28-
if (output.MD5OfMessageBody !== toHex(await hash.digest())) {
29-
throw new Error("InvalidChecksumError");
30-
}
31-
return next({
32-
...args,
33-
});
34-
};
35-
}
18+
export const sendMessageMiddleware = (options: PreviouslyResolved): InitializeMiddleware<any, any> => <
19+
Output extends MetadataBearer
20+
>(
21+
next: InitializeHandler<any, Output>
22+
): InitializeHandler<any, Output> => async (
23+
args: InitializeHandlerArguments<any>
24+
): Promise<InitializeHandlerOutput<Output>> => {
25+
const resp = await next({ ...args });
26+
const output = resp.output as SendMessageResult;
27+
const hash = new options.md5();
28+
hash.update(args.input.MessageBody || "");
29+
if (output.MD5OfMessageBody !== toHex(await hash.digest())) {
30+
throw new Error("InvalidChecksumError");
31+
}
32+
return resp;
33+
};
3634

3735
export const sendMessageMiddlewareOptions: InitializeHandlerOptions = {
3836
step: "initialize",

0 commit comments

Comments
 (0)