@byaga/cdk-patterns 0.6.1 → 0.8.0

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.
Files changed (62) hide show
  1. package/.fleet/run.json +11 -0
  2. package/lib/ApiCertificate.d.ts +2 -2
  3. package/lib/ApiCertificate.js +4 -5
  4. package/lib/CognitoApiGatewayAuthorizer.d.ts +19 -0
  5. package/lib/CognitoApiGatewayAuthorizer.js +24 -0
  6. package/lib/DeployStack.d.ts +20 -0
  7. package/lib/DeployStack.js +48 -0
  8. package/lib/DynamoDbTable.d.ts +12 -0
  9. package/lib/DynamoDbTable.js +34 -0
  10. package/lib/FunctionIntegration.d.ts +21 -0
  11. package/lib/FunctionIntegration.js +61 -0
  12. package/lib/ICorsConfig.d.ts +5 -0
  13. package/lib/ICorsConfig.js +2 -0
  14. package/lib/NodeJsLambda.d.ts +13 -0
  15. package/lib/NodeJsLambda.js +28 -0
  16. package/lib/Output.d.ts +5 -0
  17. package/lib/Output.js +13 -0
  18. package/lib/RestApi.d.ts +28 -0
  19. package/lib/RestApi.js +142 -0
  20. package/lib/Role.d.ts +2 -2
  21. package/lib/Role.js +2 -5
  22. package/lib/SsmParameter.d.ts +18 -0
  23. package/lib/SsmParameter.js +40 -0
  24. package/lib/StaticWebSite.d.ts +12 -0
  25. package/lib/StaticWebSite.js +107 -0
  26. package/lib/index.d.ts +7 -1
  27. package/lib/index.js +15 -3
  28. package/lib/methods/apply-honeycomb-to-lambda.d.ts +3 -0
  29. package/lib/methods/apply-honeycomb-to-lambda.js +22 -0
  30. package/lib/methods/build-node-source.d.ts +13 -0
  31. package/lib/methods/build-node-source.js +105 -0
  32. package/lib/methods/duration.d.ts +8 -0
  33. package/lib/methods/duration.js +22 -0
  34. package/lib/methods/empty-directory.d.ts +2 -0
  35. package/lib/methods/empty-directory.js +47 -0
  36. package/lib/methods/generate-hash.d.ts +15 -0
  37. package/lib/methods/generate-hash.js +59 -0
  38. package/lib/methods/get-source-directory.d.ts +4 -0
  39. package/lib/methods/get-source-directory.js +37 -0
  40. package/lib/methods/walk-directory.d.ts +14 -0
  41. package/lib/methods/walk-directory.js +48 -0
  42. package/package.json +4 -2
  43. package/src/ApiCertificate.ts +6 -6
  44. package/src/CognitoApiGatewayAuthorizer.ts +25 -0
  45. package/src/{IDeployStack.ts → DeployStack.ts} +4 -4
  46. package/src/DynamoDbTable.ts +40 -0
  47. package/src/FunctionIntegration.ts +83 -0
  48. package/src/ICorsConfig.ts +5 -0
  49. package/src/NodeJsLambda.ts +31 -0
  50. package/src/Output.ts +11 -0
  51. package/src/RestApi.ts +178 -0
  52. package/src/Role.ts +5 -7
  53. package/src/SsmParameter.ts +47 -0
  54. package/src/StaticWebSite.ts +99 -0
  55. package/src/index.ts +7 -1
  56. package/src/methods/apply-honeycomb-to-lambda.ts +22 -0
  57. package/src/methods/build-node-source.ts +97 -0
  58. package/src/methods/duration.ts +24 -0
  59. package/src/methods/empty-directory.ts +19 -0
  60. package/src/methods/generate-hash.ts +49 -0
  61. package/src/methods/get-source-directory.ts +11 -0
  62. package/src/methods/walk-directory.ts +30 -0
@@ -0,0 +1,97 @@
1
+ import * as path from 'path'
2
+ import * as fse from "fs-extra";
3
+ import * as fs from "fs";
4
+ import {execSync} from "child_process";
5
+ import {generateHash, HashResult} from "./generate-hash"
6
+ import {getBuildDirectory, getSourceDirectory} from './get-source-directory'
7
+ import duration from "./duration";
8
+ import emptyDirSync from './empty-directory'
9
+ import {IIgnoreOptions} from "./walk-directory";
10
+
11
+ const distributionRoot = path.resolve(process.cwd(), "../dist")
12
+ const hashRoot = path.join(distributionRoot, "hash")
13
+ fse.ensureDirSync(hashRoot)
14
+
15
+ export const defaultIgnore: IIgnoreOptions = {
16
+ included: (p: fs.Dirent): boolean => {
17
+ return !p.name.startsWith(".") &&
18
+ !p.name.endsWith(".md") &&
19
+ !p.name.includes(".test.") &&
20
+ p.name !== 'package-lock.json';
21
+ },
22
+ childrenIncluded: (p: fs.Dirent): boolean => {
23
+ return p.name === "node_modules";
24
+ }
25
+ } as IIgnoreOptions
26
+
27
+ interface IBuildOptions {
28
+ dir?: string,
29
+ ignore?: IIgnoreOptions
30
+ }
31
+
32
+ export function buildNodeSource(type: string, id: string, options: IBuildOptions = {}): {
33
+ buildDir: string,
34
+ moduleChanged: boolean,
35
+ packageChanged: boolean,
36
+ sourceChanged: boolean
37
+ } {
38
+ const {dir = ''} = options
39
+ const ignore = {
40
+ ...defaultIgnore, ...options.ignore
41
+ }
42
+ const srcDir = getSourceDirectory(type, id)
43
+ const buildDir = getBuildDirectory(type, id)
44
+ const hashFilePath = path.join(hashRoot, `${type}-${id}-hash.txt`)
45
+
46
+ const folderExists = fs.existsSync(buildDir)
47
+ const hashFileExists = folderExists && fse.existsSync(hashFilePath)
48
+ const prevHash: HashResult = (hashFileExists ? fse.readJsonSync(hashFilePath) : {}) as HashResult
49
+ const hash: string = generateHash(srcDir, {ignore});
50
+ const nextHash: HashResult = JSON.parse(generateHash(srcDir, {ignore})) as HashResult;
51
+ const moduleChanged = JSON.stringify(prevHash, null, '\t') !== hash;
52
+ let packageChanged = false, sourceChanged = false;
53
+
54
+ if (moduleChanged) {
55
+ const prevPackageHash = findPackageHash(prevHash);
56
+ const packageHash = findPackageHash(nextHash);
57
+ packageChanged = prevPackageHash !== packageHash;
58
+
59
+ sourceChanged = sourceWasUpdated(prevHash, nextHash);
60
+ if (sourceChanged) {
61
+ const rmComplete = duration()
62
+ if (folderExists) emptyDirSync(buildDir, {childrenExcluded: folder => folder.name === 'node_modules'})
63
+ console.log('Cleanup Duration (ms)', rmComplete())
64
+
65
+ const copyComplete = duration()
66
+ fse.copySync(srcDir, buildDir, {
67
+ filter: src => !~src.indexOf("node_modules") && !src.endsWith('.test.js')
68
+ });
69
+ console.log('Copy Duration (ms)', copyComplete())
70
+ }
71
+
72
+ if (packageChanged) {
73
+ const installComplete = duration()
74
+ execSync("npm i --omit=dev --omit=optional --omit=peer --quite", {
75
+ cwd: dir ? path.resolve(buildDir, dir) : buildDir
76
+ });
77
+ console.log('NPM Install Duration (ms)', installComplete())
78
+ }
79
+ fs.writeFileSync(hashFilePath, hash);
80
+ }
81
+
82
+ return {buildDir, packageChanged, sourceChanged, moduleChanged}
83
+ }
84
+
85
+ function findPackageHash(hash: HashResult): string | undefined {
86
+ return hash?.children?.find(file => file.name === 'package.json')?.hash;
87
+ }
88
+
89
+ function sourceWasUpdated(prevHash: HashResult, nextHash: HashResult): boolean {
90
+ const fileCountChanged = prevHash.children?.length != nextHash.children?.length
91
+ const updatedItem = prevHash.children?.find(prevFile => {
92
+ const nextFile = nextHash.children?.find(f => f.name = prevFile.name);
93
+ return prevFile.name !== 'package.json' && (!nextFile || nextFile.hash !== prevFile.hash)
94
+ });
95
+
96
+ return fileCountChanged || !!updatedItem
97
+ }
@@ -0,0 +1,24 @@
1
+ const hrDuration = () => {
2
+ const startTime: [number, number] = process.hrtime();
3
+ const onEnd = function duration(): number {
4
+ const hrTime:[number, number] = process.hrtime(startTime);
5
+ return hrTime[0] * 1000 + hrTime[1] / 1000000;
6
+ };
7
+ onEnd.time = startTime;
8
+
9
+ return onEnd;
10
+ };
11
+ const msDuration = () => {
12
+ const startTime: number = Date.now();
13
+ const onEnd = function duration(): number {
14
+ return Date.now() - startTime;
15
+ };
16
+ onEnd.time = startTime;
17
+
18
+ return onEnd;
19
+ };
20
+
21
+ // @ts-ignore
22
+ const duration= process.hrtime ? hrDuration : msDuration
23
+
24
+ export default duration
@@ -0,0 +1,19 @@
1
+ import * as fse from "fs-extra";
2
+ import duration from "./duration";
3
+ import walkDirectory, {DirentExtended, IIgnoreOptions} from "./walk-directory";
4
+
5
+ export default function emptyDirSync(parentFolder: string, options: IIgnoreOptions) {
6
+ const rmComplete = duration()
7
+ const files = walkDirectory(parentFolder, options);
8
+
9
+ files
10
+ .forEach((file:DirentExtended) => {
11
+ try {
12
+ fse.removeSync(file.fullpath());
13
+ console.log('Deleted file:', file.fullpath());
14
+ } catch (err) {
15
+ console.error('Error deleting file:', err);
16
+ }
17
+ });
18
+ console.log('Empty Directory Complete (ms)', parentFolder, rmComplete())
19
+ }
@@ -0,0 +1,49 @@
1
+ import * as crypto from 'crypto'
2
+ import * as path from 'path'
3
+ import * as fs from 'fs'
4
+ import walk, {IIgnoreOptions} from './walk-directory'
5
+
6
+ const ALGO = 'sha1';
7
+ const ENCODING = 'base64';
8
+
9
+ interface HashOptions {
10
+ ignore: IIgnoreOptions
11
+ }
12
+
13
+ export interface HashResult {
14
+ hash?: string
15
+ children?: HashItem[]
16
+ }
17
+ export interface HashItem {
18
+ hash: string,
19
+ path: string,
20
+ name: string
21
+ }
22
+
23
+ export function generateHash(folder: string, opt: HashOptions): string {
24
+ const files = walk(folder, opt.ignore);
25
+
26
+ const hash = crypto.createHash(ALGO);
27
+ const children = files
28
+ .sort((a, b) => a.fullpath().localeCompare(b.fullpath()))
29
+ .map((child) => {
30
+ const relPath = path.relative(process.cwd(), child.fullpath())
31
+ const fileHash = crypto.createHash(ALGO);
32
+ fileHash.update(relPath);
33
+ fileHash.update(fs.readFileSync(child.fullpath()));
34
+
35
+ const file = {
36
+ name: child.name,
37
+ path: relPath,
38
+ hash: fileHash.digest(ENCODING)
39
+ };
40
+ hash.update(file.hash);
41
+
42
+ return file;
43
+ });
44
+
45
+ return JSON.stringify({
46
+ hash: hash.digest(ENCODING),
47
+ children
48
+ }, null, '\t')
49
+ }
@@ -0,0 +1,11 @@
1
+ import * as path from 'path'
2
+
3
+ export const sourceRoot = path.resolve(process.cwd(), "../src");
4
+ export const distributionRoot = path.resolve(process.cwd(), "../dist");
5
+
6
+ export function getSourceDirectory(type: string, id: string): string {
7
+ return path.join(sourceRoot, type, id)
8
+ }
9
+ export function getBuildDirectory(type: string, id: string): string {
10
+ return path.join(distributionRoot, type, id)
11
+ }
@@ -0,0 +1,30 @@
1
+ import * as path from 'path'
2
+ import * as fs from 'fs'
3
+
4
+ export interface IIgnoreOptions {
5
+ excluded?: (stat: fs.Dirent) => boolean,
6
+ included?: (stat: fs.Dirent) => boolean,
7
+ childrenExcluded?: (stat: fs.Dirent) => boolean,
8
+ childrenIncluded?: (stat: fs.Dirent) => boolean
9
+ }
10
+ export const yes = () => true
11
+ export const no = () => false
12
+
13
+ export default function walkDirectory(directory: string, options: IIgnoreOptions, filepaths: DirentExtended[] = []): DirentExtended[] {
14
+ const { excluded = no, included = yes, childrenExcluded = no, childrenIncluded = yes} = options;
15
+ const files = fs.readdirSync(directory, {withFileTypes: true}) as unknown[] as DirentExtended[];
16
+ for (let stats of files) {
17
+ const filepath = path.join(directory, stats.name)
18
+ stats.fullpath = () => filepath
19
+ if (stats.isDirectory() && !childrenExcluded(stats) && childrenIncluded(stats)) {
20
+ walkDirectory(filepath, options, filepaths)
21
+ } else if (stats.isFile() && !excluded(stats) && included(stats)) {
22
+ filepaths.push(stats)
23
+ }
24
+ }
25
+ return filepaths;
26
+ }
27
+
28
+ export interface DirentExtended extends fs.Dirent {
29
+ fullpath: () => string
30
+ }