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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 | 5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
5x
19x
11x
2836x
15x
12x
12x
1x
5x
3x
5x
11x
10x
10x
10x
10x
2x
2x
2x
2x
1x
10x
7x
7x
6x
7x
1x
87x
2912x
2835x
2835x
77x
| import {sealed} from "./decorators";
import {struct} from "superstruct";
import {IGatewayBFFConfiguration, IStorefrontConfig} from "./types";
import {ERROR_CODES, PuzzleError} from "./errors";
import {HTTP_METHODS, INJECTABLE, RESOURCE_INJECT_TYPE, TRANSFER_PROTOCOLS} from "./enums";
import {RESOURCE_LOADING_TYPE, RESOURCE_TYPE} from "./lib/enums";
import {CookieVersionMatcher, MATCHER_FN} from "./cookie-version-matcher";
const apiEndpointsStructure = struct({
path: 'string',
middlewares: struct.optional(['string']),
method: struct.enum(Object.values(HTTP_METHODS)),
controller: 'string',
routeCache: 'number?',
cacheControl: 'string?'
});
const spdyStructure = struct({
key: struct.optional(struct.union(['string', 'buffer'])),
cert: struct.optional(struct.union(['string', 'buffer'])),
passphrase: 'string',
protocols: [struct.enum(Object.values(TRANSFER_PROTOCOLS))],
});
const customHeaderStructure = struct({
key: 'string',
value: struct.union(['string', 'number']),
isEnv: 'boolean?'
});
const apiVersionStructure = struct({
handler: 'string?',
endpoints: [apiEndpointsStructure]
});
const apiStructure = struct({
name: 'string',
testCookie: 'string',
liveVersion: 'string',
versions: struct.dict(['string', apiVersionStructure])
});
const gatewayRenderStructure = struct({
url: struct.union(['string', ['string']]),
static: 'boolean?',
selfReplace: 'boolean?',
placeholder: 'boolean?',
error: 'boolean?',
timeout: 'number?',
middlewares: struct.optional(['string']),
routeCache: 'number?'
});
const gatewayFragmentAssetsStructure = struct({
name: 'string',
fileName: 'string',
link: 'string?',
loadMethod: struct.enum(Object.values(RESOURCE_LOADING_TYPE)),
type: struct.enum(Object.values(RESOURCE_TYPE)),
dependent: struct.optional(['string'])
});
const gatewayFragmentDependenctStructure = struct({
name: 'string',
type: struct.enum(Object.values(RESOURCE_TYPE)),
link: 'string?',
preview: 'string?',
injectType: struct.optional(struct.enum(Object.values(RESOURCE_INJECT_TYPE)))
});
const gatewayFragmentVersionStructure = struct({
assets: [gatewayFragmentAssetsStructure],
dependencies: [gatewayFragmentDependenctStructure],
handler: 'string?'
});
const gatewayFragmentStructure = struct({
name: 'string',
testCookie: 'string',
prg: struct.optional('boolean'),
render: gatewayRenderStructure,
warden: struct.optional('object'),
version: 'string',
versionMatcher: 'string?',
versions: struct.dict(['string', gatewayFragmentVersionStructure])
});
const gatewayStructure = struct({
name: 'string',
url: 'string',
fragments: [gatewayFragmentStructure],
api: [apiStructure],
port: 'number',
ipv4: 'boolean?',
isMobile: 'boolean?',
authToken: 'string?',
fragmentsFolder: 'string',
corsDomains: struct.optional(['string']),
corsMaxAge: 'number?',
spdy: struct.optional(spdyStructure),
customHeaders: struct.optional([customHeaderStructure])
});
const storefrontPageStructure = struct({
page: 'string?',
html: 'string',
url: struct.union(['string', ['string']]),
condition: 'function?'
});
const storefrontGatewaysStructure = struct({
name: 'string',
url: 'string',
assetUrl: 'string?'
});
const storefrontDependencyStructure = struct({
content: 'string?',
link: 'string?'
});
const storefrontStructure = struct({
gateways: [storefrontGatewaysStructure],
port: 'number',
authToken: 'string?',
satisfyUpdateCount: 'number?',
ipv4: 'boolean?',
pages: [storefrontPageStructure],
pollInterval: 'number?',
dependencies: [storefrontDependencyStructure],
spdy: struct.optional(spdyStructure),
customHeaders: struct.optional([customHeaderStructure])
});
@sealed
export class Configurator {
configuration: IGatewayBFFConfiguration | IStorefrontConfig;
protected dependencies: { [name: string]: any } = {
[INJECTABLE.CUSTOM]: {},
[INJECTABLE.HANDLER]: {},
[INJECTABLE.MIDDLEWARE]: {},
};
register(name: string, type: INJECTABLE, dependency: any): void {
this.dependencies[type][name] = dependency;
}
get(name: string, type: INJECTABLE): any {
return this.dependencies[type][name];
}
/**
* Config json
* @param {IStorefrontConfig | IGatewayBFFConfiguration} configuration
*/
config(configuration: IStorefrontConfig | IGatewayBFFConfiguration) {
this.validate(configuration);
this.injectDependencies(configuration);
this.configuration = Object.assign({}, configuration);
}
protected validate(configuration: IStorefrontConfig | IGatewayBFFConfiguration) {
throw new PuzzleError(ERROR_CODES.CONFIGURATION_BASE_VALIDATION_ERROR);
}
protected injectDependencies(configuration: IStorefrontConfig | IGatewayBFFConfiguration) {
throw new PuzzleError(ERROR_CODES.CONFIGURATION_BASE_VALIDATION_ERROR);
}
}
@sealed
export class StorefrontConfigurator extends Configurator {
configuration: IStorefrontConfig;
protected validate(configuration: IStorefrontConfig) {
storefrontStructure(configuration);
}
protected injectDependencies(configuration: IStorefrontConfig) {
}
}
@sealed
export class GatewayConfigurator extends Configurator {
configuration: IGatewayBFFConfiguration;
protected validate(configuration: IGatewayBFFConfiguration) {
gatewayStructure(configuration);
}
protected injectDependencies(configuration: IGatewayBFFConfiguration) {
this.injectCustomDependencies(configuration);
this.injectApiHandlers(configuration);
this.prepareFragmentConfiguration(configuration);
}
private injectApiHandlers(configuration: IGatewayBFFConfiguration) {
configuration.api.forEach(api => {
(Object.values(api.versions) as any).forEach((version: any) => {
Eif (version.handler) {
version.handler = this.dependencies[INJECTABLE.HANDLER][version.handler];
}
version.endpoints.forEach((endpoint: any) => {
endpoint.middlewares = (endpoint.middlewares as any || []).map((middleware: string) => this.dependencies[INJECTABLE.MIDDLEWARE][middleware]);
});
});
});
}
private prepareFragmentConfiguration(configuration: IGatewayBFFConfiguration) {
configuration.fragments.forEach(fragment => {
(Object.values(fragment.versions) as any).forEach((version: any) => {
if (version.handler) {
version.handler = this.dependencies[INJECTABLE.HANDLER][version.handler];
}
});
fragment.render.middlewares = (fragment.render.middlewares as any || [])
.map((middleware: string) => this.dependencies[INJECTABLE.MIDDLEWARE][middleware]);
});
}
private injectCustomDependencies(configuration: { [key: string]: any }) {
Object.keys(configuration).forEach((key) => {
if (typeof configuration[key] !== 'object') {
const targetDependency = this.get(configuration[key], INJECTABLE.CUSTOM);
Iif (targetDependency) {
configuration[key] = targetDependency;
}
} else {
this.injectCustomDependencies(configuration[key]);
}
});
}
}
|