@aws-sdk/find-v2 0.6.0 → 0.6.1

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.
@@ -0,0 +1,3 @@
1
+ export const PACKAGE_JSON = "package.json";
2
+ export const NODE_MODULES = "node_modules";
3
+ export const AWS_SDK = "aws-sdk";
@@ -1,5 +1,6 @@
1
1
  import StreamZip from "node-stream-zip";
2
- const PACKAGE_JSON_FILENAME = "package.json";
2
+ import { AWS_SDK, NODE_MODULES, PACKAGE_JSON } from "./constants.js";
3
+ import { join } from "node:path";
3
4
  /**
4
5
  * Extracts the contents of a Lambda Function zip file.
5
6
  * Returns string contents of package.json files, if available.
@@ -11,6 +12,7 @@ const PACKAGE_JSON_FILENAME = "package.json";
11
12
  export const getLambdaFunctionContents = async (zipPath) => {
12
13
  const zip = new StreamZip.async({ file: zipPath });
13
14
  const packageJsonFiles = [];
15
+ const awsSdkPackageJsonMap = {};
14
16
  let zipEntries = {};
15
17
  try {
16
18
  zipEntries = await zip.entries();
@@ -20,11 +22,16 @@ export const getLambdaFunctionContents = async (zipPath) => {
20
22
  // ToDo: add warning when logging is supported in future.
21
23
  }
22
24
  for (const zipEntry of Object.values(zipEntries)) {
23
- // Skip 'node_modules' directory, as it's not the customer source code.
24
- if (zipEntry.name.includes("node_modules/"))
25
+ // Skip 'node_modules' directory, except for aws-sdk package.json file.
26
+ if (zipEntry.name.includes(`${NODE_MODULES}/`)) {
27
+ if (zipEntry.name.endsWith(join(NODE_MODULES, AWS_SDK, PACKAGE_JSON)) && zipEntry.isFile) {
28
+ const packageJsonContent = await zip.entryData(zipEntry.name);
29
+ awsSdkPackageJsonMap[zipEntry.name] = packageJsonContent.toString();
30
+ }
25
31
  continue;
32
+ }
26
33
  // Skip anything which is not 'package.json'
27
- if (!zipEntry.name.endsWith(PACKAGE_JSON_FILENAME))
34
+ if (!zipEntry.name.endsWith(PACKAGE_JSON))
28
35
  continue;
29
36
  // Skip if 'package.json' is not a file
30
37
  if (!zipEntry.isFile)
@@ -43,7 +50,10 @@ export const getLambdaFunctionContents = async (zipPath) => {
43
50
  }
44
51
  if (packageJsonFiles.length !== 0) {
45
52
  await zip.close();
46
- return { packageJsonFiles };
53
+ return {
54
+ packageJsonFiles,
55
+ ...(Object.keys(awsSdkPackageJsonMap).length > 0 && { awsSdkPackageJsonMap }),
56
+ };
47
57
  }
48
58
  for (const path of ["index.js", "index.mjs", "index.cjs"]) {
49
59
  if (!zipEntries[path])
@@ -1,10 +1,11 @@
1
- import { satisfies } from "compare-versions";
1
+ import { satisfies, validate } from "compare-versions";
2
2
  import { downloadFile } from "./downloadFile.js";
3
3
  import { getLambdaFunctionContents, } from "./getLambdaFunctionContents.js";
4
4
  import { hasSdkV2InBundle } from "./hasSdkV2InBundle.js";
5
5
  import { rm } from "node:fs/promises";
6
6
  import { tmpdir } from "node:os";
7
- import { join } from "node:path";
7
+ import { dirname, join } from "node:path";
8
+ import { AWS_SDK, NODE_MODULES, PACKAGE_JSON } from "./constants.js";
8
9
  /**
9
10
  * Scans a Lambda function to detect AWS SDK for JavaScript v2 usage.
10
11
  *
@@ -44,16 +45,37 @@ export const getLambdaFunctionScanOutput = async (client, { functionName, region
44
45
  finally {
45
46
  await rm(zipPath, { force: true });
46
47
  }
47
- const { packageJsonFiles, bundleFile } = lambdaFunctionContents;
48
- // Search for "aws-sdk" in package.json dependencies if present.
48
+ const { packageJsonFiles, awsSdkPackageJsonMap, bundleFile } = lambdaFunctionContents;
49
+ // Search for JS SDK v2 in package.json dependencies if present.
49
50
  if (packageJsonFiles && packageJsonFiles.length > 0) {
50
51
  for (const { path: packageJsonPath, content: packageJsonContent } of packageJsonFiles) {
51
52
  try {
52
53
  const packageJson = JSON.parse(packageJsonContent);
53
54
  const dependencies = packageJson.dependencies || {};
54
- if ("aws-sdk" in dependencies) {
55
+ if (AWS_SDK in dependencies) {
56
+ const awsSdkVersionInPackageJson = dependencies[AWS_SDK];
57
+ const awsSdkPackageJsonPathInNodeModules = join(NODE_MODULES, AWS_SDK, PACKAGE_JSON);
58
+ // Get aws-sdk package.json from nested node_modules or root node_modules.
59
+ const awsSdkPackageJson = awsSdkPackageJsonMap
60
+ ? (awsSdkPackageJsonMap[join(dirname(packageJsonPath), awsSdkPackageJsonPathInNodeModules)] ?? awsSdkPackageJsonMap[awsSdkPackageJsonPathInNodeModules])
61
+ : undefined;
62
+ let awsSdkVersionInNodeModules;
55
63
  try {
56
- if (!satisfies(dependencies["aws-sdk"], sdkVersionRange)) {
64
+ if (awsSdkPackageJson) {
65
+ awsSdkVersionInNodeModules = JSON.parse(awsSdkPackageJson).version;
66
+ }
67
+ }
68
+ catch {
69
+ // Skip if JSON can't be parsed.
70
+ // ToDo: add warning when logging is supported in future.
71
+ }
72
+ const sdkVersionToCheck = validate(awsSdkVersionInPackageJson) || awsSdkPackageJson === undefined
73
+ ? // Use version in package.json dependencies, if fixed version is defined or aws-sdk package.json is not available.
74
+ awsSdkVersionInPackageJson
75
+ : // Use version from aws-sdk package.json, if defined
76
+ (awsSdkVersionInNodeModules ?? awsSdkVersionInPackageJson);
77
+ try {
78
+ if (!satisfies(sdkVersionToCheck, sdkVersionRange)) {
57
79
  continue;
58
80
  }
59
81
  }
@@ -76,7 +98,7 @@ export const getLambdaFunctionScanOutput = async (client, { functionName, region
76
98
  }
77
99
  }
78
100
  }
79
- // Check for code of "aws-sdk" in bundle, if not found in package.json dependencies.
101
+ // Check for signature of JS SDK v2 in bundle, if not found in package.json dependencies.
80
102
  if (bundleFile) {
81
103
  try {
82
104
  if (hasSdkV2InBundle(bundleFile.content, sdkVersionRange)) {
@@ -92,7 +114,7 @@ export const getLambdaFunctionScanOutput = async (client, { functionName, region
92
114
  return output;
93
115
  }
94
116
  }
95
- // "aws-sdk" dependency/code not found.
117
+ // JS SDK v2 dependency/code not found.
96
118
  output.ContainsAwsSdkJsV2 = false;
97
119
  return output;
98
120
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-sdk/find-v2",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
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",