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

feat: defer loading of credential-provider-imds in util-defaults-mode-node #1149

Merged
merged 1 commit into from
Jan 29, 2024
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
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