@itee/server 8.0.0

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.
@@ -0,0 +1,501 @@
1
+ /**
2
+ * ┳ ┏┓ ┏┓ ┏┓ ┏┓ ┏┓ ┳┳┓ ┓ ┓
3
+ * ┃╋┏┓┏┓ ┗┓┏┓┏┓┓┏┏┓┏┓ ┓┏┣┓ ┫ ┃┫ ━━ ┣ ┏┃┃┃┏┓┏┫┓┏┃┏┓
4
+ * ┻┗┗ ┗ •┗┛┗ ┛ ┗┛┗ ┛ ┗┛┗┛•┗┛•┗┛ ┗┛┛┛ ┗┗┛┗┻┗┻┗┗
5
+ *
6
+ * @desc The server side of the Itee solution for 3d web content, this package is design to work with an Itee client.
7
+ * @author [Tristan Valcke]{@link https://github.com/Itee}
8
+ * @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause}
9
+ *
10
+ */
11
+ import express from 'express';
12
+ import http from 'http';
13
+ import https from 'https';
14
+ import { TAbstractObject, DefaultLogger } from 'itee-core';
15
+ import { isNull, isUndefined, isNotString, isEmptyString, isBlankString, isNotArray, isDefined, isArray } from 'itee-validators';
16
+ import path from 'path';
17
+
18
+ /**
19
+ * @author [Tristan Valcke]{@link https://github.com/Itee}
20
+ * @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause}
21
+ *
22
+ * @file Todo
23
+ *
24
+ * @example Todo
25
+ *
26
+ */
27
+
28
+
29
+ class TBackendManager extends TAbstractObject {
30
+
31
+ constructor( parameters = {} ) {
32
+
33
+ const _parameters = {
34
+ ...{
35
+ logger: DefaultLogger,
36
+ rootPath: __dirname,
37
+ applications: [],
38
+ databases: [],
39
+ servers: []
40
+ },
41
+ ...parameters
42
+ };
43
+
44
+ super( _parameters );
45
+
46
+ this.logger = _parameters.logger;
47
+ this.rootPath = _parameters.rootPath;
48
+ this.applications = express();
49
+ this.router = express.Router;
50
+ this.databases = new Map();
51
+ this.servers = new Map();
52
+ this.connections = [];
53
+
54
+ this._initApplications( _parameters.applications );
55
+ this._initDatabases( _parameters.databases );
56
+ this._initServers( _parameters.servers );
57
+
58
+ }
59
+ get applications() {
60
+ return this._applications
61
+ }
62
+ set applications( value ) {
63
+ this._applications = value;
64
+ }
65
+ get router() {
66
+ return this._router
67
+ }
68
+
69
+ // Todo remove middleware
70
+ set router( value ) {
71
+ this._router = value;
72
+ }
73
+ get databases() {
74
+ return this._databases
75
+ }
76
+ set databases( value ) {
77
+ this._databases = value;
78
+ }
79
+ get servers() {
80
+ return this._servers
81
+ }
82
+ set servers( value ) {
83
+ this._servers = value;
84
+ }
85
+ get rootPath() {
86
+
87
+ return this._rootPath
88
+
89
+ }
90
+ set rootPath( value ) {
91
+
92
+ if ( isNull( value ) ) { throw new TypeError( 'Root path cannot be null ! Expect a non empty string.' ) }
93
+ if ( isUndefined( value ) ) { throw new TypeError( 'Root path cannot be undefined ! Expect a non empty string.' ) }
94
+ if ( isNotString( value ) ) { throw new TypeError( `Root path cannot be an instance of ${ value.constructor.name } ! Expect a non empty string.` ) }
95
+ if ( isEmptyString( value ) ) { throw new TypeError( 'Root path cannot be empty ! Expect a non empty string.' ) }
96
+ if ( isBlankString( value ) ) { throw new TypeError( 'Root path cannot contain only whitespace ! Expect a non empty string.' ) }
97
+
98
+ this._rootPath = value;
99
+
100
+ }
101
+ setApplications( value ) {
102
+
103
+ this.applications = value;
104
+ return this
105
+
106
+ }
107
+ addMiddleware( middleware ) {
108
+
109
+ this.applications.use( middleware );
110
+ return this
111
+
112
+ }
113
+ setRouter( value ) {
114
+
115
+ this.router = value;
116
+ return this
117
+
118
+ }
119
+ setDatabases( value ) {
120
+
121
+ this.databases = value;
122
+ return this
123
+
124
+ }
125
+ addDatabase( databaseName, database ) {
126
+
127
+ this._databases.set( databaseName, database );
128
+ return this
129
+
130
+ }
131
+ setServers( value ) {
132
+
133
+ this.servers = value;
134
+ return this
135
+
136
+ }
137
+ setRootPath( value ) {
138
+
139
+ this.rootPath = value;
140
+ return this
141
+
142
+ }
143
+
144
+ _initApplications( config ) {
145
+
146
+ if ( config.case_sensitive_routing ) { this.applications.set( 'case sensitive routing', config.case_sensitive_routing ); }
147
+ if ( config.env ) { this.applications.set( 'env', config.env ); }
148
+ if ( config.etag ) { this.applications.set( 'etag', config.etag ); }
149
+ if ( config.jsonp_callback_name ) { this.applications.set( 'jsonp callback name', config.jsonp_callback_name ); }
150
+ if ( config.jsonp_escape ) { this.applications.set( 'json escape', config.jsonp_escape ); }
151
+ if ( config.jsonp_replacer ) { this.applications.set( 'json replacer', config.jsonp_replacer ); }
152
+ if ( config.jsonp_spaces ) { this.applications.set( 'json spaces', config.jsonp_spaces ); }
153
+ if ( config.query_parser ) { this.applications.set( 'query parser', config.query_parser ); }
154
+ if ( config.strict_routing ) { this.applications.set( 'strict routing', config.strict_routing ); }
155
+ if ( config.subdomain_offset ) { this.applications.set( 'subdomain offset', config.subdomain_offset ); }
156
+ if ( config.trust_proxy ) { this.applications.set( 'trust proxy', config.trust_proxy ); }
157
+ if ( config.views ) { this.applications.set( 'views', config.views ); }
158
+ if ( config.view_cache ) { this.applications.set( 'view cache', config.view_cache ); }
159
+ if ( config.view_engine ) { this.applications.set( 'view engine', config.view_engine ); }
160
+ if ( config.x_powered_by ) { this.applications.set( 'x-powered-by', config.x_powered_by ); }
161
+
162
+ this._initMiddlewares( config.middlewares );
163
+ this._initRouters( config.routers );
164
+
165
+ }
166
+
167
+ _initMiddlewares( middlewaresConfig ) {
168
+
169
+ for ( let [ name, config ] of Object.entries( middlewaresConfig ) ) {
170
+
171
+ if ( isNotArray( config ) ) {
172
+ throw new TypeError( `Invalid middlware configuration for ${ name }, expecting an array of arguments to spread to middleware module, got ${ config.constructor.name }` )
173
+ }
174
+
175
+ if ( this._initPackageMiddleware( name, config ) ) {
176
+
177
+ this.logger.log( `Use ${ name } middleware from node_modules` );
178
+
179
+ } else if ( this._initLocalMiddleware( name, config ) ) {
180
+
181
+ this.logger.log( `Use ${ name } middleware from local folder` );
182
+
183
+ } else {
184
+
185
+ this.logger.error( `Unable to register the middleware ${ name } the package and/or local file doesn't seem to exist ! Skip it.` );
186
+
187
+ }
188
+
189
+ }
190
+
191
+ }
192
+
193
+ _initPackageMiddleware( name, config ) {
194
+
195
+ let success = false;
196
+
197
+ try {
198
+
199
+ this.applications.use( require( name )( ...config ) );
200
+ success = true;
201
+
202
+ } catch ( error ) {
203
+
204
+ if ( !error.code || error.code !== 'MODULE_NOT_FOUND' ) {
205
+
206
+ this.logger.error( `The middleware "${ name }" seems to encounter internal error.` );
207
+ this.logger.error( error );
208
+
209
+ }
210
+
211
+ }
212
+
213
+ return success
214
+
215
+ }
216
+
217
+ _initLocalMiddleware( name, config ) {
218
+
219
+ let success = false;
220
+
221
+ try {
222
+
223
+ const localMiddlewaresPath = path.join( this.rootPath, 'middlewares', name );
224
+ this.applications.use( require( localMiddlewaresPath )( ...config ) );
225
+ success = true;
226
+
227
+ } catch ( error ) {
228
+
229
+ this.logger.error( error );
230
+
231
+ }
232
+
233
+ return success
234
+
235
+ }
236
+
237
+ _initRouters( routers ) {
238
+
239
+ for ( let [ baseRoute, routerPath ] of Object.entries( routers ) ) {
240
+
241
+ if ( this._initPackageRouter( baseRoute, routerPath ) ) {
242
+
243
+ this.logger.log( `Use ${ routerPath } router from node_modules over base route: ${ baseRoute }` );
244
+
245
+ } else if ( this._initLocalRouter( baseRoute, routerPath ) ) {
246
+
247
+ this.logger.log( `Use ${ routerPath } router from local folder over base route: ${ baseRoute }` );
248
+
249
+ } else {
250
+
251
+ this.logger.error( `Unable to register the router ${ routerPath } the package and/or local file doesn't seem to exist ! Skip it.` );
252
+
253
+ }
254
+
255
+ }
256
+
257
+ }
258
+
259
+ _initPackageRouter( baseRoute, routerPath ) {
260
+
261
+ let success = false;
262
+
263
+ try {
264
+
265
+ this.applications.use( baseRoute, require( routerPath ) );
266
+ success = true;
267
+
268
+ } catch ( error ) {
269
+
270
+ if ( !error.code || error.code !== 'MODULE_NOT_FOUND' ) {
271
+
272
+ this.logger.error( `The router "${ baseRoute }" seems to encounter internal error.` );
273
+ this.logger.error( error );
274
+
275
+ }
276
+
277
+ }
278
+
279
+ return success
280
+
281
+ }
282
+
283
+ _initLocalRouter( baseRoute, routerPath ) {
284
+
285
+ let success = false;
286
+
287
+ try {
288
+
289
+ const localRoutersPath = path.join( this.rootPath, 'routers', routerPath );
290
+ this.applications.use( baseRoute, require( localRoutersPath ) );
291
+ success = true;
292
+
293
+ } catch ( error ) {
294
+
295
+ if ( error instanceof TypeError && error.message === 'Found non-callable @@iterator' ) {
296
+
297
+ this.logger.error( `The router "${ baseRoute }" seems to encounter error ! Are you using an object instead an array for router configuration ?` );
298
+
299
+ }
300
+
301
+ this.logger.error( error );
302
+
303
+ }
304
+
305
+ return success
306
+
307
+ }
308
+
309
+ _initDatabases( config ) {
310
+
311
+ for ( let configIndex = 0, numberOfDatabasesConfigs = config.length ; configIndex < numberOfDatabasesConfigs ; configIndex++ ) {
312
+
313
+ const databaseConfig = config[ configIndex ];
314
+ const dbType = databaseConfig.type;
315
+ const dbFrom = databaseConfig.from;
316
+ const dbName = `${ ( databaseConfig.name ) ? databaseConfig.name : `${ dbType }_${ configIndex }` }`;
317
+
318
+ try {
319
+
320
+ let database = null;
321
+
322
+ if ( isDefined( dbFrom ) ) {
323
+
324
+ // In case user specify a package where take the database of type...
325
+ const databasePackage = require( dbFrom );
326
+ database = new databasePackage[ dbType ]( {
327
+ ...{
328
+ application: this.applications,
329
+ router: this.router
330
+ },
331
+ ...databaseConfig
332
+ } );
333
+
334
+ } else {
335
+
336
+ // // Else try to use auto registered database
337
+ // database = new Databases[ dbType ]( {
338
+ // ...{
339
+ // application: this.applications,
340
+ // router: this.router
341
+ // },
342
+ // ...databaseConfig
343
+ // } )
344
+
345
+ }
346
+
347
+ // Todo move in start
348
+ database.connect();
349
+
350
+ this.databases.set( dbName, database );
351
+
352
+ } catch ( error ) {
353
+
354
+ this.logger.error( `Unable to create database of type ${ dbType } due to ${ error.name }` );
355
+ this.logger.error( error.message );
356
+ this.logger.error( error.stack );
357
+
358
+ }
359
+
360
+ }
361
+
362
+ }
363
+
364
+ _initServers( config ) {
365
+
366
+ const _config = ( isArray( config ) ) ? config : [ config ];
367
+
368
+ for ( let configId = 0, numberOfConfigs = _config.length ; configId < numberOfConfigs ; configId++ ) {
369
+
370
+ let configElement = _config[ configId ];
371
+ let server = null;
372
+
373
+ if ( configElement.type === 'https' ) {
374
+
375
+ const options = {
376
+ pfx: configElement.pfx,
377
+ passphrase: configElement.passphrase
378
+ };
379
+
380
+ server = https.createServer( options, this.applications );
381
+
382
+ } else {
383
+
384
+ server = http.createServer( this.applications );
385
+
386
+ }
387
+
388
+ server.name = configElement.name || `${ ( configElement.name ) ? configElement.name : `Server_${ configId }` }`;
389
+ server.maxHeadersCount = configElement.max_headers_count;
390
+ server.timeout = configElement.timeout;
391
+ server.type = configElement.type;
392
+ server.host = configElement.host;
393
+ server.port = configElement.port;
394
+ server.env = configElement.env;
395
+ server.listen( configElement.port, configElement.host, () => {
396
+ this.logger.log( `${ server.name } start listening on ${ server.type }://${ server.host }:${ server.port } at ${ new Date() } under ${ server.env } environment.` );
397
+ } );
398
+ server.on( 'connection', connection => {
399
+ this.connections.push( connection );
400
+ connection.on( 'close', () => {
401
+ this.connections = this.connections.filter( curr => curr !== connection );
402
+ } );
403
+ } );
404
+
405
+ this.servers.set( server.name, server );
406
+
407
+ }
408
+
409
+ }
410
+
411
+ /**
412
+ *
413
+ * @param databaseKey
414
+ * @param eventName
415
+ * @param callback
416
+ */
417
+ databaseOn( databaseKey, eventName, callback ) {} // eslint-disable-line no-unused-vars
418
+
419
+ serverOn( serverName, eventName, callback ) {
420
+
421
+ this.servers[ serverName ].on( eventName, callback );
422
+
423
+ }
424
+
425
+ serversOn( serverKey, eventName, callback ) {
426
+
427
+ //TODO: filter availaible events
428
+ // [ 'request', 'connection', 'close', 'timeout', 'checkContinue', 'connect', 'upgrade', 'clientError' ]
429
+ for ( let serverKey in this.servers ) {
430
+ this.serverOn( serverKey, eventName, callback );
431
+ }
432
+
433
+ }
434
+
435
+ start() {
436
+
437
+ }
438
+
439
+ stop( callback ) {
440
+
441
+ const numberOfServers = this.servers.size;
442
+ const numberOfDatabases = this.databases.size;
443
+ let shutDownServers = 0;
444
+ let closedDatabases = 0;
445
+
446
+ if ( allClosed() ) { return }
447
+
448
+ for ( const [ databaseName, database ] of this.databases ) {
449
+
450
+ database.close( () => {
451
+
452
+ closedDatabases++;
453
+ this.logger.log( `Connection to ${ databaseName } is closed.` );
454
+
455
+ allClosed();
456
+
457
+ } );
458
+
459
+ }
460
+
461
+ for ( let connection of this.connections ) {
462
+ connection.end();
463
+ }
464
+
465
+ for ( const [ serverName, server ] of this.servers ) {
466
+
467
+ server.close( () => {
468
+
469
+ shutDownServers++;
470
+ this.logger.log( `The ${ serverName } listening on ${ server.type }://${ server.host }:${ server.port } is shutted down.` );
471
+
472
+ allClosed();
473
+
474
+ } );
475
+
476
+ }
477
+
478
+ function allClosed() {
479
+
480
+ if ( shutDownServers < numberOfServers ) {
481
+ return false
482
+ }
483
+
484
+ if ( closedDatabases < numberOfDatabases ) {
485
+ return false
486
+ }
487
+
488
+ if ( callback ) { callback(); }
489
+
490
+ }
491
+
492
+ }
493
+
494
+ closeServers() {
495
+
496
+ }
497
+
498
+ }
499
+
500
+ export { TBackendManager };
501
+ //# sourceMappingURL=itee-server.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"itee-server.esm.js","sources":["../sources/TBackendManager.js"],"sourcesContent":["/**\n * @author [Tristan Valcke]{@link https://github.com/Itee}\n * @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause}\n *\n * @file Todo\n *\n * @example Todo\n *\n */\n\nimport express from 'express'\nimport http from 'http'\nimport https from 'https'\nimport {\n DefaultLogger,\n TAbstractObject\n} from 'itee-core'\n//todo: import Databases from 'itee-database'\nimport {\n isArray,\n isBlankString,\n isDefined,\n isEmptyString,\n isNotArray,\n isNotString,\n isNull,\n isUndefined\n} from 'itee-validators'\nimport path from 'path'\n\nclass TBackendManager extends TAbstractObject {\n\n constructor( parameters = {} ) {\n\n const _parameters = {\n ...{\n logger: DefaultLogger,\n rootPath: __dirname,\n applications: [],\n databases: [],\n servers: []\n },\n ...parameters\n }\n\n super( _parameters )\n\n this.logger = _parameters.logger\n this.rootPath = _parameters.rootPath\n this.applications = express()\n this.router = express.Router\n this.databases = new Map()\n this.servers = new Map()\n this.connections = []\n\n this._initApplications( _parameters.applications )\n this._initDatabases( _parameters.databases )\n this._initServers( _parameters.servers )\n\n }\n get applications() {\n return this._applications\n }\n set applications( value ) {\n this._applications = value\n }\n get router() {\n return this._router\n }\n\n // Todo remove middleware\n set router( value ) {\n this._router = value\n }\n get databases() {\n return this._databases\n }\n set databases( value ) {\n this._databases = value\n }\n get servers() {\n return this._servers\n }\n set servers( value ) {\n this._servers = value\n }\n get rootPath() {\n\n return this._rootPath\n\n }\n set rootPath( value ) {\n\n if ( isNull( value ) ) { throw new TypeError( 'Root path cannot be null ! Expect a non empty string.' ) }\n if ( isUndefined( value ) ) { throw new TypeError( 'Root path cannot be undefined ! Expect a non empty string.' ) }\n if ( isNotString( value ) ) { throw new TypeError( `Root path cannot be an instance of ${ value.constructor.name } ! Expect a non empty string.` ) }\n if ( isEmptyString( value ) ) { throw new TypeError( 'Root path cannot be empty ! Expect a non empty string.' ) }\n if ( isBlankString( value ) ) { throw new TypeError( 'Root path cannot contain only whitespace ! Expect a non empty string.' ) }\n\n this._rootPath = value\n\n }\n setApplications( value ) {\n\n this.applications = value\n return this\n\n }\n addMiddleware( middleware ) {\n\n this.applications.use( middleware )\n return this\n\n }\n setRouter( value ) {\n\n this.router = value\n return this\n\n }\n setDatabases( value ) {\n\n this.databases = value\n return this\n\n }\n addDatabase( databaseName, database ) {\n\n this._databases.set( databaseName, database )\n return this\n\n }\n setServers( value ) {\n\n this.servers = value\n return this\n\n }\n setRootPath( value ) {\n\n this.rootPath = value\n return this\n\n }\n\n _initApplications( config ) {\n\n if ( config.case_sensitive_routing ) { this.applications.set( 'case sensitive routing', config.case_sensitive_routing ) }\n if ( config.env ) { this.applications.set( 'env', config.env ) }\n if ( config.etag ) { this.applications.set( 'etag', config.etag ) }\n if ( config.jsonp_callback_name ) { this.applications.set( 'jsonp callback name', config.jsonp_callback_name ) }\n if ( config.jsonp_escape ) { this.applications.set( 'json escape', config.jsonp_escape ) }\n if ( config.jsonp_replacer ) { this.applications.set( 'json replacer', config.jsonp_replacer ) }\n if ( config.jsonp_spaces ) { this.applications.set( 'json spaces', config.jsonp_spaces ) }\n if ( config.query_parser ) { this.applications.set( 'query parser', config.query_parser ) }\n if ( config.strict_routing ) { this.applications.set( 'strict routing', config.strict_routing ) }\n if ( config.subdomain_offset ) { this.applications.set( 'subdomain offset', config.subdomain_offset ) }\n if ( config.trust_proxy ) { this.applications.set( 'trust proxy', config.trust_proxy ) }\n if ( config.views ) { this.applications.set( 'views', config.views ) }\n if ( config.view_cache ) { this.applications.set( 'view cache', config.view_cache ) }\n if ( config.view_engine ) { this.applications.set( 'view engine', config.view_engine ) }\n if ( config.x_powered_by ) { this.applications.set( 'x-powered-by', config.x_powered_by ) }\n\n this._initMiddlewares( config.middlewares )\n this._initRouters( config.routers )\n\n }\n\n _initMiddlewares( middlewaresConfig ) {\n\n for ( let [ name, config ] of Object.entries( middlewaresConfig ) ) {\n\n if ( isNotArray( config ) ) {\n throw new TypeError( `Invalid middlware configuration for ${ name }, expecting an array of arguments to spread to middleware module, got ${ config.constructor.name }` )\n }\n\n if ( this._initPackageMiddleware( name, config ) ) {\n\n this.logger.log( `Use ${ name } middleware from node_modules` )\n\n } else if ( this._initLocalMiddleware( name, config ) ) {\n\n this.logger.log( `Use ${ name } middleware from local folder` )\n\n } else {\n\n this.logger.error( `Unable to register the middleware ${ name } the package and/or local file doesn't seem to exist ! Skip it.` )\n\n }\n\n }\n\n }\n\n _initPackageMiddleware( name, config ) {\n\n let success = false\n\n try {\n\n this.applications.use( require( name )( ...config ) )\n success = true\n\n } catch ( error ) {\n\n if ( !error.code || error.code !== 'MODULE_NOT_FOUND' ) {\n\n this.logger.error( `The middleware \"${ name }\" seems to encounter internal error.` )\n this.logger.error( error )\n\n }\n\n }\n\n return success\n\n }\n\n _initLocalMiddleware( name, config ) {\n\n let success = false\n\n try {\n\n const localMiddlewaresPath = path.join( this.rootPath, 'middlewares', name )\n this.applications.use( require( localMiddlewaresPath )( ...config ) )\n success = true\n\n } catch ( error ) {\n\n this.logger.error( error )\n\n }\n\n return success\n\n }\n\n _initRouters( routers ) {\n\n for ( let [ baseRoute, routerPath ] of Object.entries( routers ) ) {\n\n if ( this._initPackageRouter( baseRoute, routerPath ) ) {\n\n this.logger.log( `Use ${ routerPath } router from node_modules over base route: ${ baseRoute }` )\n\n } else if ( this._initLocalRouter( baseRoute, routerPath ) ) {\n\n this.logger.log( `Use ${ routerPath } router from local folder over base route: ${ baseRoute }` )\n\n } else {\n\n this.logger.error( `Unable to register the router ${ routerPath } the package and/or local file doesn't seem to exist ! Skip it.` )\n\n }\n\n }\n\n }\n\n _initPackageRouter( baseRoute, routerPath ) {\n\n let success = false\n\n try {\n\n this.applications.use( baseRoute, require( routerPath ) )\n success = true\n\n } catch ( error ) {\n\n if ( !error.code || error.code !== 'MODULE_NOT_FOUND' ) {\n\n this.logger.error( `The router \"${ baseRoute }\" seems to encounter internal error.` )\n this.logger.error( error )\n\n }\n\n }\n\n return success\n\n }\n\n _initLocalRouter( baseRoute, routerPath ) {\n\n let success = false\n\n try {\n\n const localRoutersPath = path.join( this.rootPath, 'routers', routerPath )\n this.applications.use( baseRoute, require( localRoutersPath ) )\n success = true\n\n } catch ( error ) {\n\n if ( error instanceof TypeError && error.message === 'Found non-callable @@iterator' ) {\n\n this.logger.error( `The router \"${ baseRoute }\" seems to encounter error ! Are you using an object instead an array for router configuration ?` )\n\n }\n\n this.logger.error( error )\n\n }\n\n return success\n\n }\n\n _initDatabases( config ) {\n\n for ( let configIndex = 0, numberOfDatabasesConfigs = config.length ; configIndex < numberOfDatabasesConfigs ; configIndex++ ) {\n\n const databaseConfig = config[ configIndex ]\n const dbType = databaseConfig.type\n const dbFrom = databaseConfig.from\n const dbName = `${ ( databaseConfig.name ) ? databaseConfig.name : `${ dbType }_${ configIndex }` }`\n\n try {\n\n let database = null\n\n if ( isDefined( dbFrom ) ) {\n\n // In case user specify a package where take the database of type...\n const databasePackage = require( dbFrom )\n database = new databasePackage[ dbType ]( {\n ...{\n application: this.applications,\n router: this.router\n },\n ...databaseConfig\n } )\n\n } else {\n\n // // Else try to use auto registered database\n // database = new Databases[ dbType ]( {\n // ...{\n // application: this.applications,\n // router: this.router\n // },\n // ...databaseConfig\n // } )\n\n }\n\n // Todo move in start\n database.connect()\n\n this.databases.set( dbName, database )\n\n } catch ( error ) {\n\n this.logger.error( `Unable to create database of type ${ dbType } due to ${ error.name }` )\n this.logger.error( error.message )\n this.logger.error( error.stack )\n\n }\n\n }\n\n }\n\n _initServers( config ) {\n\n const _config = ( isArray( config ) ) ? config : [ config ]\n\n for ( let configId = 0, numberOfConfigs = _config.length ; configId < numberOfConfigs ; configId++ ) {\n\n let configElement = _config[ configId ]\n let server = null\n\n if ( configElement.type === 'https' ) {\n\n const options = {\n pfx: configElement.pfx,\n passphrase: configElement.passphrase\n }\n\n server = https.createServer( options, this.applications )\n\n } else {\n\n server = http.createServer( this.applications )\n\n }\n\n server.name = configElement.name || `${ ( configElement.name ) ? configElement.name : `Server_${ configId }` }`\n server.maxHeadersCount = configElement.max_headers_count\n server.timeout = configElement.timeout\n server.type = configElement.type\n server.host = configElement.host\n server.port = configElement.port\n server.env = configElement.env\n server.listen( configElement.port, configElement.host, () => {\n this.logger.log( `${ server.name } start listening on ${ server.type }://${ server.host }:${ server.port } at ${ new Date() } under ${ server.env } environment.` )\n } )\n server.on( 'connection', connection => {\n this.connections.push( connection )\n connection.on( 'close', () => {\n this.connections = this.connections.filter( curr => curr !== connection )\n } )\n } )\n\n this.servers.set( server.name, server )\n\n }\n\n }\n\n /**\n *\n * @param databaseKey\n * @param eventName\n * @param callback\n */\n databaseOn( databaseKey, eventName, callback ) {} // eslint-disable-line no-unused-vars\n\n serverOn( serverName, eventName, callback ) {\n\n this.servers[ serverName ].on( eventName, callback )\n\n }\n\n serversOn( serverKey, eventName, callback ) {\n\n //TODO: filter availaible events\n // [ 'request', 'connection', 'close', 'timeout', 'checkContinue', 'connect', 'upgrade', 'clientError' ]\n for ( let serverKey in this.servers ) {\n this.serverOn( serverKey, eventName, callback )\n }\n\n }\n\n start() {\n\n }\n\n stop( callback ) {\n\n const numberOfServers = this.servers.size\n const numberOfDatabases = this.databases.size\n let shutDownServers = 0\n let closedDatabases = 0\n\n if ( allClosed() ) { return }\n\n for ( const [ databaseName, database ] of this.databases ) {\n\n database.close( () => {\n\n closedDatabases++\n this.logger.log( `Connection to ${ databaseName } is closed.` )\n\n allClosed()\n\n } )\n\n }\n\n for ( let connection of this.connections ) {\n connection.end()\n }\n\n for ( const [ serverName, server ] of this.servers ) {\n\n server.close( () => {\n\n shutDownServers++\n this.logger.log( `The ${ serverName } listening on ${ server.type }://${ server.host }:${ server.port } is shutted down.` )\n\n allClosed()\n\n } )\n\n }\n\n function allClosed() {\n\n if ( shutDownServers < numberOfServers ) {\n return false\n }\n\n if ( closedDatabases < numberOfDatabases ) {\n return false\n }\n\n if ( callback ) { callback() }\n\n }\n\n }\n\n closeServers() {\n\n }\n\n}\n\nexport { TBackendManager }\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AAsBA,MAAM,eAAe,SAAS,eAAe,CAAC;;AAE9C,IAAI,WAAW,EAAE,UAAU,GAAG,EAAE,GAAG;;AAEnC,QAAQ,MAAM,WAAW,GAAG;AAC5B,YAAY,GAAG;AACf,gBAAgB,MAAM,QAAQ,aAAa;AAC3C,gBAAgB,QAAQ,MAAM,SAAS;AACvC,gBAAgB,YAAY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AQ,MAAM,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,IAAI,CAAC,OAAO,GAAG;;AAE7D,YAAY,MAAM,CAAC,KAAK,EAAE,MAAM;;AAEhC,gBAAgB,eAAe;AAC/B,gBAAgB,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,GAAG,UAAU,EAAE,cAAc,GAAG,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,CAAC,IAAI,EAAE,iBAAiB,CAAC;;AAEzI,gBAAgB,SAAS;;AAEzB,YAAY,CAAC;;AAEb,QAAQ;;AAER,QAAQ,SAAS,SAAS,GAAG;;AAE7B,YAAY,KAAK,eAAe,GAAG,eAAe,GAAG;AACrD,gBAAgB,OAAO;AACvB,YAAY;;AAEZ,YAAY,KAAK,eAAe,GAAG,iBAAiB,GAAG;AACvD,gBAAgB,OAAO;AACvB,YAAY;;AAEZ,YAAY,KAAK,QAAQ,GAAG,EAAE,QAAQ,GAAE,CAAC;;AAEzC,QAAQ;;AAER,IAAI;;AAEJ,IAAI,YAAY,GAAG;;AAEnB,IAAI;;AAEJ;;;;"}
@@ -0,0 +1,10 @@
1
+ import e from"express";import t from"http";import s from"https";import{TAbstractObject as r,DefaultLogger as o}from"itee-core";import{isNull as i,isUndefined as a,isNotString as n,isEmptyString as p,isBlankString as c,isNotArray as h,isDefined as l,isArray as u}from"itee-validators";import g from"path";
2
+ /**
3
+ * @author [Tristan Valcke]{@link https://github.com/Itee}
4
+ * @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause}
5
+ *
6
+ * @file Todo
7
+ *
8
+ * @example Todo
9
+ *
10
+ */class d extends r{constructor(t={}){const s={logger:o,rootPath:__dirname,applications:[],databases:[],servers:[],...t};super(s),this.logger=s.logger,this.rootPath=s.rootPath,this.applications=e(),this.router=e.Router,this.databases=new Map,this.servers=new Map,this.connections=[],this._initApplications(s.applications),this._initDatabases(s.databases),this._initServers(s.servers)}get applications(){return this._applications}set applications(e){this._applications=e}get router(){return this._router}set router(e){this._router=e}get databases(){return this._databases}set databases(e){this._databases=e}get servers(){return this._servers}set servers(e){this._servers=e}get rootPath(){return this._rootPath}set rootPath(e){if(i(e))throw new TypeError("Root path cannot be null ! Expect a non empty string.");if(a(e))throw new TypeError("Root path cannot be undefined ! Expect a non empty string.");if(n(e))throw new TypeError(`Root path cannot be an instance of ${e.constructor.name} ! Expect a non empty string.`);if(p(e))throw new TypeError("Root path cannot be empty ! Expect a non empty string.");if(c(e))throw new TypeError("Root path cannot contain only whitespace ! Expect a non empty string.");this._rootPath=e}setApplications(e){return this.applications=e,this}addMiddleware(e){return this.applications.use(e),this}setRouter(e){return this.router=e,this}setDatabases(e){return this.databases=e,this}addDatabase(e,t){return this._databases.set(e,t),this}setServers(e){return this.servers=e,this}setRootPath(e){return this.rootPath=e,this}_initApplications(e){e.case_sensitive_routing&&this.applications.set("case sensitive routing",e.case_sensitive_routing),e.env&&this.applications.set("env",e.env),e.etag&&this.applications.set("etag",e.etag),e.jsonp_callback_name&&this.applications.set("jsonp callback name",e.jsonp_callback_name),e.jsonp_escape&&this.applications.set("json escape",e.jsonp_escape),e.jsonp_replacer&&this.applications.set("json replacer",e.jsonp_replacer),e.jsonp_spaces&&this.applications.set("json spaces",e.jsonp_spaces),e.query_parser&&this.applications.set("query parser",e.query_parser),e.strict_routing&&this.applications.set("strict routing",e.strict_routing),e.subdomain_offset&&this.applications.set("subdomain offset",e.subdomain_offset),e.trust_proxy&&this.applications.set("trust proxy",e.trust_proxy),e.views&&this.applications.set("views",e.views),e.view_cache&&this.applications.set("view cache",e.view_cache),e.view_engine&&this.applications.set("view engine",e.view_engine),e.x_powered_by&&this.applications.set("x-powered-by",e.x_powered_by),this._initMiddlewares(e.middlewares),this._initRouters(e.routers)}_initMiddlewares(e){for(let[t,s]of Object.entries(e)){if(h(s))throw new TypeError(`Invalid middlware configuration for ${t}, expecting an array of arguments to spread to middleware module, got ${s.constructor.name}`);this._initPackageMiddleware(t,s)?this.logger.log(`Use ${t} middleware from node_modules`):this._initLocalMiddleware(t,s)?this.logger.log(`Use ${t} middleware from local folder`):this.logger.error(`Unable to register the middleware ${t} the package and/or local file doesn't seem to exist ! Skip it.`)}}_initPackageMiddleware(e,t){let s=!1;try{this.applications.use(require(e)(...t)),s=!0}catch(t){t.code&&"MODULE_NOT_FOUND"===t.code||(this.logger.error(`The middleware "${e}" seems to encounter internal error.`),this.logger.error(t))}return s}_initLocalMiddleware(e,t){let s=!1;try{const r=g.join(this.rootPath,"middlewares",e);this.applications.use(require(r)(...t)),s=!0}catch(e){this.logger.error(e)}return s}_initRouters(e){for(let[t,s]of Object.entries(e))this._initPackageRouter(t,s)?this.logger.log(`Use ${s} router from node_modules over base route: ${t}`):this._initLocalRouter(t,s)?this.logger.log(`Use ${s} router from local folder over base route: ${t}`):this.logger.error(`Unable to register the router ${s} the package and/or local file doesn't seem to exist ! Skip it.`)}_initPackageRouter(e,t){let s=!1;try{this.applications.use(e,require(t)),s=!0}catch(t){t.code&&"MODULE_NOT_FOUND"===t.code||(this.logger.error(`The router "${e}" seems to encounter internal error.`),this.logger.error(t))}return s}_initLocalRouter(e,t){let s=!1;try{const r=g.join(this.rootPath,"routers",t);this.applications.use(e,require(r)),s=!0}catch(t){t instanceof TypeError&&"Found non-callable @@iterator"===t.message&&this.logger.error(`The router "${e}" seems to encounter error ! Are you using an object instead an array for router configuration ?`),this.logger.error(t)}return s}_initDatabases(e){for(let t=0,s=e.length;t<s;t++){const s=e[t],r=s.type,o=s.from,i=`${s.name?s.name:`${r}_${t}`}`;try{let e=null;if(l(o)){e=new(require(o)[r])({application:this.applications,router:this.router,...s})}e.connect(),this.databases.set(i,e)}catch(e){this.logger.error(`Unable to create database of type ${r} due to ${e.name}`),this.logger.error(e.message),this.logger.error(e.stack)}}}_initServers(e){const r=u(e)?e:[e];for(let e=0,o=r.length;e<o;e++){let o=r[e],i=null;if("https"===o.type){const e={pfx:o.pfx,passphrase:o.passphrase};i=s.createServer(e,this.applications)}else i=t.createServer(this.applications);i.name=o.name||`${o.name?o.name:`Server_${e}`}`,i.maxHeadersCount=o.max_headers_count,i.timeout=o.timeout,i.type=o.type,i.host=o.host,i.port=o.port,i.env=o.env,i.listen(o.port,o.host,()=>{this.logger.log(`${i.name} start listening on ${i.type}://${i.host}:${i.port} at ${new Date} under ${i.env} environment.`)}),i.on("connection",e=>{this.connections.push(e),e.on("close",()=>{this.connections=this.connections.filter(t=>t!==e)})}),this.servers.set(i.name,i)}}databaseOn(e,t,s){}serverOn(e,t,s){this.servers[e].on(t,s)}serversOn(e,t,s){for(let e in this.servers)this.serverOn(e,t,s)}start(){}stop(e){const t=this.servers.size,s=this.databases.size;let r=0,o=0;if(!i()){for(const[e,t]of this.databases)t.close(()=>{o++,this.logger.log(`Connection to ${e} is closed.`),i()});for(let e of this.connections)e.end();for(const[e,t]of this.servers)t.close(()=>{r++,this.logger.log(`The ${e} listening on ${t.type}://${t.host}:${t.port} is shutted down.`),i()})}function i(){return!(r<t)&&(!(o<s)&&void(e&&e()))}}closeServers(){}}export{d as TBackendManager};
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "@itee/server",
3
+ "version": "8.0.0",
4
+ "description": "The server side of the Itee solution for 3d web content, this package is design to work with an Itee client.",
5
+ "keywords": [
6
+ "itee",
7
+ "itee-server",
8
+ "webgl",
9
+ "three",
10
+ "3d",
11
+ "backend"
12
+ ],
13
+ "author": {
14
+ "name": "Itee (Tristan Valcke)",
15
+ "url": "https://github.com/Itee"
16
+ },
17
+ "contributors": [],
18
+ "license": "BSD-3-Clause",
19
+ "private": false,
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/Itee/server.git"
23
+ },
24
+ "type": "module",
25
+ "main": "builds/server.cjs.js",
26
+ "module": "builds/server.esm.js",
27
+ "browser": "",
28
+ "homepage": "https://github.com/Itee/server#readme",
29
+ "man": "./docs/index.html",
30
+ "bugs": {
31
+ "url": "https://github.com/Itee/server/issues"
32
+ },
33
+ "engines": {
34
+ "node": ">=20"
35
+ },
36
+ "os": [
37
+ "win32",
38
+ "linux"
39
+ ],
40
+ "cpu": [
41
+ "x64",
42
+ "ia32"
43
+ ],
44
+ "scripts": {
45
+ "default": "gulp --tasks",
46
+ "refresh": "node ./node_modules/@itee/tasks/scripts/refresh.mjs",
47
+ "help": "gulp help",
48
+ "patch": "gulp patch",
49
+ "clean": "gulp clean",
50
+ "lint": "gulp lint",
51
+ "doc": "gulp doc",
52
+ "build": "gulp build",
53
+ "tests:run": "gulp run-tests",
54
+ "tests:bundling": "gulp check-bundling",
55
+ "tests:bundling:build-import": "gulp check-bundling-from-esm-build-import",
56
+ "tests:bundling:files-import": "gulp check-bundling-from-esm-files-import",
57
+ "tests:bundling:files-direct": "gulp check-bundling-from-esm-files-direct",
58
+ "tests:benches:compute": "gulp compute-benchmarks",
59
+ "tests:benches:run": "gulp run-benchmarks",
60
+ "tests:benches:run:back": "gulp run-benchmarks-for-backend",
61
+ "tests:benches:run:front": "gulp run-benchmarks-for-frontend",
62
+ "tests:units:compute": "gulp compute-unit-tests",
63
+ "tests:units:run": "gulp run-unit-tests",
64
+ "tests:units:run:back": "gulp run-unit-tests-for-backend",
65
+ "tests:units:run:front": "gulp run-unit-tests-for-frontend",
66
+ "release": "gulp release",
67
+ "semantic-release": "semantic-release --dry-run --no-ci"
68
+ },
69
+ "dependencies": {
70
+ "@itee/core": "^3.0.1",
71
+ "@itee/database": "^10.0.1",
72
+ "@itee/validators": "^7.0.1",
73
+ "express": "^5.1.0"
74
+ },
75
+ "devDependencies": {
76
+ "@itee/tasks": "^1.4.6"
77
+ },
78
+ "optionalDependencies": {
79
+ "express-favicon": "^2.0.1",
80
+ "morgan": "^1.10.0",
81
+ "rotating-file-stream": "^3.0.2"
82
+ },
83
+ "overrides": {
84
+ "semantic-release-gitmoji": {
85
+ "semantic-release": "<26",
86
+ "@semantic-release/github": "12.0.2",
87
+ "@semantic-release/npm": "13.1.3"
88
+ }
89
+ }
90
+ }