-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceFactory.ts
298 lines (259 loc) · 15.3 KB
/
ServiceFactory.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { Message } from "rs-core/Message.ts";
import { MessageFunction, Service } from "rs-core/Service.ts";
import { Source } from "rs-core/Source.ts";
import { Url } from "rs-core/Url.ts";
import { config } from "./config.ts";
import { IServiceConfigTemplate, IServiceConfig } from "rs-core/IServiceConfig.ts";
import { ServiceWrapper } from "./ServiceWrapper.ts";
import { applyServiceConfigTemplate } from "./Modules.ts";
import { IAdapter } from "rs-core/adapter/IAdapter.ts";
import { getErrors } from "rs-core/utility/errors.ts";
import { IAdapterManifest, IServiceManifest } from "rs-core/IManifest.ts";
import { BaseStateClass, ServiceContext, StateFunction } from "rs-core/ServiceContext.ts";
import { handleIncomingRequest, handleOutgoingRequestWithPrivateServices } from "./handleRequest.ts";
import { pathCombine } from "rs-core/utility/utility.ts";
interface ITemplateConfigFromManifest {
serviceConfigTemplates?: Record<string, IServiceConfigTemplate>;
prePipeline?: Record<string, unknown>;
postPipeline?: Record<string, unknown>;
}
type GetPrivateManifestsOutput = (string | IServiceManifest)[];
/** Service message function creation and manifest caching for a tenant */
export class ServiceFactory {
// souce is url with domain or file path
serviceManifestsBySource = {} as { [ manifestSource: string ]: IServiceManifest };
adapterManifestsBySource = {} as { [ manifestSource: string ]: IAdapterManifest };
serviceConfigs = null as Record<string, IServiceConfig> | null;
constructor(public tenant: string, public primaryDomain: string) {
}
/** loads all manifests required by serviceConfigs and resolves private services */
async loadServiceManifests(serviceManifestSources: string[]) {
config.logger.debug(`Start -- loading manifests`, this.tenant);
// get promises to get service manifests
const uniqueServiceManifestSources = serviceManifestSources.filter((ms, i) => serviceManifestSources.indexOf(ms) === i);
const getServiceManifestPromises = uniqueServiceManifestSources.map(source => config.modules.getServiceManifest(source, this.tenant, this.primaryDomain));
const serviceManifests = await Promise.all<string | IServiceManifest>(getServiceManifestPromises);
const errors = serviceManifests.filter(m => typeof m === 'string') as string[];
if (errors.length) throw new Error('failed to load service manifests: ' + errors.join('; '));
uniqueServiceManifestSources.forEach((source, i) =>
this.serviceManifestsBySource[source] = serviceManifests[i] as IServiceManifest);
// get private service manifests
const privateServiceManifests = await this.getPrivateServiceManifests(
Object.keys(this.serviceManifestsBySource),
Object.values(this.serviceManifestsBySource)
);
const privateServiceErrors = privateServiceManifests.filter(m => typeof m === 'string') as string[];
if (privateServiceErrors.length) throw new Error('failed to load manifests: ' + privateServiceErrors.join('; '));
}
async loadAdapterManifests() {
// get promises to get adapter manifests
const adapterManifestSources = Object.values(this.serviceConfigs!)
.filter(sc => sc.adapterSource)
.map(sc => sc.adapterSource) as string[];
const infraNames = Object.values(this.serviceConfigs!)
.filter(sc => sc.infraName)
.map(sc => sc.infraName);
const missingInfraNames = infraNames.filter(i => !config.server.infra[i as string]);
if (missingInfraNames.length) {
throw new Error(`tenant ${this.tenant} has infra names that don't exist: ${missingInfraNames.join(', ')}`);
}
const adapterInfraManifestSources = infraNames
.map(i => config.server.infra[i as string].adapterSource) as string[];
const allAdapterManifestSources = [ ...adapterManifestSources, ...adapterInfraManifestSources];
const uniqueAdapterManifestSources = allAdapterManifestSources
.filter((ms, i) => allAdapterManifestSources.indexOf(ms) === i);
const getAdapterManifestPromises = uniqueAdapterManifestSources
.map(source => config.modules.getAdapterManifest(source, this.tenant, this.primaryDomain));
// get all the manifests
const adapterManifests = await Promise.all<string | IAdapterManifest>(getAdapterManifestPromises);
const errors = adapterManifests.filter(m => typeof m === 'string') as string[];
if (errors.length) throw new Error('failed to load adapter manifests: ' + errors.join('; '));
uniqueAdapterManifestSources.forEach((source, i) =>
this.adapterManifestsBySource[source] = adapterManifests[i] as IAdapterManifest);
config.logger.debug(`End -- loading manifests`, this.tenant);
}
/** Get the manifests for all the private services of the given list of service manifests */
private async getPrivateServiceManifests(existingServiceSources: string[], serviceManifests: IServiceManifest[]): Promise<GetPrivateManifestsOutput> {
if (serviceManifests.length === 0) return [];
// get manifest sources for all the private services of all the serviceManifests
const privateServiceSources = serviceManifests
.flatMap(sc => sc.privateServices
? Object.values(sc.privateServices).map(ps => ps.source)
: [])
.filter(s => !existingServiceSources.includes(s));
const manifestsLayer0 = await Promise.all(privateServiceSources.map(pss => config.modules.getServiceManifest(pss, this.tenant, this.primaryDomain)));
// bail on any error
if (manifestsLayer0.some(m => typeof m === 'string')) return manifestsLayer0;
privateServiceSources.forEach((source, i) => this.serviceManifestsBySource[source] = manifestsLayer0[i] as IServiceManifest);
const manifestsOtherLayers = await this.getPrivateServiceManifests(
[ ...existingServiceSources, ...privateServiceSources ],
manifestsLayer0 as IServiceManifest[]
);
return manifestsLayer0.concat(manifestsOtherLayers);
}
private addPrivateServiceConfig(serviceConfig: IServiceConfig, manifest: IServiceManifest): IServiceConfig {
if (!manifest.privateServices) return serviceConfig;
const privateServiceConfigs = {} as Record<string, IServiceConfig>;
Object.entries(manifest.privateServices).forEach(([ name, configTemplate ]) => {
let innerServiceConfig = applyServiceConfigTemplate(serviceConfig, configTemplate);
innerServiceConfig.source = configTemplate.source;
// Convention is the base path of a private service is the parent's basePath as the root and the private service name prefixed by '*'
innerServiceConfig.basePath = pathCombine(serviceConfig.basePath, '*' + name);
const innerManifest = this.serviceManifestsBySource[innerServiceConfig.source];
innerServiceConfig = this.addPrivateServiceConfig(innerServiceConfig, innerManifest);
privateServiceConfigs[name] = innerServiceConfig;
});
const newServiceConfig = {
...serviceConfig,
manifestConfig: {
prePipeline: manifest.prePipeline,
postPipeline: manifest.postPipeline,
privateServiceConfigs
}
} as IServiceConfig;
return newServiceConfig;
}
async infraForAdapterInterface(adapterInterface: string) {
let infraName = '';
for (const [ name, infra ] of Object.entries(config.server.infra)) {
const adapterManifest = await config.modules.getAdapterManifest(infra.adapterSource, this.tenant);
if (typeof adapterManifest === 'string') {
config.logger.error('Failed to load adapter manifest: ' + adapterManifest);
} else if (adapterManifest.adapterInterfaces.includes(adapterInterface)) {
if (adapterManifest.moduleUrl?.startsWith('./')) {
return name;
} else {
infraName = name;
}
}
}
return infraName;
}
async initService(serviceConfig: IServiceConfig, serviceContext: ServiceContext<IAdapter>, oldState?: BaseStateClass): Promise<void> {
const service = await config.modules.getService(serviceConfig.source, this.tenant, this.primaryDomain);
const manifest = this.serviceManifestsBySource[serviceConfig.source];
serviceContext.manifest = manifest;
await service.initFunc(serviceContext, serviceConfig, oldState);
}
async getMessageFunctionForService(serviceConfig: IServiceConfig, serviceContext: ServiceContext<IAdapter>, source: Source): Promise<MessageFunction> {
const service = await config.modules.getService(serviceConfig.source, this.tenant, this.primaryDomain);
const manifest = this.serviceManifestsBySource[serviceConfig.source];
serviceConfig = this.addPrivateServiceConfig(serviceConfig, manifest);
const handlerWithPrivateServices = (msg: Message, source?: Source) => {
if (!msg.url.domain) msg.url.domain = config.tenants[this.tenant].primaryDomain;
return source === Source.External
? handleIncomingRequest(msg)
: handleOutgoingRequestWithPrivateServices(
serviceConfig.basePath,
serviceConfig.manifestConfig?.privateServiceConfigs || {},
this.tenant,
serviceContext
)(msg);
}
serviceContext = { ...serviceContext, manifest, makeRequest: handlerWithPrivateServices };
const canonicalSource = config.canonicaliseUrl(serviceConfig.source, this.tenant, this.primaryDomain);
const configValidator = config.modules.validateServiceConfig[canonicalSource];
const serviceName = serviceConfig.name;
if (!configValidator(serviceConfig as any)) {
throw new Error(`failed to validate config for service ${serviceName}: ${getErrors(configValidator)}`);
}
let adapter: IAdapter | undefined = undefined;
if (serviceConfig.adapterSource || serviceConfig.infraName) {
const adapterConfig = { ...serviceConfig.adapterConfig };
let adapterSource = serviceConfig.adapterSource;
if (serviceConfig.infraName) {
const infra = config.server.infra[serviceConfig.infraName];
adapterSource = infra.adapterSource;
Object.assign(adapterConfig, infra);
}
const canonicalAdapterSource = config.canonicaliseUrl(adapterSource as string, this.tenant, this.primaryDomain);
const validator = config.modules.validateAdapterConfig[canonicalAdapterSource];
if (!validator(adapterConfig as any)) {
throw new Error(`failed to validate adapter config for service ${serviceConfig.name}: ${getErrors(validator)}`);
}
adapter = await config.modules.getAdapter(adapterSource as string, serviceContext, adapterConfig, this.primaryDomain);
serviceContext = { ...serviceContext, adapter } as ServiceContext<IAdapter>;
}
const serviceWrapper = new ServiceWrapper(service);
const sourceServiceFunc = source === Source.External || source === Source.Outer ? serviceWrapper.external(source) : serviceWrapper.internal;
// protect data sent to func against modification within it
serviceContext.manifest = structuredClone(serviceContext.manifest);
const copyServiceConfig = structuredClone(serviceConfig);
return (msg: Message) => Promise.resolve(sourceServiceFunc(msg, serviceContext, copyServiceConfig));
}
attachFilter(url: Url, func: MessageFunction, context: ServiceContext<IAdapter>): MessageFunction {
const filterUrl = url.query['$filter']?.[0];
if (filterUrl) {
const url = new Url(filterUrl);
const newFunc: MessageFunction = async (msg: Message) => {
const msg2 = await func(msg);
msg2.setUrl(url).setMethod('POST');
return context.makeRequest(msg2, Source.Outer);
};
return newFunc;
} else {
return func;
}
}
basePathFromUrl(url: Url) {
const pathParts = [ ...url.pathElements ];
let basePath = '/' + pathParts.join('/') + '.';
let serviceConfig = this.serviceConfigs![basePath];
if (serviceConfig) return basePath;
while (true) {
basePath = '/' + pathParts.join('/');
serviceConfig = this.serviceConfigs![basePath];
if (serviceConfig) return basePath;
if (pathParts.length === 0) break;
pathParts.pop();
}
return null;
}
/** select service with longest path match */
async getMessageFunctionByUrl(url: Url, serviceContext: ServiceContext<IAdapter>, stateByBasePath: (basePath: string) => StateFunction, source: Source): Promise<[MessageFunction, string]> {
if (this.serviceConfigs!['()'] && source === Source.External) {
// the service with outer basePath (i.e. "()") can only make requests with Source.Outer.
// Source.External would cause it to recurse. This service should be accessible by anyone who
// can access the site: Source.Outer delegates auth checking to services it goes on to call.
const newServiceContext = {
...serviceContext,
makeRequest: (msg: Message) => serviceContext.makeRequest(msg, Source.Outer)
} as ServiceContext<IAdapter>;
return [
await this.getMessageFunctionForService(this.serviceConfigs!['()'], newServiceContext, source),
'()'
];
}
const configPath = this.basePathFromUrl(url);
if (configPath) {
const serviceConfig = this.serviceConfigs![configPath];
serviceContext.state = stateByBasePath(configPath);
const innerFunc = await this.getMessageFunctionForService(serviceConfig, serviceContext, source);
return [
this.attachFilter(url, innerFunc, serviceContext),
serviceConfig.name
];
}
return [
(msg: Message) =>
Promise.resolve(
msg.method === 'OPTIONS'
? config.server.setServerCors(msg).setStatus(204)
: config.server.setServerCors(msg).setStatus(404, 'Not found')
),
'?'
];
}
getServiceConfigByApi(api: string): IServiceConfig | undefined {
const apiManifests = Object.entries(this.serviceManifestsBySource).filter(([, m]) => (m.apis || []).some(mApi => mApi === api));
if (apiManifests.length === 0) return undefined;
const [ manifestSource, ] = apiManifests[0];
if (!manifestSource) return undefined;
return Object.values(this.serviceConfigs!).find(config => config.source === manifestSource);
}
async getServiceAndConfigByApi(api: string): Promise<[ Service, IServiceConfig ] | null> {
const serviceConfig = this.getServiceConfigByApi(api);
if (!serviceConfig) return null;
return [ await config.modules.getService(serviceConfig.source, this.tenant, this.primaryDomain), serviceConfig ];
}
}