jimpex 8.0.0 → 10.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.
- package/CHANGELOG.md +33 -0
- package/README.md +2 -2
- package/dist/app/index.d.mts +14 -0
- package/dist/app/index.d.ts +5 -4
- package/dist/app/index.js +4 -0
- package/dist/app/jimpex.d.mts +14 -0
- package/dist/app/jimpex.d.ts +3 -2
- package/dist/app/jimpex.js +238 -2
- package/dist/app/jimpex.js.map +1 -1
- package/dist/controllers/common/config.d.mts +79 -0
- package/dist/controllers/common/config.d.ts +4 -3
- package/dist/controllers/common/config.js +18 -0
- package/dist/controllers/common/config.js.map +1 -1
- package/dist/controllers/common/health.d.mts +83 -0
- package/dist/controllers/common/health.d.ts +5 -4
- package/dist/controllers/common/health.js +18 -0
- package/dist/controllers/common/health.js.map +1 -1
- package/dist/controllers/common/index.d.mts +22 -0
- package/dist/controllers/common/index.d.ts +5 -4
- package/dist/controllers/common/index.js +6 -0
- package/dist/controllers/common/statics.d.mts +223 -0
- package/dist/controllers/common/statics.d.ts +13 -5
- package/dist/controllers/common/statics.js +60 -3
- package/dist/controllers/common/statics.js.map +1 -1
- package/dist/controllers/index.d.mts +25 -0
- package/dist/controllers/index.d.ts +5 -4
- package/dist/controllers/index.js +5 -0
- package/dist/controllers/utils/gateway.d.mts +725 -0
- package/dist/controllers/utils/gateway.d.ts +5 -4
- package/dist/controllers/utils/gateway.js +155 -0
- package/dist/controllers/utils/gateway.js.map +1 -1
- package/dist/controllers/utils/index.d.mts +17 -0
- package/dist/controllers/utils/index.d.ts +4 -3
- package/dist/controllers/utils/index.js +4 -0
- package/dist/esm/app/jimpex.js +235 -3
- package/dist/esm/app/jimpex.js.map +1 -1
- package/dist/esm/{chunk-T2T6Q22Z.js → chunk-F3FYYIAV.js} +1 -1
- package/dist/esm/controllers/common/config.js +19 -1
- package/dist/esm/controllers/common/config.js.map +1 -1
- package/dist/esm/controllers/common/health.js +19 -1
- package/dist/esm/controllers/common/health.js.map +1 -1
- package/dist/esm/controllers/common/statics.js +57 -4
- package/dist/esm/controllers/common/statics.js.map +1 -1
- package/dist/esm/controllers/utils/gateway.js +156 -1
- package/dist/esm/controllers/utils/gateway.js.map +1 -1
- package/dist/esm/middlewares/common/errorHandler.js +25 -1
- package/dist/esm/middlewares/common/errorHandler.js.map +1 -1
- package/dist/esm/middlewares/common/forceHTTPS.js +13 -1
- package/dist/esm/middlewares/common/forceHTTPS.js.map +1 -1
- package/dist/esm/middlewares/common/hsts.js +22 -1
- package/dist/esm/middlewares/common/hsts.js.map +1 -1
- package/dist/esm/middlewares/html/fastHTML.js +55 -1
- package/dist/esm/middlewares/html/fastHTML.js.map +1 -1
- package/dist/esm/middlewares/html/showHTML.js +33 -1
- package/dist/esm/middlewares/html/showHTML.js.map +1 -1
- package/dist/esm/middlewares/utils/versionValidator.js +35 -1
- package/dist/esm/middlewares/utils/versionValidator.js.map +1 -1
- package/dist/esm/services/common/appError.js +48 -2
- package/dist/esm/services/common/appError.js.map +1 -1
- package/dist/esm/services/common/httpError.js +10 -1
- package/dist/esm/services/common/httpError.js.map +1 -1
- package/dist/esm/services/common/index.js +1 -1
- package/dist/esm/services/common/sendFile.js +1 -1
- package/dist/esm/services/frontend/frontendFs.js +29 -1
- package/dist/esm/services/frontend/frontendFs.js.map +1 -1
- package/dist/esm/services/frontend/index.js +1 -1
- package/dist/esm/services/html/htmlGenerator.js +51 -1
- package/dist/esm/services/html/htmlGenerator.js.map +1 -1
- package/dist/esm/services/html/index.js +1 -1
- package/dist/esm/services/http/apiClient.js +27 -1
- package/dist/esm/services/http/apiClient.js.map +1 -1
- package/dist/esm/services/http/http.js +60 -5
- package/dist/esm/services/http/http.js.map +1 -1
- package/dist/esm/services/http/index.js +1 -1
- package/dist/esm/services/http/responsesBuilder.js +49 -2
- package/dist/esm/services/http/responsesBuilder.js.map +1 -1
- package/dist/esm/services/utils/ensureBearerToken.js +17 -1
- package/dist/esm/services/utils/ensureBearerToken.js.map +1 -1
- package/dist/esm/services/utils/index.js +1 -1
- package/dist/esm/utils/fns/others.js +1 -1
- package/dist/esm/utils/fns/routes.js +1 -1
- package/dist/esm/utils/fns/routes.js.map +1 -1
- package/dist/esm/utils/fns/statuses.js +1 -1
- package/dist/esm/utils/fns/text.js +1 -1
- package/dist/esm/utils/wrappers.js +1 -1
- package/dist/index-Bwf7JHu9.d.mts +1287 -0
- package/dist/{jimpex-7eaee271.d.ts → index-C6I3NCC-.d.ts} +15 -6
- package/dist/index.d.mts +47 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +9 -0
- package/dist/middlewares/common/errorHandler.d.mts +132 -0
- package/dist/middlewares/common/errorHandler.d.ts +5 -4
- package/dist/middlewares/common/errorHandler.js +24 -0
- package/dist/middlewares/common/errorHandler.js.map +1 -1
- package/dist/middlewares/common/forceHTTPS.d.mts +69 -0
- package/dist/middlewares/common/forceHTTPS.d.ts +6 -5
- package/dist/middlewares/common/forceHTTPS.js +12 -0
- package/dist/middlewares/common/forceHTTPS.js.map +1 -1
- package/dist/middlewares/common/hsts.d.mts +110 -0
- package/dist/middlewares/common/hsts.d.ts +6 -5
- package/dist/middlewares/common/hsts.js +21 -0
- package/dist/middlewares/common/hsts.js.map +1 -1
- package/dist/middlewares/common/index.d.mts +22 -0
- package/dist/middlewares/common/index.d.ts +4 -3
- package/dist/middlewares/common/index.js +6 -0
- package/dist/middlewares/html/fastHTML.d.mts +180 -0
- package/dist/middlewares/html/fastHTML.d.ts +5 -5
- package/dist/middlewares/html/fastHTML.js +54 -0
- package/dist/middlewares/html/fastHTML.js.map +1 -1
- package/dist/middlewares/html/index.d.mts +21 -0
- package/dist/middlewares/html/index.d.ts +4 -4
- package/dist/middlewares/html/index.js +5 -0
- package/dist/middlewares/html/showHTML.d.mts +127 -0
- package/dist/middlewares/html/showHTML.d.ts +5 -5
- package/dist/middlewares/html/showHTML.js +32 -0
- package/dist/middlewares/html/showHTML.js.map +1 -1
- package/dist/middlewares/index.d.mts +30 -0
- package/dist/middlewares/index.d.ts +4 -4
- package/dist/middlewares/index.js +6 -0
- package/dist/middlewares/utils/index.d.mts +20 -0
- package/dist/middlewares/utils/index.d.ts +4 -3
- package/dist/middlewares/utils/index.js +4 -0
- package/dist/middlewares/utils/versionValidator.d.mts +215 -0
- package/dist/middlewares/utils/versionValidator.d.ts +5 -4
- package/dist/middlewares/utils/versionValidator.js +34 -0
- package/dist/middlewares/utils/versionValidator.js.map +1 -1
- package/dist/services/common/appError.d.mts +139 -0
- package/dist/services/common/appError.d.ts +5 -4
- package/dist/services/common/appError.js +47 -1
- package/dist/services/common/appError.js.map +1 -1
- package/dist/services/common/httpError.d.mts +80 -0
- package/dist/services/common/httpError.d.ts +5 -4
- package/dist/services/common/httpError.js +9 -0
- package/dist/services/common/httpError.js.map +1 -1
- package/dist/services/common/index.d.mts +47 -0
- package/dist/services/common/index.d.ts +4 -4
- package/dist/services/common/index.js +4 -1
- package/dist/services/common/sendFile.d.mts +102 -0
- package/dist/services/common/sendFile.d.ts +4 -4
- package/dist/services/frontend/frontendFs.d.mts +96 -0
- package/dist/services/frontend/frontendFs.d.ts +4 -4
- package/dist/services/frontend/frontendFs.js +32 -0
- package/dist/services/frontend/frontendFs.js.map +1 -1
- package/dist/services/frontend/index.d.mts +40 -0
- package/dist/services/frontend/index.d.ts +4 -4
- package/dist/services/frontend/index.js +2 -1
- package/dist/services/html/htmlGenerator.d.mts +237 -0
- package/dist/services/html/htmlGenerator.d.ts +3 -3
- package/dist/services/html/htmlGenerator.js +50 -0
- package/dist/services/html/htmlGenerator.js.map +1 -1
- package/dist/services/html/index.d.mts +43 -0
- package/dist/services/html/index.d.ts +4 -4
- package/dist/services/html/index.js +2 -1
- package/dist/services/http/apiClient.d.mts +170 -0
- package/dist/services/http/apiClient.d.ts +5 -4
- package/dist/services/http/apiClient.js +26 -0
- package/dist/services/http/apiClient.js.map +1 -1
- package/dist/services/http/http.d.mts +182 -0
- package/dist/services/http/http.d.ts +15 -8
- package/dist/services/http/http.js +63 -4
- package/dist/services/http/http.js.map +1 -1
- package/dist/services/http/index.d.mts +51 -0
- package/dist/services/http/index.d.ts +4 -3
- package/dist/services/http/index.js +4 -1
- package/dist/services/http/responsesBuilder.d.mts +179 -0
- package/dist/services/http/responsesBuilder.d.ts +4 -3
- package/dist/services/http/responsesBuilder.js +48 -1
- package/dist/services/http/responsesBuilder.js.map +1 -1
- package/dist/services/index.d.mts +33 -0
- package/dist/services/index.d.ts +4 -4
- package/dist/services/index.js +8 -0
- package/dist/services/utils/ensureBearerToken.d.mts +158 -0
- package/dist/services/utils/ensureBearerToken.d.ts +6 -5
- package/dist/services/utils/ensureBearerToken.js +16 -0
- package/dist/services/utils/ensureBearerToken.js.map +1 -1
- package/dist/services/utils/index.d.mts +44 -0
- package/dist/services/utils/index.d.ts +4 -3
- package/dist/services/utils/index.js +2 -1
- package/dist/types/events.d.mts +14 -0
- package/dist/types/events.d.ts +5 -4
- package/dist/types/express.d.mts +10 -0
- package/dist/types/express.d.ts +1 -1
- package/dist/types/http.d.mts +82 -0
- package/dist/types/http.d.ts +5 -2
- package/dist/types/http.js.map +1 -1
- package/dist/types/index.d.mts +14 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.js +9 -0
- package/dist/types/options.d.mts +14 -0
- package/dist/types/options.d.ts +4 -3
- package/dist/types/utils.d.mts +48 -0
- package/dist/types/utils.d.ts +1 -1
- package/dist/types/wootils.d.mts +4 -0
- package/dist/utils/fns/index.d.mts +5 -0
- package/dist/utils/fns/index.js +7 -0
- package/dist/utils/fns/others.d.mts +17 -0
- package/dist/utils/fns/routes.d.mts +39 -0
- package/dist/utils/fns/routes.js.map +1 -1
- package/dist/utils/fns/statuses.d.mts +45 -0
- package/dist/utils/fns/statuses.d.ts +1 -1
- package/dist/utils/fns/statuses.js +4 -0
- package/dist/utils/fns/statuses.js.map +1 -1
- package/dist/utils/fns/text.d.mts +9 -0
- package/dist/utils/index.d.mts +19 -0
- package/dist/utils/index.d.ts +5 -4
- package/dist/utils/index.js +5 -0
- package/dist/utils/wrappers.d.mts +14 -0
- package/dist/utils/wrappers.d.ts +4 -3
- package/package.json +50 -49
- package/src/app/jimpex.ts +15 -2
- package/src/controllers/common/statics.ts +13 -2
- package/src/controllers/utils/gateway.ts +4 -0
- package/src/services/http/http.ts +13 -4
- package/src/types/http.ts +3 -0
- /package/dist/esm/{chunk-T2T6Q22Z.js.map → chunk-F3FYYIAV.js.map} +0 -0
package/dist/esm/app/jimpex.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__publicField
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-F3FYYIAV.js";
|
|
4
4
|
import * as path from "path";
|
|
5
5
|
import fs from "fs/promises";
|
|
6
6
|
import { createServer as createHTTPSServer } from "https";
|
|
@@ -11,6 +11,7 @@ import { envUtilsProvider } from "@homer0/env-utils";
|
|
|
11
11
|
import { packageInfoProvider } from "@homer0/package-info";
|
|
12
12
|
import { pathUtilsProvider } from "@homer0/path-utils";
|
|
13
13
|
import { rootFileProvider } from "@homer0/root-file";
|
|
14
|
+
import { tsAsyncImport } from "@homer0/ts-async-import";
|
|
14
15
|
import { EventsHub } from "@homer0/events-hub";
|
|
15
16
|
import { simpleConfigProvider } from "@homer0/simple-config";
|
|
16
17
|
import compression from "compression";
|
|
@@ -29,14 +30,50 @@ import {
|
|
|
29
30
|
statuses
|
|
30
31
|
} from "../utils";
|
|
31
32
|
class Jimpex extends Jimple {
|
|
33
|
+
/**
|
|
34
|
+
* @param options Preferences to customize the application.
|
|
35
|
+
* @param config The default settings for the configuration service. It's a
|
|
36
|
+
* shortcuit for `options.config.default`
|
|
37
|
+
*/
|
|
32
38
|
constructor(options = {}, config = {}) {
|
|
33
39
|
super();
|
|
40
|
+
/**
|
|
41
|
+
* The customization settings for the application.
|
|
42
|
+
*/
|
|
34
43
|
__publicField(this, "_options");
|
|
44
|
+
/**
|
|
45
|
+
* The Express application Jimpex uses under the hood.
|
|
46
|
+
*/
|
|
35
47
|
__publicField(this, "_express");
|
|
48
|
+
/**
|
|
49
|
+
* Since the configuration service has an async initialization, the class uses this flag
|
|
50
|
+
* internally to validate if the configuration has been initialized or not.
|
|
51
|
+
*/
|
|
36
52
|
__publicField(this, "_configReady", false);
|
|
53
|
+
/**
|
|
54
|
+
* A reference to the actuall HTTP the application will use. This can vary depending on
|
|
55
|
+
* whether HTTPS, or HTTP2 are enabled. If HTTPS is not enabled, it will be the same as
|
|
56
|
+
* the `express` property; if HTTPS is enabled, it will be an `https` server; and if
|
|
57
|
+
* HTTP2 is enabled, it will be an `spdy` server.
|
|
58
|
+
*/
|
|
37
59
|
__publicField(this, "_server");
|
|
60
|
+
/**
|
|
61
|
+
* The instance of the server that is listening for requests.
|
|
62
|
+
*/
|
|
38
63
|
__publicField(this, "_instance");
|
|
64
|
+
/**
|
|
65
|
+
* A list of functions that implement controllers and middlewares. When the application
|
|
66
|
+
* starts, the queue will be processed and those controllers and middlewares will be
|
|
67
|
+
* added to the server instance. The reason they are not added directly like with a
|
|
68
|
+
* regular Express implementation is that services on Jimple use lazy loading, and
|
|
69
|
+
* adding middlewares and controllers as they come could cause errors if they depend on
|
|
70
|
+
* services that are not yet registered.
|
|
71
|
+
*/
|
|
39
72
|
__publicField(this, "_mountQueue", []);
|
|
73
|
+
/**
|
|
74
|
+
* A list with all the top routes controlled by the application. Every time a controller
|
|
75
|
+
* is mounted, its route will be added here.
|
|
76
|
+
*/
|
|
40
77
|
__publicField(this, "_controlledRoutes", []);
|
|
41
78
|
this._options = deepAssignWithOverwrite(
|
|
42
79
|
{
|
|
@@ -87,14 +124,26 @@ class Jimpex extends Jimple {
|
|
|
87
124
|
this.boot();
|
|
88
125
|
}
|
|
89
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* This is where the app would register all its specific services, middlewares and controllers.
|
|
129
|
+
*/
|
|
90
130
|
boot() {
|
|
91
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Disables the server TLS validation. Meant to be used for development purposes.
|
|
134
|
+
*/
|
|
92
135
|
disableTLSValidation() {
|
|
93
136
|
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
|
|
94
137
|
this.logger.warn("TLS validation has been disabled");
|
|
95
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Starts the app server.
|
|
141
|
+
*
|
|
142
|
+
* @param onStart A callback function to be called when the server actually starts.
|
|
143
|
+
* @returns The server instance.
|
|
144
|
+
*/
|
|
96
145
|
async start(onStart) {
|
|
97
|
-
await this._setupConfig();
|
|
146
|
+
await Promise.all([this._setupConfig(), this._loadESMModules()]);
|
|
98
147
|
const config = this.getConfig();
|
|
99
148
|
const port = config.get("port");
|
|
100
149
|
if (!port) {
|
|
@@ -114,14 +163,28 @@ class Jimpex extends Jimple {
|
|
|
114
163
|
});
|
|
115
164
|
return this._instance;
|
|
116
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* This is an alias of `start`. The idea is for it to be used on serverless platforms,
|
|
168
|
+
* where you don't get to start your app, you just have export it.
|
|
169
|
+
*
|
|
170
|
+
* @param port In case the configuration doesn't already have it,
|
|
171
|
+
* this is the port where the application will use to run. If this
|
|
172
|
+
* parameter is used, the method will overwrite the `port`
|
|
173
|
+
* setting on the configuration service.
|
|
174
|
+
* @param onStart A callback function to be called when the server starts.
|
|
175
|
+
* @returns The server instance.
|
|
176
|
+
*/
|
|
117
177
|
async listen(port, onStart) {
|
|
118
178
|
if (port) {
|
|
119
|
-
await this._setupConfig();
|
|
179
|
+
await Promise.all([this._setupConfig(), this._loadESMModules()]);
|
|
120
180
|
const config = this.getConfig();
|
|
121
181
|
config.set("port", port);
|
|
122
182
|
}
|
|
123
183
|
return this.start(onStart);
|
|
124
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Stops the server instance.
|
|
187
|
+
*/
|
|
125
188
|
stop() {
|
|
126
189
|
if (!this._instance)
|
|
127
190
|
return;
|
|
@@ -130,6 +193,12 @@ class Jimpex extends Jimple {
|
|
|
130
193
|
this._instance = void 0;
|
|
131
194
|
this._emitEvent("afterStop", { app: this });
|
|
132
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* Mounts a route controller or a middleware into a server routes.
|
|
198
|
+
*
|
|
199
|
+
* @param route The route for the controller/middleware.
|
|
200
|
+
* @param controller The controller/middleware resource to be mounted.
|
|
201
|
+
*/
|
|
133
202
|
mount(route, controller) {
|
|
134
203
|
let useController;
|
|
135
204
|
if ("register" in controller && typeof controller.register === "function" && controller.provider === true) {
|
|
@@ -156,6 +225,11 @@ class Jimpex extends Jimple {
|
|
|
156
225
|
this._controlledRoutes.push(route);
|
|
157
226
|
});
|
|
158
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Adds a global middleware to the application.
|
|
230
|
+
*
|
|
231
|
+
* @param middleware The middleware resource to be added.
|
|
232
|
+
*/
|
|
159
233
|
use(middleware) {
|
|
160
234
|
const useMiddleware = "register" in middleware && typeof middleware.register === "function" ? middleware.register(this) : middleware;
|
|
161
235
|
this._mountQueue.push((server) => {
|
|
@@ -177,6 +251,15 @@ class Jimpex extends Jimple {
|
|
|
177
251
|
);
|
|
178
252
|
});
|
|
179
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* Gets a setting from the configuration, or the configuration itself.
|
|
256
|
+
*
|
|
257
|
+
* @param setting The setting or settings to be retrieved. If is not specified, it
|
|
258
|
+
* will return the entire configuration.
|
|
259
|
+
* @param asArray If `true` and `setting` is an array, it will return the values as
|
|
260
|
+
* an array instead of an object.
|
|
261
|
+
* @template T The type of the setting to be retrieved.
|
|
262
|
+
*/
|
|
180
263
|
getConfig(setting, asArray = false) {
|
|
181
264
|
const config = this.try("config");
|
|
182
265
|
if (!config) {
|
|
@@ -187,41 +270,107 @@ class Jimpex extends Jimple {
|
|
|
187
270
|
}
|
|
188
271
|
return config.get(setting, asArray);
|
|
189
272
|
}
|
|
273
|
+
/**
|
|
274
|
+
* Creates a new router instance.
|
|
275
|
+
*/
|
|
190
276
|
getRouter() {
|
|
191
277
|
return this.get("router");
|
|
192
278
|
}
|
|
279
|
+
/**
|
|
280
|
+
* The logger service.
|
|
281
|
+
*/
|
|
193
282
|
get logger() {
|
|
194
283
|
return this.get("logger");
|
|
195
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* The Express application Jimpex uses under the hood.
|
|
287
|
+
*/
|
|
196
288
|
get express() {
|
|
197
289
|
return this._express;
|
|
198
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* The server instance that gets created when the app is started.
|
|
293
|
+
*/
|
|
199
294
|
get instance() {
|
|
200
295
|
return this._instance;
|
|
201
296
|
}
|
|
297
|
+
/**
|
|
298
|
+
* The application customization options.
|
|
299
|
+
*/
|
|
202
300
|
get options() {
|
|
203
301
|
return deepAssignWithOverwrite({}, this._options);
|
|
204
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Gets the events service.
|
|
305
|
+
*/
|
|
205
306
|
get eventsHub() {
|
|
206
307
|
return this.get("events");
|
|
207
308
|
}
|
|
309
|
+
/**
|
|
310
|
+
* A list of the routes that have controllers mounted on them.
|
|
311
|
+
*/
|
|
208
312
|
get routes() {
|
|
209
313
|
return this._controlledRoutes.slice();
|
|
210
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* Adds a listener for an application event.
|
|
317
|
+
*
|
|
318
|
+
* @param eventName The name of the event to listen for.
|
|
319
|
+
* @param listener The listener function.
|
|
320
|
+
* @returns A function to unsubscribe the listener.
|
|
321
|
+
* @template EventName The name of the event, to match the type of the listener
|
|
322
|
+
* function.
|
|
323
|
+
*/
|
|
211
324
|
on(eventName, listener) {
|
|
212
325
|
return this.eventsHub.on(eventName, listener);
|
|
213
326
|
}
|
|
327
|
+
/**
|
|
328
|
+
* Adds a listener for an application event that will only be execuded once: the first
|
|
329
|
+
* time the event is triggered.
|
|
330
|
+
*
|
|
331
|
+
* @param eventName The name of the event to listen for.
|
|
332
|
+
* @param listener The listener function.
|
|
333
|
+
* @returns A function to unsubscribe the listener.
|
|
334
|
+
* @template EventName The name of the event, to match the type of the listener
|
|
335
|
+
* function.
|
|
336
|
+
*/
|
|
214
337
|
once(eventName, listener) {
|
|
215
338
|
return this.eventsHub.once(eventName, listener);
|
|
216
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Based on the application options, it returns wheter the application is healthy or
|
|
342
|
+
* not.
|
|
343
|
+
*/
|
|
217
344
|
isHealthy() {
|
|
218
345
|
return this._options.healthCheck(this);
|
|
219
346
|
}
|
|
347
|
+
/**
|
|
348
|
+
* This method is like a "lifecycle method", it gets executed on the constructor right
|
|
349
|
+
* before the "boot step". The idea is for the method to be a helper when the
|
|
350
|
+
* application is defined by subclassing {@link Jimpex}: the application could register
|
|
351
|
+
* all important services here and the routes on boot, then, if the implementation needs
|
|
352
|
+
* to access or overwrite a something, it can send `boot: false`, access/register what
|
|
353
|
+
* it needs, and then call `boot()`. That would be impossible for an application without
|
|
354
|
+
* overwriting the constructor and the boot functionality.
|
|
355
|
+
*/
|
|
220
356
|
_init() {
|
|
221
357
|
}
|
|
358
|
+
/**
|
|
359
|
+
* It generates overwrites for the application options when it gets created. This method
|
|
360
|
+
* is a helper for when the application is defined by subclassing {@link Jimpex}: It's
|
|
361
|
+
* highly probable that if the application needs to change the default options, it would
|
|
362
|
+
* want to do it right from the class, instead of having to do it on every
|
|
363
|
+
* implementation. A way to do it would be overwriting the constructor and calling
|
|
364
|
+
* `super` with the custom overwrites, but this method exists so that won't be
|
|
365
|
+
* necessary: when creating the `options`, the constructor will merge the result of this
|
|
366
|
+
* method on top of the default ones.
|
|
367
|
+
*/
|
|
222
368
|
_initOptions() {
|
|
223
369
|
return {};
|
|
224
370
|
}
|
|
371
|
+
/**
|
|
372
|
+
* Registers the "core services" on the container: logger, events, utils, etc.
|
|
373
|
+
*/
|
|
225
374
|
_setupCoreServices() {
|
|
226
375
|
this.register(
|
|
227
376
|
appLoggerProvider({
|
|
@@ -242,6 +391,9 @@ class Jimpex extends Jimple {
|
|
|
242
391
|
this.set("events", () => new EventsHub());
|
|
243
392
|
this.set("statuses", () => statuses);
|
|
244
393
|
}
|
|
394
|
+
/**
|
|
395
|
+
* Configures the Express application based on the class options.
|
|
396
|
+
*/
|
|
245
397
|
_setupExpress() {
|
|
246
398
|
const { statics, filesizeLimit, express: expressOptions } = this._options;
|
|
247
399
|
if (expressOptions.trustProxy) {
|
|
@@ -277,6 +429,18 @@ class Jimpex extends Jimple {
|
|
|
277
429
|
this.factory(() => express.Router())
|
|
278
430
|
);
|
|
279
431
|
}
|
|
432
|
+
/**
|
|
433
|
+
* Adds a static folder to the application.
|
|
434
|
+
*
|
|
435
|
+
* @param route The route to add the folder to.
|
|
436
|
+
* @param folder The path to the folder in the file system. If not defined, it will
|
|
437
|
+
* be use the same value as `route`. The path could be relative to the
|
|
438
|
+
* project root, or where the application executable is located,
|
|
439
|
+
* depending on the value of the `onHome` parameter.
|
|
440
|
+
* @param onHome If `true`, the path to the folder will be relative to the project
|
|
441
|
+
* root. If `false`, it will be relative to where the application
|
|
442
|
+
* executable is located.
|
|
443
|
+
*/
|
|
280
444
|
_addStaticsFolder(route, folder = "", onHome = false) {
|
|
281
445
|
const location = onHome ? "home" : "app";
|
|
282
446
|
const staticRoute = route.replace(/^\/+/, "");
|
|
@@ -287,6 +451,16 @@ class Jimpex extends Jimple {
|
|
|
287
451
|
controller: true
|
|
288
452
|
});
|
|
289
453
|
}
|
|
454
|
+
/**
|
|
455
|
+
* This helper method validates the `path` options in order to register the `app`
|
|
456
|
+
* location in the `pathUtils` service. The `app` location should be the path to where
|
|
457
|
+
* the application executable is located, but due to how ESM works, we can't infer it
|
|
458
|
+
* from the `module` object, so we need either recieved as the `appPath` setting, or try
|
|
459
|
+
* to get it from the parent module.
|
|
460
|
+
*
|
|
461
|
+
* @throws If the method should use the path from the parent module, but can't find
|
|
462
|
+
* it.
|
|
463
|
+
*/
|
|
290
464
|
_configurePath() {
|
|
291
465
|
const pathUtils = this.get("pathUtils");
|
|
292
466
|
const {
|
|
@@ -316,6 +490,12 @@ class Jimpex extends Jimple {
|
|
|
316
490
|
);
|
|
317
491
|
}
|
|
318
492
|
}
|
|
493
|
+
/**
|
|
494
|
+
* Setups the configuration service. The new configuration service requires async calls
|
|
495
|
+
* in order to load the configuration files (as it uses `import` instead of `require`),
|
|
496
|
+
* so it can't be instantiated as the other services.
|
|
497
|
+
* This method is called just before starting the application.
|
|
498
|
+
*/
|
|
319
499
|
async _setupConfig() {
|
|
320
500
|
if (this._configReady)
|
|
321
501
|
return;
|
|
@@ -346,16 +526,56 @@ class Jimpex extends Jimple {
|
|
|
346
526
|
await config.loadFromEnv();
|
|
347
527
|
}
|
|
348
528
|
}
|
|
529
|
+
/**
|
|
530
|
+
* Loads the ESM modules that are needed by Jimpex. This is called just before the starting
|
|
531
|
+
* the application so they'll be available for all the services.
|
|
532
|
+
*/
|
|
533
|
+
async _loadESMModules() {
|
|
534
|
+
const { default: nodeFetch } = await tsAsyncImport("node-fetch");
|
|
535
|
+
const { default: mime } = await tsAsyncImport("mime");
|
|
536
|
+
this.set("node-fetch", () => nodeFetch);
|
|
537
|
+
this.set("mime", () => mime);
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Processes the resources from the mount queue (added with {@link Jimpex.mount} and
|
|
541
|
+
* {@link Jimpex.use}), and adds them to the Express application.
|
|
542
|
+
*/
|
|
349
543
|
_mountResources() {
|
|
350
544
|
this._mountQueue.forEach((mount) => mount(this._express));
|
|
351
545
|
this._mountQueue.length = 0;
|
|
352
546
|
}
|
|
547
|
+
/**
|
|
548
|
+
* Emits an event using the `events` service.
|
|
549
|
+
*
|
|
550
|
+
* @param name The name of the event to emit.
|
|
551
|
+
* @param payload The event payload.
|
|
552
|
+
* @template EventName The literal name of the event, to type the event payload.
|
|
553
|
+
*/
|
|
353
554
|
_emitEvent(name, payload) {
|
|
354
555
|
this.eventsHub.emit(name, payload);
|
|
355
556
|
}
|
|
557
|
+
/**
|
|
558
|
+
* Sends a target object to a list of reducer events so they can modify or replace it.
|
|
559
|
+
*
|
|
560
|
+
* @param name The name of the event to use.
|
|
561
|
+
* @param target The object to reduce with the event.
|
|
562
|
+
* @param payload Extra context for the listeners.
|
|
563
|
+
*/
|
|
356
564
|
_reduceWithEvent(name, target, payload) {
|
|
357
565
|
return this.eventsHub.reduceSync(name, target, payload);
|
|
358
566
|
}
|
|
567
|
+
/**
|
|
568
|
+
* Loads the contents of a dictionary of credentials files that need to be used to
|
|
569
|
+
* configure HTTPS.
|
|
570
|
+
*
|
|
571
|
+
* @param credentialsInfo The dictionary where the keys are the type of credentials
|
|
572
|
+
* (`ca`, `cert`, `key`) and the values are the paths to the
|
|
573
|
+
* files.
|
|
574
|
+
* @param onHome If this is `true`, the path of the files will be relative
|
|
575
|
+
* to the project root. If it is `false`, it will be relative
|
|
576
|
+
* to where the application executable is located.
|
|
577
|
+
* @returns
|
|
578
|
+
*/
|
|
359
579
|
async _loadCredentials(credentialsInfo, onHome = true) {
|
|
360
580
|
const location = onHome ? "home" : "app";
|
|
361
581
|
const pathUtils = this.get("pathUtils");
|
|
@@ -379,6 +599,18 @@ class Jimpex extends Jimple {
|
|
|
379
599
|
return acc;
|
|
380
600
|
}, {});
|
|
381
601
|
}
|
|
602
|
+
/**
|
|
603
|
+
* Validates the configuration and chooses the server the application needs to use: If
|
|
604
|
+
* HTTP2 is enabled, it will use Spdy; if HTTPS is enabled but HTTP is not, it will use
|
|
605
|
+
* the native HTTPS server; otherwise, it will be just the Express instance.
|
|
606
|
+
*
|
|
607
|
+
* @returns {Server}
|
|
608
|
+
* @throws {Error} If HTTP2 is enabled but HTTPS is not.
|
|
609
|
+
* @throws {Error} If HTTPS is enabled but there's no `https.credentials` object.
|
|
610
|
+
* @throws {Error} If HTTPS is enabled and no creadentials are found.
|
|
611
|
+
* @access protected
|
|
612
|
+
* @ignore
|
|
613
|
+
*/
|
|
382
614
|
async _createServer() {
|
|
383
615
|
const [http2Config = {}, httpsConfig = {}] = this.getConfig(["http2", "https"], true);
|
|
384
616
|
if (!http2Config.enabled && !httpsConfig.enabled) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/app/jimpex.ts"],"sourcesContent":["import * as path from 'path';\nimport fs from 'fs/promises';\nimport { createServer as createHTTPSServer } from 'https';\nimport { Jimple } from '@homer0/jimple';\nimport { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport { appLoggerProvider } from '@homer0/simple-logger';\nimport { envUtilsProvider } from '@homer0/env-utils';\nimport { packageInfoProvider } from '@homer0/package-info';\nimport { pathUtilsProvider } from '@homer0/path-utils';\nimport { rootFileProvider } from '@homer0/root-file';\nimport { EventsHub } from '@homer0/events-hub';\nimport { simpleConfigProvider } from '@homer0/simple-config';\nimport compression from 'compression';\nimport bodyParser from 'body-parser';\nimport multer from 'multer';\nimport {\n createServer as createSpdyServer,\n type ServerOptions as SpdyServerOptions,\n} from 'spdy';\nimport express from 'express';\nimport {\n commonServicesProvider,\n httpServicesProvider,\n utilsServicesProvider,\n} from '../services';\nimport {\n statuses,\n type Controller,\n type ControllerLike,\n type MiddlewareLike,\n type MiddlewareProvider,\n type Middleware,\n} from '../utils';\nimport type {\n DeepPartial,\n Express,\n ExpressMiddlewareLike,\n PathUtils,\n Config,\n Logger,\n JimpexOptions,\n JimpexHTTPSCredentials,\n JimpexHTTP2Options,\n JimpexHTTPSOptions,\n JimpexStartCallback,\n JimpexServer,\n JimpexServerInstance,\n JimpexEventName,\n JimpexEventPayload,\n DeepReadonly,\n JimpexReducerEventName,\n JimpexReducerEventPayload,\n JimpexReducerEventTarget,\n JimpexEventNameLike,\n JimpexEventListener,\n JimpexHealthCheckFn,\n Router,\n} from '../types';\n/**\n * Jimpex is a mix of Jimple, a Javascript port of Pimple dependency injection container,\n * and Express, one of the most popular web frameworks for Node.\n *\n * @group Jimpex\n * @todo Implement `helmet`.\n */\nexport class Jimpex extends Jimple {\n /**\n * The customization settings for the application.\n */\n protected _options: JimpexOptions;\n /**\n * The Express application Jimpex uses under the hood.\n */\n protected _express: Express;\n /**\n * Since the configuration service has an async initialization, the class uses this flag\n * internally to validate if the configuration has been initialized or not.\n */\n protected _configReady: boolean = false;\n /**\n * A reference to the actuall HTTP the application will use. This can vary depending on\n * whether HTTPS, or HTTP2 are enabled. If HTTPS is not enabled, it will be the same as\n * the `express` property; if HTTPS is enabled, it will be an `https` server; and if\n * HTTP2 is enabled, it will be an `spdy` server.\n */\n protected _server?: JimpexServer;\n /**\n * The instance of the server that is listening for requests.\n */\n protected _instance?: JimpexServerInstance;\n /**\n * A list of functions that implement controllers and middlewares. When the application\n * starts, the queue will be processed and those controllers and middlewares will be\n * added to the server instance. The reason they are not added directly like with a\n * regular Express implementation is that services on Jimple use lazy loading, and\n * adding middlewares and controllers as they come could cause errors if they depend on\n * services that are not yet registered.\n */\n protected _mountQueue: Array<(server: Express) => void> = [];\n /**\n * A list with all the top routes controlled by the application. Every time a controller\n * is mounted, its route will be added here.\n */\n protected _controlledRoutes: string[] = [];\n /**\n * @param options Preferences to customize the application.\n * @param config The default settings for the configuration service. It's a\n * shortcuit for `options.config.default`\n */\n constructor(options: DeepPartial<JimpexOptions> = {}, config: unknown = {}) {\n super();\n\n this._options = deepAssignWithOverwrite(\n {\n filesizeLimit: '15MB',\n boot: true,\n path: {\n appPath: '',\n useParentPath: true,\n },\n config: {\n default: options?.config?.default || config,\n name: 'app',\n path: 'config/',\n hasFolder: false,\n loadFromEnvironment: true,\n environmentVariable: 'CONFIG',\n defaultConfigFilename: '[app-name].config.js',\n filenameFormat: '[app-name].[config-name].config.js',\n },\n statics: {\n enabled: true,\n onHome: false,\n route: 'statics',\n },\n express: {\n trustProxy: true,\n disableXPoweredBy: true,\n compression: true,\n bodyParser: true,\n multer: true,\n },\n services: {\n common: true,\n http: true,\n utils: true,\n },\n healthCheck: () => Promise.resolve(true),\n },\n options,\n this._initOptions(),\n );\n\n this._express = express();\n\n this._setupCoreServices();\n this._setupExpress();\n this._configurePath();\n\n this._init();\n if (this._options.boot) {\n this.boot();\n }\n }\n /**\n * This is where the app would register all its specific services, middlewares and controllers.\n */\n boot(): void {}\n /**\n * Disables the server TLS validation. Meant to be used for development purposes.\n */\n disableTLSValidation() {\n // eslint-disable-next-line no-process-env, dot-notation\n process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';\n this.logger.warn('TLS validation has been disabled');\n }\n /**\n * Starts the app server.\n *\n * @param onStart A callback function to be called when the server actually starts.\n * @returns The server instance.\n */\n async start(onStart?: JimpexStartCallback): Promise<JimpexServerInstance> {\n await this._setupConfig();\n const config = this.getConfig();\n const port = config.get<number | undefined>('port');\n if (!port) {\n throw new Error('No port configured');\n }\n this._emitEvent('beforeStart', { app: this });\n this._server = await this._createServer();\n this._instance = this._server!.listen(port, () => {\n this._emitEvent('start', { app: this });\n this._mountResources();\n this.logger.success(`Starting on port ${port}`);\n this._emitEvent('afterStart', { app: this });\n if (onStart) {\n onStart(config);\n }\n this._emitEvent('afterStartCallback', { app: this });\n });\n\n return this._instance!;\n }\n /**\n * This is an alias of `start`. The idea is for it to be used on serverless platforms,\n * where you don't get to start your app, you just have export it.\n *\n * @param port In case the configuration doesn't already have it,\n * this is the port where the application will use to run. If this\n * parameter is used, the method will overwrite the `port`\n * setting on the configuration service.\n * @param onStart A callback function to be called when the server starts.\n * @returns The server instance.\n */\n async listen(\n port?: number,\n onStart?: JimpexStartCallback,\n ): Promise<JimpexServerInstance> {\n if (port) {\n await this._setupConfig();\n const config = this.getConfig();\n config.set('port', port);\n }\n\n return this.start(onStart);\n }\n /**\n * Stops the server instance.\n */\n stop(): void {\n if (!this._instance) return;\n this._emitEvent('beforeStop', { app: this });\n this._instance.close();\n this._instance = undefined;\n this._emitEvent('afterStop', { app: this });\n }\n /**\n * Mounts a route controller or a middleware into a server routes.\n *\n * @param route The route for the controller/middleware.\n * @param controller The controller/middleware resource to be mounted.\n */\n mount(route: string, controller: ControllerLike): void {\n let useController: Controller | Middleware;\n if (\n 'register' in controller &&\n typeof controller.register === 'function' &&\n controller.provider === true\n ) {\n useController = controller.register(this, route);\n } else if (\n 'connect' in controller &&\n typeof controller.connect === 'function' &&\n (('middleware' in controller && controller.middleware === true) ||\n ('controller' in controller && controller.controller === true))\n ) {\n useController = controller;\n } else {\n useController = {\n middleware: true,\n connect: () => controller as ExpressMiddlewareLike,\n };\n }\n\n this._mountQueue.push((server) => {\n const connected = useController.connect(this, route);\n if (!connected) return;\n const router = this._reduceWithEvent('controllerWillBeMounted', connected, {\n route,\n controller: useController,\n app: this,\n });\n server.use(route, router);\n this._emitEvent('routeAdded', { route, app: this });\n this._controlledRoutes.push(route);\n });\n }\n /**\n * Adds a global middleware to the application.\n *\n * @param middleware The middleware resource to be added.\n */\n use(middleware: MiddlewareLike): void {\n const useMiddleware =\n 'register' in middleware && typeof middleware.register === 'function'\n ? (middleware as MiddlewareProvider).register(this)\n : (middleware as Middleware | ExpressMiddlewareLike);\n this._mountQueue.push((server) => {\n if ('connect' in useMiddleware && typeof useMiddleware.connect === 'function') {\n const handler = useMiddleware.connect(this);\n if (handler) {\n server.use(\n this._reduceWithEvent('middlewareWillBeUsed', handler, { app: this }),\n );\n }\n\n return;\n }\n\n server.use(\n this._reduceWithEvent(\n 'middlewareWillBeUsed',\n useMiddleware as ExpressMiddlewareLike,\n { app: this },\n ),\n );\n });\n }\n\n getConfig(): Config;\n getConfig<T = unknown>(setting: string | string[], asArray?: boolean): T;\n /**\n * Gets a setting from the configuration, or the configuration itself.\n *\n * @param setting The setting or settings to be retrieved. If is not specified, it\n * will return the entire configuration.\n * @param asArray If `true` and `setting` is an array, it will return the values as\n * an array instead of an object.\n * @template T The type of the setting to be retrieved.\n */\n getConfig<T = unknown>(\n setting?: string | string[],\n asArray: boolean = false,\n ): Config | T {\n const config = this.try<Config>('config');\n if (!config) {\n throw new Error('The config service is not available until the app starts');\n }\n if (typeof setting === 'undefined') {\n return config;\n }\n\n return config.get<T>(setting, asArray);\n }\n /**\n * Creates a new router instance.\n */\n getRouter(): Router {\n return this.get('router');\n }\n /**\n * The logger service.\n */\n get logger(): Logger {\n return this.get<Logger>('logger');\n }\n /**\n * The Express application Jimpex uses under the hood.\n */\n get express(): Express {\n return this._express;\n }\n /**\n * The server instance that gets created when the app is started.\n */\n get instance(): JimpexServerInstance | undefined {\n return this._instance;\n }\n /**\n * The application customization options.\n */\n get options(): DeepReadonly<JimpexOptions> {\n return deepAssignWithOverwrite({}, this._options);\n }\n /**\n * Gets the events service.\n */\n get eventsHub(): EventsHub {\n return this.get<EventsHub>('events');\n }\n /**\n * A list of the routes that have controllers mounted on them.\n */\n get routes(): string[] {\n return this._controlledRoutes.slice();\n }\n /**\n * Adds a listener for an application event.\n *\n * @param eventName The name of the event to listen for.\n * @param listener The listener function.\n * @returns A function to unsubscribe the listener.\n * @template EventName The name of the event, to match the type of the listener\n * function.\n */\n on<EventName extends JimpexEventNameLike>(\n eventName: EventName,\n listener: JimpexEventListener<EventName>,\n ): () => boolean {\n return this.eventsHub.on(eventName, listener);\n }\n /**\n * Adds a listener for an application event that will only be execuded once: the first\n * time the event is triggered.\n *\n * @param eventName The name of the event to listen for.\n * @param listener The listener function.\n * @returns A function to unsubscribe the listener.\n * @template EventName The name of the event, to match the type of the listener\n * function.\n */\n once<EventName extends JimpexEventNameLike>(\n eventName: EventName,\n listener: JimpexEventListener<EventName>,\n ): () => boolean {\n return this.eventsHub.once(eventName, listener);\n }\n /**\n * Based on the application options, it returns wheter the application is healthy or\n * not.\n */\n isHealthy(): ReturnType<JimpexHealthCheckFn> {\n return this._options.healthCheck(this);\n }\n /**\n * This method is like a \"lifecycle method\", it gets executed on the constructor right\n * before the \"boot step\". The idea is for the method to be a helper when the\n * application is defined by subclassing {@link Jimpex}: the application could register\n * all important services here and the routes on boot, then, if the implementation needs\n * to access or overwrite a something, it can send `boot: false`, access/register what\n * it needs, and then call `boot()`. That would be impossible for an application without\n * overwriting the constructor and the boot functionality.\n */\n protected _init(): void {}\n /**\n * It generates overwrites for the application options when it gets created. This method\n * is a helper for when the application is defined by subclassing {@link Jimpex}: It's\n * highly probable that if the application needs to change the default options, it would\n * want to do it right from the class, instead of having to do it on every\n * implementation. A way to do it would be overwriting the constructor and calling\n * `super` with the custom overwrites, but this method exists so that won't be\n * necessary: when creating the `options`, the constructor will merge the result of this\n * method on top of the default ones.\n */\n protected _initOptions(): DeepPartial<JimpexOptions> {\n return {};\n }\n /**\n * Registers the \"core services\" on the container: logger, events, utils, etc.\n */\n protected _setupCoreServices(): void {\n this.register(\n appLoggerProvider({\n serviceName: 'logger',\n }),\n );\n this.register(envUtilsProvider);\n this.register(packageInfoProvider);\n this.register(pathUtilsProvider);\n this.register(rootFileProvider);\n const { services: enabledServices } = this._options;\n if (enabledServices.common) this.register(commonServicesProvider);\n if (enabledServices.http) this.register(httpServicesProvider);\n if (enabledServices.utils) this.register(utilsServicesProvider);\n\n this.set('events', () => new EventsHub());\n this.set('statuses', () => statuses);\n }\n /**\n * Configures the Express application based on the class options.\n */\n protected _setupExpress(): void {\n const { statics, filesizeLimit, express: expressOptions } = this._options;\n if (expressOptions.trustProxy) {\n this._express.enable('trust proxy');\n }\n\n if (expressOptions.disableXPoweredBy) {\n this._express.disable('x-powered-by');\n }\n\n if (expressOptions.compression) {\n this._express.use(compression());\n }\n\n if (statics.enabled) {\n this._addStaticsFolder(statics.route, statics.folder, statics.onHome);\n }\n\n if (expressOptions.bodyParser) {\n this._express.use(\n bodyParser.json({\n limit: filesizeLimit,\n }),\n );\n this._express.use(\n bodyParser.urlencoded({\n extended: true,\n limit: filesizeLimit,\n }),\n );\n }\n\n if (expressOptions.multer) {\n this._express.use(multer().any());\n }\n\n this.set(\n 'router',\n this.factory(() => express.Router()),\n );\n }\n /**\n * Adds a static folder to the application.\n *\n * @param route The route to add the folder to.\n * @param folder The path to the folder in the file system. If not defined, it will\n * be use the same value as `route`. The path could be relative to the\n * project root, or where the application executable is located,\n * depending on the value of the `onHome` parameter.\n * @param onHome If `true`, the path to the folder will be relative to the project\n * root. If `false`, it will be relative to where the application\n * executable is located.\n */\n protected _addStaticsFolder(\n route: string,\n folder: string = '',\n onHome: boolean = false,\n ) {\n const location = onHome ? 'home' : 'app';\n const staticRoute = route.replace(/^\\/+/, '');\n const pathUtils = this.get<PathUtils>('pathUtils');\n const staticFolder = pathUtils.joinFrom(location, folder || staticRoute);\n this.mount(`/${staticRoute}`, {\n connect: () => express.static(staticFolder),\n controller: true,\n });\n }\n /**\n * This helper method validates the `path` options in order to register the `app`\n * location in the `pathUtils` service. The `app` location should be the path to where\n * the application executable is located, but due to how ESM works, we can't infer it\n * from the `module` object, so we need either recieved as the `appPath` setting, or try\n * to get it from the parent module.\n *\n * @throws If the method should use the path from the parent module, but can't find\n * it.\n */\n protected _configurePath(): void {\n const pathUtils = this.get<PathUtils>('pathUtils');\n const {\n path: { appPath, useParentPath },\n } = this._options;\n if (appPath) {\n pathUtils.addLocation('app', appPath);\n return;\n }\n let foundPath = false;\n if (useParentPath) {\n const stack = new Error().stack!;\n const stackList = stack.split('\\n');\n stackList.shift();\n const parentFromStack = stackList.find((line) => !line.includes(__filename));\n if (parentFromStack) {\n const parentFile = parentFromStack.replace(/^.*?\\s\\(([^\\s]+):\\d+:\\d+\\)/, '$1');\n if (parentFile !== parentFromStack) {\n foundPath = true;\n pathUtils.addLocation('app', path.dirname(parentFile));\n }\n }\n }\n\n if (!foundPath) {\n throw new Error(\n 'The app location cannot be determined. Please specify the appPath option.',\n );\n }\n }\n /**\n * Setups the configuration service. The new configuration service requires async calls\n * in order to load the configuration files (as it uses `import` instead of `require`),\n * so it can't be instantiated as the other services.\n * This method is called just before starting the application.\n */\n protected async _setupConfig(): Promise<void> {\n if (this._configReady) return;\n this._configReady = true;\n const { config: options } = this._options;\n\n let configsPath = options.path.replace(/\\/$/, '');\n if (options.hasFolder) {\n configsPath = `${configsPath}${path.sep}${options.name}${path.sep}`;\n }\n\n const filenameFormat = options.filenameFormat\n .replace(/\\[app-name\\]/gi, options.name)\n .replace(/\\[config-name\\]/gi, '[name]');\n const defaultConfigFilename = options.defaultConfigFilename.replace(\n /\\[app-name\\]/gi,\n options.name,\n );\n\n this.register(\n simpleConfigProvider({\n name: options.name,\n defaultConfig: options.default,\n defaultConfigFilename,\n envVarName: options.environmentVariable,\n path: configsPath,\n filenameFormat,\n }),\n );\n\n const config = this.getConfig();\n await config.loadFromFile('', true, false);\n if (options.loadFromEnvironment) {\n await config.loadFromEnv();\n }\n }\n /**\n * Processes the resources from the mount queue (added with {@link Jimpex.mount} and\n * {@link Jimpex.use}), and adds them to the Express application.\n */\n protected _mountResources(): void {\n this._mountQueue.forEach((mount) => mount(this._express));\n this._mountQueue.length = 0;\n }\n /**\n * Emits an event using the `events` service.\n *\n * @param name The name of the event to emit.\n * @param payload The event payload.\n * @template EventName The literal name of the event, to type the event payload.\n */\n protected _emitEvent<EventName extends JimpexEventName>(\n name: EventName,\n payload: JimpexEventPayload<EventName>,\n ): void {\n this.eventsHub.emit(name, payload);\n }\n /**\n * Sends a target object to a list of reducer events so they can modify or replace it.\n *\n * @param name The name of the event to use.\n * @param target The object to reduce with the event.\n * @param payload Extra context for the listeners.\n */\n protected _reduceWithEvent<EventName extends JimpexReducerEventName>(\n name: EventName,\n target: JimpexReducerEventTarget<EventName>,\n payload: JimpexReducerEventPayload<EventName>,\n ): JimpexReducerEventTarget<EventName> {\n return this.eventsHub.reduceSync(name, target, payload);\n }\n /**\n * Loads the contents of a dictionary of credentials files that need to be used to\n * configure HTTPS.\n *\n * @param credentialsInfo The dictionary where the keys are the type of credentials\n * (`ca`, `cert`, `key`) and the values are the paths to the\n * files.\n * @param onHome If this is `true`, the path of the files will be relative\n * to the project root. If it is `false`, it will be relative\n * to where the application executable is located.\n * @returns\n */\n protected async _loadCredentials(\n credentialsInfo: JimpexHTTPSCredentials,\n onHome: boolean = true,\n ): Promise<JimpexHTTPSCredentials> {\n const location = onHome ? 'home' : 'app';\n const pathUtils = this.get<PathUtils>('pathUtils');\n const keys: Array<keyof JimpexHTTPSCredentials> = ['ca', 'cert', 'key'];\n const info = await Promise.all(\n keys.map(async (key) => {\n const filepath = credentialsInfo[key];\n if (!filepath) return undefined;\n const file = await fs.readFile(pathUtils.joinFrom(location, filepath), 'utf8');\n return {\n key,\n file,\n };\n }),\n );\n\n return info.reduce<JimpexHTTPSCredentials>((acc, item) => {\n if (item) {\n acc[item.key] = item.file;\n }\n\n return acc;\n }, {});\n }\n /**\n * Validates the configuration and chooses the server the application needs to use: If\n * HTTP2 is enabled, it will use Spdy; if HTTPS is enabled but HTTP is not, it will use\n * the native HTTPS server; otherwise, it will be just the Express instance.\n *\n * @returns {Server}\n * @throws {Error} If HTTP2 is enabled but HTTPS is not.\n * @throws {Error} If HTTPS is enabled but there's no `https.credentials` object.\n * @throws {Error} If HTTPS is enabled and no creadentials are found.\n * @access protected\n * @ignore\n */\n protected async _createServer(): Promise<JimpexServer> {\n const [http2Config = {}, httpsConfig = {}] = this.getConfig<\n [JimpexHTTP2Options, JimpexHTTPSOptions]\n >(['http2', 'https'], true);\n\n if (!http2Config.enabled && !httpsConfig.enabled) {\n return this._express;\n }\n\n if (http2Config.enabled && !httpsConfig.enabled) {\n throw new Error('HTTP2 requires for HTTPS to be enabled');\n }\n\n if (!httpsConfig.credentials) {\n throw new Error('The `credentials` object on the HTTPS settings is missing');\n }\n\n const credentials = await this._loadCredentials(\n httpsConfig.credentials,\n httpsConfig.credentials.onHome,\n );\n\n if (!Object.keys(credentials).length) {\n throw new Error('No credentials were found for HTTPS');\n }\n\n if (http2Config.enabled) {\n const serverOptions: SpdyServerOptions = {\n ...credentials,\n spdy: http2Config.spdy,\n };\n\n return createSpdyServer(serverOptions, this._express);\n }\n\n return createHTTPSServer(credentials, this._express);\n }\n}\n/**\n * Shorthand for `new Jimpex()`.\n *\n * @param args The same parameters as the {@link Jimpex} constructor.\n * @returns A new instance of {@link Jimpex}.\n * @group Jimpex\n */\nexport const jimpex = (...args: ConstructorParameters<typeof Jimpex>): Jimpex =>\n new Jimpex(...args);\n"],"mappings":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eAAO;AAAA,UACL;AAAA,UACA;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAEA,WAAO,KAAK,OAA+B,CAAC,KAAK,SAAS;AACxD,UAAI,MAAM;AACR,YAAI,KAAK,OAAO,KAAK;AAAA,MACvB;AAEA,aAAO;AAAA,IACT,GAAG,CAAC,CAAC;AAAA,EACP;AAAA,EAaA,MAAgB,gBAAuC;AACrD,UAAM,CAAC,cAAc,CAAC,GAAG,cAAc,CAAC,CAAC,IAAI,KAAK,UAEhD,CAAC,SAAS,OAAO,GAAG,IAAI;AAE1B,QAAI,CAAC,YAAY,WAAW,CAAC,YAAY,SAAS;AAChD,aAAO,KAAK;AAAA,IACd;AAEA,QAAI,YAAY,WAAW,CAAC,YAAY,SAAS;AAC/C,YAAM,IAAI,MAAM,wCAAwC;AAAA,IAC1D;AAEA,QAAI,CAAC,YAAY,aAAa;AAC5B,YAAM,IAAI,MAAM,2DAA2D;AAAA,IAC7E;AAEA,UAAM,cAAc,MAAM,KAAK;AAAA,MAC7B,YAAY;AAAA,MACZ,YAAY,YAAY;AAAA,IAC1B;AAEA,QAAI,CAAC,OAAO,KAAK,WAAW,EAAE,QAAQ;AACpC,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AAEA,QAAI,YAAY,SAAS;AACvB,YAAM,gBAAmC;AAAA,QACvC,GAAG;AAAA,QACH,MAAM,YAAY;AAAA,MACpB;AAEA,aAAO,iBAAiB,eAAe,KAAK,QAAQ;AAAA,IACtD;AAEA,WAAO,kBAAkB,aAAa,KAAK,QAAQ;AAAA,EACrD;AACF;AAQO,MAAM,SAAS,IAAI,SACxB,IAAI,OAAO,GAAG,IAAI;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/app/jimpex.ts"],"sourcesContent":["import * as path from 'path';\nimport fs from 'fs/promises';\nimport { createServer as createHTTPSServer } from 'https';\nimport { Jimple } from '@homer0/jimple';\nimport { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport { appLoggerProvider } from '@homer0/simple-logger';\nimport { envUtilsProvider } from '@homer0/env-utils';\nimport { packageInfoProvider } from '@homer0/package-info';\nimport { pathUtilsProvider } from '@homer0/path-utils';\nimport { rootFileProvider } from '@homer0/root-file';\nimport { tsAsyncImport } from '@homer0/ts-async-import';\nimport { EventsHub } from '@homer0/events-hub';\nimport { simpleConfigProvider } from '@homer0/simple-config';\nimport compression from 'compression';\nimport bodyParser from 'body-parser';\nimport multer from 'multer';\nimport {\n createServer as createSpdyServer,\n type ServerOptions as SpdyServerOptions,\n} from 'spdy';\nimport express from 'express';\nimport {\n commonServicesProvider,\n httpServicesProvider,\n utilsServicesProvider,\n} from '../services';\nimport {\n statuses,\n type Controller,\n type ControllerLike,\n type MiddlewareLike,\n type MiddlewareProvider,\n type Middleware,\n} from '../utils';\nimport type {\n DeepPartial,\n Express,\n ExpressMiddlewareLike,\n PathUtils,\n Config,\n Logger,\n JimpexOptions,\n JimpexHTTPSCredentials,\n JimpexHTTP2Options,\n JimpexHTTPSOptions,\n JimpexStartCallback,\n JimpexServer,\n JimpexServerInstance,\n JimpexEventName,\n JimpexEventPayload,\n DeepReadonly,\n JimpexReducerEventName,\n JimpexReducerEventPayload,\n JimpexReducerEventTarget,\n JimpexEventNameLike,\n JimpexEventListener,\n JimpexHealthCheckFn,\n Router,\n} from '../types';\n/**\n * Jimpex is a mix of Jimple, a Javascript port of Pimple dependency injection container,\n * and Express, one of the most popular web frameworks for Node.\n *\n * @group Jimpex\n * @todo Implement `helmet`.\n */\nexport class Jimpex extends Jimple {\n /**\n * The customization settings for the application.\n */\n protected _options: JimpexOptions;\n /**\n * The Express application Jimpex uses under the hood.\n */\n protected _express: Express;\n /**\n * Since the configuration service has an async initialization, the class uses this flag\n * internally to validate if the configuration has been initialized or not.\n */\n protected _configReady: boolean = false;\n /**\n * A reference to the actuall HTTP the application will use. This can vary depending on\n * whether HTTPS, or HTTP2 are enabled. If HTTPS is not enabled, it will be the same as\n * the `express` property; if HTTPS is enabled, it will be an `https` server; and if\n * HTTP2 is enabled, it will be an `spdy` server.\n */\n protected _server?: JimpexServer;\n /**\n * The instance of the server that is listening for requests.\n */\n protected _instance?: JimpexServerInstance;\n /**\n * A list of functions that implement controllers and middlewares. When the application\n * starts, the queue will be processed and those controllers and middlewares will be\n * added to the server instance. The reason they are not added directly like with a\n * regular Express implementation is that services on Jimple use lazy loading, and\n * adding middlewares and controllers as they come could cause errors if they depend on\n * services that are not yet registered.\n */\n protected _mountQueue: Array<(server: Express) => void> = [];\n /**\n * A list with all the top routes controlled by the application. Every time a controller\n * is mounted, its route will be added here.\n */\n protected _controlledRoutes: string[] = [];\n /**\n * @param options Preferences to customize the application.\n * @param config The default settings for the configuration service. It's a\n * shortcuit for `options.config.default`\n */\n constructor(options: DeepPartial<JimpexOptions> = {}, config: unknown = {}) {\n super();\n\n this._options = deepAssignWithOverwrite(\n {\n filesizeLimit: '15MB',\n boot: true,\n path: {\n appPath: '',\n useParentPath: true,\n },\n config: {\n default: options?.config?.default || config,\n name: 'app',\n path: 'config/',\n hasFolder: false,\n loadFromEnvironment: true,\n environmentVariable: 'CONFIG',\n defaultConfigFilename: '[app-name].config.js',\n filenameFormat: '[app-name].[config-name].config.js',\n },\n statics: {\n enabled: true,\n onHome: false,\n route: 'statics',\n },\n express: {\n trustProxy: true,\n disableXPoweredBy: true,\n compression: true,\n bodyParser: true,\n multer: true,\n },\n services: {\n common: true,\n http: true,\n utils: true,\n },\n healthCheck: () => Promise.resolve(true),\n },\n options,\n this._initOptions(),\n );\n\n this._express = express();\n\n this._setupCoreServices();\n this._setupExpress();\n this._configurePath();\n\n this._init();\n if (this._options.boot) {\n this.boot();\n }\n }\n /**\n * This is where the app would register all its specific services, middlewares and controllers.\n */\n boot(): void {}\n /**\n * Disables the server TLS validation. Meant to be used for development purposes.\n */\n disableTLSValidation() {\n // eslint-disable-next-line no-process-env, dot-notation\n process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';\n this.logger.warn('TLS validation has been disabled');\n }\n /**\n * Starts the app server.\n *\n * @param onStart A callback function to be called when the server actually starts.\n * @returns The server instance.\n */\n async start(onStart?: JimpexStartCallback): Promise<JimpexServerInstance> {\n await Promise.all([this._setupConfig(), this._loadESMModules()]);\n const config = this.getConfig();\n const port = config.get<number | undefined>('port');\n if (!port) {\n throw new Error('No port configured');\n }\n this._emitEvent('beforeStart', { app: this });\n this._server = await this._createServer();\n this._instance = this._server!.listen(port, () => {\n this._emitEvent('start', { app: this });\n this._mountResources();\n this.logger.success(`Starting on port ${port}`);\n this._emitEvent('afterStart', { app: this });\n if (onStart) {\n onStart(config);\n }\n this._emitEvent('afterStartCallback', { app: this });\n });\n\n return this._instance!;\n }\n /**\n * This is an alias of `start`. The idea is for it to be used on serverless platforms,\n * where you don't get to start your app, you just have export it.\n *\n * @param port In case the configuration doesn't already have it,\n * this is the port where the application will use to run. If this\n * parameter is used, the method will overwrite the `port`\n * setting on the configuration service.\n * @param onStart A callback function to be called when the server starts.\n * @returns The server instance.\n */\n async listen(\n port?: number,\n onStart?: JimpexStartCallback,\n ): Promise<JimpexServerInstance> {\n if (port) {\n await Promise.all([this._setupConfig(), this._loadESMModules()]);\n const config = this.getConfig();\n config.set('port', port);\n }\n\n return this.start(onStart);\n }\n /**\n * Stops the server instance.\n */\n stop(): void {\n if (!this._instance) return;\n this._emitEvent('beforeStop', { app: this });\n this._instance.close();\n this._instance = undefined;\n this._emitEvent('afterStop', { app: this });\n }\n /**\n * Mounts a route controller or a middleware into a server routes.\n *\n * @param route The route for the controller/middleware.\n * @param controller The controller/middleware resource to be mounted.\n */\n mount(route: string, controller: ControllerLike): void {\n let useController: Controller | Middleware;\n if (\n 'register' in controller &&\n typeof controller.register === 'function' &&\n controller.provider === true\n ) {\n useController = controller.register(this, route);\n } else if (\n 'connect' in controller &&\n typeof controller.connect === 'function' &&\n (('middleware' in controller && controller.middleware === true) ||\n ('controller' in controller && controller.controller === true))\n ) {\n useController = controller;\n } else {\n useController = {\n middleware: true,\n connect: () => controller as ExpressMiddlewareLike,\n };\n }\n\n this._mountQueue.push((server) => {\n const connected = useController.connect(this, route);\n if (!connected) return;\n const router = this._reduceWithEvent('controllerWillBeMounted', connected, {\n route,\n controller: useController,\n app: this,\n });\n server.use(route, router);\n this._emitEvent('routeAdded', { route, app: this });\n this._controlledRoutes.push(route);\n });\n }\n /**\n * Adds a global middleware to the application.\n *\n * @param middleware The middleware resource to be added.\n */\n use(middleware: MiddlewareLike): void {\n const useMiddleware =\n 'register' in middleware && typeof middleware.register === 'function'\n ? (middleware as MiddlewareProvider).register(this)\n : (middleware as Middleware | ExpressMiddlewareLike);\n this._mountQueue.push((server) => {\n if ('connect' in useMiddleware && typeof useMiddleware.connect === 'function') {\n const handler = useMiddleware.connect(this);\n if (handler) {\n server.use(\n this._reduceWithEvent('middlewareWillBeUsed', handler, { app: this }),\n );\n }\n\n return;\n }\n\n server.use(\n this._reduceWithEvent(\n 'middlewareWillBeUsed',\n useMiddleware as ExpressMiddlewareLike,\n { app: this },\n ),\n );\n });\n }\n\n getConfig(): Config;\n getConfig<T = unknown>(setting: string | string[], asArray?: boolean): T;\n /**\n * Gets a setting from the configuration, or the configuration itself.\n *\n * @param setting The setting or settings to be retrieved. If is not specified, it\n * will return the entire configuration.\n * @param asArray If `true` and `setting` is an array, it will return the values as\n * an array instead of an object.\n * @template T The type of the setting to be retrieved.\n */\n getConfig<T = unknown>(\n setting?: string | string[],\n asArray: boolean = false,\n ): Config | T {\n const config = this.try<Config>('config');\n if (!config) {\n throw new Error('The config service is not available until the app starts');\n }\n if (typeof setting === 'undefined') {\n return config;\n }\n\n return config.get<T>(setting, asArray);\n }\n /**\n * Creates a new router instance.\n */\n getRouter(): Router {\n return this.get('router');\n }\n /**\n * The logger service.\n */\n get logger(): Logger {\n return this.get<Logger>('logger');\n }\n /**\n * The Express application Jimpex uses under the hood.\n */\n get express(): Express {\n return this._express;\n }\n /**\n * The server instance that gets created when the app is started.\n */\n get instance(): JimpexServerInstance | undefined {\n return this._instance;\n }\n /**\n * The application customization options.\n */\n get options(): DeepReadonly<JimpexOptions> {\n return deepAssignWithOverwrite({}, this._options);\n }\n /**\n * Gets the events service.\n */\n get eventsHub(): EventsHub {\n return this.get<EventsHub>('events');\n }\n /**\n * A list of the routes that have controllers mounted on them.\n */\n get routes(): string[] {\n return this._controlledRoutes.slice();\n }\n /**\n * Adds a listener for an application event.\n *\n * @param eventName The name of the event to listen for.\n * @param listener The listener function.\n * @returns A function to unsubscribe the listener.\n * @template EventName The name of the event, to match the type of the listener\n * function.\n */\n on<EventName extends JimpexEventNameLike>(\n eventName: EventName,\n listener: JimpexEventListener<EventName>,\n ): () => boolean {\n return this.eventsHub.on(eventName, listener);\n }\n /**\n * Adds a listener for an application event that will only be execuded once: the first\n * time the event is triggered.\n *\n * @param eventName The name of the event to listen for.\n * @param listener The listener function.\n * @returns A function to unsubscribe the listener.\n * @template EventName The name of the event, to match the type of the listener\n * function.\n */\n once<EventName extends JimpexEventNameLike>(\n eventName: EventName,\n listener: JimpexEventListener<EventName>,\n ): () => boolean {\n return this.eventsHub.once(eventName, listener);\n }\n /**\n * Based on the application options, it returns wheter the application is healthy or\n * not.\n */\n isHealthy(): ReturnType<JimpexHealthCheckFn> {\n return this._options.healthCheck(this);\n }\n /**\n * This method is like a \"lifecycle method\", it gets executed on the constructor right\n * before the \"boot step\". The idea is for the method to be a helper when the\n * application is defined by subclassing {@link Jimpex}: the application could register\n * all important services here and the routes on boot, then, if the implementation needs\n * to access or overwrite a something, it can send `boot: false`, access/register what\n * it needs, and then call `boot()`. That would be impossible for an application without\n * overwriting the constructor and the boot functionality.\n */\n protected _init(): void {}\n /**\n * It generates overwrites for the application options when it gets created. This method\n * is a helper for when the application is defined by subclassing {@link Jimpex}: It's\n * highly probable that if the application needs to change the default options, it would\n * want to do it right from the class, instead of having to do it on every\n * implementation. A way to do it would be overwriting the constructor and calling\n * `super` with the custom overwrites, but this method exists so that won't be\n * necessary: when creating the `options`, the constructor will merge the result of this\n * method on top of the default ones.\n */\n protected _initOptions(): DeepPartial<JimpexOptions> {\n return {};\n }\n /**\n * Registers the \"core services\" on the container: logger, events, utils, etc.\n */\n protected _setupCoreServices(): void {\n this.register(\n appLoggerProvider({\n serviceName: 'logger',\n }),\n );\n this.register(envUtilsProvider);\n this.register(packageInfoProvider);\n this.register(pathUtilsProvider);\n this.register(rootFileProvider);\n const { services: enabledServices } = this._options;\n if (enabledServices.common) this.register(commonServicesProvider);\n if (enabledServices.http) this.register(httpServicesProvider);\n if (enabledServices.utils) this.register(utilsServicesProvider);\n\n this.set('events', () => new EventsHub());\n this.set('statuses', () => statuses);\n }\n /**\n * Configures the Express application based on the class options.\n */\n protected _setupExpress(): void {\n const { statics, filesizeLimit, express: expressOptions } = this._options;\n if (expressOptions.trustProxy) {\n this._express.enable('trust proxy');\n }\n\n if (expressOptions.disableXPoweredBy) {\n this._express.disable('x-powered-by');\n }\n\n if (expressOptions.compression) {\n this._express.use(compression());\n }\n\n if (statics.enabled) {\n this._addStaticsFolder(statics.route, statics.folder, statics.onHome);\n }\n\n if (expressOptions.bodyParser) {\n this._express.use(\n bodyParser.json({\n limit: filesizeLimit,\n }),\n );\n this._express.use(\n bodyParser.urlencoded({\n extended: true,\n limit: filesizeLimit,\n }),\n );\n }\n\n if (expressOptions.multer) {\n this._express.use(multer().any());\n }\n\n this.set(\n 'router',\n this.factory(() => express.Router()),\n );\n }\n /**\n * Adds a static folder to the application.\n *\n * @param route The route to add the folder to.\n * @param folder The path to the folder in the file system. If not defined, it will\n * be use the same value as `route`. The path could be relative to the\n * project root, or where the application executable is located,\n * depending on the value of the `onHome` parameter.\n * @param onHome If `true`, the path to the folder will be relative to the project\n * root. If `false`, it will be relative to where the application\n * executable is located.\n */\n protected _addStaticsFolder(\n route: string,\n folder: string = '',\n onHome: boolean = false,\n ) {\n const location = onHome ? 'home' : 'app';\n const staticRoute = route.replace(/^\\/+/, '');\n const pathUtils = this.get<PathUtils>('pathUtils');\n const staticFolder = pathUtils.joinFrom(location, folder || staticRoute);\n this.mount(`/${staticRoute}`, {\n connect: () => express.static(staticFolder),\n controller: true,\n });\n }\n /**\n * This helper method validates the `path` options in order to register the `app`\n * location in the `pathUtils` service. The `app` location should be the path to where\n * the application executable is located, but due to how ESM works, we can't infer it\n * from the `module` object, so we need either recieved as the `appPath` setting, or try\n * to get it from the parent module.\n *\n * @throws If the method should use the path from the parent module, but can't find\n * it.\n */\n protected _configurePath(): void {\n const pathUtils = this.get<PathUtils>('pathUtils');\n const {\n path: { appPath, useParentPath },\n } = this._options;\n if (appPath) {\n pathUtils.addLocation('app', appPath);\n return;\n }\n let foundPath = false;\n if (useParentPath) {\n const stack = new Error().stack!;\n const stackList = stack.split('\\n');\n stackList.shift();\n const parentFromStack = stackList.find((line) => !line.includes(__filename));\n if (parentFromStack) {\n const parentFile = parentFromStack.replace(/^.*?\\s\\(([^\\s]+):\\d+:\\d+\\)/, '$1');\n if (parentFile !== parentFromStack) {\n foundPath = true;\n pathUtils.addLocation('app', path.dirname(parentFile));\n }\n }\n }\n\n if (!foundPath) {\n throw new Error(\n 'The app location cannot be determined. Please specify the appPath option.',\n );\n }\n }\n /**\n * Setups the configuration service. The new configuration service requires async calls\n * in order to load the configuration files (as it uses `import` instead of `require`),\n * so it can't be instantiated as the other services.\n * This method is called just before starting the application.\n */\n protected async _setupConfig(): Promise<void> {\n if (this._configReady) return;\n this._configReady = true;\n const { config: options } = this._options;\n\n let configsPath = options.path.replace(/\\/$/, '');\n if (options.hasFolder) {\n configsPath = `${configsPath}${path.sep}${options.name}${path.sep}`;\n }\n\n const filenameFormat = options.filenameFormat\n .replace(/\\[app-name\\]/gi, options.name)\n .replace(/\\[config-name\\]/gi, '[name]');\n const defaultConfigFilename = options.defaultConfigFilename.replace(\n /\\[app-name\\]/gi,\n options.name,\n );\n\n this.register(\n simpleConfigProvider({\n name: options.name,\n defaultConfig: options.default,\n defaultConfigFilename,\n envVarName: options.environmentVariable,\n path: configsPath,\n filenameFormat,\n }),\n );\n\n const config = this.getConfig();\n await config.loadFromFile('', true, false);\n if (options.loadFromEnvironment) {\n await config.loadFromEnv();\n }\n }\n /**\n * Loads the ESM modules that are needed by Jimpex. This is called just before the starting\n * the application so they'll be available for all the services.\n */\n protected async _loadESMModules(): Promise<void> {\n const { default: nodeFetch } =\n await tsAsyncImport<typeof import('node-fetch')>('node-fetch');\n const { default: mime } = await tsAsyncImport<typeof import('mime')>('mime');\n\n this.set('node-fetch', () => nodeFetch);\n this.set('mime', () => mime);\n }\n /**\n * Processes the resources from the mount queue (added with {@link Jimpex.mount} and\n * {@link Jimpex.use}), and adds them to the Express application.\n */\n protected _mountResources(): void {\n this._mountQueue.forEach((mount) => mount(this._express));\n this._mountQueue.length = 0;\n }\n /**\n * Emits an event using the `events` service.\n *\n * @param name The name of the event to emit.\n * @param payload The event payload.\n * @template EventName The literal name of the event, to type the event payload.\n */\n protected _emitEvent<EventName extends JimpexEventName>(\n name: EventName,\n payload: JimpexEventPayload<EventName>,\n ): void {\n this.eventsHub.emit(name, payload);\n }\n /**\n * Sends a target object to a list of reducer events so they can modify or replace it.\n *\n * @param name The name of the event to use.\n * @param target The object to reduce with the event.\n * @param payload Extra context for the listeners.\n */\n protected _reduceWithEvent<EventName extends JimpexReducerEventName>(\n name: EventName,\n target: JimpexReducerEventTarget<EventName>,\n payload: JimpexReducerEventPayload<EventName>,\n ): JimpexReducerEventTarget<EventName> {\n return this.eventsHub.reduceSync(name, target, payload);\n }\n /**\n * Loads the contents of a dictionary of credentials files that need to be used to\n * configure HTTPS.\n *\n * @param credentialsInfo The dictionary where the keys are the type of credentials\n * (`ca`, `cert`, `key`) and the values are the paths to the\n * files.\n * @param onHome If this is `true`, the path of the files will be relative\n * to the project root. If it is `false`, it will be relative\n * to where the application executable is located.\n * @returns\n */\n protected async _loadCredentials(\n credentialsInfo: JimpexHTTPSCredentials,\n onHome: boolean = true,\n ): Promise<JimpexHTTPSCredentials> {\n const location = onHome ? 'home' : 'app';\n const pathUtils = this.get<PathUtils>('pathUtils');\n const keys: Array<keyof JimpexHTTPSCredentials> = ['ca', 'cert', 'key'];\n const info = await Promise.all(\n keys.map(async (key) => {\n const filepath = credentialsInfo[key];\n if (!filepath) return undefined;\n const file = await fs.readFile(pathUtils.joinFrom(location, filepath), 'utf8');\n return {\n key,\n file,\n };\n }),\n );\n\n return info.reduce<JimpexHTTPSCredentials>((acc, item) => {\n if (item) {\n acc[item.key] = item.file;\n }\n\n return acc;\n }, {});\n }\n /**\n * Validates the configuration and chooses the server the application needs to use: If\n * HTTP2 is enabled, it will use Spdy; if HTTPS is enabled but HTTP is not, it will use\n * the native HTTPS server; otherwise, it will be just the Express instance.\n *\n * @returns {Server}\n * @throws {Error} If HTTP2 is enabled but HTTPS is not.\n * @throws {Error} If HTTPS is enabled but there's no `https.credentials` object.\n * @throws {Error} If HTTPS is enabled and no creadentials are found.\n * @access protected\n * @ignore\n */\n protected async _createServer(): Promise<JimpexServer> {\n const [http2Config = {}, httpsConfig = {}] = this.getConfig<\n [JimpexHTTP2Options, JimpexHTTPSOptions]\n >(['http2', 'https'], true);\n\n if (!http2Config.enabled && !httpsConfig.enabled) {\n return this._express;\n }\n\n if (http2Config.enabled && !httpsConfig.enabled) {\n throw new Error('HTTP2 requires for HTTPS to be enabled');\n }\n\n if (!httpsConfig.credentials) {\n throw new Error('The `credentials` object on the HTTPS settings is missing');\n }\n\n const credentials = await this._loadCredentials(\n httpsConfig.credentials,\n httpsConfig.credentials.onHome,\n );\n\n if (!Object.keys(credentials).length) {\n throw new Error('No credentials were found for HTTPS');\n }\n\n if (http2Config.enabled) {\n const serverOptions: SpdyServerOptions = {\n ...credentials,\n spdy: http2Config.spdy,\n };\n\n return createSpdyServer(serverOptions, this._express);\n }\n\n return createHTTPSServer(credentials, this._express);\n }\n}\n/**\n * Shorthand for `new Jimpex()`.\n *\n * @param args The same parameters as the {@link Jimpex} constructor.\n * @returns A new instance of {@link Jimpex}.\n * @group Jimpex\n */\nexport const jimpex = (...args: Constructor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,MACZ,YAAY,YAAY;AAAA,IAC1B;AAEA,QAAI,CAAC,OAAO,KAAK,WAAW,EAAE,QAAQ;AACpC,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACvD;AAEA,QAAI,YAAY,SAAS;AACvB,YAAM,gBAAmC;AAAA,QACvC,GAAG;AAAA,QACH,MAAM,YAAY;AAAA,MACpB;AAEA,aAAO,iBAAiB,eAAe,KAAK,QAAQ;AAAA,IACtD;AAEA,WAAO,kBAAkB,aAAa,KAAK,QAAQ;AAAA,EACrD;AACF;AAQO,MAAM,SAAS,IAAI,SACxB,IAAI,OAAO,GAAG,IAAI;","names":[]}
|
|
@@ -1,19 +1,34 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__publicField
|
|
3
|
-
} from "../../chunk-
|
|
3
|
+
} from "../../chunk-F3FYYIAV.js";
|
|
4
4
|
import { controller } from "../../utils";
|
|
5
5
|
class ConfigController {
|
|
6
|
+
/**
|
|
7
|
+
* @param options The options to construct the controller.
|
|
8
|
+
*/
|
|
6
9
|
constructor({ inject }) {
|
|
10
|
+
/**
|
|
11
|
+
* The service in charge or building the responses.
|
|
12
|
+
*/
|
|
7
13
|
__publicField(this, "_responsesBuilder");
|
|
14
|
+
/**
|
|
15
|
+
* The service in charge of the configuration.
|
|
16
|
+
*/
|
|
8
17
|
__publicField(this, "_config");
|
|
9
18
|
this._responsesBuilder = inject.responsesBuilder;
|
|
10
19
|
this._config = inject.config;
|
|
11
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates the middleware the shows the current configuration.
|
|
23
|
+
*/
|
|
12
24
|
showConfig() {
|
|
13
25
|
return (_, res) => {
|
|
14
26
|
this.respondWithConfig(res);
|
|
15
27
|
};
|
|
16
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates the middleware the allows to switch the configuration.
|
|
31
|
+
*/
|
|
17
32
|
switchConfig() {
|
|
18
33
|
return async (req, res, next) => {
|
|
19
34
|
const { name } = req.params;
|
|
@@ -29,6 +44,9 @@ class ConfigController {
|
|
|
29
44
|
}
|
|
30
45
|
};
|
|
31
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Utility to respond with the current configuration.
|
|
49
|
+
*/
|
|
32
50
|
respondWithConfig(res) {
|
|
33
51
|
const name = this._config.get("name");
|
|
34
52
|
const data = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/controllers/common/config.ts"],"sourcesContent":["import { controller } from '../../utils';\nimport type { ResponsesBuilder } from '../../services';\nimport type {\n Config,\n Response,\n AsyncExpressMiddleware,\n ExpressMiddleware,\n} from '../../types';\n/**\n * The options to contruct a {@link ConfigController}.\n *\n * @group Controllers/Config\n */\nexport type ConfigControllerOptions = {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n responsesBuilder: ResponsesBuilder;\n config: Config;\n };\n};\n/**\n * The controller class that allows to show and switch the application configuration.\n *\n * @group Controller Classes\n * @group Controllers/Config\n * @prettierignore\n */\nexport class ConfigController {\n /**\n * The service in charge or building the responses.\n */\n protected readonly _responsesBuilder: ResponsesBuilder;\n /**\n * The service in charge of the configuration.\n */\n protected readonly _config: Config;\n /**\n * @param options The options to construct the controller.\n */\n constructor({ inject }: ConfigControllerOptions) {\n this._responsesBuilder = inject.responsesBuilder;\n this._config = inject.config;\n }\n /**\n * Creates the middleware the shows the current configuration.\n */\n showConfig(): ExpressMiddleware {\n return (_, res) => {\n this.respondWithConfig(res);\n };\n }\n /**\n * Creates the middleware the allows to switch the configuration.\n */\n switchConfig(): AsyncExpressMiddleware {\n return async (req, res, next) => {\n const { name } = req.params;\n if (!name || !this._config.canSwitchConfigs()) {\n next();\n return;\n }\n\n try {\n await this._config.switch(name);\n this.respondWithConfig(res);\n } catch (error) {\n next(error);\n }\n };\n }\n /**\n * Utility to respond with the current configuration.\n */\n protected respondWithConfig(res: Response): void {\n const name = this._config.get<string>('name');\n const data = {\n name,\n ...this._config.getConfig<Record<string, unknown>>(),\n };\n\n this._responsesBuilder.json({\n res,\n data,\n });\n }\n}\n/**\n * This controller is kind of special as it will only mount the routes if the\n * `debug.configController` setting of the app configuration is set to `true`.\n *\n * It provides routes for:\n * - `/`: Showing the current configuration.\n * - `/switch/:name`: Switching the configuration, but only if the service allows it.\n *\n * @group Controllers\n * @group Controllers/Config\n */\nexport const configController = controller((app) => {\n const config = app.getConfig();\n const router = app.getRouter();\n if (config.get<boolean | undefined>('debug.configController') !== true) {\n return router;\n }\n\n const ctrl = new ConfigController({\n inject: {\n config,\n responsesBuilder: app.get('responsesBuilder'),\n },\n });\n\n return router.get('/', ctrl.showConfig()).get('/switch/:name', ctrl.switchConfig());\n});\n"],"mappings":";;;AAAA,SAAS,kBAAkB;AA6BpB,MAAM,iBAAiB;AAAA,EAY5B,YAAY,EAAE,OAAO,GAA4B;AARjD,wBAAmB;AAInB,wBAAmB;AAKjB,SAAK,oBAAoB,OAAO;AAChC,SAAK,UAAU,OAAO;AAAA,EACxB;AAAA,EAIA,aAAgC;AAC9B,WAAO,CAAC,GAAG,QAAQ;AACjB,WAAK,kBAAkB,GAAG;AAAA,IAC5B;AAAA,EACF;AAAA,EAIA,eAAuC;AACrC,WAAO,OAAO,KAAK,KAAK,SAAS;AAC/B,YAAM,EAAE,KAAK,IAAI,IAAI;AACrB,UAAI,CAAC,QAAQ,CAAC,KAAK,QAAQ,iBAAiB,GAAG;AAC7C,aAAK;AACL;AAAA,MACF;AAEA,UAAI;AACF,cAAM,KAAK,QAAQ,OAAO,IAAI;AAC9B,aAAK,kBAAkB,GAAG;AAAA,MAC5B,SAAS,
|
|
1
|
+
{"version":3,"sources":["../../../../src/controllers/common/config.ts"],"sourcesContent":["import { controller } from '../../utils';\nimport type { ResponsesBuilder } from '../../services';\nimport type {\n Config,\n Response,\n AsyncExpressMiddleware,\n ExpressMiddleware,\n} from '../../types';\n/**\n * The options to contruct a {@link ConfigController}.\n *\n * @group Controllers/Config\n */\nexport type ConfigControllerOptions = {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n responsesBuilder: ResponsesBuilder;\n config: Config;\n };\n};\n/**\n * The controller class that allows to show and switch the application configuration.\n *\n * @group Controller Classes\n * @group Controllers/Config\n * @prettierignore\n */\nexport class ConfigController {\n /**\n * The service in charge or building the responses.\n */\n protected readonly _responsesBuilder: ResponsesBuilder;\n /**\n * The service in charge of the configuration.\n */\n protected readonly _config: Config;\n /**\n * @param options The options to construct the controller.\n */\n constructor({ inject }: ConfigControllerOptions) {\n this._responsesBuilder = inject.responsesBuilder;\n this._config = inject.config;\n }\n /**\n * Creates the middleware the shows the current configuration.\n */\n showConfig(): ExpressMiddleware {\n return (_, res) => {\n this.respondWithConfig(res);\n };\n }\n /**\n * Creates the middleware the allows to switch the configuration.\n */\n switchConfig(): AsyncExpressMiddleware {\n return async (req, res, next) => {\n const { name } = req.params;\n if (!name || !this._config.canSwitchConfigs()) {\n next();\n return;\n }\n\n try {\n await this._config.switch(name);\n this.respondWithConfig(res);\n } catch (error) {\n next(error);\n }\n };\n }\n /**\n * Utility to respond with the current configuration.\n */\n protected respondWithConfig(res: Response): void {\n const name = this._config.get<string>('name');\n const data = {\n name,\n ...this._config.getConfig<Record<string, unknown>>(),\n };\n\n this._responsesBuilder.json({\n res,\n data,\n });\n }\n}\n/**\n * This controller is kind of special as it will only mount the routes if the\n * `debug.configController` setting of the app configuration is set to `true`.\n *\n * It provides routes for:\n * - `/`: Showing the current configuration.\n * - `/switch/:name`: Switching the configuration, but only if the service allows it.\n *\n * @group Controllers\n * @group Controllers/Config\n */\nexport const configController = controller((app) => {\n const config = app.getConfig();\n const router = app.getRouter();\n if (config.get<boolean | undefined>('debug.configController') !== true) {\n return router;\n }\n\n const ctrl = new ConfigController({\n inject: {\n config,\n responsesBuilder: app.get('responsesBuilder'),\n },\n });\n\n return router.get('/', ctrl.showConfig()).get('/switch/:name', ctrl.switchConfig());\n});\n"],"mappings":";;;AAAA,SAAS,kBAAkB;AA6BpB,MAAM,iBAAiB;AAAA;AAAA;AAAA;AAAA,EAY5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