Skip to content

Commit

Permalink
fix(signature-v4-multi-region): avoid require call on optional depend…
Browse files Browse the repository at this point in the history
…ency
  • Loading branch information
kuhe committed Sep 12, 2023
1 parent 91e51ab commit a3b1fa9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/signature-v4-crt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/signature-v4-multi-region": "*",
"@smithy/querystring-parser": "^2.0.0",
"@smithy/signature-v4": "^2.0.0",
"@smithy/util-middleware": "^2.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/signature-v4-crt/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
import { signatureV4CrtContainer } from "@aws-sdk/signature-v4-multi-region";

import { CrtSignerV4 } from "./CrtSignerV4";

signatureV4CrtContainer.CrtSignerV4 = CrtSignerV4;

export * from "./CrtSignerV4";
15 changes: 0 additions & 15 deletions packages/signature-v4-multi-region/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
"main": "./dist-cjs/index.js",
"module": "./dist-es/index.js",
"types": "./dist-types/index.d.ts",
"browser": {
"@aws-sdk/signature-v4-crt": false
},
"react-native": {
"@aws-sdk/signature-v4-crt": false
},
"author": {
"name": "AWS SDK for JavaScript Team",
"url": "https://aws.amazon.com/javascript/"
Expand All @@ -33,22 +27,13 @@
"tslib": "^2.5.0"
},
"devDependencies": {
"@aws-sdk/signature-v4-crt": "*",
"@tsconfig/recommended": "1.0.1",
"concurrently": "7.0.0",
"downlevel-dts": "0.10.1",
"rimraf": "3.0.2",
"typedoc": "0.23.23",
"typescript": "~4.9.5"
},
"peerDependencies": {
"@aws-sdk/signature-v4-crt": "^3.118.0"
},
"peerDependenciesMeta": {
"@aws-sdk/signature-v4-crt": {
"optional": true
}
},
"engines": {
"node": ">=14.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jest.mock("@aws-sdk/signature-v4-crt");
import { CrtSignerV4 } from "@aws-sdk/signature-v4-crt";
import { SignatureV4 } from "@smithy/signature-v4";

import { signatureV4CrtContainer } from "./signature-v4-crt-container";
import { SignatureV4MultiRegion, SignatureV4MultiRegionInit } from "./SignatureV4MultiRegion";

describe("SignatureV4MultiRegion", () => {
Expand All @@ -26,6 +27,7 @@ describe("SignatureV4MultiRegion", () => {
});

beforeEach(() => {
signatureV4CrtContainer.CrtSignerV4 = CrtSignerV4 as any;
jest.clearAllMocks();
});

Expand Down Expand Up @@ -72,4 +74,15 @@ describe("SignatureV4MultiRegion", () => {
"This request requires signing with SigV4Asymmetric algorithm. It's only available in Node.js"
);
});

it("should throw if sign with SigV4a and signature-v4-crt is not installed", async () => {
signatureV4CrtContainer.CrtSignerV4 = null;
expect.assertions(1);
const signer = new SignatureV4MultiRegion({ ...params });
await expect(async () => await signer.sign(minimalRequest, { signingRegion: "*" })).rejects.toThrow(
`\nPlease check if you have installed "@aws-sdk/signature-v4-crt" package explicitly. \n` +
"For more information please go to " +
"https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt"
);
});
});
13 changes: 8 additions & 5 deletions packages/signature-v4-multi-region/src/SignatureV4MultiRegion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { CrtSignerV4, CrtSignerV4Init } from "@aws-sdk/signature-v4-crt";
import { SignatureV4, SignatureV4CryptoInit, SignatureV4Init } from "@smithy/signature-v4";
import {
HttpRequest,
Expand All @@ -8,6 +7,8 @@ import {
RequestSigningArguments,
} from "@smithy/types";

import { OptionalCrtSignerV4, signatureV4CrtContainer } from "./signature-v4-crt-container";

/**
* @internal
*/
Expand All @@ -26,7 +27,7 @@ export type SignatureV4MultiRegionInit = SignatureV4Init &
*/
export class SignatureV4MultiRegion implements RequestPresigner, RequestSigner {
private readonly sigv4Signer: SignatureV4;
private sigv4aSigner?: CrtSignerV4;
private sigv4aSigner?: InstanceType<OptionalCrtSignerV4>;
private readonly signerOptions: SignatureV4MultiRegionInit;

constructor(options: SignatureV4MultiRegionInit) {
Expand All @@ -52,11 +53,12 @@ export class SignatureV4MultiRegion implements RequestPresigner, RequestSigner {
return this.sigv4Signer.presign(originalRequest, options);
}

private getSigv4aSigner(): CrtSignerV4 {
private getSigv4aSigner(): InstanceType<OptionalCrtSignerV4> {
if (!this.sigv4aSigner) {
let CrtSignerV4: new (options: CrtSignerV4Init & SignatureV4CryptoInit) => CrtSignerV4;
let CrtSignerV4: OptionalCrtSignerV4 | null = null;

try {
CrtSignerV4 = typeof require === "function" && require("@aws-sdk/signature-v4-crt").CrtSignerV4;
CrtSignerV4 = signatureV4CrtContainer.CrtSignerV4;
if (typeof CrtSignerV4 !== "function") throw new Error();
} catch (e) {
e.message =
Expand All @@ -65,6 +67,7 @@ export class SignatureV4MultiRegion implements RequestPresigner, RequestSigner {
"https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt";
throw e;
}

this.sigv4aSigner = new CrtSignerV4({
...this.signerOptions,
signingAlgorithm: 1,
Expand Down
1 change: 1 addition & 0 deletions packages/signature-v4-multi-region/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
* @internal
*/
export * from "./SignatureV4MultiRegion";
export * from "./signature-v4-crt-container";
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { RequestPresigner, RequestSigner } from "@smithy/types";

/**
* @public
*/
export type OptionalCrtSignerV4 = {
/**
* This constructor is not typed so as not to require a type import
* from the signature-v4-crt package.
*
* The true type is CrtSignerV4 from \@aws-sdk/signature-v4-crt.
*/
new (options: any): RequestPresigner & RequestSigner;
};

/**
* @public
*
* \@aws-sdk/signature-v4-crt will install the constructor in this
* container if it is installed.
*
* This avoids a runtime-require being interpreted statically by bundlers.
*
*/
export const signatureV4CrtContainer: {
CrtSignerV4: null | OptionalCrtSignerV4;
} = {
CrtSignerV4: null,
};

0 comments on commit a3b1fa9

Please sign in to comment.