Skip to content

Commit 655ddba

Browse files
committed
fix(signature-v4-multi-region): add check for node and error messages
1 parent b3ce48a commit 655ddba

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

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

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -78,49 +78,51 @@ export class SignatureV4MultiRegion implements RequestPresigner, RequestSigner {
7878

7979
private getSigv4aSigner(): InstanceType<OptionalCrtSignerV4> | InstanceType<OptionalSigV4aSigner> {
8080
if (!this.sigv4aSigner) {
81-
let CrtSignerV4: OptionalCrtSignerV4 | null = null;
82-
let JsSigV4aSigner: OptionalSigV4aSigner | null = null;
81+
const CrtSignerV4 = signatureV4CrtContainer.CrtSignerV4;
82+
const JsSigV4aSigner = signatureV4aContainer.SignatureV4a;
8383

84-
if (signatureV4CrtContainer.CrtSignerV4) {
85-
try {
86-
CrtSignerV4 = signatureV4CrtContainer.CrtSignerV4;
87-
if (typeof CrtSignerV4 !== "function") throw new Error();
88-
} catch (e) {
89-
e.message =
90-
`${e.message}\n` +
91-
`Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly. \n` +
92-
`You must also register the package by calling [require("@aws-sdk/signature-v4-crt");] ` +
93-
`or an ESM equivalent such as [import "@aws-sdk/signature-v4-crt";]. \n` +
94-
"For more information please go to " +
95-
"https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt";
96-
throw e;
84+
if (this.signerOptions.runtime === "node") {
85+
if (!CrtSignerV4 && !JsSigV4aSigner) {
86+
throw new Error(
87+
"Neither CRT nor JS SigV4a implementation is available. " +
88+
"Please load either @aws-sdk/signature-v4-crt or @smithy/signature-v4a. " +
89+
"For more information please go to " +
90+
"https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt"
91+
);
9792
}
9893

99-
this.sigv4aSigner = new CrtSignerV4({
100-
...this.signerOptions,
101-
signingAlgorithm: 1,
102-
});
103-
} else if (signatureV4aContainer.SignatureV4a) {
104-
try {
105-
JsSigV4aSigner = signatureV4aContainer.SignatureV4a;
106-
if (typeof JsSigV4aSigner !== "function") throw new Error();
107-
} catch (e) {
108-
e.message =
109-
`${e.message}\n` +
110-
`Please check whether you have installed the "@smithy/signature-v4a" package explicitly. \n` +
111-
`You must also register the package by calling [require("@smithy/signature-v4a");] ` +
112-
`or an ESM equivalent such as [import "@smithy/signature-v4a";]. \n`;
113-
throw e;
94+
if (CrtSignerV4 && typeof CrtSignerV4 === "function") {
95+
this.sigv4aSigner = new CrtSignerV4({
96+
...this.signerOptions,
97+
signingAlgorithm: 1,
98+
});
99+
} else if (JsSigV4aSigner && typeof JsSigV4aSigner === "function") {
100+
this.sigv4aSigner = new JsSigV4aSigner({
101+
...this.signerOptions,
102+
});
103+
} else {
104+
throw new Error(
105+
"Available SigV4a implementation is not a valid constructor. " +
106+
"Please ensure you've properly imported @aws-sdk/signature-v4-crt or @smithy/signature-v4a." +
107+
"For more information please go to " +
108+
"https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt"
109+
);
110+
}
111+
} else {
112+
if (!JsSigV4aSigner || typeof JsSigV4aSigner !== "function") {
113+
throw new Error(
114+
"JS SigV4a implementation is not available or not a valid constructor. " +
115+
"Please check whether you have installed the @smithy/signature-v4a package explicitly. " +
116+
"You must also register the package by calling [require('@smithy/signature-v4a');] " +
117+
"or an ESM equivalent such as [import '@smithy/signature-v4a';]. " +
118+
"For more information please go to " +
119+
"https://github.com/aws/aws-sdk-js-v3#using-javascript-non-crt-implementation-of-sigv4a"
120+
);
114121
}
115122

116123
this.sigv4aSigner = new JsSigV4aSigner({
117124
...this.signerOptions,
118125
});
119-
} else {
120-
throw new Error(
121-
"Neither CRT nor JS SigV4a implementation is available. " +
122-
"Please load either @aws-sdk/signature-v4-crt or @smithy/signature-v4a."
123-
);
124126
}
125127
}
126128
return this.sigv4aSigner;

0 commit comments

Comments
 (0)