Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(signature-v4-multi-region): add check for node and error messages
Browse files Browse the repository at this point in the history
siddsriv committed Aug 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent b3ce48a commit 655ddba
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions packages/signature-v4-multi-region/src/SignatureV4MultiRegion.ts
Original file line number Diff line number Diff line change
@@ -78,49 +78,51 @@ export class SignatureV4MultiRegion implements RequestPresigner, RequestSigner {

private getSigv4aSigner(): InstanceType<OptionalCrtSignerV4> | InstanceType<OptionalSigV4aSigner> {
if (!this.sigv4aSigner) {
let CrtSignerV4: OptionalCrtSignerV4 | null = null;
let JsSigV4aSigner: OptionalSigV4aSigner | null = null;
const CrtSignerV4 = signatureV4CrtContainer.CrtSignerV4;
const JsSigV4aSigner = signatureV4aContainer.SignatureV4a;

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

this.sigv4aSigner = new CrtSignerV4({
...this.signerOptions,
signingAlgorithm: 1,
});
} else if (signatureV4aContainer.SignatureV4a) {
try {
JsSigV4aSigner = signatureV4aContainer.SignatureV4a;
if (typeof JsSigV4aSigner !== "function") throw new Error();
} catch (e) {
e.message =
`${e.message}\n` +
`Please check whether you have installed the "@smithy/signature-v4a" package explicitly. \n` +
`You must also register the package by calling [require("@smithy/signature-v4a");] ` +
`or an ESM equivalent such as [import "@smithy/signature-v4a";]. \n`;
throw e;
if (CrtSignerV4 && typeof CrtSignerV4 === "function") {
this.sigv4aSigner = new CrtSignerV4({
...this.signerOptions,
signingAlgorithm: 1,
});
} else if (JsSigV4aSigner && typeof JsSigV4aSigner === "function") {
this.sigv4aSigner = new JsSigV4aSigner({
...this.signerOptions,
});
} else {
throw new Error(
"Available SigV4a implementation is not a valid constructor. " +
"Please ensure you've properly imported @aws-sdk/signature-v4-crt or @smithy/signature-v4a." +
"For more information please go to " +
"https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt"
);
}
} else {
if (!JsSigV4aSigner || typeof JsSigV4aSigner !== "function") {
throw new Error(
"JS SigV4a implementation is not available or not a valid constructor. " +
"Please check whether you have installed the @smithy/signature-v4a package explicitly. " +
"You must also register the package by calling [require('@smithy/signature-v4a');] " +
"or an ESM equivalent such as [import '@smithy/signature-v4a';]. " +
"For more information please go to " +
"https://github.com/aws/aws-sdk-js-v3#using-javascript-non-crt-implementation-of-sigv4a"
);
}

this.sigv4aSigner = new JsSigV4aSigner({
...this.signerOptions,
});
} else {
throw new Error(
"Neither CRT nor JS SigV4a implementation is available. " +
"Please load either @aws-sdk/signature-v4-crt or @smithy/signature-v4a."
);
}
}
return this.sigv4aSigner;

0 comments on commit 655ddba

Please sign in to comment.