@@ -3,26 +3,15 @@ import * as fs from "fs/promises";
3
3
import * as path from "path" ;
4
4
import { BundlingType , LambdaResource } from "../types/resourcesDiscovery.js" ;
5
5
import { outputFolder } from "../constants.js" ;
6
- import type { BundlingOptions } from "aws-cdk-lib/aws-lambda-nodejs" ;
7
6
import { findPackageJson } from "../utils/findPackageJson.js" ;
8
7
import { IFramework } from "./iFrameworks.js" ;
9
8
import { CloudFormation } from "../cloudFormation.js" ;
10
9
import { AwsConfiguration } from "../types/awsConfiguration.js" ;
11
10
import { LldConfigBase } from "../types/lldConfig.js" ;
12
11
import { Logger } from "../logger.js" ;
13
-
14
- // this is global variable to store the data from the CDK code once it is executed
15
- declare global {
16
- var lambdas : Array < {
17
- code : any ;
18
- node : any ;
19
- //cdkPath: string;
20
- stackName : string ;
21
- codePath : string ;
22
- handler : string ;
23
- bundling : BundlingOptions ;
24
- } > ;
25
- }
12
+ import { Worker } from "node:worker_threads" ;
13
+ import { getModuleDirname } from "../getDirname.js" ;
14
+ import { Configuration } from "../configuration.js" ;
26
15
27
16
/**
28
17
* Support for AWS CDK framework
@@ -219,10 +208,6 @@ export class CdkFramework implements IFramework {
219
208
config : LldConfigBase
220
209
) {
221
210
const entryFile = await this . getCdkEntryFile ( cdkConfigPath ) ;
222
-
223
- // this is global variable to store the data from the CDK code once it is executed
224
- global . lambdas = [ ] ;
225
-
226
211
// Define a plugin to prepend custom code to .ts or .tsx files
227
212
const injectCodePlugin : esbuild . Plugin = {
228
213
name : "injectCode" ,
@@ -319,30 +304,49 @@ export class CdkFramework implements IFramework {
319
304
process . env . CDK_CONTEXT_JSON = JSON . stringify ( CDK_CONTEXT_JSON ) ;
320
305
Logger . verbose ( `[CDK] context:` , JSON . stringify ( CDK_CONTEXT_JSON , null , 2 ) ) ;
321
306
322
- // execute code to get the data into global.lambdas
323
- const codeFile = await fs . readFile ( compileOutput , "utf8" ) ;
324
- //const __dirname = path.resolve("x/"); // CDK needs this, pure magic
325
- const __dirname = path . resolve ( "./node_modules/aws-cdk-lib/x/x" ) ; // CDK needs this, pure magic
326
- eval ( codeFile ) ;
307
+ const lambdas : any [ ] = await new Promise ( ( resolve , reject ) => {
308
+ const worker = new Worker (
309
+ path . resolve (
310
+ path . join ( getModuleDirname ( ) , "frameworks/cdkFrameworkWorker.mjs" )
311
+ ) ,
312
+ {
313
+ workerData : {
314
+ verbose : Configuration . config . verbose ,
315
+ } ,
316
+ }
317
+ ) ;
327
318
328
- if ( global . lambdas . length === 0 ) {
329
- throw new Error ( "No Lambda functions found in the CDK code" ) ;
330
- }
319
+ worker . on ( "message" , ( message ) => {
320
+ resolve ( message ) ;
321
+ worker . terminate ( ) ;
322
+ } ) ;
323
+
324
+ worker . on ( "error" , ( error ) => {
325
+ reject (
326
+ new Error ( `Error running CDK code in worker: ${ error . message } ` , {
327
+ cause : error ,
328
+ } )
329
+ ) ;
330
+ } ) ;
331
331
332
- const lambdasPrettified = global . lambdas . map ( ( lambda : any ) => ( {
333
- stackName : lambda . stackName ,
334
- codePath : lambda . codePath ,
335
- handler : lambda . handler ,
336
- bundling : lambda . bundling ,
337
- } ) ) ;
332
+ worker . on ( "exit" , ( code ) => {
333
+ if ( code !== 0 ) {
334
+ reject ( new Error ( `CDK worker stopped with exit code ${ code } ` ) ) ;
335
+ }
336
+ } ) ;
337
+
338
+ worker . postMessage ( {
339
+ compileOutput,
340
+ } ) ;
341
+ } ) ;
338
342
339
343
Logger . verbose (
340
344
`[CDK] Found the following Lambda functions in the CDK code:` ,
341
- JSON . stringify ( lambdasPrettified , null , 2 )
345
+ JSON . stringify ( lambdas , null , 2 )
342
346
) ;
343
347
344
348
const list = await Promise . all (
345
- global . lambdas . map ( async ( lambda : any ) => {
349
+ lambdas . map ( async ( lambda : any ) => {
346
350
// handler slit into file and file name
347
351
const handlerSplit = lambda . handler . split ( "." ) ;
348
352
@@ -375,7 +379,7 @@ export class CdkFramework implements IFramework {
375
379
Logger . verbose ( `[CDK] package.json path: ${ packageJsonPath } ` ) ;
376
380
377
381
return {
378
- cdkPath : lambda . node . defaultChild . node . path ,
382
+ cdkPath : lambda . cdkPath ,
379
383
stackName : lambda . stackName ,
380
384
packageJsonPath,
381
385
codePath,
0 commit comments