From 0510f731a64e0d06481b61fbe889bc5e8481fc66 Mon Sep 17 00:00:00 2001 From: George Fu Date: Fri, 15 Sep 2023 18:34:01 +0000 Subject: [PATCH] fix(signature-v4-crt): remove dynamic imports (!) --- packages/util-user-agent-node/package.json | 3 +- .../scripts/post-build.js | 15 ++++++++++ .../src/crt-availability.ts | 9 ++++++ .../src/is-crt-available.esm.ts | 30 +++++++++++++++++++ .../src/is-crt-available.ts | 7 ++++- .../util-user-agent-node/tsconfig.cjs.json | 3 +- .../util-user-agent-node/tsconfig.types.json | 3 +- 7 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 packages/util-user-agent-node/scripts/post-build.js create mode 100644 packages/util-user-agent-node/src/crt-availability.ts create mode 100644 packages/util-user-agent-node/src/is-crt-available.esm.ts diff --git a/packages/util-user-agent-node/package.json b/packages/util-user-agent-node/package.json index c447dd7022831..e0df0ebfbe631 100644 --- a/packages/util-user-agent-node/package.json +++ b/packages/util-user-agent-node/package.json @@ -2,12 +2,13 @@ "name": "@aws-sdk/util-user-agent-node", "version": "3.413.0", "scripts": { - "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'", + "build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types' && yarn build:custom", "build:cjs": "tsc -p tsconfig.cjs.json", "build:es": "tsc -p tsconfig.es.json", "build:include:deps": "lerna run --scope $npm_package_name --include-dependencies build", "build:types": "tsc -p tsconfig.types.json", "build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4", + "build:custom": "node ./scripts/post-build", "clean": "rimraf ./dist-* && rimraf *.tsbuildinfo", "test": "jest" }, diff --git a/packages/util-user-agent-node/scripts/post-build.js b/packages/util-user-agent-node/scripts/post-build.js new file mode 100644 index 0000000000000..81629023555fb --- /dev/null +++ b/packages/util-user-agent-node/scripts/post-build.js @@ -0,0 +1,15 @@ +const fs = require("fs"); +const path = require("path"); + +const pkg = path.join(__dirname, ".."); + +function rm(file) { + if (fs.existsSync(file)) { + fs.rmSync(file); + } +} + +if (fs.existsSync(path.join(pkg, "dist-es", "is-crt-available.esm.js"))) { + rm(path.join(pkg, "dist-es", "is-crt-available.js")); + fs.renameSync(path.join(pkg, "dist-es", "is-crt-available.esm.js"), path.join(pkg, "dist-es", "is-crt-available.js")); +} diff --git a/packages/util-user-agent-node/src/crt-availability.ts b/packages/util-user-agent-node/src/crt-availability.ts new file mode 100644 index 0000000000000..5fa6d554dced2 --- /dev/null +++ b/packages/util-user-agent-node/src/crt-availability.ts @@ -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, +}; diff --git a/packages/util-user-agent-node/src/is-crt-available.esm.ts b/packages/util-user-agent-node/src/is-crt-available.esm.ts new file mode 100644 index 0000000000000..78eca0daba204 --- /dev/null +++ b/packages/util-user-agent-node/src/is-crt-available.esm.ts @@ -0,0 +1,30 @@ +import { UserAgentPair } from "@smithy/types"; + +let isAvailable = false; + +import("aws-crt") + .then((result) => (isAvailable = !!result)) + .catch(() => { + isAvailable = false; + }); + +/** + * @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 (isAvailable) { + // 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; + } +}; diff --git a/packages/util-user-agent-node/src/is-crt-available.ts b/packages/util-user-agent-node/src/is-crt-available.ts index 8714ab658a26d..64c65c4974994 100644 --- a/packages/util-user-agent-node/src/is-crt-available.ts +++ b/packages/util-user-agent-node/src/is-crt-available.ts @@ -9,7 +9,12 @@ export const isCrtAvailable = (): UserAgentPair | null => { // 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")) { + if ( + typeof require === "function" && + typeof require?.resolve === "function" && + typeof module !== "undefined" && + require.resolve("aws-crt") + ) { // Validate `module` to make sure this is not in a `require.js` scope. // TODO: load package version. return ["md/crt-avail"]; diff --git a/packages/util-user-agent-node/tsconfig.cjs.json b/packages/util-user-agent-node/tsconfig.cjs.json index 96198be81644a..80abdeb575437 100644 --- a/packages/util-user-agent-node/tsconfig.cjs.json +++ b/packages/util-user-agent-node/tsconfig.cjs.json @@ -5,5 +5,6 @@ "rootDir": "src" }, "extends": "../../tsconfig.cjs.json", - "include": ["src/"] + "include": ["src/"], + "exclude": ["src/is-crt-available.esm.ts", "node_modules/", "**/*.spec.ts"] } diff --git a/packages/util-user-agent-node/tsconfig.types.json b/packages/util-user-agent-node/tsconfig.types.json index 6cdf9f52ea065..f500bea80a750 100644 --- a/packages/util-user-agent-node/tsconfig.types.json +++ b/packages/util-user-agent-node/tsconfig.types.json @@ -5,5 +5,6 @@ "rootDir": "src" }, "extends": "../../tsconfig.types.json", - "include": ["src/"] + "include": ["src/"], + "exclude": ["src/is-crt-available.esm.ts", "node_modules/", "**/*.spec.ts"] }