diff --git a/packages/s3-request-presigner/src/getSignedUrl.spec.ts b/packages/s3-request-presigner/src/getSignedUrl.spec.ts index 2f0aaae7797e..cad7709a9b0a 100644 --- a/packages/s3-request-presigner/src/getSignedUrl.spec.ts +++ b/packages/s3-request-presigner/src/getSignedUrl.spec.ts @@ -42,7 +42,12 @@ describe("getSignedUrl", () => { Bucket: "Bucket", Key: "Key", }); - const signed = await getSignedUrl(client, command); + const presignPromise = getSignedUrl(client, command); + // do not mutate to the client or command + expect(client.middlewareStack.remove("presignInterceptMiddleware")).toBe(false); + expect(command.middlewareStack.remove("presignInterceptMiddleware")).toBe(false); + + const signed = await presignPromise; expect(signed).toBe(mockPresigned); expect(mockPresign).toBeCalled(); expect(mockV4Presign).not.toBeCalled(); diff --git a/packages/s3-request-presigner/src/getSignedUrl.ts b/packages/s3-request-presigner/src/getSignedUrl.ts index 41aa94a5144a..93201e56a047 100644 --- a/packages/s3-request-presigner/src/getSignedUrl.ts +++ b/packages/s3-request-presigner/src/getSignedUrl.ts @@ -26,7 +26,6 @@ export const getSignedUrl = async < delete request.headers["amz-sdk-request"]; // User agent header would leak sensitive information delete request.headers["x-amz-user-agent"]; - const presigned = await s3Presigner.presign(request, { ...options, signingRegion: options.signingRegion ?? context["signing_region"], @@ -42,21 +41,18 @@ export const getSignedUrl = async < } as any; }; const middlewareName = "presignInterceptMiddleware"; - client.middlewareStack.addRelativeTo(presignInterceptMiddleware, { + const clientStack = client.middlewareStack.clone(); + clientStack.addRelativeTo(presignInterceptMiddleware, { name: middlewareName, relation: "before", toMiddleware: "awsAuthMiddleware", override: true, }); - 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(middlewareName); - } + const handler = command.resolveMiddleware(clientStack, client.config, {}); + const { output } = await handler({ input: command.input }); + //@ts-ignore the output is faked, so it's not actually OutputType + const { presigned } = output; return formatUrl(presigned); };