@aws-sdk/find-v2 0.7.1 → 0.7.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.
@@ -1,5 +1,7 @@
1
1
  import { Lambda } from "@aws-sdk/client-lambda";
2
2
  import pLimit from "p-limit";
3
+ import { getCodeSizeToDownload } from "./utils/getCodeSizeToDownload.js";
4
+ import { getCodeSizeToSaveOnDisk } from "./utils/getCodeSizeToSaveOnDisk.js";
3
5
  import { getDownloadConfirmation } from "./utils/getDownloadConfirmation.js";
4
6
  import { getLambdaFunctions } from "./utils/getLambdaFunctions.js";
5
7
  import { getLambdaFunctionScanOutput } from "./utils/getLambdaFunctionScanOutput.js";
@@ -18,19 +20,13 @@ export const scanLambdaFunctions = async (options) => {
18
20
  }
19
21
  const functions = await getLambdaFunctions(client, lambdaNodeJsMajorVersions);
20
22
  const functionCount = functions.length;
21
- const concurrency = Math.min(functionCount, jobs || 1);
22
- const codeSizeToDownload = functions.reduce((acc, fn) => acc + (fn.CodeSize || 0), 0);
23
- const codeSizeToSaveOnDisk = functions
24
- .map((fn) => fn.CodeSize || 0)
25
- .sort((a, b) => b - a)
26
- .slice(0, concurrency)
27
- .reduce((acc, size) => acc + size, 0);
28
23
  if (functionCount === 0) {
29
24
  printLambdaCommandOutput([], output);
30
25
  return;
31
26
  }
27
+ const concurrency = Math.min(functionCount, jobs || 1);
32
28
  if (!yes) {
33
- const confirmation = await getDownloadConfirmation(functionCount, codeSizeToDownload, codeSizeToSaveOnDisk);
29
+ const confirmation = await getDownloadConfirmation(functionCount, getCodeSizeToDownload(functions), getCodeSizeToSaveOnDisk(functions, concurrency));
34
30
  console.log();
35
31
  if (!confirmation) {
36
32
  console.log("Exiting.");
@@ -42,7 +38,6 @@ export const scanLambdaFunctions = async (options) => {
42
38
  const scanOutput = await Promise.all(functions.map((fn) => limit(() => getLambdaFunctionScanOutput(client, {
43
39
  functionName: fn.FunctionName,
44
40
  region: clientRegion,
45
- runtime: fn.Runtime,
46
41
  sdkVersionRange: sdk,
47
42
  }))));
48
43
  printLambdaCommandOutput(scanOutput, output);
@@ -1,3 +1,5 @@
1
- export const PACKAGE_JSON = "package.json";
1
+ import { join } from "node:path";
2
2
  export const NODE_MODULES = "node_modules";
3
3
  export const AWS_SDK = "aws-sdk";
4
+ export const PACKAGE_JSON = "package.json";
5
+ export const AWS_SDK_PACKAGE_JSON = join(NODE_MODULES, AWS_SDK, PACKAGE_JSON);
@@ -1,6 +1,5 @@
1
1
  import { dirname, join } from "node:path";
2
- import { AWS_SDK, NODE_MODULES, PACKAGE_JSON } from "./constants.js";
3
- const AWS_SDK_PACKAGE_JSON = join(NODE_MODULES, AWS_SDK, PACKAGE_JSON);
2
+ import { AWS_SDK, AWS_SDK_PACKAGE_JSON, PACKAGE_JSON } from "./constants.js";
4
3
  const safeParse = (json) => {
5
4
  try {
6
5
  return JSON.parse(json);
@@ -0,0 +1,3 @@
1
+ import { getLambdaLayerToCodeSizeMap } from "./getLambdaLayerToCodeSizeMap.js";
2
+ export const getCodeSizeToDownload = (functions) => functions.reduce((acc, fn) => acc + (fn.CodeSize || 0), 0) +
3
+ [...getLambdaLayerToCodeSizeMap(functions).values()].reduce((acc, size) => acc + size, 0);
@@ -0,0 +1,10 @@
1
+ import { getLambdaLayerToCodeSizeMap } from "./getLambdaLayerToCodeSizeMap.js";
2
+ const getFunctionTotalSize = (fn) => (fn.CodeSize || 0) + (fn.Layers?.reduce((acc, l) => acc + (l.CodeSize || 0), 0) || 0);
3
+ export const getCodeSizeToSaveOnDisk = (functions, num) => {
4
+ const largestFunctions = [...functions]
5
+ .sort((a, b) => getFunctionTotalSize(b) - getFunctionTotalSize(a))
6
+ .slice(0, num);
7
+ const functionSize = largestFunctions.reduce((acc, fn) => acc + (fn.CodeSize || 0), 0);
8
+ const layerSize = [...getLambdaLayerToCodeSizeMap(largestFunctions).values()].reduce((acc, size) => acc + size, 0);
9
+ return functionSize + layerSize;
10
+ };
@@ -1,68 +1,74 @@
1
- import StreamZip from "node-stream-zip";
2
- import { AWS_SDK, NODE_MODULES, PACKAGE_JSON } from "./constants.js";
3
- import { join } from "node:path";
1
+ import { AWS_SDK_PACKAGE_JSON, NODE_MODULES, PACKAGE_JSON } from "./constants.js";
2
+ import { getLambdaLayerContents } from "./getLambdaLayerContents.js";
3
+ import { getSdkVersionFromLambdaLayerContents } from "./getSdkVersionFromLambdaLayerContents.js";
4
+ import { processRemoteZip } from "./processRemoteZip.js";
5
+ import { processZipEntries } from "./processZipEntries.js";
4
6
  /**
5
- * Extracts the contents of a Lambda Function zip file.
7
+ * Cache for Lambda layer contents to avoid redundant downloads.
8
+ * Maps layer ARN to extracted layer contents.
9
+ */
10
+ const lambdaLayerCache = new Map();
11
+ /**
12
+ * Extracts and categorizes contents from a Lambda Function deployment package.
6
13
  *
7
- * Parses the zip and returns:
8
- * - JS/TS source files (excluding node_modules)
14
+ * Downloads and processes the Lambda function's zip file to extract:
15
+ * - JavaScript/TypeScript source files (excluding node_modules)
9
16
  * - package.json files (excluding node_modules)
10
- * - aws-sdk package.json from node_modules (for version detection)
17
+ * - AWS SDK package.json files from node_modules and layers (for version detection)
11
18
  *
12
- * @param zipPath - The path to the zip file of Lambda Function.
13
- * @returns Extracted contents categorized by file type.
19
+ * @param client - The Lambda client instance for API calls
20
+ * @param options - Configuration options for content extraction
21
+ * @param options.codeLocation - Presigned URL to download the Lambda function code
22
+ * @param options.runtime - Lambda runtime identifier (e.g., 'nodejs20.x')
23
+ * @param options.layers - Array of Lambda layers attached to the function
24
+ * @returns Promise resolving to categorized file contents with optional maps for package.json and AWS SDK files
14
25
  */
15
- export const getLambdaFunctionContents = async (zipPath) => {
16
- const zip = new StreamZip.async({ file: zipPath });
26
+ export const getLambdaFunctionContents = async (client, { codeLocation, runtime, layers = [] }) => {
17
27
  const codeMap = new Map();
18
28
  const packageJsonMap = new Map();
19
29
  const awsSdkPackageJsonMap = new Map();
20
- let zipEntries = {};
21
- try {
22
- zipEntries = await zip.entries();
23
- }
24
- catch {
25
- // Continue with empty object, if zip entries can't be read.
26
- // ToDo: add warning when logging is supported in future.
27
- }
28
- for (const zipEntry of Object.values(zipEntries)) {
29
- // Skip 'node_modules' directory, except for aws-sdk package.json file.
30
- if (zipEntry.name.includes(`${NODE_MODULES}/`)) {
31
- if (zipEntry.name.endsWith(join(NODE_MODULES, AWS_SDK, PACKAGE_JSON)) && zipEntry.isFile) {
32
- const packageJsonContent = await zip.entryData(zipEntry.name);
33
- awsSdkPackageJsonMap.set(zipEntry.name, packageJsonContent.toString());
34
- }
35
- continue;
36
- }
37
- // Skip if it is not a file
38
- if (!zipEntry.isFile)
39
- continue;
40
- // Populate 'package.json' files.
41
- if (zipEntry.name.endsWith(PACKAGE_JSON)) {
42
- try {
43
- const packageJsonContent = await zip.entryData(zipEntry.name);
44
- packageJsonMap.set(zipEntry.name, packageJsonContent.toString());
45
- }
46
- catch {
47
- // Continue without adding package.json file, if entry data can't be read.
48
- // ToDo: add warning when logging is supported in future.
49
- }
30
+ // Populate awsSdkPackageJsonMap with layers first.
31
+ for (const layer of layers) {
32
+ if (!layer.Arn)
50
33
  continue;
34
+ if (!lambdaLayerCache.has(layer.Arn)) {
35
+ const response = await client.getLayerVersionByArn({ Arn: layer.Arn });
36
+ const layerContents = response.Content?.Location
37
+ ? await getLambdaLayerContents(response.Content.Location)
38
+ : new Map();
39
+ lambdaLayerCache.set(layer.Arn, layerContents);
51
40
  }
52
- // Populate JavaScript/TypeScript files.
53
- if (zipEntry.name.match(/\.(js|ts|mjs|cjs)$/)) {
41
+ const layerContents = lambdaLayerCache.get(layer.Arn) || new Map();
42
+ const version = getSdkVersionFromLambdaLayerContents(layerContents, runtime);
43
+ if (version)
44
+ awsSdkPackageJsonMap.set(AWS_SDK_PACKAGE_JSON, JSON.stringify({ version }));
45
+ }
46
+ await processRemoteZip(codeLocation, async (zipPath) => {
47
+ await processZipEntries(zipPath, async (entry, getData) => {
48
+ if (!entry.isFile)
49
+ return;
54
50
  try {
55
- const codeContent = await zip.entryData(zipEntry.name);
56
- codeMap.set(zipEntry.name, codeContent.toString());
51
+ // Handle aws-sdk package.json in node_modules
52
+ if (entry.name.endsWith(AWS_SDK_PACKAGE_JSON)) {
53
+ awsSdkPackageJsonMap.set(entry.name, (await getData()).toString());
54
+ }
55
+ // Handle files outside of node_modules
56
+ else if (!entry.name.includes(`${NODE_MODULES}/`)) {
57
+ // Handle package.json
58
+ if (entry.name.endsWith(PACKAGE_JSON)) {
59
+ packageJsonMap.set(entry.name, (await getData()).toString());
60
+ }
61
+ // Handle JS/TS files
62
+ else if (entry.name.match(/\.(js|ts|mjs|cjs)$/)) {
63
+ codeMap.set(entry.name, (await getData()).toString());
64
+ }
65
+ }
57
66
  }
58
67
  catch {
59
- // Continue without adding code, if entry data can't be read.
60
- // ToDo: add warning when logging is supported in future.
68
+ // Continue if entry data can't be read.
61
69
  }
62
- continue;
63
- }
64
- }
65
- await zip.close();
70
+ });
71
+ });
66
72
  return {
67
73
  codeMap,
68
74
  ...(packageJsonMap.size > 0 && { packageJsonMap }),
@@ -1,13 +1,9 @@
1
1
  import { satisfies, validate } from "compare-versions";
2
- import { downloadFile } from "./downloadFile.js";
2
+ import { getCodePathToSdkVersionMap } from "./getCodePathToSdkVersionMap.js";
3
3
  import { getLambdaFunctionContents, } from "./getLambdaFunctionContents.js";
4
4
  import { getPossibleHandlerFiles } from "./getPossibleHandlerFiles.js";
5
5
  import { hasSdkV2InBundle } from "./hasSdkV2InBundle.js";
6
6
  import { hasSdkV2InFile } from "./hasSdkV2InFile.js";
7
- import { rm } from "node:fs/promises";
8
- import { tmpdir } from "node:os";
9
- import { join } from "node:path";
10
- import { getCodePathToSdkVersionMap } from "./getCodePathToSdkVersionMap.js";
11
7
  /**
12
8
  * Scans a Lambda function to detect AWS SDK for JavaScript v2 usage.
13
9
  *
@@ -19,11 +15,12 @@ import { getCodePathToSdkVersionMap } from "./getCodePathToSdkVersionMap.js";
19
15
  * @param options - Scan configuration options
20
16
  * @param options.functionName - The name of the Lambda function
21
17
  * @param options.region - AWS region the Lambda function is deployed to
22
- * @param options.runtime - Lambda Function's Node.js runtime
23
18
  * @param options.sdkVersionRange - Semver range string to check for AWS SDK for JavaScript v2
24
19
  * @returns Scan results including SDK v2 detection status and locations
25
20
  */
26
- export const getLambdaFunctionScanOutput = async (client, { functionName, region, runtime, sdkVersionRange }) => {
21
+ export const getLambdaFunctionScanOutput = async (client, { functionName, region, sdkVersionRange }) => {
22
+ const response = await client.getFunction({ FunctionName: functionName });
23
+ const runtime = response.Configuration?.Runtime;
27
24
  const output = {
28
25
  FunctionName: functionName,
29
26
  Region: region,
@@ -31,16 +28,18 @@ export const getLambdaFunctionScanOutput = async (client, { functionName, region
31
28
  SdkVersion: sdkVersionRange,
32
29
  ContainsAwsSdkJsV2: null,
33
30
  };
34
- const response = await client.getFunction({ FunctionName: functionName });
35
- if (!response.Code?.Location) {
31
+ const codeLocation = response.Code?.Location;
32
+ if (!codeLocation) {
36
33
  output.AwsSdkJsV2Error = "Function Code location not found.";
37
34
  return output;
38
35
  }
39
- const zipPath = join(tmpdir(), `${functionName}.zip`);
40
36
  let lambdaFunctionContents;
41
37
  try {
42
- await downloadFile(response.Code.Location, zipPath);
43
- lambdaFunctionContents = await getLambdaFunctionContents(zipPath);
38
+ lambdaFunctionContents = await getLambdaFunctionContents(client, {
39
+ codeLocation,
40
+ runtime,
41
+ layers: response.Configuration?.Layers,
42
+ });
44
43
  }
45
44
  catch (error) {
46
45
  const errorPrefix = "Error downloading or reading Lambda function code";
@@ -48,9 +47,6 @@ export const getLambdaFunctionScanOutput = async (client, { functionName, region
48
47
  error instanceof Error ? `${errorPrefix}: ${error.message}` : errorPrefix;
49
48
  return output;
50
49
  }
51
- finally {
52
- await rm(zipPath, { force: true });
53
- }
54
50
  const { packageJsonMap, awsSdkPackageJsonMap, codeMap } = lambdaFunctionContents;
55
51
  // Process handler as bundle file first.
56
52
  const possibleHandlerFiles = getPossibleHandlerFiles(response.Configuration?.Handler ?? "index.handler");
@@ -0,0 +1,27 @@
1
+ import { AWS_SDK_PACKAGE_JSON } from "./constants.js";
2
+ import { processZipEntries } from "./processZipEntries.js";
3
+ import { processRemoteZip } from "./processRemoteZip.js";
4
+ /**
5
+ * Downloads and extracts the contents of a Lambda layer from its presigned URL.
6
+ * Parses the zip and returns aws-sdk package.json from node_modules
7
+ *
8
+ * @param codeLocation - The presigned URL to download the Lambda layer.
9
+ * @returns Map of aws-sdk package.json files with their versions found in the layer.
10
+ */
11
+ export const getLambdaLayerContents = async (codeLocation) => {
12
+ const lambdaLayerContents = new Map();
13
+ await processRemoteZip(codeLocation, async (zipPath) => {
14
+ await processZipEntries(zipPath, async (entry, getData) => {
15
+ if (!entry.isFile || !entry.name.endsWith(AWS_SDK_PACKAGE_JSON))
16
+ return;
17
+ try {
18
+ const { version } = JSON.parse((await getData()).toString());
19
+ lambdaLayerContents.set(entry.name, { version });
20
+ }
21
+ catch {
22
+ // Continue without adding package.json file, if entry data can't be read or there's parse error.
23
+ }
24
+ });
25
+ });
26
+ return lambdaLayerContents;
27
+ };
@@ -0,0 +1,4 @@
1
+ export const getLambdaLayerToCodeSizeMap = (functions) => new Map(functions
2
+ .flatMap((fn) => fn.Layers ?? [])
3
+ .filter((layer) => layer.Arn && layer.CodeSize)
4
+ .map((layer) => [layer.Arn, layer.CodeSize]));
@@ -0,0 +1,24 @@
1
+ import { AWS_SDK_PACKAGE_JSON } from "./constants.js";
2
+ /**
3
+ * Returns version from aws-sdk package.json value based on specificity
4
+ * - nodejs/node{major-version}/node_modules/aws-sdk/package.json
5
+ * - nodejs/node_modules/aws-sdk/package.json
6
+ * - node_modules/aws-sdk/package.json
7
+ *
8
+ * @param lambdaLayerContents - Map with aws-sdk package.json filepath as key and contents as value.
9
+ * @param runtime - Lambda runtime (e.g., nodejs20.x)
10
+ * @returns The sdk version string, or undefined if not found.
11
+ */
12
+ export const getSdkVersionFromLambdaLayerContents = (lambdaLayerContents, runtime) => {
13
+ const majorVersion = runtime.match(/nodejs(\d+)/)?.[1];
14
+ const paths = [
15
+ `nodejs/node${majorVersion}/${AWS_SDK_PACKAGE_JSON}`,
16
+ `nodejs/${AWS_SDK_PACKAGE_JSON}`,
17
+ AWS_SDK_PACKAGE_JSON,
18
+ ];
19
+ for (const path of paths) {
20
+ const content = lambdaLayerContents.get(path);
21
+ if (content)
22
+ return content.version;
23
+ }
24
+ };
@@ -0,0 +1,27 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ /**
6
+ * Downloads a zip file, runs a processor, then cleans up.
7
+ *
8
+ * @param url - The URL to download the zip from.
9
+ * @param processor - Function that processes the zip file at the given path.
10
+ */
11
+ export const processRemoteZip = async (url, processor) => {
12
+ const zipPath = join(tmpdir(), `${randomUUID()}.zip`);
13
+ const response = await fetch(url);
14
+ if (!response.ok) {
15
+ throw new Error(`Failed to download '${url}'. Received ${response.status} with '${response.statusText}'.`);
16
+ }
17
+ if (!response.body) {
18
+ throw new Error(`Response body is null for '${url}'`);
19
+ }
20
+ await writeFile(zipPath, response.body);
21
+ try {
22
+ await processor(zipPath);
23
+ }
24
+ finally {
25
+ await rm(zipPath, { force: true });
26
+ }
27
+ };
@@ -0,0 +1,17 @@
1
+ import StreamZip from "node-stream-zip";
2
+ /**
3
+ * Processes entries in a zip file using a callback function.
4
+ *
5
+ * @param zipPath - Path to the zip file.
6
+ * @param processor - Callback to process each entry.
7
+ */
8
+ export const processZipEntries = async (zipPath, processor) => {
9
+ const zip = new StreamZip.async({ file: zipPath });
10
+ // Continue with empty object, if zip entries can't be read.
11
+ const zipEntries = await zip.entries().catch(() => ({}));
12
+ for (const entry of Object.values(zipEntries)) {
13
+ // Processor callback is provided by callee, and it should handle errors.
14
+ await processor(entry, () => zip.entryData(entry.name)).catch(() => { });
15
+ }
16
+ await zip.close();
17
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-sdk/find-v2",
3
- "version": "0.7.1",
3
+ "version": "0.7.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",
@@ -31,7 +31,7 @@
31
31
  "oxfmt": "^0.21.0",
32
32
  "oxlint": "^1.33.0",
33
33
  "parcel": "^2.16.3",
34
- "rolldown": "1.0.0-beta.58",
34
+ "rolldown": "1.0.0-beta.59",
35
35
  "rollup": "^4.53.3",
36
36
  "typescript": "^5.9.3",
37
37
  "vitest": "^4.0.15",
@@ -1,19 +0,0 @@
1
- import { writeFile } from "node:fs/promises";
2
- /**
3
- * Downloads a file from a URL and saves it to the specified path.
4
- *
5
- * @param url - The URL of the file to download.
6
- * @param outputPath - The local file path where the downloaded file should be saved.
7
- * @throws {Error} If the download fails or response body is null.
8
- * @returns Promise that resolves when file is downloaded and saved.
9
- */
10
- export const downloadFile = async (url, outputPath) => {
11
- const response = await fetch(url);
12
- if (!response.ok) {
13
- throw new Error(`Failed to download '${url}'. Received ${response.status} with '${response.statusText}'.`);
14
- }
15
- if (!response.body) {
16
- throw new Error(`Response body is null for '${url}'`);
17
- }
18
- await writeFile(outputPath, response.body);
19
- };