@fy-stack/cdn-construct 0.0.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.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # cdn-construct
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build cdn-construct` to build the library.
@@ -0,0 +1,3 @@
1
+ export { CDNConstruct } from './lib/cdn-construct';
2
+ export type { CDNConstructProps } from './lib/types';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CDNConstruct = void 0;
4
+ var cdn_construct_1 = require("./lib/cdn-construct");
5
+ Object.defineProperty(exports, "CDNConstruct", { enumerable: true, get: function () { return cdn_construct_1.CDNConstruct; } });
@@ -0,0 +1,8 @@
1
+ import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
2
+ import { Construct } from 'constructs';
3
+ import { CDNConstructProps } from './types';
4
+ export declare class CDNConstruct extends Construct {
5
+ distribution: cloudfront.Distribution;
6
+ constructor(scope: Construct, id: string, props: CDNConstructProps);
7
+ }
8
+ //# sourceMappingURL=cdn-construct.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cdn-construct.d.ts","sourceRoot":"","sources":["../../src/lib/cdn-construct.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,UAAU,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE5C,qBAAa,YAAa,SAAQ,SAAS;IAClC,YAAY,EAAE,UAAU,CAAC,YAAY,CAAC;gBAEjC,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,iBAAiB;CA+DnE"}
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CDNConstruct = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const cloudfront = tslib_1.__importStar(require("aws-cdk-lib/aws-cloudfront"));
6
+ const constructs_1 = require("constructs");
7
+ class CDNConstruct extends constructs_1.Construct {
8
+ distribution;
9
+ constructor(scope, id, props) {
10
+ super(scope, id);
11
+ const routes = {};
12
+ Object.assign(routes, Object.fromEntries(Object.entries(props.routes).map(([key, val]) => {
13
+ if ('$app' in val) {
14
+ const app = props.apps?.[val.$app];
15
+ if (!app)
16
+ throw new Error(`"${val.$app}" app not found`);
17
+ app.function.addEnvironment('BASE_PATH', key);
18
+ return [key, app];
19
+ }
20
+ else {
21
+ const resource = props.resources?.[val.$resource];
22
+ if (!resource)
23
+ throw new Error(`${val.$resource} resource not found`);
24
+ return [key, resource];
25
+ }
26
+ })));
27
+ const { '/*': base, ...otherRoutes } = routes;
28
+ if (!base)
29
+ throw new Error('no base route');
30
+ const { '/*': defaultBehavior, ...additionalBehaviors } = base.cloudfront('');
31
+ if (!defaultBehavior)
32
+ throw new Error('no default behaviour');
33
+ for (const i in otherRoutes) {
34
+ Object.assign(additionalBehaviors, otherRoutes[i]?.cloudfront(i));
35
+ }
36
+ this.distribution = new cloudfront.Distribution(this, 'CDN', {
37
+ defaultBehavior,
38
+ additionalBehaviors,
39
+ priceClass: cloudfront.PriceClass.PRICE_CLASS_100,
40
+ });
41
+ /*if (props.storage && uploadEnabled) {
42
+ props.storage.bucket.addToResourcePolicy(
43
+ new iam.PolicyStatement({
44
+ effect: iam.Effect.ALLOW,
45
+ actions: ['s3:GetObject'],
46
+ principals: [new iam.ServicePrincipal('cloudfront.amazonaws.com')],
47
+ resources: ['arn:aws:s3:::' + props.storage.bucket.bucketName + '/!*'],
48
+ conditions: {
49
+ StringEquals: {
50
+ 'AWS:SourceArn':
51
+ 'arn:aws:cloudfront::' +
52
+ account +
53
+ ':distribution/' +
54
+ this.distribution.distributionId,
55
+ },
56
+ },
57
+ })
58
+ );
59
+ }*/
60
+ }
61
+ }
62
+ exports.CDNConstruct = CDNConstruct;
@@ -0,0 +1,14 @@
1
+ import type { CDNResource } from '@fy-stack/types';
2
+ import type { Function } from 'aws-cdk-lib/aws-lambda';
3
+ export interface CDNConstructProps {
4
+ routes: Record<string, {
5
+ $app: string;
6
+ } | {
7
+ $resource: string;
8
+ }>;
9
+ apps?: Record<string, (CDNResource & {
10
+ function: Function;
11
+ }) | undefined>;
12
+ resources?: Record<string, CDNResource | undefined>;
13
+ }
14
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAEvD,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACjE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,WAAW,GAAG;QAAE,QAAQ,EAAE,QAAQ,CAAA;KAAE,CAAC,GAAG,SAAS,CAAC,CAAC;IAC1E,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC,CAAC;CACrD"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@fy-stack/cdn-construct",
3
+ "version": "0.0.1",
4
+ "dependencies": {
5
+ "tslib": "^2.3.0"
6
+ },
7
+ "peerDependencies": {
8
+ "aws-cdk-lib": "2.164.1",
9
+ "constructs": "10.4.2",
10
+ "@fy-stack/types": "0.0.1"
11
+ },
12
+ "type": "commonjs",
13
+ "main": "./dist/index.js",
14
+ "typings": "./dist/index.d.ts",
15
+ "files": [
16
+ "dist",
17
+ "!**/*.tsbuildinfo"
18
+ ],
19
+ "nx": {
20
+ "name": "cdn-construct"
21
+ }
22
+ }