Skip to content

Commit 7bf07d8

Browse files
committed
chore(signature-v4-multi-region): refactor to move sigv4a container out and error msg update
1 parent 12ac1b0 commit 7bf07d8

File tree

3 files changed

+63
-62
lines changed

3 files changed

+63
-62
lines changed

packages/signature-v4-multi-region/src/SignatureV4MultiRegion.spec.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ vi.mock("@aws-sdk/signature-v4-crt");
88

99
import { SignatureV4S3Express } from "@aws-sdk/middleware-sdk-s3";
1010
import { CrtSignerV4 } from "@aws-sdk/signature-v4-crt";
11+
import { signatureV4aContainer } from "@smithy/signature-v4";
1112
import { SignatureV4a } from "@smithy/signature-v4a";
1213
import { Checksum } from "@smithy/types";
1314

1415
import { signatureV4CrtContainer } from "./signature-v4-crt-container";
15-
import { signatureV4aContainer } from "./signature-v4a-container";
1616
import { SignatureV4MultiRegion, SignatureV4MultiRegionInit } from "./SignatureV4MultiRegion";
1717

1818
describe("SignatureV4MultiRegion", () => {
@@ -100,6 +100,34 @@ describe("SignatureV4MultiRegion", () => {
100100
).rejects.toThrow("Method presignWithCredentials is not supported for [signingRegion=*].");
101101
});
102102

103+
it("should THROW when presigning with signingRegion '*' if CRT is NOT available", async () => {
104+
signatureV4CrtContainer.CrtSignerV4 = null; // Simulate CRT not being available
105+
const signer = new SignatureV4MultiRegion(params);
106+
// Expect the new combined error message
107+
await expect(signer.presign(minimalRequest, { signingRegion: "*" })).rejects.toThrow(
108+
`presign with signingRegion '*' is only supported when using the CRT dependency @aws-sdk/signature-v4-crt. ` +
109+
`Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly. ` +
110+
`You must also register the package by calling [require("@aws-sdk/signature-v4-crt");] ` +
111+
`or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";]. ` +
112+
`For more information please go to https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt`
113+
);
114+
expect(CrtSignerV4).not.toHaveBeenCalled();
115+
});
116+
117+
it("should THROW when presigning with signingRegion '*' in non-node runtime (CRT unavailable)", async () => {
118+
const nonNodeParams = { ...params, runtime: "browser" };
119+
const signer = new SignatureV4MultiRegion(nonNodeParams);
120+
// Expect the new combined error message
121+
await expect(signer.presign(minimalRequest, { signingRegion: "*" })).rejects.toThrow(
122+
`presign with signingRegion '*' is only supported when using the CRT dependency @aws-sdk/signature-v4-crt. ` +
123+
`Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly. ` +
124+
`You must also register the package by calling [require("@aws-sdk/signature-v4-crt");] ` +
125+
`or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";]. ` +
126+
`For more information please go to https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt`
127+
);
128+
expect(CrtSignerV4).not.toHaveBeenCalled();
129+
});
130+
103131
it("should throw an error if neither CrtSignerV4 nor JsSigV4aSigner is available in node runtime", async () => {
104132
signatureV4CrtContainer.CrtSignerV4 = null;
105133
signatureV4aContainer.SignatureV4a = null;
@@ -132,19 +160,4 @@ describe("SignatureV4MultiRegion", () => {
132160
await signer.sign(minimalRequest, { signingRegion: "*" });
133161
expect(SignatureV4a).toHaveBeenCalledTimes(1);
134162
});
135-
136-
it("should throw if sign with SigV4a and signature-v4-crt is not installed", async () => {
137-
signatureV4CrtContainer.CrtSignerV4 = null;
138-
expect.assertions(1);
139-
const signer = new SignatureV4MultiRegion({ ...params });
140-
// // Use presign here, as presign with '*' requires CRT and has no JS fallback
141-
await expect(async () => await signer.presign(minimalRequest, { signingRegion: "*" })).rejects.toThrow(
142-
"\n" +
143-
`Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly. \n` +
144-
`You must also register the package by calling [require("@aws-sdk/signature-v4-crt");] ` +
145-
`or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";]. \n` +
146-
"For more information please go to " +
147-
"https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt"
148-
);
149-
});
150163
});

packages/signature-v4-multi-region/src/SignatureV4MultiRegion.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { SignatureV4S3Express } from "@aws-sdk/middleware-sdk-s3";
2-
import { SignatureV4CryptoInit, SignatureV4Init } from "@smithy/signature-v4";
2+
import {
3+
OptionalSigV4aSigner,
4+
signatureV4aContainer,
5+
SignatureV4CryptoInit,
6+
SignatureV4Init,
7+
} from "@smithy/signature-v4";
38
import {
49
AwsCredentialIdentity,
510
HttpRequest,
@@ -10,7 +15,6 @@ import {
1015
} from "@smithy/types";
1116

1217
import { OptionalCrtSignerV4, signatureV4CrtContainer } from "./signature-v4-crt-container";
13-
import { OptionalSigV4aSigner, signatureV4aContainer } from "./signature-v4a-container";
1418

1519
/**
1620
* @internal
@@ -46,32 +50,52 @@ export class SignatureV4MultiRegion implements RequestPresigner, RequestSigner {
4650

4751
/**
4852
* Sign with alternate credentials to the ones provided in the constructor.
53+
* Note: This is only supported for SigV4a when using the CRT implementation.
4954
*/
5055
public async signWithCredentials(
5156
requestToSign: HttpRequest,
5257
credentials: AwsCredentialIdentity,
5358
options: RequestSigningArguments = {}
5459
): Promise<HttpRequest> {
5560
if (options.signingRegion === "*") {
56-
return this.getSigv4aSigner().signWithCredentials(requestToSign, credentials, options);
61+
const signer = this.getSigv4aSigner();
62+
const CrtSignerV4 = signatureV4CrtContainer.CrtSignerV4;
63+
// Check if the SigV4a signer is the CRT implementation, as JS doesn't support signWithCredentials
64+
if (CrtSignerV4 && signer instanceof CrtSignerV4) {
65+
return signer.signWithCredentials(requestToSign, credentials, options);
66+
} else {
67+
throw new Error(
68+
`signWithCredentials with signingRegion '*' is only supported when using the CRT dependency @aws-sdk/signature-v4-crt. ` +
69+
`Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly. ` +
70+
`You must also register the package by calling [require("@aws-sdk/signature-v4-crt");] ` +
71+
`or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";]. ` +
72+
`For more information please go to https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt`
73+
);
74+
}
5775
}
5876
return this.sigv4Signer.signWithCredentials(requestToSign, credentials, options);
5977
}
6078

79+
/**
80+
* Presign a request.
81+
* Note: This is only supported for SigV4a when using the CRT implementation.
82+
*/
6183
public async presign(originalRequest: HttpRequest, options: RequestPresigningArguments = {}): Promise<HttpRequest> {
6284
if (options.signingRegion === "*") {
85+
const signer = this.getSigv4aSigner();
6386
const CrtSignerV4 = signatureV4CrtContainer.CrtSignerV4;
64-
if (!CrtSignerV4 || typeof CrtSignerV4 !== "function") {
87+
// Check if the SigV4a signer is the CRT implementation, as JS doesn't support presign
88+
if (CrtSignerV4 && signer instanceof CrtSignerV4) {
89+
return signer.presign(originalRequest, options);
90+
} else {
6591
throw new Error(
66-
"\n" +
67-
`Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly. \n` +
92+
`presign with signingRegion '*' is only supported when using the CRT dependency @aws-sdk/signature-v4-crt. ` +
93+
`Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly. ` +
6894
`You must also register the package by calling [require("@aws-sdk/signature-v4-crt");] ` +
69-
`or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";]. \n` +
70-
"For more information please go to " +
71-
"https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt"
95+
`or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";]. ` +
96+
`For more information please go to https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt`
7297
);
7398
}
74-
return this.getSigv4aSigner().presign(originalRequest, options);
7599
}
76100
return this.sigv4Signer.presign(originalRequest, options);
77101
}

packages/signature-v4-multi-region/src/signature-v4a-container.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)