Skip to content

Commit

Permalink
fix(signature-v4-crt): remove dynamic imports (!)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Sep 18, 2023
1 parent db97698 commit 1f27302
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 54 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,11 @@ If the required AWS Common Runtime components are not installed you will receive
```console
Cannot find module '@aws-sdk/signature-v4-crt'
...
Please check if you have installed "@aws-sdk/signature-v4-crt" package explicitly.
For more information please go to https://github.com/aws/aws-sdk-js-v3#known-issues
Please check whether you have installed the "@aws-sdk/signature-v4-crt" package explicitly.
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";].
For more information please go to
https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt"
```

indicating that the required dependency is missing to use the associated functionality. To install this dependency follow
Expand All @@ -584,6 +587,17 @@ If you are using Yarn:
yarn add @aws-sdk/signature-v4-crt
```

Additionally, load the signature-v4-crt package by importing it.

```js
require("@aws-sdk/signature-v4-crt");
// or ESM
import "@aws-sdk/signature-v4-crt";
```

Only the import statement is needed. The implementation then registers itself with `@aws-sdk/signature-v4-multi-region`
and becomes available for its use. You do not need to use any imported objects directly.

#### Related issues

1. [S3 Multi-Region Access Point(MRAP) is not available unless with additional dependency](https://github.com/aws/aws-sdk-js-v3/issues/2822)
2 changes: 2 additions & 0 deletions packages/signature-v4-crt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@

This package contains native modules that only executable in Node.js runtime.
Please refer to [this issue](https://github.com/aws/aws-sdk-js-v3/issues/2822) for more information.

See also https://github.com/aws/aws-sdk-js-v3/tree/main#functionality-requiring-aws-common-runtime-crt.
1 change: 1 addition & 0 deletions packages/signature-v4-crt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/signature-v4-multi-region": "*",
"@aws-sdk/util-user-agent-node": "*",
"@smithy/querystring-parser": "^2.0.0",
"@smithy/signature-v4": "^2.0.0",
"@smithy/util-middleware": "^2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/signature-v4-crt/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { signatureV4CrtContainer } from "@aws-sdk/signature-v4-multi-region";
import { crtAvailability } from "@aws-sdk/util-user-agent-node";

import { CrtSignerV4 } from "./CrtSignerV4";

signatureV4CrtContainer.CrtSignerV4 = CrtSignerV4;
crtAvailability.isCrtAvailable = true;

export * from "./CrtSignerV4";
5 changes: 1 addition & 4 deletions packages/signature-v4-multi-region/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
[![NPM version](https://img.shields.io/npm/v/@aws-sdk/signature-v4-multi-region/latest.svg)](https://www.npmjs.com/package/@aws-sdk/signature-v4-multi-region)
[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/signature-v4-multi-region.svg)](https://www.npmjs.com/package/@aws-sdk/signature-v4-multi-region)

> An internal package
See also https://github.com/aws/aws-sdk-js-v3/tree/main#functionality-requiring-aws-common-runtime-crt.
## Usage

You probably shouldn't, at least directly.

This package contains optional dependency [`@aws-sdk/signature-v4-crt`](https://www.npmjs.com/package/@aws-sdk/signature-v4).
You need to install this package explicitly to sign an un-regional request using SigV4a algorithm. The package contains
Node.js native implementation which requires building at installation. The installed package MAY NOT work if the
Expand Down
6 changes: 0 additions & 6 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": {
"./dist-es/load-crt": "./dist-es/load-crt.browser"
},
"react-native": {
"./dist-es/load-crt": "./dist-es/load-crt.browser"
},
"author": {
"name": "AWS SDK for JavaScript Team",
"url": "https://aws.amazon.com/javascript/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ describe("SignatureV4MultiRegion", () => {
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` +
"\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"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
RequestSigningArguments,
} from "@smithy/types";

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

/**
Expand Down Expand Up @@ -59,12 +58,14 @@ export class SignatureV4MultiRegion implements RequestPresigner, RequestSigner {
let CrtSignerV4: OptionalCrtSignerV4 | null = null;

try {
loadCrt();
CrtSignerV4 = signatureV4CrtContainer.CrtSignerV4;
if (typeof CrtSignerV4 !== "function") throw new Error();
} catch (e) {
e.message =
`${e.message}\nPlease check if you have installed "@aws-sdk/signature-v4-crt" package explicitly. \n` +
`${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;
Expand Down
4 changes: 0 additions & 4 deletions packages/signature-v4-multi-region/src/load-crt.browser.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/signature-v4-multi-region/src/load-crt.ts

This file was deleted.

9 changes: 9 additions & 0 deletions packages/util-user-agent-node/src/crt-availability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @internal
*
* If \@aws-sdk/signature-v4-crt is installed and loaded, it will register
* this value to true.
*/
export const crtAvailability = {
isCrtAvailable: false,
};
2 changes: 2 additions & 0 deletions packages/util-user-agent-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { env, versions } from "process";

import { isCrtAvailable } from "./is-crt-available";

export { crtAvailability } from "./crt-availability";

/**
* @internal
*/
Expand Down
19 changes: 5 additions & 14 deletions packages/util-user-agent-node/src/is-crt-available.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { UserAgentPair } from "@smithy/types";

import { crtAvailability } from "./crt-availability";

/**
* @internal
*/
export const isCrtAvailable = (): UserAgentPair | null => {
try {
// Attempt to load ambient package aws-crt to verify if it exists.
// We cannot use dynamic import(https://github.com/tc39/proposal-dynamic-import) here because bundlers
// (WebPack, Rollup) will try to bundle this optional dependency and fail to build if not exist.
// Thus this user agent key will only available in Node.js runtime.
if (typeof require === "function" && typeof module !== "undefined" && require("aws-crt")) {
// Validate `module` to make sure this is not in a `require.js` scope.
// TODO: load package version.
return ["md/crt-avail"];
}
return null;
} catch (e) {
// No aws-crt package available in the runtime.
return null;
if (crtAvailability.isCrtAvailable) {
return ["md/crt-avail"];
}
return null;
};

0 comments on commit 1f27302

Please sign in to comment.