Skip to content

Commit 38df226

Browse files
authored
Fix bucket location patch for the TemplateURL when deploying a stack (#101)
1 parent da79f55 commit 38df226

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

bin/cdklocal

+38-13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ const getLocalEndpoint = async () => process.env.AWS_ENDPOINT_URL || `${PROTOCOL
2828

2929
var resolvedHostname = undefined;
3030

31+
const runAsyncFunctionAsSync = (asyncFn) => {
32+
return (...args) => {
33+
asyncFn(...args).catch((e) => {
34+
console.error(e);
35+
});
36+
};
37+
};
38+
3139
const getLocalHost = async () => {
3240
if (resolvedHostname) {
3341
// early exit to not resolve again
@@ -204,34 +212,51 @@ const patchCurrentAccount = (SDK) => {
204212
};
205213

206214
const patchToolkitInfo = (ToolkitInfo) => {
207-
const {
208-
BUCKET_NAME_OUTPUT, BUCKET_DOMAIN_NAME_OUTPUT
209-
} = require("aws-cdk/lib/api/bootstrap/bootstrap-props");
210-
211-
const setBucketUrl = function setBucketUrl(object) {
215+
const setBucketUrl = function setBucketUrl(object, bucket, domain) {
216+
const newBucketUrl = `https://${domain.replace(`${bucket}.`, "")}:${EDGE_PORT}/${bucket}`;
212217
Object.defineProperty(object, "bucketUrl", {
213-
async get() {
214-
const bucket = this.requireOutput(BUCKET_NAME_OUTPUT);
215-
const domain = this.requireOutput(BUCKET_DOMAIN_NAME_OUTPUT) || await getLocalHost();
216-
return `https://${domain.replace(`${bucket}.`, "")}:${EDGE_PORT}/${bucket}`;
218+
get() {
219+
return newBucketUrl;
217220
}
218221
});
219222
};
220223

224+
// Pre-fetch the necessary values for the bucket URL
225+
const prefetchBucketUrl = async (object) => {
226+
// Has been observed that the object is not always an instance of ToolkitInfo
227+
if (object && Object.prototype.hasOwnProperty.call(object, "bucketName") && Object.prototype.hasOwnProperty.call(object, "bucketUrl")) {
228+
try {
229+
const bucket = object.bucketName;
230+
const domain = object.bucketUrl.replace("https://", "") || await getLocalHost();
231+
// When object is ExistingToolkitInfo & the bucketName/bucketUrl attributes are non-null
232+
setBucketUrl(object, bucket, domain);
233+
} catch (e) {
234+
// ToolkitInfo: bucketName/bucketUrl attributes don't exist or if implemented, they throw exceptions
235+
// so the exceptions have to be ignored.
236+
//
237+
// The following is an example of how the bucketName/bucketUrl attributes are implemented in the BootstrapStackNotFoundInfos class:
238+
// I.e.: https://github.com/aws/aws-cdk/blob/87e21d625af86873716734dd5568940d41096c45/packages/aws-cdk/lib/api/toolkit-info.ts#L190-L196
239+
//
240+
// The following is an example of how the bucketName/bucketUrl attributes are implemented in the ExistingToolkitInfo class:
241+
// I.e.: https://github.com/aws/aws-cdk/blob/87e21d625af86873716734dd5568940d41096c45/packages/aws-cdk/lib/api/toolkit-info.ts#L124-L130
242+
}
243+
}
244+
};
245+
221246
// for compatibility with with older versions of CDK
222-
setBucketUrl(ToolkitInfo.prototype);
247+
runAsyncFunctionAsSync(prefetchBucketUrl(ToolkitInfo.prototype));
223248

224249
const cdkLookupFn = ToolkitInfo.lookup;
225250
ToolkitInfo.lookup = async (...args) => {
226251
const toolkitInfoObject = await cdkLookupFn(...args);
227-
setBucketUrl(toolkitInfoObject);
252+
await prefetchBucketUrl(toolkitInfoObject);
228253
return toolkitInfoObject;
229254
};
230-
255+
231256
const fromStackFn = ToolkitInfo.fromStack;
232257
ToolkitInfo.fromStack = (...args) => {
233258
const toolkitInfoObject = fromStackFn(...args);
234-
setBucketUrl(toolkitInfoObject);
259+
runAsyncFunctionAsSync(prefetchBucketUrl(toolkitInfoObject));
235260
return toolkitInfoObject;
236261
};
237262
};

0 commit comments

Comments
 (0)