-
Notifications
You must be signed in to change notification settings - Fork 2
/
tenant.ts
273 lines (242 loc) · 12.1 KB
/
tenant.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
import { Source } from "rs-core/Source.ts";
import { Url } from "rs-core/Url.ts";
import { Message } from "rs-core/Message.ts";
import { ServiceFactory } from "./ServiceFactory.ts";
import { AuthService } from "rs-core/Service.ts";
import { IChordServiceConfig, IServiceConfig, PrePost } from "rs-core/IServiceConfig.ts";
import { config } from "./config.ts";
import { AuthUser } from "./auth/AuthUser.ts";
import { IChord } from "./IChord.ts";
import { after, deepEqualIfPresent, mergeDeep, upTo } from "rs-core/utility/utility.ts";
import { getErrors } from "rs-core/utility/errors.ts";
import { makeServiceContext } from "./makeServiceContext.ts";
import { StateClass, nullState, BaseStateClass, BaseContext } from "rs-core/ServiceContext.ts";
import { applyServiceConfigTemplate } from "./Modules.ts";
export interface IServicesConfig {
services: Record<string, IServiceConfig>;
authServicePath?: string;
}
export interface IRawServicesConfig extends IServicesConfig {
chords?: Record<string, IChord>;
defaults?: Record<string, unknown>;
}
const baseChord: IChord = {
id: 'sys.base',
newServices: [
{
"access": { "readRoles": "all", "writeRoles": "A" },
"name": "Services Service",
"source": "./services/services.rsm.json",
"basePath": "/.well-known/restspace"
}
]
}
export class Tenant {
serviceFactory: ServiceFactory
authService?: AuthService;
authServiceConfig?: IServiceConfig;
servicesConfig = null as IServicesConfig | null;
chordMap: Record<string, Record<string, string>> = {};
_state: Record<string, BaseStateClass> = {};
readyBasePaths: string[] = [];
state = (basePath: string) => async <T extends BaseStateClass>(cons: StateClass<T>, context: BaseContext, config: unknown) => {
const stateKey = `${basePath}#${cons.name}`;
if (this._state[stateKey] === undefined) {
const newState = new cons();
this._state[stateKey] = newState;
await newState.load(context, config);
}
if (!(this._state[stateKey] instanceof cons)) throw new Error('Changed type of state attached to service');
return this._state[stateKey] as T;
}
get primaryDomain() {
const name = this.name === "main" ? '' : this.name;
return this.domains[0] || (name + '.' + config.server.mainDomain);
}
get isEmpty() {
return Object.keys(this.rawServicesConfig.services).length === 0;
}
constructor(public name: string, public rawServicesConfig: IRawServicesConfig, public domains: string[]) {
const primaryDomain = domains[0] || ((name === 'main' ? '' : name + '.') + config.server.mainDomain);
this.serviceFactory = new ServiceFactory(name, primaryDomain);
}
private getSources(serviceRecord: Record<string, IServiceConfig> | IServiceConfig[] | IChordServiceConfig[]) {
return Array.isArray(serviceRecord)
? serviceRecord.map(serv => serv.source)
: Object.values(serviceRecord).map(serv => serv.source);
}
private extractSources(rawServicesConfig: IRawServicesConfig) {
return [
...this.getSources(rawServicesConfig.services),
...[ ...Object.values(rawServicesConfig?.chords || {}), baseChord ]
.flatMap(chord => [
...this.getSources(chord.newServices || [])
])
];
}
private sourceIsLocalHttp(source: string) {
if (source.startsWith('/')) return true;
if (source.startsWith('http://') || source.startsWith('https://')) {
const domain = upTo(after(source, '://'), '/');
return this.domains.includes(domain);
}
return false;
}
private filterConfigByLocalSource(rawServicesConfig: IRawServicesConfig, isLocal: boolean) {
const servicesEntries = Object.entries(rawServicesConfig.services);
const filteredConfig = {
...rawServicesConfig,
services: Object.fromEntries(
servicesEntries.filter(([,s]) => isLocal === !!(
this.sourceIsLocalHttp(s.source)
|| (s.adapterSource && this.sourceIsLocalHttp(s.adapterSource))
))
)
} as IRawServicesConfig;
if (rawServicesConfig.chords) {
const chordEntries = Object.entries(rawServicesConfig.chords);
filteredConfig.chords = Object.fromEntries(
chordEntries.filter(([,c]) => isLocal === c.newServices?.some(s => this.sourceIsLocalHttp(s.source)))
);
}
return filteredConfig;
}
private async applyChord(services: Record<string, IServiceConfig>, chordKey: string, chord: IChord) {
this.chordMap[chordKey] = {};
for (const service of chord.newServices || []) {
if (!config.validateChordService(service as any)) {
throw new Error(`Chord ${chord['id']} service ${service['name'] || '<unnamed>'} was misformatted: ${getErrors(config.validateChordService)}` );
}
if (services[service.basePath]) {
const matchService = { ...service } as any;
delete matchService.name;
delete matchService.localDir;
if (!deepEqualIfPresent(services[service.basePath], matchService)) {
throw new Error(`chord ${chord.id} chord key ${chordKey} fails to match existing service on ${service.basePath}`);
}
} else {
const newService = this.applyDefaults(service);
if (!(newService.infraName || newService.adapterSource)) {
// We need to find an infra for the service as none was specified
const manifest = this.serviceFactory.serviceManifestsBySource[newService.source];
if (manifest.adapterInterface) {
const infraName = await this.serviceFactory.infraForAdapterInterface(manifest.adapterInterface);
if (!infraName) {
throw new Error(`chord ${chord.id} chord key ${chordKey} service ${service.name} requires infraName or adapterSource property to be manually set`);
}
newService.infraName = infraName;
}
}
services[service.basePath] = newService as IServiceConfig;
}
this.chordMap[chordKey][service.name] = service.basePath;
}
}
private applyDefaults<T extends IServiceConfig | IChordServiceConfig>(service: T): T {
const defaults = this.serviceFactory.serviceManifestsBySource[service.source].defaults;
if (!defaults) return service;
// defaults is by reference, create a new copy to avoid overwriting original
const defaultedService = structuredClone(defaults);
mergeDeep(defaultedService, service);
return defaultedService as T;
}
private applyTemplate<T extends IServiceConfig>(service: T): T {
const template = this.serviceFactory.serviceManifestsBySource[service.source].configTemplate;
if (!template) return service;
const serviceFromTemplate = applyServiceConfigTemplate(service, template);
return serviceFromTemplate as T;
}
private async buildServicesConfig(rawServicesConfig: IRawServicesConfig): Promise<IServicesConfig> {
const services = { ...rawServicesConfig.services };
Object.keys(services).forEach(k => {
services[k] = this.applyDefaults(services[k]);
services[k] = this.applyTemplate(services[k]);
});
const servicesConfig: IServicesConfig = {
services,
authServicePath: rawServicesConfig.authServicePath
};
// set up universally required services in the baseChord
await this.applyChord(servicesConfig.services, "base", baseChord);
for (const [chordKey, chord] of Object.entries(rawServicesConfig.chords || {})) {
await this.applyChord(servicesConfig.services, chordKey, chord);
}
Object.keys(servicesConfig.services).forEach(keyPath => servicesConfig.services[keyPath].basePath = keyPath);
return servicesConfig;
}
async loadConfig(rawServicesConfig: IRawServicesConfig, oldTenant?: Tenant) {
// we need the service manifests for their default values before building servicesConfig
await this.serviceFactory.loadServiceManifests(this.extractSources(rawServicesConfig));
const newServicesConfig = await this.buildServicesConfig(rawServicesConfig);
if (this.servicesConfig === null) {
this.servicesConfig = newServicesConfig;
} else {
this.servicesConfig = {
services: { ...this.servicesConfig.services, ...newServicesConfig.services },
authServicePath: this.servicesConfig.authServicePath || newServicesConfig.authServicePath
};
}
if (this.serviceFactory.serviceConfigs === null) {
this.serviceFactory.serviceConfigs = this.servicesConfig.services;
} else {
this.serviceFactory.serviceConfigs = { ...this.serviceFactory.serviceConfigs, ...this.servicesConfig.services };
}
await this.serviceFactory.loadAdapterManifests();
// init state for the services here
await Promise.all(Object.values(newServicesConfig.services).map(config => {
const context = makeServiceContext(this.name, this.state(config.basePath));
return this.serviceFactory.initService(config, context, oldTenant?._state[config.basePath])
.catch(reason => {
throw new Error(`Service ${config.name} failed to initialize: ${reason}`);
});
})).catch((reason) => {
config.logger.error(`Failed to init all services, ${reason}`, this.name);
throw new Error(`${reason}`);
});
if (!this.authService) {
const res = await this.serviceFactory.getServiceAndConfigByApi("auth");
if (res) {
const [ authService, authServiceConfig ] = res;
this.authService = authService as AuthService;
this.authServiceConfig = authServiceConfig;
}
}
}
pathIsReady(url: Url): boolean {
const basePath = this.serviceFactory.basePathFromUrl(url);
return !!basePath && this.readyBasePaths.includes(basePath);
}
async init(oldTenant?: Tenant) {
await this.loadConfig(this.filterConfigByLocalSource(this.rawServicesConfig, false), oldTenant);
this.readyBasePaths = Object.keys(this.filterConfigByLocalSource(this.rawServicesConfig, false).services);
await this.loadConfig(this.filterConfigByLocalSource(this.rawServicesConfig, true), oldTenant);
}
async unload(newTenant: Tenant) {
await Promise.allSettled(Object.entries(this._state).map(([key, cls]) => {
const newState = newTenant._state[key];
// check newState is the same class as cls
if (newState && newState.constructor.name === cls.constructor.name) {
cls.unload(newState);
} else {
cls.unload();
}
}));
}
getMessageFunctionByUrl(url: Url, source: Source) {
// we assign the state in serviceFactory as we don't know the basePath yet
return this.serviceFactory.getMessageFunctionByUrl(url, makeServiceContext(this.name, nullState), this.state, source);
}
getMessageFunctionForService(serviceConfig: IServiceConfig, source: Source, prePost?: PrePost) {
return this.serviceFactory.getMessageFunctionForService(serviceConfig, makeServiceContext(this.name, this.state(serviceConfig.basePath), prePost), source);
}
async attachUser(msg: Message) {
if (this.authService) {
msg = await this.authService.setUserFunc(msg, makeServiceContext(this.name, this.state(this.authServiceConfig!.basePath)), this.authServiceConfig as IServiceConfig);
}
if (!msg.user) {
msg.user = AuthUser.anon;
msg.authenticated = false;
}
return msg;
}
}