-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFabrix.ts
292 lines (257 loc) · 8.06 KB
/
Fabrix.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
import { EventEmitter } from 'events'
import { union } from 'lodash'
import { Core } from './Core'
import { Configuration } from './Configuration'
import { LoggerProxy } from './LoggerProxy'
import { Spool, IApi, IPkg, IConfig, IEnv } from './common'
import * as Errors from './errors'
import * as pkg from '../package.json'
import { FabrixGeneric } from './common/Generic'
import { FabrixController } from './common/Controller'
import { FabrixService } from './common/Service'
import { FabrixPolicy } from './common/Policy'
import { FabrixModel } from './common/Model'
import { FabrixResolver } from './common/Resolver'
import { ServerSpool } from './common/spools/server'
import { ExtensionSpool } from './common/spools/extension'
import { DatastoreSpool } from './common/spools/datastore'
import { SystemSpool } from './common/spools/system'
import { ToolSpool } from './common/spools/tool'
import { MiscSpool } from './common/spools/misc'
// inject Error and Resource types into the global namespace
Core.assignGlobals()
/**
* The Fabrix Application. Merges the configuration and API resources
* loads Spools, initializes logging and event listeners.
*/
export interface FabrixApp {
[key: string]: any
}
export class FabrixApp extends EventEmitter {
private _logger: LoggerProxy
private _env: IEnv
private _pkg: any // IPkg
private _config: Configuration
private _versions: {[key: string]: any }
private _api: IApi
private _fabrix: any
private _spools: {[key: string]: Spool | ServerSpool | ExtensionSpool | DatastoreSpool | SystemSpool | ToolSpool | MiscSpool }
private _resources: string[] = [ ]
public controllers: {[key: string]: any } // FabrixController }
public services: {[key: string]: any } // FabrixService }
public policies: {[key: string]: any } // FabrixPolicy }
public models: {[key: string]: any } // FabrixModel }
public resolvers: {[key: string]: any } // FabrixResolver }
/**
* @param app.pkg The application package.json
* @param app.api The application api (api/ folder)
* @param app.config The application configuration (config/ folder)
*
* Initialize the Fabrix Application and its EventEmitter parent class. Set
* some necessary default configuration.
*/
constructor (app: {
pkg: IPkg,
api: IApi,
config: IConfig
}) {
super()
if (!app) {
throw new RangeError('No app definition provided to Fabrix constructor')
}
if (!app.pkg) {
throw new Errors.PackageNotDefinedError()
}
if (!app.api) {
throw new Errors.ApiNotDefinedError()
}
// set the process node env if not established by environment
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development'
}
const processEnv = Object.freeze(Object.assign({}, JSON.parse(JSON.stringify(process.env))))
this._logger = new LoggerProxy(this)
this._env = processEnv
this._pkg = app.pkg
this._versions = process.versions
this._config = new Configuration(app.config, processEnv)
this._spools = {}
this._api = app.api
this._fabrix = pkg
// Set the max listeners from the config
this.setMaxListeners(this.config.get('main.maxListeners'))
// Set the resources from the configuration (this bypasses the setter with the initial config
// in case the resourceLock is configured)
this._resources = this.config.get('main.resources')
// See if additional resources can be set
this.resources = union(Object.keys(app.api), this.config.get('main.resources'))
// Set each api resource to make sure it's provided as an object in the app
this.resources.forEach(resource => {
app.api[resource] = app.api[resource] || (app.api[resource] = { })
})
// instantiate spools TOTO type of Spool
this.config.get('main.spools').forEach((NewSpool: any) => {
try {
// Create new Instance of the Spool
const spoolContext = <typeof NewSpool> NewSpool
const spool = new spoolContext(this, {})
// Add the spool instance to the app.spools namespace
this.spools[spool.name] = spool
// Reconcile the spool.config with the app.config
this.config.merge(spool.config, spoolContext.configAction)
// Merge extensions into app.<ext>
Core.mergeExtensions(this, spool)
// Merge the spool.api with app.api
Core.mergeSpoolApi(this, spool)
// Bind the Spool Listeners to app.emit
Core.bindSpoolMethodListeners(this, spool)
}
catch (e) {
console.log(e.stack)
throw new Errors.SpoolError(Spool, e, 'constructor')
}
})
// Merge the API from the spools
Core.mergeApi(this)
// Instantiate resource classes and bind resource methods
Core.bindResourceMethods(this, this.resources)
// Bind Application Listeners
Core.bindApplicationListeners(this)
// Bind the Phase listeners for the Spool lifecycle
Core.bindSpoolPhaseListeners(this, Object.values(this.spools))
this.emit('fabrix:constructed')
}
get logger () {
return this._logger
}
get env () {
return this._env
}
get pkg () {
return this._pkg
}
get versions () {
return this._versions
}
get config () {
return this._config
}
/**
* Gets the package.json of the Fabrix module
*/
get fabrix () {
return this._fabrix
}
/**
* Gets the Spools that have been installed
*/
get spools () {
return this._spools
}
/**
* Gets the api
*/
get api () {
return this._api
}
/**
* Return the Fabrix logger
* fires fabrix:log:* log events
*/
get log () {
return this.logger
}
/**
* Sets available/allowed resources from Api and Spool Apis
*/
set resources (values) {
if (!this.config.get('main.lockResources')) {
this._resources = Object.assign([], Configuration.initialResources(this.config, values))
this.config.set('main.resources', this._resources)
}
}
/**
* Gets the Api resources that have been set
*/
get resources() {
return this._resources
}
/**
* Start the App. Load all Spools.
*/
async start (): Promise<any> {
this.emit('fabrix:start')
await this.after('fabrix:ready')
return this
}
/**
* Shutdown. Unbind listeners, unload spools.
*/
async stop (): Promise<any> {
this.emit('fabrix:stop')
await Promise
.all(Object.values(this.spools).map(spool => {
spool.stage = 'unloading'
this.log.debug('Unloading spool', spool.name, '...')
return spool.unload()
}))
.then(() => {
Object.values(this.spools).forEach(spool => { spool.stage = 'unloaded' })
this.log.debug('All spools unloaded. Done.')
this.removeAllListeners()
})
.catch(err => {
this.log.error(err, 'while handling stop.')
throw err
})
return this
}
/**
* Resolve Promise once ANY of the events in the list have emitted.
*/
async onceAny (events: any): Promise<any> {
if (!Array.isArray(events)) {
events = [events]
}
let resolveCallback: any
return Promise
.race(events.map((eventName: any) => {
return new Promise(resolve => {
resolveCallback = resolve
this.once(eventName, resolveCallback)
})
}))
.then((...args: any[]) => {
events.forEach((eventName: any) => this.removeListener(eventName, resolveCallback))
return args
})
.catch(err => {
this.log.error(err, 'handling onceAny events', events)
throw err
})
}
/**
* Resolve Promise once all events in the list have emitted. Also accepts
* a callback.
*/
async after (events: any): Promise<any> {
if (!Array.isArray(events)) {
events = [ events ]
}
return Promise
.all(events.map((eventName: any) => {
return new Promise(resolve => {
if (eventName instanceof Array) {
resolve(this.onceAny(eventName))
}
else {
this.once(eventName, resolve)
}
})
}))
.catch(err => {
this.log.error(err, 'handling after events', events)
throw err
})
}
}