@@ -28,6 +28,14 @@ const getLocalEndpoint = async () => process.env.AWS_ENDPOINT_URL || `${PROTOCOL
28
28
29
29
var resolvedHostname = undefined ;
30
30
31
+ const runAsyncFunctionAsSync = ( asyncFn ) => {
32
+ return ( ...args ) => {
33
+ asyncFn ( ...args ) . catch ( ( e ) => {
34
+ console . error ( e ) ;
35
+ } ) ;
36
+ } ;
37
+ } ;
38
+
31
39
const getLocalHost = async ( ) => {
32
40
if ( resolvedHostname ) {
33
41
// early exit to not resolve again
@@ -204,34 +212,51 @@ const patchCurrentAccount = (SDK) => {
204
212
} ;
205
213
206
214
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 } ` ;
212
217
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 ;
217
220
}
218
221
} ) ;
219
222
} ;
220
223
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
+
221
246
// for compatibility with with older versions of CDK
222
- setBucketUrl ( ToolkitInfo . prototype ) ;
247
+ runAsyncFunctionAsSync ( prefetchBucketUrl ( ToolkitInfo . prototype ) ) ;
223
248
224
249
const cdkLookupFn = ToolkitInfo . lookup ;
225
250
ToolkitInfo . lookup = async ( ...args ) => {
226
251
const toolkitInfoObject = await cdkLookupFn ( ...args ) ;
227
- setBucketUrl ( toolkitInfoObject ) ;
252
+ await prefetchBucketUrl ( toolkitInfoObject ) ;
228
253
return toolkitInfoObject ;
229
254
} ;
230
-
255
+
231
256
const fromStackFn = ToolkitInfo . fromStack ;
232
257
ToolkitInfo . fromStack = ( ...args ) => {
233
258
const toolkitInfoObject = fromStackFn ( ...args ) ;
234
- setBucketUrl ( toolkitInfoObject ) ;
259
+ runAsyncFunctionAsSync ( prefetchBucketUrl ( toolkitInfoObject ) ) ;
235
260
return toolkitInfoObject ;
236
261
} ;
237
262
} ;
0 commit comments