Skip to content

Commit 6f38f77

Browse files
committed
ix(polly-request-presigner): not to throw when presign concurrently
#2417
1 parent b9c1904 commit 6f38f77

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

packages/polly-request-presigner/src/getSignedUrls.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,17 @@ describe("getSignedUrl", () => {
9393
expect(mockPresign).toBeCalled();
9494
expect(mockPresign.mock.calls[0][1]).toMatchObject(options);
9595
});
96-
});
96+
97+
it("should not throw if it's called concurrently", async () => {
98+
const mockPresigned = "a presigned url";
99+
mockPresign.mockReturnValue(mockPresigned);
100+
const client = new PollyClient(clientParams);
101+
const command = new SynthesizeSpeechCommand({
102+
Text: "hello world, this is alex",
103+
OutputFormat: "mp3",
104+
VoiceId: "Kimberly",
105+
});
106+
const commands = [command, command];
107+
return expect(Promise.all(commands.map((command) => getSignedUrl(client, command)))).resolves.toBeInstanceOf(Array);
108+
});
109+
});

packages/polly-request-presigner/src/getSignedUrls.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,28 @@ export const getSignedUrl = async (
5151
} as any;
5252
};
5353

54-
client.middlewareStack.addRelativeTo(presignInterceptMiddleware, {
55-
name: "presignInterceptMiddleware",
56-
relation: "before",
57-
toMiddleware: "awsAuthMiddleware",
58-
});
54+
const middlewareName = "presignInterceptMiddleware";
55+
try {
56+
client.middlewareStack.addRelativeTo(presignInterceptMiddleware, {
57+
name: middlewareName,
58+
relation: "before",
59+
toMiddleware: "awsAuthMiddleware",
60+
});
61+
} catch (e) {
62+
if (e.message!.includes(`Duplicated middleware name '${middlewareName}'`)) {
63+
// Swallow if the interceptor is already added. See https://github.com/aws/aws-sdk-js-v3/issues/2417
64+
} else {
65+
throw e;
66+
}
67+
}
5968

6069
let presigned: HttpRequest;
6170
try {
6271
const output = await client.send(command);
6372
//@ts-ignore the output is faked, so it's not actually OutputType
6473
presigned = output.presigned;
6574
} finally {
66-
client.middlewareStack.remove("presignInterceptMiddleware");
75+
client.middlewareStack.remove(middlewareName);
6776
}
6877

6978
return formatUrl(presigned);

0 commit comments

Comments
 (0)