@@ -3,26 +3,15 @@ import * as fs from "fs/promises";
33import * as path from "path" ;
44import { BundlingType , LambdaResource } from "../types/resourcesDiscovery.js" ;
55import { outputFolder } from "../constants.js" ;
6- import type { BundlingOptions } from "aws-cdk-lib/aws-lambda-nodejs" ;
76import { findPackageJson } from "../utils/findPackageJson.js" ;
87import { IFramework } from "./iFrameworks.js" ;
98import { CloudFormation } from "../cloudFormation.js" ;
109import { AwsConfiguration } from "../types/awsConfiguration.js" ;
1110import { LldConfigBase } from "../types/lldConfig.js" ;
1211import { 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" ;
2615
2716/**
2817 * Support for AWS CDK framework
@@ -219,10 +208,6 @@ export class CdkFramework implements IFramework {
219208 config : LldConfigBase
220209 ) {
221210 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-
226211 // Define a plugin to prepend custom code to .ts or .tsx files
227212 const injectCodePlugin : esbuild . Plugin = {
228213 name : "injectCode" ,
@@ -319,30 +304,49 @@ export class CdkFramework implements IFramework {
319304 process . env . CDK_CONTEXT_JSON = JSON . stringify ( CDK_CONTEXT_JSON ) ;
320305 Logger . verbose ( `[CDK] context:` , JSON . stringify ( CDK_CONTEXT_JSON , null , 2 ) ) ;
321306
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+ ) ;
327318
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+ } ) ;
331331
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+ } ) ;
338342
339343 Logger . verbose (
340344 `[CDK] Found the following Lambda functions in the CDK code:` ,
341- JSON . stringify ( lambdasPrettified , null , 2 )
345+ JSON . stringify ( lambdas , null , 2 )
342346 ) ;
343347
344348 const list = await Promise . all (
345- global . lambdas . map ( async ( lambda : any ) => {
349+ lambdas . map ( async ( lambda : any ) => {
346350 // handler slit into file and file name
347351 const handlerSplit = lambda . handler . split ( "." ) ;
348352
@@ -375,7 +379,7 @@ export class CdkFramework implements IFramework {
375379 Logger . verbose ( `[CDK] package.json path: ${ packageJsonPath } ` ) ;
376380
377381 return {
378- cdkPath : lambda . node . defaultChild . node . path ,
382+ cdkPath : lambda . cdkPath ,
379383 stackName : lambda . stackName ,
380384 packageJsonPath,
381385 codePath,
0 commit comments