-
Notifications
You must be signed in to change notification settings - Fork 0
/
jimpex.ts
756 lines (722 loc) · 24.7 KB
/
jimpex.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import * as path from 'path';
import fs from 'fs/promises';
import { createServer as createHTTPSServer } from 'https';
import { Jimple } from '@homer0/jimple';
import { deepAssignWithOverwrite } from '@homer0/deep-assign';
import { appLoggerProvider } from '@homer0/simple-logger';
import { envUtilsProvider } from '@homer0/env-utils';
import { packageInfoProvider } from '@homer0/package-info';
import { pathUtilsProvider } from '@homer0/path-utils';
import { rootFileProvider } from '@homer0/root-file';
import { tsAsyncImport } from '@homer0/ts-async-import';
import { EventsHub } from '@homer0/events-hub';
import { simpleConfigProvider } from '@homer0/simple-config';
import compression from 'compression';
import bodyParser from 'body-parser';
import multer from 'multer';
import {
createServer as createSpdyServer,
type ServerOptions as SpdyServerOptions,
} from 'spdy';
import express from 'express';
import {
commonServicesProvider,
httpServicesProvider,
utilsServicesProvider,
} from '../services';
import {
statuses,
type Controller,
type ControllerLike,
type MiddlewareLike,
type MiddlewareProvider,
type Middleware,
} from '../utils';
import type {
DeepPartial,
Express,
ExpressMiddlewareLike,
PathUtils,
Config,
Logger,
JimpexOptions,
JimpexHTTPSCredentials,
JimpexHTTP2Options,
JimpexHTTPSOptions,
JimpexStartCallback,
JimpexServer,
JimpexServerInstance,
JimpexEventName,
JimpexEventPayload,
DeepReadonly,
JimpexReducerEventName,
JimpexReducerEventPayload,
JimpexReducerEventTarget,
JimpexEventNameLike,
JimpexEventListener,
JimpexHealthCheckFn,
Router,
} from '../types';
/**
* Jimpex is a mix of Jimple, a Javascript port of Pimple dependency injection container,
* and Express, one of the most popular web frameworks for Node.
*
* @group Jimpex
* @todo Implement `helmet`.
*/
export class Jimpex extends Jimple {
/**
* The customization settings for the application.
*/
protected _options: JimpexOptions;
/**
* The Express application Jimpex uses under the hood.
*/
protected _express: Express;
/**
* Since the configuration service has an async initialization, the class uses this flag
* internally to validate if the configuration has been initialized or not.
*/
protected _configReady: boolean = false;
/**
* A reference to the actuall HTTP the application will use. This can vary depending on
* whether HTTPS, or HTTP2 are enabled. If HTTPS is not enabled, it will be the same as
* the `express` property; if HTTPS is enabled, it will be an `https` server; and if
* HTTP2 is enabled, it will be an `spdy` server.
*/
protected _server?: JimpexServer;
/**
* The instance of the server that is listening for requests.
*/
protected _instance?: JimpexServerInstance;
/**
* A list of functions that implement controllers and middlewares. When the application
* starts, the queue will be processed and those controllers and middlewares will be
* added to the server instance. The reason they are not added directly like with a
* regular Express implementation is that services on Jimple use lazy loading, and
* adding middlewares and controllers as they come could cause errors if they depend on
* services that are not yet registered.
*/
protected _mountQueue: Array<(server: Express) => void> = [];
/**
* A list with all the top routes controlled by the application. Every time a controller
* is mounted, its route will be added here.
*/
protected _controlledRoutes: string[] = [];
/**
* @param options Preferences to customize the application.
* @param config The default settings for the configuration service. It's a
* shortcuit for `options.config.default`
*/
constructor(options: DeepPartial<JimpexOptions> = {}, config: unknown = {}) {
super();
this._options = deepAssignWithOverwrite(
{
filesizeLimit: '15MB',
boot: true,
path: {
appPath: '',
useParentPath: true,
},
config: {
default: options?.config?.default || config,
name: 'app',
path: 'config/',
hasFolder: false,
loadFromEnvironment: true,
environmentVariable: 'CONFIG',
defaultConfigFilename: '[app-name].config.js',
filenameFormat: '[app-name].[config-name].config.js',
},
statics: {
enabled: true,
onHome: false,
route: 'statics',
},
express: {
trustProxy: true,
disableXPoweredBy: true,
compression: true,
bodyParser: true,
multer: true,
},
services: {
common: true,
http: true,
utils: true,
},
healthCheck: () => Promise.resolve(true),
},
options,
this._initOptions(),
);
this._express = express();
this._setupCoreServices();
this._setupExpress();
this._configurePath();
this._init();
if (this._options.boot) {
this.boot();
}
}
/**
* This is where the app would register all its specific services, middlewares and controllers.
*/
boot(): void {}
/**
* Disables the server TLS validation. Meant to be used for development purposes.
*/
disableTLSValidation() {
// eslint-disable-next-line no-process-env, dot-notation
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
this.logger.warn('TLS validation has been disabled');
}
/**
* Starts the app server.
*
* @param onStart A callback function to be called when the server actually starts.
* @returns The server instance.
*/
async start(onStart?: JimpexStartCallback): Promise<JimpexServerInstance> {
await Promise.all([this._setupConfig(), this._loadESMModules()]);
const config = this.getConfig();
const port = config.get<number | undefined>('port');
if (!port) {
throw new Error('No port configured');
}
this._emitEvent('beforeStart', { app: this });
this._server = await this._createServer();
this._instance = this._server!.listen(port, () => {
this._emitEvent('start', { app: this });
this._mountResources();
this.logger.success(`Starting on port ${port}`);
this._emitEvent('afterStart', { app: this });
if (onStart) {
onStart(config);
}
this._emitEvent('afterStartCallback', { app: this });
});
return this._instance!;
}
/**
* This is an alias of `start`. The idea is for it to be used on serverless platforms,
* where you don't get to start your app, you just have export it.
*
* @param port In case the configuration doesn't already have it,
* this is the port where the application will use to run. If this
* parameter is used, the method will overwrite the `port`
* setting on the configuration service.
* @param onStart A callback function to be called when the server starts.
* @returns The server instance.
*/
async listen(
port?: number,
onStart?: JimpexStartCallback,
): Promise<JimpexServerInstance> {
if (port) {
await Promise.all([this._setupConfig(), this._loadESMModules()]);
const config = this.getConfig();
config.set('port', port);
}
return this.start(onStart);
}
/**
* Stops the server instance.
*/
stop(): void {
if (!this._instance) return;
this._emitEvent('beforeStop', { app: this });
this._instance.close();
this._instance = undefined;
this._emitEvent('afterStop', { app: this });
}
/**
* Mounts a route controller or a middleware into a server routes.
*
* @param route The route for the controller/middleware.
* @param controller The controller/middleware resource to be mounted.
*/
mount(route: string, controller: ControllerLike): void {
let useController: Controller | Middleware;
if (
'register' in controller &&
typeof controller.register === 'function' &&
controller.provider === true
) {
useController = controller.register(this, route);
} else if (
'connect' in controller &&
typeof controller.connect === 'function' &&
(('middleware' in controller && controller.middleware === true) ||
('controller' in controller && controller.controller === true))
) {
useController = controller;
} else {
useController = {
middleware: true,
connect: () => controller as ExpressMiddlewareLike,
};
}
this._mountQueue.push((server) => {
const connected = useController.connect(this, route);
if (!connected) return;
const router = this._reduceWithEvent('controllerWillBeMounted', connected, {
route,
controller: useController,
app: this,
});
server.use(route, router);
this._emitEvent('routeAdded', { route, app: this });
this._controlledRoutes.push(route);
});
}
/**
* Adds a global middleware to the application.
*
* @param middleware The middleware resource to be added.
*/
use(middleware: MiddlewareLike): void {
const useMiddleware =
'register' in middleware && typeof middleware.register === 'function'
? (middleware as MiddlewareProvider).register(this)
: (middleware as Middleware | ExpressMiddlewareLike);
this._mountQueue.push((server) => {
if ('connect' in useMiddleware && typeof useMiddleware.connect === 'function') {
const handler = useMiddleware.connect(this);
if (handler) {
server.use(
this._reduceWithEvent('middlewareWillBeUsed', handler, { app: this }),
);
}
return;
}
server.use(
this._reduceWithEvent(
'middlewareWillBeUsed',
useMiddleware as ExpressMiddlewareLike,
{ app: this },
),
);
});
}
getConfig(): Config;
getConfig<T = unknown>(setting: string | string[], asArray?: boolean): T;
/**
* Gets a setting from the configuration, or the configuration itself.
*
* @param setting The setting or settings to be retrieved. If is not specified, it
* will return the entire configuration.
* @param asArray If `true` and `setting` is an array, it will return the values as
* an array instead of an object.
* @template T The type of the setting to be retrieved.
*/
getConfig<T = unknown>(
setting?: string | string[],
asArray: boolean = false,
): Config | T {
const config = this.try<Config>('config');
if (!config) {
throw new Error('The config service is not available until the app starts');
}
if (typeof setting === 'undefined') {
return config;
}
return config.get<T>(setting, asArray);
}
/**
* Creates a new router instance.
*/
getRouter(): Router {
return this.get('router');
}
/**
* The logger service.
*/
get logger(): Logger {
return this.get<Logger>('logger');
}
/**
* The Express application Jimpex uses under the hood.
*/
get express(): Express {
return this._express;
}
/**
* The server instance that gets created when the app is started.
*/
get instance(): JimpexServerInstance | undefined {
return this._instance;
}
/**
* The application customization options.
*/
get options(): DeepReadonly<JimpexOptions> {
return deepAssignWithOverwrite({}, this._options);
}
/**
* Gets the events service.
*/
get eventsHub(): EventsHub {
return this.get<EventsHub>('events');
}
/**
* A list of the routes that have controllers mounted on them.
*/
get routes(): string[] {
return this._controlledRoutes.slice();
}
/**
* Adds a listener for an application event.
*
* @param eventName The name of the event to listen for.
* @param listener The listener function.
* @returns A function to unsubscribe the listener.
* @template EventName The name of the event, to match the type of the listener
* function.
*/
on<EventName extends JimpexEventNameLike>(
eventName: EventName,
listener: JimpexEventListener<EventName>,
): () => boolean {
return this.eventsHub.on(eventName, listener);
}
/**
* Adds a listener for an application event that will only be execuded once: the first
* time the event is triggered.
*
* @param eventName The name of the event to listen for.
* @param listener The listener function.
* @returns A function to unsubscribe the listener.
* @template EventName The name of the event, to match the type of the listener
* function.
*/
once<EventName extends JimpexEventNameLike>(
eventName: EventName,
listener: JimpexEventListener<EventName>,
): () => boolean {
return this.eventsHub.once(eventName, listener);
}
/**
* Based on the application options, it returns wheter the application is healthy or
* not.
*/
isHealthy(): ReturnType<JimpexHealthCheckFn> {
return this._options.healthCheck(this);
}
/**
* This method is like a "lifecycle method", it gets executed on the constructor right
* before the "boot step". The idea is for the method to be a helper when the
* application is defined by subclassing {@link Jimpex}: the application could register
* all important services here and the routes on boot, then, if the implementation needs
* to access or overwrite a something, it can send `boot: false`, access/register what
* it needs, and then call `boot()`. That would be impossible for an application without
* overwriting the constructor and the boot functionality.
*/
protected _init(): void {}
/**
* It generates overwrites for the application options when it gets created. This method
* is a helper for when the application is defined by subclassing {@link Jimpex}: It's
* highly probable that if the application needs to change the default options, it would
* want to do it right from the class, instead of having to do it on every
* implementation. A way to do it would be overwriting the constructor and calling
* `super` with the custom overwrites, but this method exists so that won't be
* necessary: when creating the `options`, the constructor will merge the result of this
* method on top of the default ones.
*/
protected _initOptions(): DeepPartial<JimpexOptions> {
return {};
}
/**
* Registers the "core services" on the container: logger, events, utils, etc.
*/
protected _setupCoreServices(): void {
this.register(
appLoggerProvider({
serviceName: 'logger',
}),
);
this.register(envUtilsProvider);
this.register(packageInfoProvider);
this.register(pathUtilsProvider);
this.register(rootFileProvider);
const { services: enabledServices } = this._options;
if (enabledServices.common) this.register(commonServicesProvider);
if (enabledServices.http) this.register(httpServicesProvider);
if (enabledServices.utils) this.register(utilsServicesProvider);
this.set('events', () => new EventsHub());
this.set('statuses', () => statuses);
}
/**
* Configures the Express application based on the class options.
*/
protected _setupExpress(): void {
const { statics, filesizeLimit, express: expressOptions } = this._options;
if (expressOptions.trustProxy) {
this._express.enable('trust proxy');
}
if (expressOptions.disableXPoweredBy) {
this._express.disable('x-powered-by');
}
if (expressOptions.compression) {
this._express.use(compression());
}
if (statics.enabled) {
this._addStaticsFolder(statics.route, statics.folder, statics.onHome);
}
if (expressOptions.bodyParser) {
this._express.use(
bodyParser.json({
limit: filesizeLimit,
}),
);
this._express.use(
bodyParser.urlencoded({
extended: true,
limit: filesizeLimit,
}),
);
}
if (expressOptions.multer) {
this._express.use(multer().any());
}
this.set(
'router',
this.factory(() => express.Router()),
);
}
/**
* Adds a static folder to the application.
*
* @param route The route to add the folder to.
* @param folder The path to the folder in the file system. If not defined, it will
* be use the same value as `route`. The path could be relative to the
* project root, or where the application executable is located,
* depending on the value of the `onHome` parameter.
* @param onHome If `true`, the path to the folder will be relative to the project
* root. If `false`, it will be relative to where the application
* executable is located.
*/
protected _addStaticsFolder(
route: string,
folder: string = '',
onHome: boolean = false,
) {
const location = onHome ? 'home' : 'app';
const staticRoute = route.replace(/^\/+/, '');
const pathUtils = this.get<PathUtils>('pathUtils');
const staticFolder = pathUtils.joinFrom(location, folder || staticRoute);
this.mount(`/${staticRoute}`, {
connect: () => express.static(staticFolder),
controller: true,
});
}
/**
* This helper method validates the `path` options in order to register the `app`
* location in the `pathUtils` service. The `app` location should be the path to where
* the application executable is located, but due to how ESM works, we can't infer it
* from the `module` object, so we need either recieved as the `appPath` setting, or try
* to get it from the parent module.
*
* @throws If the method should use the path from the parent module, but can't find
* it.
*/
protected _configurePath(): void {
const pathUtils = this.get<PathUtils>('pathUtils');
const {
path: { appPath, useParentPath },
} = this._options;
if (appPath) {
pathUtils.addLocation('app', appPath);
return;
}
let foundPath = false;
if (useParentPath) {
const stack = new Error().stack!;
const stackList = stack.split('\n');
stackList.shift();
const parentFromStack = stackList.find((line) => !line.includes(__filename));
if (parentFromStack) {
const parentFile = parentFromStack.replace(/^.*?\s\(([^\s]+):\d+:\d+\)/, '$1');
if (parentFile !== parentFromStack) {
foundPath = true;
pathUtils.addLocation('app', path.dirname(parentFile));
}
}
}
if (!foundPath) {
throw new Error(
'The app location cannot be determined. Please specify the appPath option.',
);
}
}
/**
* Setups the configuration service. The new configuration service requires async calls
* in order to load the configuration files (as it uses `import` instead of `require`),
* so it can't be instantiated as the other services.
* This method is called just before starting the application.
*/
protected async _setupConfig(): Promise<void> {
if (this._configReady) return;
this._configReady = true;
const { config: options } = this._options;
let configsPath = options.path.replace(/\/$/, '');
if (options.hasFolder) {
configsPath = `${configsPath}${path.sep}${options.name}${path.sep}`;
}
const filenameFormat = options.filenameFormat
.replace(/\[app-name\]/gi, options.name)
.replace(/\[config-name\]/gi, '[name]');
const defaultConfigFilename = options.defaultConfigFilename.replace(
/\[app-name\]/gi,
options.name,
);
this.register(
simpleConfigProvider({
name: options.name,
defaultConfig: options.default,
defaultConfigFilename,
envVarName: options.environmentVariable,
path: configsPath,
filenameFormat,
}),
);
const config = this.getConfig();
await config.loadFromFile('', true, false);
if (options.loadFromEnvironment) {
await config.loadFromEnv();
}
}
/**
* Loads the ESM modules that are needed by Jimpex. This is called just before the starting
* the application so they'll be available for all the services.
*/
protected async _loadESMModules(): Promise<void> {
const { default: nodeFetch } =
await tsAsyncImport<typeof import('node-fetch')>('node-fetch');
const { default: mime } = await tsAsyncImport<typeof import('mime')>('mime');
this.set('node-fetch', () => nodeFetch);
this.set('mime', () => mime);
}
/**
* Processes the resources from the mount queue (added with {@link Jimpex.mount} and
* {@link Jimpex.use}), and adds them to the Express application.
*/
protected _mountResources(): void {
this._mountQueue.forEach((mount) => mount(this._express));
this._mountQueue.length = 0;
}
/**
* Emits an event using the `events` service.
*
* @param name The name of the event to emit.
* @param payload The event payload.
* @template EventName The literal name of the event, to type the event payload.
*/
protected _emitEvent<EventName extends JimpexEventName>(
name: EventName,
payload: JimpexEventPayload<EventName>,
): void {
this.eventsHub.emit(name, payload);
}
/**
* Sends a target object to a list of reducer events so they can modify or replace it.
*
* @param name The name of the event to use.
* @param target The object to reduce with the event.
* @param payload Extra context for the listeners.
*/
protected _reduceWithEvent<EventName extends JimpexReducerEventName>(
name: EventName,
target: JimpexReducerEventTarget<EventName>,
payload: JimpexReducerEventPayload<EventName>,
): JimpexReducerEventTarget<EventName> {
return this.eventsHub.reduceSync(name, target, payload);
}
/**
* Loads the contents of a dictionary of credentials files that need to be used to
* configure HTTPS.
*
* @param credentialsInfo The dictionary where the keys are the type of credentials
* (`ca`, `cert`, `key`) and the values are the paths to the
* files.
* @param onHome If this is `true`, the path of the files will be relative
* to the project root. If it is `false`, it will be relative
* to where the application executable is located.
* @returns
*/
protected async _loadCredentials(
credentialsInfo: JimpexHTTPSCredentials,
onHome: boolean = true,
): Promise<JimpexHTTPSCredentials> {
const location = onHome ? 'home' : 'app';
const pathUtils = this.get<PathUtils>('pathUtils');
const keys: Array<keyof JimpexHTTPSCredentials> = ['ca', 'cert', 'key'];
const info = await Promise.all(
keys.map(async (key) => {
const filepath = credentialsInfo[key];
if (!filepath) return undefined;
const file = await fs.readFile(pathUtils.joinFrom(location, filepath), 'utf8');
return {
key,
file,
};
}),
);
return info.reduce<JimpexHTTPSCredentials>((acc, item) => {
if (item) {
acc[item.key] = item.file;
}
return acc;
}, {});
}
/**
* Validates the configuration and chooses the server the application needs to use: If
* HTTP2 is enabled, it will use Spdy; if HTTPS is enabled but HTTP is not, it will use
* the native HTTPS server; otherwise, it will be just the Express instance.
*
* @returns {Server}
* @throws {Error} If HTTP2 is enabled but HTTPS is not.
* @throws {Error} If HTTPS is enabled but there's no `https.credentials` object.
* @throws {Error} If HTTPS is enabled and no creadentials are found.
* @access protected
* @ignore
*/
protected async _createServer(): Promise<JimpexServer> {
const [http2Config = {}, httpsConfig = {}] = this.getConfig<
[JimpexHTTP2Options, JimpexHTTPSOptions]
>(['http2', 'https'], true);
if (!http2Config.enabled && !httpsConfig.enabled) {
return this._express;
}
if (http2Config.enabled && !httpsConfig.enabled) {
throw new Error('HTTP2 requires for HTTPS to be enabled');
}
if (!httpsConfig.credentials) {
throw new Error('The `credentials` object on the HTTPS settings is missing');
}
const credentials = await this._loadCredentials(
httpsConfig.credentials,
httpsConfig.credentials.onHome,
);
if (!Object.keys(credentials).length) {
throw new Error('No credentials were found for HTTPS');
}
if (http2Config.enabled) {
const serverOptions: SpdyServerOptions = {
...credentials,
spdy: http2Config.spdy,
};
return createSpdyServer(serverOptions, this._express);
}
return createHTTPSServer(credentials, this._express);
}
}
/**
* Shorthand for `new Jimpex()`.
*
* @param args The same parameters as the {@link Jimpex} constructor.
* @returns A new instance of {@link Jimpex}.
* @group Jimpex
*/
export const jimpex = (...args: ConstructorParameters<typeof Jimpex>): Jimpex =>
new Jimpex(...args);