From 09316af9e96372b69370fdcd74091fc20ea73f14 Mon Sep 17 00:00:00 2001 From: siddsriv Date: Thu, 15 Aug 2024 20:08:20 +0000 Subject: [PATCH] fix(signature-v4-multi-region): add check for node and error messages --- .../src/SignatureV4MultiRegion.ts | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/packages/signature-v4-multi-region/src/SignatureV4MultiRegion.ts b/packages/signature-v4-multi-region/src/SignatureV4MultiRegion.ts index 5297b5d6a311c..e10dfa9527740 100644 --- a/packages/signature-v4-multi-region/src/SignatureV4MultiRegion.ts +++ b/packages/signature-v4-multi-region/src/SignatureV4MultiRegion.ts @@ -78,49 +78,49 @@ export class SignatureV4MultiRegion implements RequestPresigner, RequestSigner { private getSigv4aSigner(): InstanceType | InstanceType { 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." + ); + } + } 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;