forked from aws-solutions/video-on-demand-on-aws
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_context-variables.ts
159 lines (133 loc) · 5.27 KB
/
_context-variables.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
import { Stack } from 'aws-cdk-lib';
const convertToBool = (value: string | boolean | Number | null | undefined) => {
if (value === undefined || !value) {
return null;
}
switch (value) {
case true:
case 'true':
case 1:
case '1':
case 'on':
case 'yes':
return true;
default:
return false;
}
};
export class ContextVariables {
public readonly acceleratedTranscoding: string;
public readonly adminEmail: string;
public readonly authenticationSubDomain: string | undefined;
public readonly authenticationDomain: string | undefined;
public readonly cloudFrontDomainBase: string | undefined;
public readonly enableMediaPackage: boolean;
public readonly enableSns: boolean;
public readonly enableSqs: boolean;
public readonly frameCapture: boolean;
public readonly glacier: string;
public readonly hostedZoneId: string;
public readonly prependDomainWithStackStage: boolean;
public readonly sendAnonymousMetrics: boolean;
public readonly stackStage: string;
public readonly videosSubDomain: string | undefined;
public readonly videosDomain: string | undefined;
public readonly workflowTrigger: string;
constructor(stack: Stack) {
this.stackStage =
stack.node.tryGetContext('stackStage') !== undefined
? stack.node.tryGetContext('stackStage')
: undefined;
this.acceleratedTranscoding =
stack.node.tryGetContext('acceleratedTranscoding') ?? 'PREFERRED';
if (this.acceleratedTranscoding === undefined) {
throw new Error(
`The 'acceleratedTranscoding' context variable is required.`
);
}
if (
this.acceleratedTranscoding &&
!['DISABLED', 'ENABLED', 'PREFERRED'].includes(
this.acceleratedTranscoding
)
) {
throw new Error(
`The 'acceleratedTranscoding' context variable must be one of 'DISABLED', 'ENABLED', or 'PREFERRED'; found value was '${this.acceleratedTranscoding}'.`
);
}
this.adminEmail = stack.node.tryGetContext('adminEmail');
if (this.adminEmail === undefined) {
throw new Error(`The 'adminEmail' context variable is required.`);
}
const adminEmailValidation = this.adminEmail.match(
/^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$/g
);
if (adminEmailValidation && adminEmailValidation.length !== 1) {
throw new Error(
`The 'adminEmail' context variable must be a valid email address and must contain only one email; found value was '${this.adminEmail}'.`
);
}
this.prependDomainWithStackStage =
stack.node.tryGetContext('prependDomainWithStackStage') ?? false;
this.cloudFrontDomainBase =
stack.node.tryGetContext('cloudFrontDomainBase') !== undefined
? stack.node.tryGetContext('cloudFrontDomainBase')
: undefined;
this.authenticationSubDomain =
stack.node.tryGetContext('authenticationSubDomain') !== undefined
? stack.node.tryGetContext('authenticationSubDomain')
: undefined;
this.authenticationDomain =
(this.cloudFrontDomainBase !== undefined &&
this.authenticationSubDomain) !== undefined
? this.prependDomainWithStackStage && this.stackStage !== undefined
? `${this.stackStage.toLowerCase()}.${this.authenticationSubDomain}.${
this.cloudFrontDomainBase
}`
: `${this.authenticationSubDomain}.${this.cloudFrontDomainBase}`
: undefined;
this.videosSubDomain =
stack.node.tryGetContext('videosSubDomain') !== undefined
? stack.node.tryGetContext('videosSubDomain')
: undefined;
this.videosDomain =
(this.cloudFrontDomainBase !== undefined && this.videosSubDomain) !==
undefined
? this.prependDomainWithStackStage && this.stackStage !== undefined
? `${this.stackStage.toLowerCase()}.${this.videosSubDomain}.${
this.cloudFrontDomainBase
}`
: `${this.videosSubDomain}.${this.cloudFrontDomainBase}`
: undefined;
this.enableMediaPackage =
convertToBool(stack.node.tryGetContext('enableMediaPackage')) ?? false;
this.enableSns =
convertToBool(stack.node.tryGetContext('enableSns')) ?? true;
this.enableSqs =
convertToBool(stack.node.tryGetContext('enableSqs')) ?? true;
this.frameCapture =
convertToBool(stack.node.tryGetContext('frameCapture')) ?? false;
this.glacier = stack.node.tryGetContext('glacier') ?? 'DISABLED';
if (
this.glacier &&
!['DEEP_ARCHIVE', 'DISABLED', 'GLACIER'].includes(this.glacier)
) {
throw new Error(
`The 'glacier' context variable must be one of 'DEEP_ARCHIVE', 'DISABLED', or 'GLACIER'; found value was '${this.glacier}'.`
);
}
this.hostedZoneId = stack.node.tryGetContext('hostedZoneId');
this.sendAnonymousMetrics =
convertToBool(stack.node.tryGetContext('sendAnonymousMetrics')) ?? false;
this.workflowTrigger =
stack.node.tryGetContext('workflowTrigger') ?? 'VideoFile';
if (
this.workflowTrigger &&
!['VideoFile', 'MetadataFile'].includes(this.workflowTrigger)
) {
throw new Error(
`The 'workflowTrigger' context variable must either be 'VideoFile' or 'MetadataFile'; found value was '${this.workflowTrigger}'.`
);
}
}
}