Skip to content

Commit

Permalink
fix(s3-request-presigner): not to throw when get signed urls concurre…
Browse files Browse the repository at this point in the history
…ntly (#1884)
  • Loading branch information
AllanZhengYP authored Jan 8, 2021
1 parent 22a11a7 commit 741bb99
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
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

0 comments on commit 741bb99

Please sign in to comment.