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 0510f73
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/util-user-agent-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
15 changes: 15 additions & 0 deletions packages/util-user-agent-node/scripts/post-build.js
Original file line number Diff line number Diff line change
@@ -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"));
}
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,
};
30 changes: 30 additions & 0 deletions packages/util-user-agent-node/src/is-crt-available.esm.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};
7 changes: 6 additions & 1 deletion packages/util-user-agent-node/src/is-crt-available.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
3 changes: 2 additions & 1 deletion packages/util-user-agent-node/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
3 changes: 2 additions & 1 deletion packages/util-user-agent-node/tsconfig.types.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

0 comments on commit 0510f73

Please sign in to comment.