@bifravst/aws-cdk-lambda-helpers 2.2.0 → 2.2.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.
@@ -9,9 +9,13 @@ export const findDependencies = (args) => {
9
9
  const visited = args.visited ?? [];
10
10
  const dependencies = args.imports ?? [];
11
11
  const packages = args.packages ?? new Set();
12
- let importsSubpathPatterns = args.importsSubpathPatterns ?? {};
12
+ const importsSubpathPatterns = args.importsSubpathPatterns ?? {};
13
13
  if (visited.includes(sourceFilePath))
14
- return { dependencies, importsSubpathPatterns, packages };
14
+ return {
15
+ dependencies,
16
+ importsSubpathPatterns,
17
+ packages,
18
+ };
15
19
  const tsConfigFilePath = args.tsConfigFilePath;
16
20
  const tsConfig = tsConfigFilePath !== undefined
17
21
  ? JSON.parse(readFileSync(tsConfigFilePath, 'utf-8').toString())
@@ -23,14 +27,13 @@ export const findDependencies = (args) => {
23
27
  node.kind !== ts.SyntaxKind.ExportDeclaration)
24
28
  return;
25
29
  const moduleSpecifier = node.moduleSpecifier.text;
26
- const { resolvedPath: file, importsSubpathPatterns: updatedImportsSubpathPatterns, } = resolve({
30
+ const { resolvedPath: file } = resolve({
27
31
  moduleSpecifier,
28
32
  sourceFilePath,
29
33
  tsConfigFilePath,
30
34
  tsConfig,
31
35
  importsSubpathPatterns,
32
36
  });
33
- importsSubpathPatterns = updatedImportsSubpathPatterns;
34
37
  try {
35
38
  const s = statSync(file);
36
39
  if (!s.isDirectory())
@@ -54,7 +57,25 @@ export const findDependencies = (args) => {
54
57
  packages,
55
58
  });
56
59
  }
57
- return { dependencies, importsSubpathPatterns, packages };
60
+ return {
61
+ dependencies,
62
+ importsSubpathPatterns,
63
+ packages: new Set([
64
+ ...packages.difference(new Set([
65
+ 'aws-lambda', // Ignore type-only package
66
+ ])),
67
+ ]
68
+ .filter((p) => !p.startsWith('node:'))
69
+ .filter((p) => !p.startsWith('@aws-crypto/'))
70
+ .filter((p) => !p.startsWith('@aws-sdk/'))
71
+ .map((d) => {
72
+ if (d.startsWith('@')) {
73
+ const [org, packageName] = d.split('/');
74
+ return `${org}/${packageName}`;
75
+ }
76
+ return d.split('/')[0];
77
+ })),
78
+ };
58
79
  };
59
80
  const resolve = ({ moduleSpecifier, sourceFilePath, tsConfigFilePath, tsConfig, importsSubpathPatterns, }) => {
60
81
  if (moduleSpecifier.startsWith('.'))
@@ -67,7 +88,6 @@ const resolve = ({ moduleSpecifier, sourceFilePath, tsConfigFilePath, tsConfig,
67
88
  // Example: import { Network, notifyClients } from './notifyClients.js'
68
89
  // The source file for that is actually in './notifyClients.ts'
69
90
  .replace(/\.js$/, '.ts'),
70
- importsSubpathPatterns,
71
91
  };
72
92
  if (tsConfigFilePath !== undefined &&
73
93
  tsConfig?.compilerOptions?.paths !== undefined) {
@@ -78,16 +98,13 @@ const resolve = ({ moduleSpecifier, sourceFilePath, tsConfigFilePath, tsConfig,
78
98
  // Exact match
79
99
  if (moduleSpecifier === key) {
80
100
  const fullResolvedPath = path.join(path.parse(tsConfigFilePath).dir, tsConfig.compilerOptions.baseUrl, resolvedPath);
101
+ importsSubpathPatterns[key] = [
102
+ tsConfig.compilerOptions.baseUrl,
103
+ path.sep,
104
+ resolvedPath.replace(/\.ts$/, '.js'),
105
+ ].join('');
81
106
  return {
82
107
  resolvedPath: fullResolvedPath,
83
- importsSubpathPatterns: {
84
- ...importsSubpathPatterns,
85
- [key]: [
86
- tsConfig.compilerOptions.baseUrl,
87
- path.sep,
88
- resolvedPath.replace(/\.ts$/, '.js'),
89
- ].join(''),
90
- },
91
108
  };
92
109
  }
93
110
  // Wildcard match
@@ -97,24 +114,20 @@ const resolve = ({ moduleSpecifier, sourceFilePath, tsConfigFilePath, tsConfig,
97
114
  const maybeMatch = rx.exec(moduleSpecifier);
98
115
  if (maybeMatch?.groups?.wildcard === undefined)
99
116
  continue;
117
+ importsSubpathPatterns[key] = [
118
+ tsConfig.compilerOptions.baseUrl,
119
+ path.sep,
120
+ resolvedPath.replace(/\.ts$/, '.js'),
121
+ ].join('');
100
122
  return {
101
123
  resolvedPath: path
102
124
  .resolve(path.parse(tsConfigFilePath).dir, tsConfig.compilerOptions.baseUrl, resolvedPath.replace('*', maybeMatch.groups.wildcard))
103
125
  // Same as above, replace `.js` with `.ts`
104
126
  .replace(/\.js$/, '.ts'),
105
- importsSubpathPatterns: {
106
- ...importsSubpathPatterns,
107
- [key]: [
108
- tsConfig.compilerOptions.baseUrl,
109
- path.sep,
110
- resolvedPath.replace(/\.ts$/, '.js'),
111
- ].join(''),
112
- },
113
127
  };
114
128
  }
115
129
  }
116
130
  return {
117
131
  resolvedPath: moduleSpecifier,
118
- importsSubpathPatterns,
119
132
  };
120
133
  };
@@ -9,8 +9,11 @@ void describe('findDependencies()', () => {
9
9
  const { packages } = findDependencies({
10
10
  sourceFilePath: path.join(__dirname, '..', 'cdk', 'lambda.ts'),
11
11
  });
12
- assert.equal(packages.has('aws-lambda'), true, "Should include the 'aws-lambda' package");
13
12
  assert.equal(packages.has('id128'), true, "Should include the 'id128' package");
13
+ assert.equal(packages.has('aws-lambda'), false, "Should not include the type-only 'aws-lambda' package");
14
+ assert.equal(packages.has('node:crypto'), false, 'Should not include built-in node dependencies');
15
+ assert.equal(packages.has('fp-ts'), true, 'Should include the top-level package only');
16
+ assert.equal(packages.has('@aws-sdk/client-dynamodb'), false, 'Should not include AWS SDK packages');
14
17
  });
15
18
  void it('should honor tsconfig.json paths', () => {
16
19
  const { dependencies } = findDependencies({
@@ -26,7 +26,10 @@ export const packLambda = async ({ sourceFilePath, zipFilePath, tsConfigFilePath
26
26
  sourceFilePath,
27
27
  tsConfigFilePath,
28
28
  });
29
- debug?.(`dependencies`, [...packages].join(', '));
29
+ debug?.(`${sourceFilePath}: dependencies`, [...packages].join(', '));
30
+ Object.entries(importsSubpathPatterns).forEach(([k, v]) => {
31
+ debug?.(`${sourceFilePath}:importsSubpathPattern`, `${k} -> ${v}`);
32
+ });
30
33
  const lambdaFiles = [sourceFilePath, ...deps];
31
34
  const zipfile = new yazl.ZipFile();
32
35
  const stripCommon = removeCommonAncestor(commonParent(lambdaFiles));
@@ -63,7 +66,9 @@ export const packLambda = async ({ sourceFilePath, zipFilePath, tsConfigFilePath
63
66
  zipfile.addBuffer(Buffer.from(JSON.stringify({
64
67
  type: 'module',
65
68
  imports: importsSubpathPatterns,
66
- dependencies: Object.fromEntries(packages.values().map((pkg) => [pkg, '*'])),
69
+ dependencies: Object.fromEntries([...packages.values()]
70
+ .sort((a, b) => a.localeCompare(b))
71
+ .map((pkg) => [pkg, '*'])),
67
72
  }), 'utf-8'), 'package.json');
68
73
  progress?.(`added`, 'package.json');
69
74
  await new Promise((resolve) => {
@@ -1,5 +1,5 @@
1
1
  import { type PackedLambda } from './packLambda.js';
2
- export declare const packLambdaFromPath: ({ id, sourceFilePath, handlerFunction: handlerFunctionArg, baseDir: baseDirArg, distDir: distDirArg, tsConfigFilePath, }: {
2
+ export declare const packLambdaFromPath: ({ id, sourceFilePath, handlerFunction: handlerFunctionArg, baseDir: baseDirArg, distDir: distDirArg, tsConfigFilePath, debug, progress, }: {
3
3
  id: string;
4
4
  sourceFilePath: string;
5
5
  handlerFunction?: string;
@@ -15,4 +15,6 @@ export declare const packLambdaFromPath: ({ id, sourceFilePath, handlerFunction:
15
15
  * Pass the path to the tsconfig.json file if you want to use paths from the tsconfig.json file.
16
16
  */
17
17
  tsConfigFilePath?: string;
18
+ debug?: (label: string, info: string) => void;
19
+ progress?: (label: string, info: string) => void;
18
20
  }) => Promise<PackedLambda>;
@@ -1,7 +1,7 @@
1
1
  import { mkdir } from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
  import { packLambda } from './packLambda.js';
4
- export const packLambdaFromPath = async ({ id, sourceFilePath, handlerFunction: handlerFunctionArg, baseDir: baseDirArg, distDir: distDirArg, tsConfigFilePath, }) => {
4
+ export const packLambdaFromPath = async ({ id, sourceFilePath, handlerFunction: handlerFunctionArg, baseDir: baseDirArg, distDir: distDirArg, tsConfigFilePath, debug, progress, }) => {
5
5
  const distDir = distDirArg ?? path.join(process.cwd(), 'dist', 'lambdas');
6
6
  const baseDir = baseDirArg ?? process.cwd();
7
7
  const handlerFunction = handlerFunctionArg ?? 'handler';
@@ -18,6 +18,8 @@ export const packLambdaFromPath = async ({ id, sourceFilePath, handlerFunction:
18
18
  sourceFilePath: path.join(baseDir, sourceFilePath),
19
19
  zipFilePath: zipFile,
20
20
  tsConfigFilePath,
21
+ debug,
22
+ progress,
21
23
  });
22
24
  return {
23
25
  id,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bifravst/aws-cdk-lambda-helpers",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "Helper functions which simplify working with TypeScript lambdas for AWS CDK.",
5
5
  "exports": {
6
6
  ".": {
@@ -52,6 +52,7 @@
52
52
  "license": "BSD-3-Clause",
53
53
  "devDependencies": {
54
54
  "@aws-sdk/client-cloudformation": "3.693.0",
55
+ "@aws-sdk/client-dynamodb": "3.693.0",
55
56
  "@bifravst/cloudformation-helpers": "9.1.1",
56
57
  "@bifravst/eslint-config-typescript": "6.1.18",
57
58
  "@bifravst/from-env": "3.0.2",
@@ -108,6 +109,7 @@
108
109
  "prettier": "@bifravst/prettier-config",
109
110
  "dependencies": {
110
111
  "@swc/core": "1.9.2",
112
+ "fp-ts": "2.16.9",
111
113
  "glob": "11.0.0",
112
114
  "typescript": "5.6.3",
113
115
  "yazl": "3.3.0"