jimpex 8.0.0 → 9.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 +13 -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 +225 -0
- package/dist/app/jimpex.js.map +1 -1
- package/dist/controllers/common/config.d.mts +79 -0
- package/dist/controllers/common/config.d.ts +3 -2
- 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 +4 -3
- package/dist/controllers/common/health.js +18 -0
- package/dist/controllers/common/health.js.map +1 -1
- package/dist/controllers/common/index.d.mts +21 -0
- package/dist/controllers/common/index.d.ts +4 -4
- package/dist/controllers/common/index.js +6 -0
- package/dist/controllers/common/statics.d.mts +215 -0
- package/dist/controllers/common/statics.d.ts +4 -4
- package/dist/controllers/common/statics.js +51 -0
- package/dist/controllers/common/statics.js.map +1 -1
- package/dist/controllers/index.d.mts +24 -0
- package/dist/controllers/index.d.ts +4 -4
- package/dist/controllers/index.js +5 -0
- package/dist/controllers/utils/gateway.d.mts +725 -0
- package/dist/controllers/utils/gateway.d.ts +4 -3
- package/dist/controllers/utils/gateway.js +152 -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 +222 -1
- package/dist/esm/app/jimpex.js.map +1 -1
- package/dist/esm/{chunk-T2T6Q22Z.js → chunk-2B2CG5KL.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 +48 -1
- package/dist/esm/controllers/common/statics.js.map +1 -1
- package/dist/esm/controllers/utils/gateway.js +153 -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 +50 -1
- 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/{jimpex-7eaee271.d.ts → index-b2a04c78.d.ts} +9 -5
- package/dist/index-efeb437e.d.ts +1282 -0
- package/dist/index.d.mts +46 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -0
- package/dist/middlewares/common/errorHandler.d.mts +132 -0
- package/dist/middlewares/common/errorHandler.d.ts +4 -3
- 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 +5 -4
- 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 +5 -4
- 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 +4 -4
- 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 +4 -4
- 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 +4 -3
- 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 +4 -3
- 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 +4 -3
- 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 +3 -3
- package/dist/services/frontend/frontendFs.d.mts +96 -0
- package/dist/services/frontend/frontendFs.d.ts +3 -3
- 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 +2 -2
- 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 +4 -3
- package/dist/services/http/apiClient.js +26 -0
- package/dist/services/http/apiClient.js.map +1 -1
- package/dist/services/http/http.d.mts +176 -0
- package/dist/services/http/http.d.ts +4 -3
- package/dist/services/http/http.js +53 -0
- 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 +3 -2
- 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 +5 -4
- 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/http.d.mts +79 -0
- package/dist/types/index.d.mts +14 -0
- package/dist/types/index.d.ts +1 -1
- 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/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.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 +43 -43
- /package/dist/esm/{chunk-T2T6Q22Z.js.map → chunk-2B2CG5KL.js.map} +0 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import * as _homer0_jimple from '@homer0/jimple';
|
|
2
|
+
import { J as Jimpex, a1 as MiddlewareLike, C as ControllerConnectFn } from '../../index-efeb437e.js';
|
|
3
|
+
import { RouterMethod, ExpressMiddleware } from '../../types/express.mjs';
|
|
4
|
+
import { DeepPartial } from '../../types/utils.mjs';
|
|
5
|
+
import { SendFile } from '../../services/common/sendFile.mjs';
|
|
6
|
+
import { Router } from 'express';
|
|
7
|
+
import '../../types/http.mjs';
|
|
8
|
+
import 'https';
|
|
9
|
+
import 'http';
|
|
10
|
+
import 'spdy';
|
|
11
|
+
import 'node-fetch';
|
|
12
|
+
import '@homer0/path-utils';
|
|
13
|
+
import '@homer0/simple-logger';
|
|
14
|
+
import '@homer0/simple-config';
|
|
15
|
+
import '@homer0/events-hub';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The definition for each file the controller handles.
|
|
19
|
+
*
|
|
20
|
+
* @group Controllers/Statics
|
|
21
|
+
*/
|
|
22
|
+
type StaticsControllerFile = {
|
|
23
|
+
/**
|
|
24
|
+
* The route, relative to the controller root, to the file.
|
|
25
|
+
*/
|
|
26
|
+
route: string;
|
|
27
|
+
/**
|
|
28
|
+
* The path to the file in the filesystem. Since the file is served using the
|
|
29
|
+
* {@link SendFile} service, whether the file is relative to the project root or the
|
|
30
|
+
* application executable depends on how the service is configured (relative to the
|
|
31
|
+
* executable by default).
|
|
32
|
+
*/
|
|
33
|
+
path: string;
|
|
34
|
+
/**
|
|
35
|
+
* A dictionary of headers for the response.
|
|
36
|
+
*/
|
|
37
|
+
headers?: Record<string, string>;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* These are like "master paths" that get prepended to all the file paths and routes the
|
|
41
|
+
* controller use.
|
|
42
|
+
*
|
|
43
|
+
* @group Controllers/Statics
|
|
44
|
+
*/
|
|
45
|
+
type StaticsControllerPathsOptions = {
|
|
46
|
+
/**
|
|
47
|
+
* A custom route to prefix all the file routes with.
|
|
48
|
+
*/
|
|
49
|
+
route: string;
|
|
50
|
+
/**
|
|
51
|
+
* A custom path to prefix all the file paths with.
|
|
52
|
+
*/
|
|
53
|
+
source: string;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* The options to customize the controller.
|
|
57
|
+
*
|
|
58
|
+
* @group Controllers/Statics
|
|
59
|
+
*/
|
|
60
|
+
type StaticsControllerOptions = {
|
|
61
|
+
/**
|
|
62
|
+
* A list of filenames, or definitions for the files to handle.
|
|
63
|
+
*/
|
|
64
|
+
files: Array<string | StaticsControllerFile>;
|
|
65
|
+
/**
|
|
66
|
+
* A dictionary with the allowed router (HTTP) methods the controller can use to serve
|
|
67
|
+
* the files. If `all` is set to `true`, the rest of the values will be ignored.
|
|
68
|
+
*
|
|
69
|
+
* @default {get: true, all: false}
|
|
70
|
+
*/
|
|
71
|
+
methods: Partial<Record<RouterMethod, boolean>>;
|
|
72
|
+
/**
|
|
73
|
+
* The "master paths" the controller can use to prefix the file paths and routes.
|
|
74
|
+
*
|
|
75
|
+
* @default {route: '', source: './',}
|
|
76
|
+
*/
|
|
77
|
+
paths: StaticsControllerPathsOptions;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* The options to construct a {@link StaticsController}.
|
|
81
|
+
*
|
|
82
|
+
* @group Controllers/Statics
|
|
83
|
+
*/
|
|
84
|
+
type StaticsControllerConstructorOptions = DeepPartial<StaticsControllerOptions> & {
|
|
85
|
+
/**
|
|
86
|
+
* A dictionary with the dependencies to inject.
|
|
87
|
+
*/
|
|
88
|
+
inject: {
|
|
89
|
+
sendFile: SendFile;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* A function to generate a list of middlewares that can be executed before the tontroller
|
|
94
|
+
* main middleware.
|
|
95
|
+
*
|
|
96
|
+
* @group Controllers/Statics
|
|
97
|
+
*/
|
|
98
|
+
type StaticsControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[];
|
|
99
|
+
/**
|
|
100
|
+
* The options for the controller creator that mounts {@link StaticsController}.
|
|
101
|
+
*
|
|
102
|
+
* @group Controllers/Statics
|
|
103
|
+
*/
|
|
104
|
+
type StaticsControllerCreatorOptions = DeepPartial<StaticsControllerOptions> & {
|
|
105
|
+
/**
|
|
106
|
+
* A function to generate a list of middlewares that can be executed before the
|
|
107
|
+
* tontroller main middleware.
|
|
108
|
+
*/
|
|
109
|
+
getMiddlewares?: StaticsControllerGetMiddlewaresFn;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* The options for {@link StaticsController._addRoute}.
|
|
113
|
+
*
|
|
114
|
+
* @access protected
|
|
115
|
+
* @group Controllers/Statics
|
|
116
|
+
*/
|
|
117
|
+
type AddStaticRouteOptions = {
|
|
118
|
+
/**
|
|
119
|
+
* The reference for the router in which the middlewares will be added.
|
|
120
|
+
*/
|
|
121
|
+
router: Router;
|
|
122
|
+
/**
|
|
123
|
+
* The router method in which the middlewares will be added.
|
|
124
|
+
*/
|
|
125
|
+
method: RouterMethod;
|
|
126
|
+
/**
|
|
127
|
+
* The definition of the file to serve.
|
|
128
|
+
*/
|
|
129
|
+
file: StaticsControllerFile;
|
|
130
|
+
/**
|
|
131
|
+
* The middleware created by {@link StaticsController}, that will serve the file.
|
|
132
|
+
*/
|
|
133
|
+
fileMiddleware: ExpressMiddleware;
|
|
134
|
+
/**
|
|
135
|
+
* A list of extra middlewares to execute before the file middleware.
|
|
136
|
+
*/
|
|
137
|
+
middlewares: ExpressMiddleware[];
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* The controller class that allows the application to serve specific files from any
|
|
141
|
+
* folder to any route without the need of mounting directories as "static".
|
|
142
|
+
*
|
|
143
|
+
* @group Controller Classes
|
|
144
|
+
* @group Controllers/Statics
|
|
145
|
+
* @prettierignore
|
|
146
|
+
*/
|
|
147
|
+
declare class StaticsController {
|
|
148
|
+
/**
|
|
149
|
+
* The service that serves static files.
|
|
150
|
+
*/
|
|
151
|
+
protected readonly _sendFile: SendFile;
|
|
152
|
+
/**
|
|
153
|
+
* The controller customization options.
|
|
154
|
+
*/
|
|
155
|
+
protected _options: StaticsControllerOptions;
|
|
156
|
+
/**
|
|
157
|
+
* A dictionary with the formatted definitions of the files that will be served.
|
|
158
|
+
* It uses the files' routes as keys, for easy access in the middleware.
|
|
159
|
+
*/
|
|
160
|
+
protected files: Record<string, StaticsControllerFile>;
|
|
161
|
+
/**
|
|
162
|
+
* @param options The options to construct the controller.
|
|
163
|
+
*/
|
|
164
|
+
constructor({ inject, ...options }: StaticsControllerConstructorOptions);
|
|
165
|
+
/**
|
|
166
|
+
* Mounts the middlewares in the router in order to serve the files.
|
|
167
|
+
*
|
|
168
|
+
* @param router A reference to the application router.
|
|
169
|
+
* @param middlewares A list of extra middlewares to execute before the file
|
|
170
|
+
* middleware.
|
|
171
|
+
*/
|
|
172
|
+
addRoutes(router: Router, middlewares?: ExpressMiddleware[]): Router;
|
|
173
|
+
/**
|
|
174
|
+
* The controller options.
|
|
175
|
+
*/
|
|
176
|
+
get options(): Readonly<StaticsControllerOptions>;
|
|
177
|
+
/**
|
|
178
|
+
* Generates the middleware that will serve the file.
|
|
179
|
+
*
|
|
180
|
+
* @param file The definition of the file to serve.
|
|
181
|
+
*/
|
|
182
|
+
protected _getMiddleware(file: StaticsControllerFile): ExpressMiddleware;
|
|
183
|
+
/**
|
|
184
|
+
* Mounts the middleware(s) for a file in the router.
|
|
185
|
+
*
|
|
186
|
+
* @param options The information of the file and how it needs to be added.
|
|
187
|
+
*/
|
|
188
|
+
protected _addRoute({ router, method, file, fileMiddleware, middlewares, }: AddStaticRouteOptions): void;
|
|
189
|
+
/**
|
|
190
|
+
* Validates and formats the options sent to the constructor in order to get the final
|
|
191
|
+
* set that will be stored in the controller.
|
|
192
|
+
*
|
|
193
|
+
* @param options The options to validate.
|
|
194
|
+
* @throws If no files are specified.
|
|
195
|
+
* @throws If methods is not defined.
|
|
196
|
+
* @throws If no methods are enabled.
|
|
197
|
+
* @throws If there's an invalid HTTP method.
|
|
198
|
+
*/
|
|
199
|
+
protected _validateOptions(options: StaticsControllerOptions): StaticsControllerOptions;
|
|
200
|
+
/**
|
|
201
|
+
* Parses the files received from the constructor's options, and formats them into
|
|
202
|
+
* proper definitions the controller can use.
|
|
203
|
+
*/
|
|
204
|
+
protected _createFiles(): Record<string, StaticsControllerFile>;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* A controller that allows the application to server specific files from any folder to
|
|
208
|
+
* any route without the need of mounting directories as "static" folders.
|
|
209
|
+
*
|
|
210
|
+
* @group Controllers
|
|
211
|
+
* @group Controllers/Statics
|
|
212
|
+
*/
|
|
213
|
+
declare const staticsController: _homer0_jimple.ResourceCreator<"controller", "connect", ({ getMiddlewares, ...options }?: StaticsControllerCreatorOptions) => <ContainerType extends Jimpex = Jimpex>(app: ContainerType) => Router, ControllerConnectFn>;
|
|
214
|
+
|
|
215
|
+
export { AddStaticRouteOptions, StaticsController, StaticsControllerConstructorOptions, StaticsControllerCreatorOptions, StaticsControllerFile, StaticsControllerGetMiddlewaresFn, StaticsControllerOptions, StaticsControllerPathsOptions, staticsController };
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import * as _homer0_jimple from '@homer0/jimple';
|
|
2
|
-
import { J as Jimpex, a1 as MiddlewareLike, C as ControllerConnectFn } from '../../
|
|
2
|
+
import { J as Jimpex, a1 as MiddlewareLike, C as ControllerConnectFn } from '../../index-b2a04c78.js';
|
|
3
3
|
import { RouterMethod, ExpressMiddleware } from '../../types/express.js';
|
|
4
4
|
import { DeepPartial } from '../../types/utils.js';
|
|
5
5
|
import { SendFile } from '../../services/common/sendFile.js';
|
|
6
6
|
import { Router } from 'express';
|
|
7
|
-
import '@homer0/events-hub';
|
|
8
7
|
import '../../types/http.js';
|
|
9
8
|
import 'https';
|
|
10
9
|
import 'http';
|
|
11
10
|
import 'spdy';
|
|
12
11
|
import 'node-fetch';
|
|
13
|
-
import '@homer0/simple-config';
|
|
14
|
-
import '@homer0/simple-logger';
|
|
15
12
|
import '@homer0/path-utils';
|
|
13
|
+
import '@homer0/simple-logger';
|
|
14
|
+
import '@homer0/simple-config';
|
|
15
|
+
import '@homer0/events-hub';
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* The definition for each file the controller handles.
|
|
@@ -19,6 +19,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
19
19
|
return to;
|
|
20
20
|
};
|
|
21
21
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
26
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
27
|
mod
|
|
24
28
|
));
|
|
@@ -38,9 +42,22 @@ var mime = __toESM(require("mime"));
|
|
|
38
42
|
var import_deep_assign = require("@homer0/deep-assign");
|
|
39
43
|
var import_utils = require("../../utils");
|
|
40
44
|
class StaticsController {
|
|
45
|
+
/**
|
|
46
|
+
* @param options The options to construct the controller.
|
|
47
|
+
*/
|
|
41
48
|
constructor({ inject, ...options }) {
|
|
49
|
+
/**
|
|
50
|
+
* The service that serves static files.
|
|
51
|
+
*/
|
|
42
52
|
__publicField(this, "_sendFile");
|
|
53
|
+
/**
|
|
54
|
+
* The controller customization options.
|
|
55
|
+
*/
|
|
43
56
|
__publicField(this, "_options");
|
|
57
|
+
/**
|
|
58
|
+
* A dictionary with the formatted definitions of the files that will be served.
|
|
59
|
+
* It uses the files' routes as keys, for easy access in the middleware.
|
|
60
|
+
*/
|
|
44
61
|
__publicField(this, "files");
|
|
45
62
|
this._sendFile = inject.sendFile;
|
|
46
63
|
this._options = this._validateOptions(
|
|
@@ -61,6 +78,13 @@ class StaticsController {
|
|
|
61
78
|
);
|
|
62
79
|
this.files = this._createFiles();
|
|
63
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Mounts the middlewares in the router in order to serve the files.
|
|
83
|
+
*
|
|
84
|
+
* @param router A reference to the application router.
|
|
85
|
+
* @param middlewares A list of extra middlewares to execute before the file
|
|
86
|
+
* middleware.
|
|
87
|
+
*/
|
|
64
88
|
addRoutes(router, middlewares = []) {
|
|
65
89
|
const { methods } = this._options;
|
|
66
90
|
const use = methods.all ? ["all"] : Object.keys(methods).reduce((acc, name) => {
|
|
@@ -79,9 +103,17 @@ class StaticsController {
|
|
|
79
103
|
});
|
|
80
104
|
return router;
|
|
81
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* The controller options.
|
|
108
|
+
*/
|
|
82
109
|
get options() {
|
|
83
110
|
return { ...this._options };
|
|
84
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Generates the middleware that will serve the file.
|
|
114
|
+
*
|
|
115
|
+
* @param file The definition of the file to serve.
|
|
116
|
+
*/
|
|
85
117
|
_getMiddleware(file) {
|
|
86
118
|
return (_, res, next) => {
|
|
87
119
|
const extension = path.parse(file.path).ext.substring(1);
|
|
@@ -99,6 +131,11 @@ class StaticsController {
|
|
|
99
131
|
});
|
|
100
132
|
};
|
|
101
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Mounts the middleware(s) for a file in the router.
|
|
136
|
+
*
|
|
137
|
+
* @param options The information of the file and how it needs to be added.
|
|
138
|
+
*/
|
|
102
139
|
_addRoute({
|
|
103
140
|
router,
|
|
104
141
|
method,
|
|
@@ -109,6 +146,16 @@ class StaticsController {
|
|
|
109
146
|
const { route } = file;
|
|
110
147
|
router[method](route, [...middlewares, fileMiddleware]);
|
|
111
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Validates and formats the options sent to the constructor in order to get the final
|
|
151
|
+
* set that will be stored in the controller.
|
|
152
|
+
*
|
|
153
|
+
* @param options The options to validate.
|
|
154
|
+
* @throws If no files are specified.
|
|
155
|
+
* @throws If methods is not defined.
|
|
156
|
+
* @throws If no methods are enabled.
|
|
157
|
+
* @throws If there's an invalid HTTP method.
|
|
158
|
+
*/
|
|
112
159
|
_validateOptions(options) {
|
|
113
160
|
if (!options.files || !options.files.length) {
|
|
114
161
|
throw new Error("You need to specify a list of files");
|
|
@@ -148,6 +195,10 @@ class StaticsController {
|
|
|
148
195
|
methods: newMethods
|
|
149
196
|
};
|
|
150
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* Parses the files received from the constructor's options, and formats them into
|
|
200
|
+
* proper definitions the controller can use.
|
|
201
|
+
*/
|
|
151
202
|
_createFiles() {
|
|
152
203
|
const { files, paths } = this._options;
|
|
153
204
|
const routePath = (0, import_utils.removeSlashes)(paths.route, false, true);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/controllers/common/statics.ts"],"sourcesContent":["import * as path from 'path';\nimport * as mime from 'mime';\nimport { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport {\n controllerCreator,\n removeSlashes,\n notUndefined,\n type MiddlewareLike,\n} from '../../utils';\nimport type { SendFile } from '../../services';\nimport type { Jimpex } from '../../app';\nimport type { DeepPartial, ExpressMiddleware, Router, RouterMethod } from '../../types';\n/**\n * The definition for each file the controller handles.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerFile = {\n /**\n * The route, relative to the controller root, to the file.\n */\n route: string;\n /**\n * The path to the file in the filesystem. Since the file is served using the\n * {@link SendFile} service, whether the file is relative to the project root or the\n * application executable depends on how the service is configured (relative to the\n * executable by default).\n */\n path: string;\n /**\n * A dictionary of headers for the response.\n */\n headers?: Record<string, string>;\n};\n/**\n * These are like \"master paths\" that get prepended to all the file paths and routes the\n * controller use.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerPathsOptions = {\n /**\n * A custom route to prefix all the file routes with.\n */\n route: string;\n /**\n * A custom path to prefix all the file paths with.\n */\n source: string;\n};\n/**\n * The options to customize the controller.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerOptions = {\n /**\n * A list of filenames, or definitions for the files to handle.\n */\n files: Array<string | StaticsControllerFile>;\n /**\n * A dictionary with the allowed router (HTTP) methods the controller can use to serve\n * the files. If `all` is set to `true`, the rest of the values will be ignored.\n *\n * @default {get: true, all: false}\n */\n methods: Partial<Record<RouterMethod, boolean>>;\n /**\n * The \"master paths\" the controller can use to prefix the file paths and routes.\n *\n * @default {route: '', source: './',}\n */\n paths: StaticsControllerPathsOptions;\n};\n/**\n * The options to construct a {@link StaticsController}.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerConstructorOptions =\n DeepPartial<StaticsControllerOptions> & {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n sendFile: SendFile;\n };\n };\n/**\n * A function to generate a list of middlewares that can be executed before the tontroller\n * main middleware.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[];\n/**\n * The options for the controller creator that mounts {@link StaticsController}.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerCreatorOptions = DeepPartial<StaticsControllerOptions> & {\n /**\n * A function to generate a list of middlewares that can be executed before the\n * tontroller main middleware.\n */\n getMiddlewares?: StaticsControllerGetMiddlewaresFn;\n};\n/**\n * The options for {@link StaticsController._addRoute}.\n *\n * @access protected\n * @group Controllers/Statics\n */\nexport type AddStaticRouteOptions = {\n /**\n * The reference for the router in which the middlewares will be added.\n */\n router: Router;\n /**\n * The router method in which the middlewares will be added.\n */\n method: RouterMethod;\n /**\n * The definition of the file to serve.\n */\n file: StaticsControllerFile;\n /**\n * The middleware created by {@link StaticsController}, that will serve the file.\n */\n fileMiddleware: ExpressMiddleware;\n /**\n * A list of extra middlewares to execute before the file middleware.\n */\n middlewares: ExpressMiddleware[];\n};\n\n/**\n * The controller class that allows the application to serve specific files from any\n * folder to any route without the need of mounting directories as \"static\".\n *\n * @group Controller Classes\n * @group Controllers/Statics\n * @prettierignore\n */\nexport class StaticsController {\n /**\n * The service that serves static files.\n */\n protected readonly _sendFile: SendFile;\n /**\n * The controller customization options.\n */\n protected _options: StaticsControllerOptions;\n /**\n * A dictionary with the formatted definitions of the files that will be served.\n * It uses the files' routes as keys, for easy access in the middleware.\n */\n protected files: Record<string, StaticsControllerFile>;\n /**\n * @param options The options to construct the controller.\n */\n constructor({ inject, ...options }: StaticsControllerConstructorOptions) {\n this._sendFile = inject.sendFile;\n this._options = this._validateOptions(\n deepAssignWithOverwrite(\n {\n files: ['favicon.ico', 'index.html'],\n methods: options.methods || {\n all: false,\n get: true,\n },\n paths: {\n route: '',\n source: './',\n },\n },\n options,\n ),\n );\n this.files = this._createFiles();\n }\n /**\n * Mounts the middlewares in the router in order to serve the files.\n *\n * @param router A reference to the application router.\n * @param middlewares A list of extra middlewares to execute before the file\n * middleware.\n */\n addRoutes(router: Router, middlewares: ExpressMiddleware[] = []): Router {\n const { methods } = this._options;\n const use: RouterMethod[] = methods.all\n ? ['all']\n : Object.keys(methods).reduce<RouterMethod[]>((acc, name) => {\n const methodName = name as RouterMethod;\n if (methods[methodName]) {\n acc.push(methodName);\n }\n\n return acc;\n }, []);\n\n Object.keys(this.files).forEach((route) => {\n const file = this.files[route as keyof typeof this.files]!;\n const fileMiddleware = this._getMiddleware(file);\n use.forEach((method) =>\n this._addRoute({ router, method, file, fileMiddleware, middlewares }),\n );\n });\n\n return router;\n }\n /**\n * The controller options.\n */\n get options(): Readonly<StaticsControllerOptions> {\n return { ...this._options };\n }\n /**\n * Generates the middleware that will serve the file.\n *\n * @param file The definition of the file to serve.\n */\n protected _getMiddleware(file: StaticsControllerFile): ExpressMiddleware {\n return (_, res, next) => {\n const extension = path.parse(file.path).ext.substring(1);\n const headers = {\n 'Content-Type': mime.getType(extension) || 'text/html',\n ...file.headers,\n };\n\n Object.entries(headers).forEach(([key, value]) => {\n res.setHeader(key, value);\n });\n\n this._sendFile({\n res,\n filepath: file.path,\n next,\n });\n };\n }\n /**\n * Mounts the middleware(s) for a file in the router.\n *\n * @param options The information of the file and how it needs to be added.\n */\n protected _addRoute({\n router,\n method,\n file,\n fileMiddleware,\n middlewares,\n }: AddStaticRouteOptions): void {\n const { route } = file;\n router[method](route, [...middlewares, fileMiddleware]);\n }\n /**\n * Validates and formats the options sent to the constructor in order to get the final\n * set that will be stored in the controller.\n *\n * @param options The options to validate.\n * @throws If no files are specified.\n * @throws If methods is not defined.\n * @throws If no methods are enabled.\n * @throws If there's an invalid HTTP method.\n */\n protected _validateOptions(\n options: StaticsControllerOptions,\n ): StaticsControllerOptions {\n if (!options.files || !options.files.length) {\n throw new Error('You need to specify a list of files');\n }\n\n if (!options.methods) {\n throw new Error('You need to specify which HTTP methods are allowed for the files');\n }\n\n const methods = Object.keys(options.methods) as RouterMethod[];\n\n const atLeastOne = methods.some((method) => options.methods[method]);\n if (!atLeastOne) {\n throw new Error('You need to enable at least one HTTP method to serve the files');\n }\n\n const allowedMethods: RouterMethod[] = [\n 'all',\n 'get',\n 'head',\n 'post',\n 'patch',\n 'put',\n 'delete',\n 'connect',\n 'options',\n 'trace',\n ];\n\n const invalid = methods.find(\n (method) => !allowedMethods.includes(method.toLowerCase() as RouterMethod),\n );\n\n if (invalid) {\n throw new Error(`${invalid} is not a valid HTTP method`);\n }\n\n const newMethods = methods.reduce<Record<string, boolean>>((acc, method) => {\n acc[method.toLowerCase()] = !!options.methods[method];\n return acc;\n }, {});\n\n return {\n ...options,\n methods: newMethods,\n };\n }\n /**\n * Parses the files received from the constructor's options, and formats them into\n * proper definitions the controller can use.\n */\n protected _createFiles(): Record<string, StaticsControllerFile> {\n const { files, paths } = this._options;\n const routePath = removeSlashes(paths.route, false, true);\n return files.reduce<Record<string, StaticsControllerFile>>((acc, file) => {\n let src;\n let route;\n let headers;\n if (typeof file === 'object') {\n ({ route, path: src, headers } = file);\n } else {\n src = file;\n route = file;\n }\n\n src = path.join(paths.source, src);\n route = removeSlashes(route, true, false);\n route = `${routePath}/${route}`;\n acc[route] = {\n path: src,\n route,\n headers: headers || {},\n };\n\n return acc;\n }, {});\n }\n}\n/**\n * A controller that allows the application to server specific files from any folder to\n * any route without the need of mounting directories as \"static\" folders.\n *\n * @group Controllers\n * @group Controllers/Statics\n */\nexport const staticsController = controllerCreator(\n ({ getMiddlewares, ...options }: StaticsControllerCreatorOptions = {}) =>\n (app) => {\n const router = app.getRouter();\n const ctrl = new StaticsController({\n inject: {\n sendFile: app.get('sendFile'),\n },\n ...options,\n });\n\n let useMiddlewares: ExpressMiddleware[] | undefined;\n if (getMiddlewares) {\n useMiddlewares = getMiddlewares(app)\n .map((middleware) => {\n if ('middleware' in middleware) {\n return middleware.connect(app) as ExpressMiddleware | undefined;\n }\n\n return middleware as ExpressMiddleware;\n })\n .filter(notUndefined);\n }\n\n return ctrl.addRoutes(router, useMiddlewares);\n },\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,WAAsB;AACtB,WAAsB;AACtB,yBAAwC;AACxC,mBAKO;AAwIA,MAAM,kBAAkB;AAAA,EAiB7B,YAAY,EAAE,WAAW,QAAQ,GAAwC;AAbzE,wBAAmB;AAInB,wBAAU;AAKV,wBAAU;AAKR,SAAK,YAAY,OAAO;AACxB,SAAK,WAAW,KAAK;AAAA,UACnB;AAAA,QACE;AAAA,UACE,OAAO,CAAC,eAAe,YAAY;AAAA,UACnC,SAAS,QAAQ,WAAW;AAAA,YAC1B,KAAK;AAAA,YACL,KAAK;AAAA,UACP;AAAA,UACA,OAAO;AAAA,YACL,OAAO;AAAA,YACP,QAAQ;AAAA,UACV;AAAA,QACF;AAAA,QACA;AAAA,MACF;AAAA,IACF;AACA,SAAK,QAAQ,KAAK,aAAa;AAAA,EACjC;AAAA,EAQA,UAAU,QAAgB,cAAmC,CAAC,GAAW;AACvE,UAAM,EAAE,QAAQ,IAAI,KAAK;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AQ,GAAG;AACjC,kBAAQ,4BAAc,OAAO,MAAM,KAAK;AACxC,cAAQ,GAAG,aAAa;AACxB,UAAI,SAAS;AAAA,QACX,MAAM;AAAA,QACN;AAAA,QACA,SAAS,WAAW,CAAC;AAAA,MACvB;AAEA,aAAO;AAAA,IACT,GAAG,CAAC,CAAC;AAAA,EACP;AACF;AAQO,MAAM,wBAAoB;AAAA,EAC/B,CAAC,EAAE,mBAAmB,QAAQ,IAAqC,CAAC,MAClE,CAAC,QAAQ;AACP,UAAM,SAAS,IAAI,UAAU;AAC7B,UAAM,OAAO,IAAI,kBAAkB;AAAA,MACjC,QAAQ;AAAA,QACN,UAAU,IAAI,IAAI,UAAU;AAAA,MAC9B;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAED,QAAI;AACJ,QAAI,gBAAgB;AAClB,uBAAiB,eAAe,GAAG,EAChC,IAAI,CAAC,eAAe;AACnB,YAAI,gBAAgB,YAAY;AAC9B,iBAAO,WAAW,QAAQ,GAAG;AAAA,QAC/B;AAEA,eAAO;AAAA,MACT,CAAC,EACA,OAAO,yBAAY;AAAA,IACxB;AAEA,WAAO,KAAK,UAAU,QAAQ,cAAc;AAAA,EAC9C;AACJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/controllers/common/statics.ts"],"sourcesContent":["import * as path from 'path';\nimport * as mime from 'mime';\nimport { deepAssignWithOverwrite } from '@homer0/deep-assign';\nimport {\n controllerCreator,\n removeSlashes,\n notUndefined,\n type MiddlewareLike,\n} from '../../utils';\nimport type { SendFile } from '../../services';\nimport type { Jimpex } from '../../app';\nimport type { DeepPartial, ExpressMiddleware, Router, RouterMethod } from '../../types';\n/**\n * The definition for each file the controller handles.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerFile = {\n /**\n * The route, relative to the controller root, to the file.\n */\n route: string;\n /**\n * The path to the file in the filesystem. Since the file is served using the\n * {@link SendFile} service, whether the file is relative to the project root or the\n * application executable depends on how the service is configured (relative to the\n * executable by default).\n */\n path: string;\n /**\n * A dictionary of headers for the response.\n */\n headers?: Record<string, string>;\n};\n/**\n * These are like \"master paths\" that get prepended to all the file paths and routes the\n * controller use.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerPathsOptions = {\n /**\n * A custom route to prefix all the file routes with.\n */\n route: string;\n /**\n * A custom path to prefix all the file paths with.\n */\n source: string;\n};\n/**\n * The options to customize the controller.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerOptions = {\n /**\n * A list of filenames, or definitions for the files to handle.\n */\n files: Array<string | StaticsControllerFile>;\n /**\n * A dictionary with the allowed router (HTTP) methods the controller can use to serve\n * the files. If `all` is set to `true`, the rest of the values will be ignored.\n *\n * @default {get: true, all: false}\n */\n methods: Partial<Record<RouterMethod, boolean>>;\n /**\n * The \"master paths\" the controller can use to prefix the file paths and routes.\n *\n * @default {route: '', source: './',}\n */\n paths: StaticsControllerPathsOptions;\n};\n/**\n * The options to construct a {@link StaticsController}.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerConstructorOptions =\n DeepPartial<StaticsControllerOptions> & {\n /**\n * A dictionary with the dependencies to inject.\n */\n inject: {\n sendFile: SendFile;\n };\n };\n/**\n * A function to generate a list of middlewares that can be executed before the tontroller\n * main middleware.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerGetMiddlewaresFn = (app: Jimpex) => MiddlewareLike[];\n/**\n * The options for the controller creator that mounts {@link StaticsController}.\n *\n * @group Controllers/Statics\n */\nexport type StaticsControllerCreatorOptions = DeepPartial<StaticsControllerOptions> & {\n /**\n * A function to generate a list of middlewares that can be executed before the\n * tontroller main middleware.\n */\n getMiddlewares?: StaticsControllerGetMiddlewaresFn;\n};\n/**\n * The options for {@link StaticsController._addRoute}.\n *\n * @access protected\n * @group Controllers/Statics\n */\nexport type AddStaticRouteOptions = {\n /**\n * The reference for the router in which the middlewares will be added.\n */\n router: Router;\n /**\n * The router method in which the middlewares will be added.\n */\n method: RouterMethod;\n /**\n * The definition of the file to serve.\n */\n file: StaticsControllerFile;\n /**\n * The middleware created by {@link StaticsController}, that will serve the file.\n */\n fileMiddleware: ExpressMiddleware;\n /**\n * A list of extra middlewares to execute before the file middleware.\n */\n middlewares: ExpressMiddleware[];\n};\n\n/**\n * The controller class that allows the application to serve specific files from any\n * folder to any route without the need of mounting directories as \"static\".\n *\n * @group Controller Classes\n * @group Controllers/Statics\n * @prettierignore\n */\nexport class StaticsController {\n /**\n * The service that serves static files.\n */\n protected readonly _sendFile: SendFile;\n /**\n * The controller customization options.\n */\n protected _options: StaticsControllerOptions;\n /**\n * A dictionary with the formatted definitions of the files that will be served.\n * It uses the files' routes as keys, for easy access in the middleware.\n */\n protected files: Record<string, StaticsControllerFile>;\n /**\n * @param options The options to construct the controller.\n */\n constructor({ inject, ...options }: StaticsControllerConstructorOptions) {\n this._sendFile = inject.sendFile;\n this._options = this._validateOptions(\n deepAssignWithOverwrite(\n {\n files: ['favicon.ico', 'index.html'],\n methods: options.methods || {\n all: false,\n get: true,\n },\n paths: {\n route: '',\n source: './',\n },\n },\n options,\n ),\n );\n this.files = this._createFiles();\n }\n /**\n * Mounts the middlewares in the router in order to serve the files.\n *\n * @param router A reference to the application router.\n * @param middlewares A list of extra middlewares to execute before the file\n * middleware.\n */\n addRoutes(router: Router, middlewares: ExpressMiddleware[] = []): Router {\n const { methods } = this._options;\n const use: RouterMethod[] = methods.all\n ? ['all']\n : Object.keys(methods).reduce<RouterMethod[]>((acc, name) => {\n const methodName = name as RouterMethod;\n if (methods[methodName]) {\n acc.push(methodName);\n }\n\n return acc;\n }, []);\n\n Object.keys(this.files).forEach((route) => {\n const file = this.files[route as keyof typeof this.files]!;\n const fileMiddleware = this._getMiddleware(file);\n use.forEach((method) =>\n this._addRoute({ router, method, file, fileMiddleware, middlewares }),\n );\n });\n\n return router;\n }\n /**\n * The controller options.\n */\n get options(): Readonly<StaticsControllerOptions> {\n return { ...this._options };\n }\n /**\n * Generates the middleware that will serve the file.\n *\n * @param file The definition of the file to serve.\n */\n protected _getMiddleware(file: StaticsControllerFile): ExpressMiddleware {\n return (_, res, next) => {\n const extension = path.parse(file.path).ext.substring(1);\n const headers = {\n 'Content-Type': mime.getType(extension) || 'text/html',\n ...file.headers,\n };\n\n Object.entries(headers).forEach(([key, value]) => {\n res.setHeader(key, value);\n });\n\n this._sendFile({\n res,\n filepath: file.path,\n next,\n });\n };\n }\n /**\n * Mounts the middleware(s) for a file in the router.\n *\n * @param options The information of the file and how it needs to be added.\n */\n protected _addRoute({\n router,\n method,\n file,\n fileMiddleware,\n middlewares,\n }: AddStaticRouteOptions): void {\n const { route } = file;\n router[method](route, [...middlewares, fileMiddleware]);\n }\n /**\n * Validates and formats the options sent to the constructor in order to get the final\n * set that will be stored in the controller.\n *\n * @param options The options to validate.\n * @throws If no files are specified.\n * @throws If methods is not defined.\n * @throws If no methods are enabled.\n * @throws If there's an invalid HTTP method.\n */\n protected _validateOptions(\n options: StaticsControllerOptions,\n ): StaticsControllerOptions {\n if (!options.files || !options.files.length) {\n throw new Error('You need to specify a list of files');\n }\n\n if (!options.methods) {\n throw new Error('You need to specify which HTTP methods are allowed for the files');\n }\n\n const methods = Object.keys(options.methods) as RouterMethod[];\n\n const atLeastOne = methods.some((method) => options.methods[method]);\n if (!atLeastOne) {\n throw new Error('You need to enable at least one HTTP method to serve the files');\n }\n\n const allowedMethods: RouterMethod[] = [\n 'all',\n 'get',\n 'head',\n 'post',\n 'patch',\n 'put',\n 'delete',\n 'connect',\n 'options',\n 'trace',\n ];\n\n const invalid = methods.find(\n (method) => !allowedMethods.includes(method.toLowerCase() as RouterMethod),\n );\n\n if (invalid) {\n throw new Error(`${invalid} is not a valid HTTP method`);\n }\n\n const newMethods = methods.reduce<Record<string, boolean>>((acc, method) => {\n acc[method.toLowerCase()] = !!options.methods[method];\n return acc;\n }, {});\n\n return {\n ...options,\n methods: newMethods,\n };\n }\n /**\n * Parses the files received from the constructor's options, and formats them into\n * proper definitions the controller can use.\n */\n protected _createFiles(): Record<string, StaticsControllerFile> {\n const { files, paths } = this._options;\n const routePath = removeSlashes(paths.route, false, true);\n return files.reduce<Record<string, StaticsControllerFile>>((acc, file) => {\n let src;\n let route;\n let headers;\n if (typeof file === 'object') {\n ({ route, path: src, headers } = file);\n } else {\n src = file;\n route = file;\n }\n\n src = path.join(paths.source, src);\n route = removeSlashes(route, true, false);\n route = `${routePath}/${route}`;\n acc[route] = {\n path: src,\n route,\n headers: headers || {},\n };\n\n return acc;\n }, {});\n }\n}\n/**\n * A controller that allows the application to server specific files from any folder to\n * any route without the need of mounting directories as \"static\" folders.\n *\n * @group Controllers\n * @group Controllers/Statics\n */\nexport const staticsController = controllerCreator(\n ({ getMiddlewares, ...options }: StaticsControllerCreatorOptions = {}) =>\n (app) => {\n const router = app.getRouter();\n const ctrl = new StaticsController({\n inject: {\n sendFile: app.get('sendFile'),\n },\n ...options,\n });\n\n let useMiddlewares: ExpressMiddleware[] | undefined;\n if (getMiddlewares) {\n useMiddlewares = getMiddlewares(app)\n .map((middleware) => {\n if ('middleware' in middleware) {\n return middleware.connect(app) as ExpressMiddleware | undefined;\n }\n\n return middleware as ExpressMiddleware;\n })\n .filter(notUndefined);\n }\n\n return ctrl.addRoutes(router, useMiddlewares);\n },\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,WAAsB;AACtB,WAAsB;AACtB,yBAAwC;AACxC,mBAKO;AAwIA,MAAM,kBAAkB;AAAA;AAAA;AAAA;AAAA,EAiB7B,YAAY,EAAE,QAAQ,GAAG,QAAQ,GAAwC;AAbzE;AAAA;AAAA;AAAA,wBAAmB;AAInB;AAAA;AAAA;AAAA,wBAAU;AAKV;AAAA;AAAA;AAAA;AAAA,wBAAU;AAKR,SAAK,YAAY,OAAO;AACxB,SAAK,WAAW,KAAK;AAAA,UACnB;AAAA,QACE;AAAA,UACE,OAAO,CAAC,eAAe,YAAY;AAAA,UACnC,SAAS,QAAQ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;AAAA,IACxB;AAEA,WAAO,KAAK,UAAU,QAAQ,cAAc;AAAA,EAC9C;AACJ;","names":[]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export { ConfigController, ConfigControllerOptions, configController } from './common/config.mjs';
|
|
2
|
+
export { GetHealthStatus, HealthController, HealthControllerOptions, healthController } from './common/health.mjs';
|
|
3
|
+
export { AddStaticRouteOptions, StaticsController, StaticsControllerConstructorOptions, StaticsControllerCreatorOptions, StaticsControllerFile, StaticsControllerGetMiddlewaresFn, StaticsControllerOptions, StaticsControllerPathsOptions, staticsController } from './common/statics.mjs';
|
|
4
|
+
export { AddGatewayRouteOptions, GatewayConfig, GatewayConfigEndpointDefinition, GatewayConfigEndpointProps, GatewayConfigEndpoints, GatewayController, GatewayControllerAPIConfig, GatewayControllerAPIConfigOptions, GatewayControllerConstructorOptions, GatewayControllerCreatorOptions, GatewayControllerEndpointInfo, GatewayControllerExtraOptions, GatewayControllerGetMiddlewaresFn, GatewayControllerHeaderOptions, GatewayControllerHelperOptions, GatewayControllerOptions, GatewayControllerRequest, GatewayControllerRoute, GatewayControllerRouteMethod, GatewayHelperService, GatewayHelperServiceBaseFnOptions, GatewayHelperServiceErrorHandler, GatewayHelperServiceErrorHandlerOptions, GatewayHelperServiceRequestReducer, GatewayHelperServiceRequestReducerOptions, GatewayHelperServiceResponseHandler, GatewayHelperServiceResponseReducer, GatewayHelperServiceResponseReducerOptions, GatewayHelperServiceStreamVerification, gatewayController } from './utils/gateway.mjs';
|
|
5
|
+
import '@homer0/jimple';
|
|
6
|
+
import '../index-efeb437e.js';
|
|
7
|
+
import '../types/express.mjs';
|
|
8
|
+
import 'express';
|
|
9
|
+
import '../types/http.mjs';
|
|
10
|
+
import 'https';
|
|
11
|
+
import 'http';
|
|
12
|
+
import 'spdy';
|
|
13
|
+
import 'node-fetch';
|
|
14
|
+
import '../types/utils.mjs';
|
|
15
|
+
import '@homer0/path-utils';
|
|
16
|
+
import '@homer0/simple-logger';
|
|
17
|
+
import '@homer0/simple-config';
|
|
18
|
+
import '@homer0/events-hub';
|
|
19
|
+
import '../services/http/responsesBuilder.mjs';
|
|
20
|
+
import '../utils/fns/statuses.mjs';
|
|
21
|
+
import 'statuses';
|
|
22
|
+
import '../services/common/sendFile.mjs';
|
|
23
|
+
import '@homer0/api-utils';
|
|
24
|
+
import '../services/http/http.mjs';
|
|
@@ -3,8 +3,7 @@ export { GetHealthStatus, HealthController, HealthControllerOptions, healthContr
|
|
|
3
3
|
export { AddStaticRouteOptions, StaticsController, StaticsControllerConstructorOptions, StaticsControllerCreatorOptions, StaticsControllerFile, StaticsControllerGetMiddlewaresFn, StaticsControllerOptions, StaticsControllerPathsOptions, staticsController } from './common/statics.js';
|
|
4
4
|
export { AddGatewayRouteOptions, GatewayConfig, GatewayConfigEndpointDefinition, GatewayConfigEndpointProps, GatewayConfigEndpoints, GatewayController, GatewayControllerAPIConfig, GatewayControllerAPIConfigOptions, GatewayControllerConstructorOptions, GatewayControllerCreatorOptions, GatewayControllerEndpointInfo, GatewayControllerExtraOptions, GatewayControllerGetMiddlewaresFn, GatewayControllerHeaderOptions, GatewayControllerHelperOptions, GatewayControllerOptions, GatewayControllerRequest, GatewayControllerRoute, GatewayControllerRouteMethod, GatewayHelperService, GatewayHelperServiceBaseFnOptions, GatewayHelperServiceErrorHandler, GatewayHelperServiceErrorHandlerOptions, GatewayHelperServiceRequestReducer, GatewayHelperServiceRequestReducerOptions, GatewayHelperServiceResponseHandler, GatewayHelperServiceResponseReducer, GatewayHelperServiceResponseReducerOptions, GatewayHelperServiceStreamVerification, gatewayController } from './utils/gateway.js';
|
|
5
5
|
import '@homer0/jimple';
|
|
6
|
-
import '../
|
|
7
|
-
import '@homer0/events-hub';
|
|
6
|
+
import '../index-b2a04c78.js';
|
|
8
7
|
import '../types/express.js';
|
|
9
8
|
import 'express';
|
|
10
9
|
import '../types/http.js';
|
|
@@ -12,13 +11,14 @@ import 'https';
|
|
|
12
11
|
import 'http';
|
|
13
12
|
import 'spdy';
|
|
14
13
|
import 'node-fetch';
|
|
15
|
-
import '@homer0/simple-config';
|
|
16
14
|
import '../types/utils.js';
|
|
15
|
+
import '@homer0/path-utils';
|
|
17
16
|
import '@homer0/simple-logger';
|
|
17
|
+
import '@homer0/simple-config';
|
|
18
|
+
import '@homer0/events-hub';
|
|
18
19
|
import '../services/http/responsesBuilder.js';
|
|
19
20
|
import '../utils/fns/statuses.js';
|
|
20
21
|
import 'statuses';
|
|
21
22
|
import '../services/common/sendFile.js';
|
|
22
|
-
import '@homer0/path-utils';
|
|
23
23
|
import '@homer0/api-utils';
|
|
24
24
|
import '../services/http/http.js';
|
|
@@ -17,4 +17,9 @@ var controllers_exports = {};
|
|
|
17
17
|
module.exports = __toCommonJS(controllers_exports);
|
|
18
18
|
__reExport(controllers_exports, require("./common"), module.exports);
|
|
19
19
|
__reExport(controllers_exports, require("./utils"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./common"),
|
|
23
|
+
...require("./utils")
|
|
24
|
+
});
|
|
20
25
|
//# sourceMappingURL=index.js.map
|