@aws-sdk/find-v2 0.3.0 → 0.3.2

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.
package/dist/cli.js CHANGED
@@ -15,10 +15,11 @@ export const createProgram = () => {
15
15
  .version(packageJson.version, "-v, --version");
16
16
  program
17
17
  .command("lambda")
18
- .description("Scans Lambda Node.js Functions for JavaScript SDK v2.")
18
+ .description("Scans Lambda Node.js Functions for JavaScript SDK v2")
19
19
  .option("-r, --region <region>", "AWS region to scan")
20
+ .option("-y, --yes", "answer yes for all prompts")
20
21
  .action(async (options) => {
21
- await scanLambdaFunctions(options.region);
22
+ await scanLambdaFunctions(options);
22
23
  });
23
24
  return program;
24
25
  };
@@ -1,31 +1,40 @@
1
- import { Lambda, paginateListFunctions } from "@aws-sdk/client-lambda";
2
- import { cpus } from "node:os";
1
+ import { Lambda } from "@aws-sdk/client-lambda";
3
2
  import pLimit from "p-limit";
3
+ import { cpus } from "node:os";
4
4
  import { JS_SDK_V2_MARKER } from "./constants.js";
5
5
  import { scanLambdaFunction } from "./scanLambdaFunction.js";
6
- const getNodeJsFunctionNames = (functions) => (functions ?? [])
7
- .filter((fn) => fn.Runtime?.startsWith("nodejs"))
8
- .map((fn) => fn.FunctionName)
9
- .filter((fnName) => fnName !== undefined);
10
- export const scanLambdaFunctions = async (region) => {
6
+ import { getDownloadConfirmation } from "./utils/getDownloadConfirmation.js";
7
+ import { getLambdaFunctions } from "./utils/getLambdaFunctions.js";
8
+ export const scanLambdaFunctions = async ({ region, yes } = {}) => {
11
9
  const client = new Lambda({ region });
12
- const functions = [];
13
- const paginator = paginateListFunctions({ client }, {});
14
- for await (const page of paginator) {
15
- functions.push(...getNodeJsFunctionNames(page.Functions));
16
- }
17
- const functionsLength = functions.length;
18
- if (functionsLength === 0) {
10
+ const functions = await getLambdaFunctions(client);
11
+ const functionCount = functions.length;
12
+ const concurrency = Math.min(functionCount, cpus().length || 1);
13
+ const codeSizeToDownload = functions.reduce((acc, fn) => acc + (fn.CodeSize || 0), 0);
14
+ const codeSizeToSaveOnDisk = functions
15
+ .map((fn) => fn.CodeSize || 0)
16
+ .sort((a, b) => b - a)
17
+ .slice(0, concurrency)
18
+ .reduce((acc, size) => acc + size, 0);
19
+ if (functionCount === 0) {
19
20
  console.log("No functions found.");
20
21
  process.exit(0);
21
22
  }
23
+ if (!yes) {
24
+ const confirmation = await getDownloadConfirmation(functionCount, codeSizeToDownload, codeSizeToSaveOnDisk);
25
+ console.log();
26
+ if (!confirmation) {
27
+ console.log("Exiting.");
28
+ process.exit(0);
29
+ }
30
+ }
22
31
  console.log(`Note about output:`);
23
32
  console.log(`- ${JS_SDK_V2_MARKER.Y} means "aws-sdk" is found in Lambda function, and migration is recommended.`);
24
33
  console.log(`- ${JS_SDK_V2_MARKER.N} means "aws-sdk" is not found in Lambda function.`);
25
34
  console.log(`- ${JS_SDK_V2_MARKER.UNKNOWN} means script was not able to proceed, and it emits reason.\n`);
26
35
  const clientRegion = await client.config.region();
27
- console.log(`Reading ${functionsLength} function${functionsLength > 1 ? "s" : ""} from "${clientRegion}" region.`);
28
- const limit = pLimit(Math.min(functionsLength, cpus().length || 1));
29
- await Promise.all(functions.map((fn) => limit(() => scanLambdaFunction(client, fn))));
36
+ console.log(`Reading ${functionCount} function${functionCount > 1 ? "s" : ""} from "${clientRegion}" region.`);
37
+ const limit = pLimit(concurrency);
38
+ await Promise.all(functions.map((fn) => limit(() => scanLambdaFunction(client, fn.FunctionName))));
30
39
  console.log("\nDone.");
31
40
  };
@@ -0,0 +1,26 @@
1
+ import { createInterface } from "node:readline/promises";
2
+ import { getHumanReadableBytes } from "./getHumanReadableBytes.js";
3
+ /**
4
+ * Prompts user for confirmation before downloading Lambda function code
5
+ *
6
+ * @param functionCount - Number of Lambda functions to be processed
7
+ * @param codeSizeToDownload - Total size of all function code in bytes
8
+ * @param codeSizeToSaveOnDisk - Maximum disk space used at any point
9
+ * @returns Promise that resolves to boolean indicating user's choice
10
+ * @description
11
+ * - Creates interactive readline interface for user input
12
+ * - Displays summary of operations including function count and total size
13
+ * - Returns true if user confirms with 'y' or 'yes', false otherwise
14
+ */
15
+ export const getDownloadConfirmation = async (functionCount, codeSizeToDownload, codeSizeToSaveOnDisk) => {
16
+ const rl = createInterface({
17
+ input: process.stdin,
18
+ output: process.stdout,
19
+ });
20
+ const answer = await rl.question(`This command will process ${functionCount} Lambda Node.js functions, and` +
21
+ `\ndownload ${getHumanReadableBytes(codeSizeToDownload)} of compressed archives over the network.` +
22
+ `\nIt'll store maximum of ${getHumanReadableBytes(codeSizeToSaveOnDisk)} on disk at any point.` +
23
+ `\n\nDo you want to continue? (y/N): `);
24
+ rl.close();
25
+ return ["y", "yes"].includes(answer.trim().toLowerCase());
26
+ };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Converts bytes to human readable format with appropriate units
3
+ *
4
+ * @param bytes - Size in bytes to convert
5
+ * @param decimals - Number of decimal places to show (default: 2)
6
+ * @returns String representation of size with unit (e.g., "1.50 MB")
7
+ * @description
8
+ * - Handles edge case where bytes is 0 or negative
9
+ * - Automatically selects appropriate unit (Bytes, KB, MB, GB, etc.)
10
+ * - Uses base-1000 conversion for SI units
11
+ * - Rounds to specified number of decimal places
12
+ */
13
+ export const getHumanReadableBytes = (bytes, decimals = 2) => {
14
+ if (bytes <= 0)
15
+ return "0 Bytes";
16
+ const k = 1000;
17
+ const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB"];
18
+ const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1);
19
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + " " + sizes[i];
20
+ };
@@ -0,0 +1,19 @@
1
+ import { paginateListFunctions } from "@aws-sdk/client-lambda";
2
+ /**
3
+ * Retrieves all Lambda functions with Node.js runtime from AWS
4
+ *
5
+ * @param client - AWS Lambda client instance
6
+ * @returns Promise that resolves to array of Lambda function configurations
7
+ * @description
8
+ * - Uses AWS SDK v3 pagination to handle large number of functions
9
+ * - Filters results to only include Node.js runtimes
10
+ * - Returns empty array if no functions found
11
+ */
12
+ export const getLambdaFunctions = async (client) => {
13
+ const functions = [];
14
+ const paginator = paginateListFunctions({ client }, {});
15
+ for await (const page of paginator) {
16
+ functions.push(...(page.Functions ?? []).filter((fn) => fn.Runtime?.startsWith("nodejs")));
17
+ }
18
+ return functions;
19
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-sdk/find-v2",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "CLI to find resources which call AWS using JavaScript SDK v2",
5
5
  "main": "dist/cli.js",
6
6
  "types": "dist/cli.d.ts",