Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(signature-v4-multi-region): make conditional require call in node.js only #5203

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
13 changes: 2 additions & 11 deletions packages/signature-v4-multi-region/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"module": "./dist-es/index.js",
"types": "./dist-types/index.d.ts",
"browser": {
"@aws-sdk/signature-v4-crt": false
"./dist-es/load-crt": "./dist-es/load-crt.browser"
},
"react-native": {
"@aws-sdk/signature-v4-crt": false
"./dist-es/load-crt": "./dist-es/load-crt.browser"
},
"author": {
"name": "AWS SDK for JavaScript Team",
Expand All @@ -33,22 +33,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"
);
});
});
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,9 @@ import {
RequestSigningArguments,
} from "@smithy/types";

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

/**
* @internal
*/
Expand All @@ -26,7 +28,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 +54,13 @@ 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;
loadCrt();
CrtSignerV4 = signatureV4CrtContainer.CrtSignerV4;
if (typeof CrtSignerV4 !== "function") throw new Error();
} catch (e) {
e.message =
Expand All @@ -65,6 +69,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";
4 changes: 4 additions & 0 deletions packages/signature-v4-multi-region/src/load-crt.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* @internal
*/
export function loadCrt(): void {}
20 changes: 20 additions & 0 deletions packages/signature-v4-multi-region/src/load-crt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { signatureV4CrtContainer } from "./signature-v4-crt-container";

/**
* @internal
*/
export function loadCrt(): void {
if (signatureV4CrtContainer.CrtSignerV4) {
return;
}
try {
if (typeof require === "function") {
// add some indirection to avoid static analysis.
const __require = require;
const moduleName = "@aws-sdk/signature-v4-crt";
__require.call(null, moduleName);
}
} catch (e) {
// ignored.
}
}
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,
};