-
Notifications
You must be signed in to change notification settings - Fork 4k
/
function.ts
215 lines (183 loc) · 4.74 KB
/
function.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import * as fs from 'fs';
import { IResource, Names, Resource, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnFunction } from './cloudfront.generated';
/**
* Represents the function's source code
*/
export abstract class FunctionCode {
/**
* Inline code for function
* @returns code object with inline code.
* @param code The actual function code
*/
public static fromInline(code: string): FunctionCode {
return new InlineCode(code);
}
/**
* Code from external file for function
* @returns code object with contents from file.
* @param options the options for the external file
*/
public static fromFile(options: FileCodeOptions): FunctionCode {
return new FileCode(options);
}
/**
* renders the function code
*/
public abstract render(): string;
}
/**
* Options when reading the function's code from an external file
*/
export interface FileCodeOptions {
/**
* The path of the file to read the code from
*/
readonly filePath: string;
}
/**
* Represents the function's source code as inline code
*/
class InlineCode extends FunctionCode {
constructor(private code: string) {
super();
}
public render(): string {
return this.code;
}
}
/**
* Represents the function's source code loaded from an external file
*/
class FileCode extends FunctionCode {
constructor(private options: FileCodeOptions) {
super();
}
public render(): string {
return fs.readFileSync(this.options.filePath, { encoding: 'utf-8' });
}
}
/**
* Represents a CloudFront Function
*/
export interface IFunction extends IResource {
/**
* The name of the function.
* @attribute
*/
readonly functionName: string;
/**
* The ARN of the function.
* @attribute
*/
readonly functionArn: string;
}
/**
* Attributes of an existing CloudFront Function to import it
*/
export interface FunctionAttributes {
/**
* The name of the function.
*/
readonly functionName: string;
/**
* The ARN of the function.
*/
readonly functionArn: string;
}
/**
* Properties for creating a CloudFront Function
*/
export interface FunctionProps {
/**
* A name to identify the function.
* @default - generated from the `id`
*/
readonly functionName?: string;
/**
* A comment to describe the function.
* @default - same as `functionName`
*/
readonly comment?: string;
/**
* The source code of the function.
*/
readonly code: FunctionCode;
}
/**
* A CloudFront Function
*
* @resource AWS::CloudFront::Function
*/
export class Function extends Resource implements IFunction {
/** Imports a function by its name and ARN */
public static fromFunctionAttributes(scope: Construct, id: string, attrs: FunctionAttributes): IFunction {
return new class extends Resource implements IFunction {
public readonly functionName = attrs.functionName;
public readonly functionArn = attrs.functionArn;
}(scope, id);
}
/**
* the name of the CloudFront function
* @attribute
*/
public readonly functionName: string;
/**
* the ARN of the CloudFront function
* @attribute
*/
public readonly functionArn: string;
/**
* the deployment stage of the CloudFront function
* @attribute
*/
public readonly functionStage: string;
constructor(scope: Construct, id: string, props: FunctionProps) {
super(scope, id);
this.functionName = props.functionName ?? this.generateName();
const resource = new CfnFunction(this, 'Resource', {
autoPublish: true,
functionCode: props.code.render(),
functionConfig: {
comment: props.comment ?? this.functionName,
runtime: 'cloudfront-js-1.0',
},
name: this.functionName,
});
this.functionArn = resource.attrFunctionArn;
this.functionStage = resource.attrStage;
}
private generateName(): string {
const name = Stack.of(this).region + Names.uniqueId(this);
if (name.length > 64) {
return name.substring(0, 32) + name.substring(name.length - 32);
}
return name;
}
}
/**
* The type of events that a CloudFront function can be invoked in response to.
*/
export enum FunctionEventType {
/**
* The viewer-request specifies the incoming request
*/
VIEWER_REQUEST = 'viewer-request',
/**
* The viewer-response specifies the outgoing response
*/
VIEWER_RESPONSE = 'viewer-response',
}
/**
* Represents a CloudFront function and event type when using CF Functions.
* The type of the {@link AddBehaviorOptions.functionAssociations} property.
*/
export interface FunctionAssociation {
/**
* The CloudFront function that will be invoked.
*/
readonly function: IFunction;
/** The type of event which should invoke the function. */
readonly eventType: FunctionEventType;
}