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

fix(s3-request-presigner): not to throw when presign concurrently #1884

Merged
merged 1 commit into from
Jan 8, 2021
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
12 changes: 12 additions & 0 deletions packages/s3-request-presigner/src/getSignedUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,16 @@ describe("getSignedUrl", () => {
expect(mockPresign).toBeCalled();
expect(mockPresign.mock.calls[0][1]).toMatchObject(options);
});

it("should not throw if it's called concurrently", async () => {
const mockPresigned = "a presigned url";
mockPresign.mockReturnValue(mockPresigned);
const client = new S3Client(clientParams);
const command = new GetObjectCommand({
Bucket: "Bucket",
Key: "Key",
});
const commands = [command, command];
return expect(Promise.all(commands.map((command) => getSignedUrl(client, command)))).resolves.toBeInstanceOf(Array);
});
});
21 changes: 15 additions & 6 deletions packages/s3-request-presigner/src/getSignedUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,28 @@ export const getSignedUrl = async <
},
} as any;
};
client.middlewareStack.addRelativeTo(presignInterceptMiddleware, {
name: "presignInterceptMiddleware",
relation: "before",
toMiddleware: "awsAuthMiddleware",
});
const middlewareName = "presignInterceptMiddleware";
try {
client.middlewareStack.addRelativeTo(presignInterceptMiddleware, {
name: middlewareName,
relation: "before",
toMiddleware: "awsAuthMiddleware",
});
} catch (e) {
if (e.message!.includes(`Duplicated middleware name '${middlewareName}'`)) {
// Swallow if the interceptor is already added. See https://github.com/aws/aws-sdk-js-v3/issues/1857
} else {
throw e;
}
}

let presigned: HttpRequest;
try {
const output = await client.send(command);
//@ts-ignore the output is faked, so it's not actually OutputType
presigned = output.presigned;
} finally {
client.middlewareStack.remove("presignInterceptMiddleware");
client.middlewareStack.remove(middlewareName);
}

return formatUrl(presigned);
Expand Down