Skip to content

Commit

Permalink
feat: defer loading of credential-provider-imds in util-defaults-mode…
Browse files Browse the repository at this point in the history
…-node (#1149)
  • Loading branch information
kuhe authored Jan 29, 2024
1 parent 3769699 commit 280ef3a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tidy-humans-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/util-defaults-mode-node": minor
---

defer loading of credential-provider-imds in util-defaults-mode-node
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NODE_REGION_CONFIG_OPTIONS } from "@smithy/config-resolver";
import { getInstanceMetadataEndpoint, httpRequest } from "@smithy/credential-provider-imds";
import { loadConfig } from "@smithy/node-config-provider";
import { memoize } from "@smithy/property-provider";
import type { DefaultsMode, ResolvedDefaultsMode } from "@smithy/smithy-client";
Expand Down Expand Up @@ -82,6 +81,7 @@ const inferPhysicalRegion = async (): Promise<string | undefined> => {
if (!process.env[ENV_IMDS_DISABLED]) {
// We couldn't figure out the region from environment variables. Check IMDSv2
try {
const { getInstanceMetadataEndpoint, httpRequest } = await import("@smithy/credential-provider-imds");
const endpoint = await getInstanceMetadataEndpoint();
return (await httpRequest({ ...endpoint, path: IMDS_REGION_PATH })).toString();
} catch (e) {
Expand Down
16 changes: 16 additions & 0 deletions scripts/compilation/Inliner.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module.exports = class Inliner {
async tsc() {
await spawnProcess("yarn", ["g:tsc", "-p", "tsconfig.cjs.json"], { cwd: this.packageDirectory });
console.log("Finished recompiling ./dist-cjs in " + this.package);
this.canonicalExports = Object.keys(require(this.outfile));
return this;
}

Expand All @@ -57,6 +58,7 @@ module.exports = class Inliner {
*/
async discoverVariants() {
if (this.bailout) {
console.log("Inliner bailout.");
return this;
}
this.variantEntries = Object.entries(this.pkgJson["react-native"] ?? {});
Expand Down Expand Up @@ -155,6 +157,9 @@ module.exports = class Inliner {
mainFields: ["main"],
allowOverwrite: true,
entryPoints: [path.join(root, this.subfolder, this.package, "src", "index.ts")],
supported: {
"dynamic-import": false,
},
outfile: this.outfile,
keepNames: true,
packages: "external",
Expand Down Expand Up @@ -338,6 +343,17 @@ module.exports = class Inliner {
" were not found in the index."
);
}

// check ESM compat.
const tmpFileContents = this.canonicalExports
.filter((sym) => !sym.includes(":"))
.map((sym) => `import { ${sym} } from "${this.pkgJson.name}";`)
.join("\n");
fs.writeFileSync(path.join(__dirname, "tmp", this.package + ".mjs"), tmpFileContents, "utf-8");
await spawnProcess("node", [path.join(__dirname, "tmp", this.package + ".mjs")]);
console.log("ESM compatibility verified.");
fs.rmSync(path.join(__dirname, "tmp", this.package + ".mjs"));

return this;
}
};
1 change: 1 addition & 0 deletions scripts/compilation/tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.mjs

0 comments on commit 280ef3a

Please sign in to comment.