@alterior/runtime 3.5.2 → 3.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -4
- package/src/app-options.ts +72 -0
- package/src/application.ts +207 -0
- package/src/args.ts +9 -0
- package/src/expose.ts +46 -0
- package/src/index.ts +9 -0
- package/src/lifecycle.ts +26 -0
- package/src/modules.ts +325 -0
- package/src/reflector.ts +433 -0
- package/src/roles.service.ts +171 -0
- package/src/service.ts +34 -0
- package/dist.esm/module.test.js +0 -374
- package/dist.esm/test.js +0 -7
package/src/modules.ts
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import { ModuleAnnotation, ModuleOptions, Module, ModuleLike } from "@alterior/di";
|
|
2
|
+
import { Injector, Provider, ReflectiveInjector } from "@alterior/di";
|
|
3
|
+
import { timeout, Environment } from "@alterior/common";
|
|
4
|
+
import { RolesService } from "./roles.service";
|
|
5
|
+
/**
|
|
6
|
+
* Combines a module annotation and a target class into a single
|
|
7
|
+
* object for storage purposes in Runtime and related objects.
|
|
8
|
+
*/
|
|
9
|
+
export class ModuleDefinition {
|
|
10
|
+
metadata : ModuleAnnotation;
|
|
11
|
+
target : any;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Represents a live instance of a module.
|
|
16
|
+
* Contains both the module definition as well
|
|
17
|
+
* as a reference to the module instance itself.
|
|
18
|
+
*/
|
|
19
|
+
export class ModuleInstance {
|
|
20
|
+
constructor(
|
|
21
|
+
readonly definition : ModuleDefinition,
|
|
22
|
+
readonly instance : any
|
|
23
|
+
) {
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Used to construct a runtime environment for a given entry module.
|
|
29
|
+
* Handles resolving the module tree into an injector as well as constructing
|
|
30
|
+
* the module instances and running lifecycle events.
|
|
31
|
+
*/
|
|
32
|
+
export class Runtime {
|
|
33
|
+
constructor(entryModule : Function) {
|
|
34
|
+
this.resolveModule(entryModule);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
definitions : ModuleDefinition[] = [];
|
|
38
|
+
visited : ModuleLike[] = [];
|
|
39
|
+
instances : ModuleInstance[] = null;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get a specific service from the dependency injector.
|
|
43
|
+
* @param ctor
|
|
44
|
+
*/
|
|
45
|
+
getService<T>(ctor : { new(...args) : T }): T {
|
|
46
|
+
return this.injector.get(ctor);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Retrieve the providers that were collected from the
|
|
51
|
+
* module graph and used to create the primary injector.
|
|
52
|
+
*/
|
|
53
|
+
providers : Provider[] = [];
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Iterate over the module definitions which are part of this
|
|
57
|
+
* runtime and append to the given array the set of dependency injection
|
|
58
|
+
* providers which are specified in the module definitions.
|
|
59
|
+
*
|
|
60
|
+
* @param providers An array which will be populated
|
|
61
|
+
*/
|
|
62
|
+
contributeProviders(providers : Provider[]) {
|
|
63
|
+
providers.push({ provide: Runtime, useValue: this });
|
|
64
|
+
this.definitions
|
|
65
|
+
.filter(defn => defn.metadata && defn.metadata.providers)
|
|
66
|
+
.forEach(defn => providers.push(...defn.metadata.providers))
|
|
67
|
+
;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Perform runtime configuration steps
|
|
72
|
+
*/
|
|
73
|
+
configure() {
|
|
74
|
+
|
|
75
|
+
let environment = this.injector.get(Environment);
|
|
76
|
+
let rolesService = this.injector.get(RolesService);
|
|
77
|
+
let allRoles = rolesService.roles;
|
|
78
|
+
|
|
79
|
+
if (environment.get<any>().ALT_ROLES_ONLY) {
|
|
80
|
+
let value = environment.get<any>().ALT_ROLES_ONLY;
|
|
81
|
+
let roles = value.split(',')
|
|
82
|
+
.map(x => allRoles.find(y => y.identifier == x))
|
|
83
|
+
.map(x => x.class)
|
|
84
|
+
;
|
|
85
|
+
|
|
86
|
+
rolesService.configure({ mode: 'only', roles });
|
|
87
|
+
} else if (environment.get<any>().ALT_ROLES_ALL_EXCEPT) {
|
|
88
|
+
let value = environment.get<any>().ALT_ROLES_ALL_EXCEPT;
|
|
89
|
+
let roles = value.split(',')
|
|
90
|
+
.map(x => allRoles.find(y => y.identifier == x))
|
|
91
|
+
.map(x => x.class)
|
|
92
|
+
;
|
|
93
|
+
|
|
94
|
+
rolesService.configure({ mode: 'all-except', roles });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (typeof process !== 'undefined' && process.argv) {
|
|
98
|
+
this.processCommandLine(process.argv.slice(2));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private _selfTest = false;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* True if the `--self-test` option was used to launch the application.
|
|
106
|
+
*/
|
|
107
|
+
get selfTest() {
|
|
108
|
+
return this._selfTest;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
processCommandLine(args : string[]) {
|
|
112
|
+
let rolesService = this.injector.get(RolesService);
|
|
113
|
+
let argIndex = 0;
|
|
114
|
+
|
|
115
|
+
let getArgumentValue = () => {
|
|
116
|
+
let arg = args[argIndex];
|
|
117
|
+
if (argIndex + 1 >= args.length)
|
|
118
|
+
throw new Error(`You must specify a value for option ${arg}`);
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
let value = args[++argIndex];
|
|
122
|
+
|
|
123
|
+
if (value.startsWith('-')) {
|
|
124
|
+
throw new Error(`You must specify a value for option ${arg} (encountered option '${value}' instead)`);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return value;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
let allRoles = rolesService.roles;
|
|
131
|
+
for (; argIndex < args.length; ++argIndex) {
|
|
132
|
+
let arg = args[argIndex];
|
|
133
|
+
|
|
134
|
+
if (arg === '--self-test') {
|
|
135
|
+
this._selfTest = true;
|
|
136
|
+
} else if (arg == '-r' || arg == '--roles-only') {
|
|
137
|
+
let value = getArgumentValue();
|
|
138
|
+
|
|
139
|
+
let roles = value.split(',')
|
|
140
|
+
.map(x => allRoles.find(y => y.identifier == x))
|
|
141
|
+
.filter(x => x)
|
|
142
|
+
.map(x => x.class)
|
|
143
|
+
;
|
|
144
|
+
|
|
145
|
+
rolesService.configure({ mode: 'only', roles });
|
|
146
|
+
} else if (arg == '-R' || arg == '--roles-all-except') {
|
|
147
|
+
let value = getArgumentValue();
|
|
148
|
+
|
|
149
|
+
let roles = value.split(',')
|
|
150
|
+
.map(x => allRoles.find(y => y.identifier == x))
|
|
151
|
+
.filter(x => x)
|
|
152
|
+
.map(x => x.class)
|
|
153
|
+
;
|
|
154
|
+
|
|
155
|
+
rolesService.configure({ mode: 'all-except', roles });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Fire an event to all modules which understand it. Should be upper-camel-case, meaning
|
|
162
|
+
* to fire the altOnStart() method, send "OnStart".
|
|
163
|
+
* @param eventName
|
|
164
|
+
*/
|
|
165
|
+
fireEvent(eventName : string) {
|
|
166
|
+
for (let modInstance of this.instances) {
|
|
167
|
+
if (modInstance.instance[`alt${eventName}`])
|
|
168
|
+
modInstance.instance[`alt${eventName}`]();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private _injector : Injector = null;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Get the runtime's dependency injector. This injector can provide all dependencies specified
|
|
176
|
+
* in the imported modules' `providers` definitions.
|
|
177
|
+
*/
|
|
178
|
+
get injector() {
|
|
179
|
+
return this._injector;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Instantiate the modules of this runtime using the given dependency injector.
|
|
184
|
+
* The injector will be inherited into an injector that provides the dependencies
|
|
185
|
+
* specified in the imported modules' `providers` definitions.
|
|
186
|
+
*
|
|
187
|
+
* @param injector
|
|
188
|
+
*/
|
|
189
|
+
load(injector : Injector): ModuleInstance[] {
|
|
190
|
+
if (this.instances)
|
|
191
|
+
return;
|
|
192
|
+
|
|
193
|
+
let ownInjector : ReflectiveInjector;
|
|
194
|
+
let providers = this.definitions.map(x => x.target);
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
ownInjector = ReflectiveInjector.resolveAndCreate(
|
|
198
|
+
providers,
|
|
199
|
+
injector
|
|
200
|
+
);
|
|
201
|
+
} catch (e) {
|
|
202
|
+
console.error(`Failed to construct injector:`);
|
|
203
|
+
console.error(`Providers:`);
|
|
204
|
+
console.dir(providers);
|
|
205
|
+
console.error(`Definitions:`);
|
|
206
|
+
console.dir(this.definitions);
|
|
207
|
+
throw e;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
this._injector = ownInjector;
|
|
211
|
+
|
|
212
|
+
let moduleInstances = this.definitions.map (defn => new ModuleInstance(defn, ownInjector.get(defn.target)));
|
|
213
|
+
this.instances = moduleInstances;
|
|
214
|
+
|
|
215
|
+
return this.instances;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Stop any services, as defined by imported modules of this runtime. For instance, if you import WebServerModule
|
|
220
|
+
* from @alterior/web-server, calling this will instruct the module to stop serving on the configured port.
|
|
221
|
+
* Also builds in a timeout to allow for all services and operations to stop before resolving.
|
|
222
|
+
*
|
|
223
|
+
* This will send the `OnStop` lifecycle event to all modules, which triggers the `altOnStop()` method of any module
|
|
224
|
+
* which implements it to be called. It also instructs the RolesService to stop any roles which are currently running.
|
|
225
|
+
* For more information about Roles, see the documentation for RolesService.
|
|
226
|
+
*/
|
|
227
|
+
async stop() {
|
|
228
|
+
this.fireEvent('OnStop');
|
|
229
|
+
let rolesService = this.injector.get(RolesService);
|
|
230
|
+
rolesService.stopAll();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Start any services, as defined by modules. For instance, if you import WebServerModule from @alterior/web-server,
|
|
235
|
+
* calling this will instruct the module to begin serving on the configured port.
|
|
236
|
+
*
|
|
237
|
+
* This will send the `OnStart` lifecycle event to all modules, which triggers the `altOnStart()` method of any module
|
|
238
|
+
* which implements it to be called. It also instructs the RolesService to start roles as per it's configuration.
|
|
239
|
+
* For more information about Roles, see the documentation for RolesService.
|
|
240
|
+
*/
|
|
241
|
+
start() {
|
|
242
|
+
this.fireEvent('OnStart');
|
|
243
|
+
|
|
244
|
+
let rolesService = this.injector.get(RolesService);
|
|
245
|
+
rolesService.startAll();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Stop all running services and shut down the process
|
|
250
|
+
*/
|
|
251
|
+
async shutdown() {
|
|
252
|
+
await this.stop();
|
|
253
|
+
process.exit();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Retrieve the ModuleAnnotation for a given Module definition, whether it be a class annotated
|
|
258
|
+
* with `@Module()` or a plain object with `$module` which configures a module class.
|
|
259
|
+
*
|
|
260
|
+
* This is an alias of ModuleAnnotation.getForClass(module)
|
|
261
|
+
*
|
|
262
|
+
* @param module
|
|
263
|
+
*/
|
|
264
|
+
public getMetadataForModule(module : ModuleLike): ModuleAnnotation {
|
|
265
|
+
return ModuleAnnotation.getForClass(module);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Resolves the given module, adding it to this runtime.
|
|
270
|
+
* Calls itself for all imports. Visited modules are tracked per runtime,
|
|
271
|
+
* so resolveModule() on the same runtime object will not work, preventing
|
|
272
|
+
* loops.
|
|
273
|
+
*/
|
|
274
|
+
private resolveModule(module : ModuleLike) {
|
|
275
|
+
|
|
276
|
+
// Prevent infinite recursion
|
|
277
|
+
|
|
278
|
+
if (this.visited.includes(module))
|
|
279
|
+
return;
|
|
280
|
+
this.visited.push(module);
|
|
281
|
+
|
|
282
|
+
// Construct this compilation unit
|
|
283
|
+
let isExtension = false;
|
|
284
|
+
|
|
285
|
+
if (module['$module']) {
|
|
286
|
+
isExtension = true;
|
|
287
|
+
// This is a mask
|
|
288
|
+
module = Object.assign({}, module);
|
|
289
|
+
let parentModule = module['$module'];
|
|
290
|
+
let options : ModuleOptions = Object.assign({}, module as any);
|
|
291
|
+
delete module['$module'];
|
|
292
|
+
|
|
293
|
+
if (!options.imports)
|
|
294
|
+
options.imports = [];
|
|
295
|
+
|
|
296
|
+
options.imports.push(parentModule);
|
|
297
|
+
|
|
298
|
+
@Module(options) class subModule {};
|
|
299
|
+
module = subModule;
|
|
300
|
+
|
|
301
|
+
let metadata = this.getMetadataForModule(module);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
let metadata = this.getMetadataForModule(module);
|
|
305
|
+
|
|
306
|
+
if (metadata && metadata.imports) {
|
|
307
|
+
let position = 0;
|
|
308
|
+
|
|
309
|
+
for (let importedModule of metadata.imports) {
|
|
310
|
+
if (!importedModule) {
|
|
311
|
+
throw new Error(`Failed to resolve module referenced in position ${position} by ${module.toString()}`);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
this.resolveModule(importedModule);
|
|
315
|
+
++position;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
this.definitions.push({
|
|
320
|
+
target: module,
|
|
321
|
+
metadata,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
}
|
|
325
|
+
}
|