lambda-live-debugger 0.0.92 → 0.0.93

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,7 @@
1
1
  import { LambdaResource } from "../types/resourcesDiscovery.js";
2
- import type { BundlingOptions } from "aws-cdk-lib/aws-lambda-nodejs";
3
2
  import { IFramework } from "./iFrameworks.js";
4
3
  import { AwsConfiguration } from "../types/awsConfiguration.js";
5
4
  import { LldConfigBase } from "../types/lldConfig.js";
6
- declare global {
7
- var lambdas: Array<{
8
- code: any;
9
- node: any;
10
- stackName: string;
11
- codePath: string;
12
- handler: string;
13
- bundling: BundlingOptions;
14
- }>;
15
- }
16
5
  /**
17
6
  * Support for AWS CDK framework
18
7
  */
@@ -6,6 +6,9 @@ import { outputFolder } from "../constants.mjs";
6
6
  import { findPackageJson } from "../utils/findPackageJson.mjs";
7
7
  import { CloudFormation } from "../cloudFormation.mjs";
8
8
  import { Logger } from "../logger.mjs";
9
+ import { Worker } from "node:worker_threads";
10
+ import { getModuleDirname } from "../getDirname.mjs";
11
+ import { Configuration } from "../configuration.mjs";
9
12
  /**
10
13
  * Support for AWS CDK framework
11
14
  */
@@ -136,8 +139,6 @@ export class CdkFramework {
136
139
  */
137
140
  async getLambdasDataFromCdkByCompilingAndRunning(cdkConfigPath, config) {
138
141
  const entryFile = await this.getCdkEntryFile(cdkConfigPath);
139
- // this is global variable to store the data from the CDK code once it is executed
140
- global.lambdas = [];
141
142
  // Define a plugin to prepend custom code to .ts or .tsx files
142
143
  const injectCodePlugin = {
143
144
  name: "injectCode",
@@ -215,22 +216,32 @@ export class CdkFramework {
215
216
  };
216
217
  process.env.CDK_CONTEXT_JSON = JSON.stringify(CDK_CONTEXT_JSON);
217
218
  Logger.verbose(`[CDK] context:`, JSON.stringify(CDK_CONTEXT_JSON, null, 2));
218
- // execute code to get the data into global.lambdas
219
- const codeFile = await fs.readFile(compileOutput, "utf8");
220
- //const __dirname = path.resolve("x/"); // CDK needs this, pure magic
221
- const __dirname = path.resolve("./node_modules/aws-cdk-lib/x/x"); // CDK needs this, pure magic
222
- eval(codeFile);
223
- if (global.lambdas.length === 0) {
224
- throw new Error("No Lambda functions found in the CDK code");
225
- }
226
- const lambdasPrettified = global.lambdas.map((lambda) => ({
227
- stackName: lambda.stackName,
228
- codePath: lambda.codePath,
229
- handler: lambda.handler,
230
- bundling: lambda.bundling,
231
- }));
232
- Logger.verbose(`[CDK] Found the following Lambda functions in the CDK code:`, JSON.stringify(lambdasPrettified, null, 2));
233
- const list = await Promise.all(global.lambdas.map(async (lambda) => {
219
+ const lambdas = await new Promise((resolve, reject) => {
220
+ const worker = new Worker(path.resolve(path.join(getModuleDirname(), "frameworks/cdkFrameworkWorker.mjs")), {
221
+ workerData: {
222
+ verbose: Configuration.config.verbose,
223
+ },
224
+ });
225
+ worker.on("message", (message) => {
226
+ resolve(message);
227
+ worker.terminate();
228
+ });
229
+ worker.on("error", (error) => {
230
+ reject(new Error(`Error running CDK code in worker: ${error.message}`, {
231
+ cause: error,
232
+ }));
233
+ });
234
+ worker.on("exit", (code) => {
235
+ if (code !== 0) {
236
+ reject(new Error(`CDK worker stopped with exit code ${code}`));
237
+ }
238
+ });
239
+ worker.postMessage({
240
+ compileOutput,
241
+ });
242
+ });
243
+ Logger.verbose(`[CDK] Found the following Lambda functions in the CDK code:`, JSON.stringify(lambdas, null, 2));
244
+ const list = await Promise.all(lambdas.map(async (lambda) => {
234
245
  // handler slit into file and file name
235
246
  const handlerSplit = lambda.handler.split(".");
236
247
  const handler = handlerSplit.pop();
@@ -252,7 +263,7 @@ export class CdkFramework {
252
263
  const packageJsonPath = await findPackageJson(codePath);
253
264
  Logger.verbose(`[CDK] package.json path: ${packageJsonPath}`);
254
265
  return {
255
- cdkPath: lambda.node.defaultChild.node.path,
266
+ cdkPath: lambda.cdkPath,
256
267
  stackName: lambda.stackName,
257
268
  packageJsonPath,
258
269
  codePath,
@@ -0,0 +1,61 @@
1
+ import { createRequire as topLevelCreateRequire } from "module";
2
+ const require = topLevelCreateRequire(import.meta.url);
3
+
4
+ import { workerData, parentPort } from "node:worker_threads";
5
+ import fs from "fs/promises";
6
+ import path from "path";
7
+
8
+ import { Logger } from "../logger.mjs";
9
+
10
+ Logger.setVerbose(workerData.verbose);
11
+ Logger.verbose(`[CDK] [Worker] Started`);
12
+
13
+ parentPort.on("message", async (data) => {
14
+ // this is global variable to store the data from the CDK code once it is executed
15
+ global.lambdas = [];
16
+
17
+ Logger.verbose(`[Worker ${workerData.workerId}] Received message`, data);
18
+
19
+ // execute code to get the data into global.lambdas
20
+ const codeFile = await fs.readFile(data.compileOutput, "utf8");
21
+ const __dirname = path.resolve("./node_modules/aws-cdk-lib/x/x"); // CDK needs this, pure magic
22
+ eval(codeFile);
23
+
24
+ if (global.lambdas.length === 0) {
25
+ throw new Error("No Lambda functions found in the CDK code");
26
+ }
27
+
28
+ const lambdas = global.lambdas.map((lambda) => ({
29
+ handler: lambda.handler,
30
+ stackName: lambda.stackName,
31
+ codePath: lambda.codePath,
32
+ code: {
33
+ path: lambda.code?.path,
34
+ },
35
+ cdkPath: lambda.node.defaultChild.node.path,
36
+ bundling: lambda.bundling,
37
+ }));
38
+
39
+ try {
40
+ Logger.verbose(
41
+ `[CDK] [Worker] Sending found lambdas`,
42
+ JSON.stringify(lambdas, null, 2)
43
+ );
44
+ parentPort.postMessage(lambdas);
45
+ } catch (error) {
46
+ handleError(error);
47
+ }
48
+ });
49
+
50
+ process.on("unhandledRejection", (error) => {
51
+ Logger.error(`[CDK] [Worker] Unhandled Rejection`, error);
52
+ handleError(error);
53
+ });
54
+
55
+ function handleError(error) {
56
+ parentPort.postMessage({
57
+ errorType: error.name ?? "Error",
58
+ errorMessage: error.message,
59
+ trace: error.stack,
60
+ });
61
+ }
@@ -1,6 +1,7 @@
1
1
  import * as iot from "aws-iot-device-sdk";
2
2
  import { splitMessageToChunks } from "./utils/splitIoTMessage.mjs";
3
3
  import { IoTClient, DescribeEndpointCommand } from "@aws-sdk/client-iot";
4
+ import { Logger } from "./logger.mjs";
4
5
  let device;
5
6
  const chunks = new Map();
6
7
  /**
@@ -49,20 +50,20 @@ async function connect(props) {
49
50
  });
50
51
  if (props?.topic) {
51
52
  device.subscribe(props.topic, { qos: 1 });
52
- console.debug("Subscribed to topic ", props.topic);
53
+ Logger.verbose("[IoT] Subscribed to topic ", props.topic);
53
54
  }
54
55
  device.on("connect", () => {
55
- console.debug("IoT connected");
56
+ Logger.verbose("[IoT] Connected");
56
57
  connectedPromiseResolve();
57
58
  });
58
59
  device.on("error", (err) => {
59
- console.debug("IoT error", err);
60
+ Logger.error("[IoT] Error", err);
60
61
  });
61
62
  device.on("close", () => {
62
- console.debug("IoT closed");
63
+ Logger.verbose("[IoT] Closed");
63
64
  });
64
65
  device.on("reconnect", () => {
65
- console.debug("IoT reconnecting...");
66
+ Logger.verbose("[IoT] Reconnecting...");
66
67
  });
67
68
  if (props?.onMessage) {
68
69
  const messageReceived = (topic, buffer) => {
@@ -88,6 +89,7 @@ async function connect(props) {
88
89
  };
89
90
  device.on("message", messageReceived);
90
91
  }
92
+ await connectedPromise;
91
93
  return {
92
94
  publish: async (payload, topic) => {
93
95
  await connectedPromise;
@@ -71,7 +71,7 @@ async function run() {
71
71
  // get the uppermost folder of all lambdas or the project root to watch for changes
72
72
  const rootFolderForWarchingChanges = getRootFolder(folders);
73
73
  FileWatcher.watchForFileChanges(rootFolderForWarchingChanges);
74
- LambdaConnection.connect();
74
+ await LambdaConnection.connect();
75
75
  Logger.log("Debugger started!");
76
76
  }
77
77
  run().catch(Logger.error);
@@ -2,7 +2,7 @@ import { createRequire as topLevelCreateRequire } from "module";
2
2
  const require = topLevelCreateRequire(import.meta.url);
3
3
 
4
4
  import { workerData, parentPort } from "node:worker_threads";
5
- import { Logger } from "../dist/logger.mjs";
5
+ import { Logger } from "./logger.mjs";
6
6
 
7
7
  Logger.setVerbose(workerData.verbose);
8
8
  Logger.verbose(
package/package.json CHANGED
@@ -1,7 +1,37 @@
1
1
  {
2
2
  "name": "lambda-live-debugger",
3
- "version": "0.0.92",
3
+ "version": "0.0.93",
4
4
  "type": "module",
5
+ "description": "Debug Lambda functions locally like it is running in the cloud",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git@github.com:ServerlessLife/lambda-live-debugger.git"
9
+ },
10
+ "license": "MPL-2.0",
11
+ "author": {
12
+ "name": "Marko (ServerlessLife.com)",
13
+ "email": "marko@serverlesslife.com",
14
+ "organization": false
15
+ },
16
+ "keywords": [
17
+ "aws",
18
+ "lambda",
19
+ "debugger",
20
+ "serverless",
21
+ "aws-lambda",
22
+ "javascript",
23
+ "typescript",
24
+ "dev-tools",
25
+ "lambda-debugger",
26
+ "aws-cdk",
27
+ "serverless-framework",
28
+ "sls",
29
+ "aws-sam",
30
+ "sam",
31
+ "terraform",
32
+ "local-debugging",
33
+ "cloud-development"
34
+ ],
5
35
  "bin": {
6
36
  "lld": "dist/lldebugger.mjs"
7
37
  },
@@ -9,7 +39,7 @@
9
39
  "scripts": {
10
40
  "typecheck": "tsc --noEmit -p tsconfig.typecheck.json && npx tsc --noEmit -p src/extension/tsconfig.json",
11
41
  "add-bang": "sed -i '1s|^|#!/usr/bin/env node\\n|' ./dist/lldebugger.mjs",
12
- "build": "tsc -p tsconfig.build.json && cp src/nodeWorkerRunner.mjs dist && node fix-imports.js && npm run add-bang && npm run bundle-extension",
42
+ "build": "tsc -p tsconfig.build.json && cp src/nodeWorkerRunner.mjs dist && cp src/frameworks/cdkFrameworkWorker.mjs dist/frameworks && node fix-imports.js && npm run add-bang && npm run bundle-extension",
13
43
  "bundle-extension": "cd src/extension && npm run build && cd ../../",
14
44
  "deploy-github-role": "aws cloudformation deploy --stack-name lld-deploy-role --template-file cloudformation/gitHubDeployRole.yaml --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM --profile lldebugger",
15
45
  "deploy-tests": "npm run deploy --workspaces",