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

Fix bucket location patch for the TemplateURL when deploying a stack #101

Merged
merged 8 commits into from
Dec 20, 2024
Merged
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
51 changes: 38 additions & 13 deletions bin/cdklocal
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ const getLocalEndpoint = async () => process.env.AWS_ENDPOINT_URL || `${PROTOCOL

var resolvedHostname = undefined;

const runAsyncFunctionAsSync = (asyncFn) => {
return (...args) => {
asyncFn(...args).catch((e) => {
console.error(e);
});
};
};

const getLocalHost = async () => {
if (resolvedHostname) {
// early exit to not resolve again
Expand Down Expand Up @@ -204,34 +212,51 @@ const patchCurrentAccount = (SDK) => {
};

const patchToolkitInfo = (ToolkitInfo) => {
const {
BUCKET_NAME_OUTPUT, BUCKET_DOMAIN_NAME_OUTPUT
} = require("aws-cdk/lib/api/bootstrap/bootstrap-props");

const setBucketUrl = function setBucketUrl(object) {
const setBucketUrl = function setBucketUrl(object, bucket, domain) {
const newBucketUrl = `https://${domain.replace(`${bucket}.`, "")}:${EDGE_PORT}/${bucket}`;
Object.defineProperty(object, "bucketUrl", {
async get() {
const bucket = this.requireOutput(BUCKET_NAME_OUTPUT);
const domain = this.requireOutput(BUCKET_DOMAIN_NAME_OUTPUT) || await getLocalHost();
return `https://${domain.replace(`${bucket}.`, "")}:${EDGE_PORT}/${bucket}`;
get() {
return newBucketUrl;
}
});
};

// Pre-fetch the necessary values for the bucket URL
const prefetchBucketUrl = async (object) => {
// Has been observed that the object is not always an instance of ToolkitInfo
if (object && Object.prototype.hasOwnProperty.call(object, "bucketName") && Object.prototype.hasOwnProperty.call(object, "bucketUrl")) {
try {
const bucket = object.bucketName;
const domain = object.bucketUrl.replace("https://", "") || await getLocalHost();
// When object is ExistingToolkitInfo & the bucketName/bucketUrl attributes are non-null
setBucketUrl(object, bucket, domain);
} catch (e) {
// ToolkitInfo: bucketName/bucketUrl attributes don't exist or if implemented, they throw exceptions
// so the exceptions have to be ignored.
//
// The following is an example of how the bucketName/bucketUrl attributes are implemented in the BootstrapStackNotFoundInfos class:
// I.e.: https://github.com/aws/aws-cdk/blob/87e21d625af86873716734dd5568940d41096c45/packages/aws-cdk/lib/api/toolkit-info.ts#L190-L196
//
// The following is an example of how the bucketName/bucketUrl attributes are implemented in the ExistingToolkitInfo class:
// I.e.: https://github.com/aws/aws-cdk/blob/87e21d625af86873716734dd5568940d41096c45/packages/aws-cdk/lib/api/toolkit-info.ts#L124-L130
}
}
};

// for compatibility with with older versions of CDK
setBucketUrl(ToolkitInfo.prototype);
runAsyncFunctionAsSync(prefetchBucketUrl(ToolkitInfo.prototype));

const cdkLookupFn = ToolkitInfo.lookup;
ToolkitInfo.lookup = async (...args) => {
const toolkitInfoObject = await cdkLookupFn(...args);
setBucketUrl(toolkitInfoObject);
await prefetchBucketUrl(toolkitInfoObject);
return toolkitInfoObject;
};

const fromStackFn = ToolkitInfo.fromStack;
ToolkitInfo.fromStack = (...args) => {
const toolkitInfoObject = fromStackFn(...args);
setBucketUrl(toolkitInfoObject);
runAsyncFunctionAsSync(prefetchBucketUrl(toolkitInfoObject));
return toolkitInfoObject;
};
};
Expand Down
Loading