@tsed/platform-http 8.11.0 → 8.11.2-rc.1

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.
Files changed (228) hide show
  1. package/lib/esm/common/builder/PlatformBuilder.js +0 -1
  2. package/lib/esm/common/builder/__fixtures__/PlatformCustom.js +0 -1
  3. package/lib/esm/common/config/interfaces/PlatformJsonMapperSettings.js +0 -1
  4. package/lib/esm/common/config/interfaces/PlatformMulterSettings.js +0 -1
  5. package/lib/esm/common/config/interfaces/PlatformRouterSettings.js +0 -1
  6. package/lib/esm/common/config/interfaces/PlatformStaticsSettings.js +0 -1
  7. package/lib/esm/common/config/services/PlatformConfiguration.js +0 -1
  8. package/lib/esm/common/constants/routerOptions.js +0 -1
  9. package/lib/esm/common/decorators/PlatformProvider.js +0 -1
  10. package/lib/esm/common/decorators/multer/multerFileSize.js +0 -1
  11. package/lib/esm/common/decorators/multer/multerOptions.js +0 -1
  12. package/lib/esm/common/decorators/multer/multipartFile.js +0 -1
  13. package/lib/esm/common/decorators/params/error.js +0 -1
  14. package/lib/esm/common/decorators/params/next.js +0 -1
  15. package/lib/esm/common/decorators/params/request.js +0 -1
  16. package/lib/esm/common/decorators/params/response.js +0 -1
  17. package/lib/esm/common/domain/AnyToPromiseWithCtx.js +0 -1
  18. package/lib/esm/common/domain/EndpointMetadata.js +0 -1
  19. package/lib/esm/common/domain/PlatformContext.js +0 -1
  20. package/lib/esm/common/domain/PlatformRouteDetails.js +0 -1
  21. package/lib/esm/common/exports.js +0 -1
  22. package/lib/esm/common/fn/adapter.js +0 -1
  23. package/lib/esm/common/fn/application.js +0 -1
  24. package/lib/esm/common/index.js +0 -1
  25. package/lib/esm/common/interfaces/AfterInit.js +0 -1
  26. package/lib/esm/common/interfaces/AfterListen.js +0 -1
  27. package/lib/esm/common/interfaces/AfterRoutesInit.js +0 -1
  28. package/lib/esm/common/interfaces/BeforeInit.js +0 -1
  29. package/lib/esm/common/interfaces/BeforeListen.js +0 -1
  30. package/lib/esm/common/interfaces/BeforeRoutesInit.js +0 -1
  31. package/lib/esm/common/interfaces/HandlerType.js +0 -1
  32. package/lib/esm/common/interfaces/IncomingEvent.js +0 -1
  33. package/lib/esm/common/interfaces/OnReady.js +0 -1
  34. package/lib/esm/common/interfaces/OnRequest.js +0 -1
  35. package/lib/esm/common/interfaces/OnResponse.js +0 -1
  36. package/lib/esm/common/interfaces/OnRoutesInit.js +0 -1
  37. package/lib/esm/common/interfaces/PlatformRouteOptions.js +0 -1
  38. package/lib/esm/common/interfaces/ResponseErrorObject.js +0 -1
  39. package/lib/esm/common/interfaces/Route.js +0 -1
  40. package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js +0 -1
  41. package/lib/esm/common/middlewares/PlatformMulterMiddleware.js +0 -1
  42. package/lib/esm/common/services/Platform.js +0 -1
  43. package/lib/esm/common/services/PlatformAdapter.js +0 -1
  44. package/lib/esm/common/services/PlatformApplication.js +0 -1
  45. package/lib/esm/common/services/PlatformHandler.js +0 -1
  46. package/lib/esm/common/services/PlatformRequest.js +0 -1
  47. package/lib/esm/common/services/PlatformResponse.js +0 -1
  48. package/lib/esm/common/utils/closeServer.js +0 -1
  49. package/lib/esm/common/utils/createContext.js +0 -1
  50. package/lib/esm/common/utils/createHttpServer.js +0 -1
  51. package/lib/esm/common/utils/createHttpsServer.js +0 -1
  52. package/lib/esm/common/utils/createInjector.js +0 -1
  53. package/lib/esm/common/utils/createServer.js +0 -1
  54. package/lib/esm/common/utils/ensureContext.js +0 -1
  55. package/lib/esm/common/utils/getConfiguration.js +0 -1
  56. package/lib/esm/common/utils/getStaticsOptions.js +0 -1
  57. package/lib/esm/common/utils/listenServer.js +0 -1
  58. package/lib/esm/common/utils/mapReturnedResponse.js +0 -1
  59. package/lib/esm/common/utils/printRoutes.js +0 -1
  60. package/lib/esm/common/utils/resolveControllers.js +0 -1
  61. package/lib/esm/common/utils/setResponseHeaders.js +0 -1
  62. package/lib/esm/index.js +0 -1
  63. package/lib/esm/testing/FakeAdapter.js +0 -1
  64. package/lib/esm/testing/FakeResponse.js +0 -1
  65. package/lib/esm/testing/PlatformTest.js +0 -1
  66. package/lib/esm/testing/index.js +0 -1
  67. package/package.json +20 -17
  68. package/.barrelsby.json +0 -5
  69. package/lib/esm/common/builder/PlatformBuilder.js.map +0 -1
  70. package/lib/esm/common/builder/__fixtures__/PlatformCustom.js.map +0 -1
  71. package/lib/esm/common/config/interfaces/PlatformJsonMapperSettings.js.map +0 -1
  72. package/lib/esm/common/config/interfaces/PlatformMulterSettings.js.map +0 -1
  73. package/lib/esm/common/config/interfaces/PlatformRouterSettings.js.map +0 -1
  74. package/lib/esm/common/config/interfaces/PlatformStaticsSettings.js.map +0 -1
  75. package/lib/esm/common/config/services/PlatformConfiguration.js.map +0 -1
  76. package/lib/esm/common/constants/routerOptions.js.map +0 -1
  77. package/lib/esm/common/decorators/PlatformProvider.js.map +0 -1
  78. package/lib/esm/common/decorators/multer/multerFileSize.js.map +0 -1
  79. package/lib/esm/common/decorators/multer/multerOptions.js.map +0 -1
  80. package/lib/esm/common/decorators/multer/multipartFile.js.map +0 -1
  81. package/lib/esm/common/decorators/params/error.js.map +0 -1
  82. package/lib/esm/common/decorators/params/next.js.map +0 -1
  83. package/lib/esm/common/decorators/params/request.js.map +0 -1
  84. package/lib/esm/common/decorators/params/response.js.map +0 -1
  85. package/lib/esm/common/domain/AnyToPromiseWithCtx.js.map +0 -1
  86. package/lib/esm/common/domain/EndpointMetadata.js.map +0 -1
  87. package/lib/esm/common/domain/PlatformContext.js.map +0 -1
  88. package/lib/esm/common/domain/PlatformRouteDetails.js.map +0 -1
  89. package/lib/esm/common/exports.js.map +0 -1
  90. package/lib/esm/common/fn/adapter.js.map +0 -1
  91. package/lib/esm/common/fn/application.js.map +0 -1
  92. package/lib/esm/common/index.js.map +0 -1
  93. package/lib/esm/common/interfaces/AfterInit.js.map +0 -1
  94. package/lib/esm/common/interfaces/AfterListen.js.map +0 -1
  95. package/lib/esm/common/interfaces/AfterRoutesInit.js.map +0 -1
  96. package/lib/esm/common/interfaces/BeforeInit.js.map +0 -1
  97. package/lib/esm/common/interfaces/BeforeListen.js.map +0 -1
  98. package/lib/esm/common/interfaces/BeforeRoutesInit.js.map +0 -1
  99. package/lib/esm/common/interfaces/HandlerType.js.map +0 -1
  100. package/lib/esm/common/interfaces/IncomingEvent.js.map +0 -1
  101. package/lib/esm/common/interfaces/OnReady.js.map +0 -1
  102. package/lib/esm/common/interfaces/OnRequest.js.map +0 -1
  103. package/lib/esm/common/interfaces/OnResponse.js.map +0 -1
  104. package/lib/esm/common/interfaces/OnRoutesInit.js.map +0 -1
  105. package/lib/esm/common/interfaces/PlatformRouteOptions.js.map +0 -1
  106. package/lib/esm/common/interfaces/ResponseErrorObject.js.map +0 -1
  107. package/lib/esm/common/interfaces/Route.js.map +0 -1
  108. package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js.map +0 -1
  109. package/lib/esm/common/middlewares/PlatformMulterMiddleware.js.map +0 -1
  110. package/lib/esm/common/services/Platform.js.map +0 -1
  111. package/lib/esm/common/services/PlatformAdapter.js.map +0 -1
  112. package/lib/esm/common/services/PlatformApplication.js.map +0 -1
  113. package/lib/esm/common/services/PlatformHandler.js.map +0 -1
  114. package/lib/esm/common/services/PlatformRequest.js.map +0 -1
  115. package/lib/esm/common/services/PlatformResponse.js.map +0 -1
  116. package/lib/esm/common/utils/closeServer.js.map +0 -1
  117. package/lib/esm/common/utils/createContext.js.map +0 -1
  118. package/lib/esm/common/utils/createHttpServer.js.map +0 -1
  119. package/lib/esm/common/utils/createHttpsServer.js.map +0 -1
  120. package/lib/esm/common/utils/createInjector.js.map +0 -1
  121. package/lib/esm/common/utils/createServer.js.map +0 -1
  122. package/lib/esm/common/utils/ensureContext.js.map +0 -1
  123. package/lib/esm/common/utils/getConfiguration.js.map +0 -1
  124. package/lib/esm/common/utils/getStaticsOptions.js.map +0 -1
  125. package/lib/esm/common/utils/listenServer.js.map +0 -1
  126. package/lib/esm/common/utils/mapReturnedResponse.js.map +0 -1
  127. package/lib/esm/common/utils/printRoutes.js.map +0 -1
  128. package/lib/esm/common/utils/resolveControllers.js.map +0 -1
  129. package/lib/esm/common/utils/setResponseHeaders.js.map +0 -1
  130. package/lib/esm/index.js.map +0 -1
  131. package/lib/esm/testing/FakeAdapter.js.map +0 -1
  132. package/lib/esm/testing/FakeResponse.js.map +0 -1
  133. package/lib/esm/testing/PlatformTest.js.map +0 -1
  134. package/lib/esm/testing/index.js.map +0 -1
  135. package/src/common/builder/PlatformBuilder.spec.ts +0 -331
  136. package/src/common/builder/PlatformBuilder.ts +0 -426
  137. package/src/common/builder/__fixtures__/PlatformCustom.ts +0 -39
  138. package/src/common/config/interfaces/PlatformJsonMapperSettings.ts +0 -11
  139. package/src/common/config/interfaces/PlatformMulterSettings.ts +0 -22
  140. package/src/common/config/interfaces/PlatformRouterSettings.ts +0 -11
  141. package/src/common/config/interfaces/PlatformStaticsSettings.ts +0 -23
  142. package/src/common/config/services/PlatformConfiguration.spec.ts +0 -181
  143. package/src/common/config/services/PlatformConfiguration.ts +0 -158
  144. package/src/common/constants/routerOptions.ts +0 -1
  145. package/src/common/decorators/PlatformProvider.ts +0 -14
  146. package/src/common/decorators/multer/multerFileSize.ts +0 -31
  147. package/src/common/decorators/multer/multerOptions.ts +0 -45
  148. package/src/common/decorators/multer/multipartFile.ts +0 -51
  149. package/src/common/decorators/params/error.spec.ts +0 -15
  150. package/src/common/decorators/params/error.ts +0 -16
  151. package/src/common/decorators/params/next.spec.ts +0 -15
  152. package/src/common/decorators/params/next.ts +0 -31
  153. package/src/common/decorators/params/request.spec.ts +0 -50
  154. package/src/common/decorators/params/request.ts +0 -86
  155. package/src/common/decorators/params/response.spec.ts +0 -37
  156. package/src/common/decorators/params/response.ts +0 -74
  157. package/src/common/domain/AnyToPromiseWithCtx.spec.ts +0 -20
  158. package/src/common/domain/AnyToPromiseWithCtx.ts +0 -30
  159. package/src/common/domain/EndpointMetadata.ts +0 -1
  160. package/src/common/domain/PlatformContext.spec.ts +0 -124
  161. package/src/common/domain/PlatformContext.ts +0 -136
  162. package/src/common/domain/PlatformRouteDetails.ts +0 -7
  163. package/src/common/exports.ts +0 -4
  164. package/src/common/fn/adapter.ts +0 -42
  165. package/src/common/fn/application.ts +0 -11
  166. package/src/common/index.ts +0 -61
  167. package/src/common/interfaces/AfterInit.ts +0 -3
  168. package/src/common/interfaces/AfterListen.ts +0 -3
  169. package/src/common/interfaces/AfterRoutesInit.ts +0 -3
  170. package/src/common/interfaces/BeforeInit.ts +0 -3
  171. package/src/common/interfaces/BeforeListen.ts +0 -3
  172. package/src/common/interfaces/BeforeRoutesInit.ts +0 -3
  173. package/src/common/interfaces/HandlerType.ts +0 -12
  174. package/src/common/interfaces/IncomingEvent.ts +0 -6
  175. package/src/common/interfaces/OnReady.ts +0 -3
  176. package/src/common/interfaces/OnRequest.ts +0 -5
  177. package/src/common/interfaces/OnResponse.ts +0 -5
  178. package/src/common/interfaces/OnRoutesInit.ts +0 -3
  179. package/src/common/interfaces/PlatformRouteOptions.ts +0 -17
  180. package/src/common/interfaces/ResponseErrorObject.ts +0 -8
  181. package/src/common/interfaces/Route.ts +0 -12
  182. package/src/common/middlewares/PlatformAcceptMimesMiddleware.spec.ts +0 -215
  183. package/src/common/middlewares/PlatformAcceptMimesMiddleware.ts +0 -34
  184. package/src/common/middlewares/PlatformMulterMiddleware.ts +0 -6
  185. package/src/common/services/Platform.spec.ts +0 -145
  186. package/src/common/services/Platform.ts +0 -79
  187. package/src/common/services/PlatformAdapter.ts +0 -102
  188. package/src/common/services/PlatformApplication.spec.ts +0 -89
  189. package/src/common/services/PlatformApplication.ts +0 -38
  190. package/src/common/services/PlatformHandler.spec.ts +0 -152
  191. package/src/common/services/PlatformHandler.ts +0 -137
  192. package/src/common/services/PlatformRequest.spec.ts +0 -185
  193. package/src/common/services/PlatformRequest.ts +0 -182
  194. package/src/common/services/PlatformResponse.spec.ts +0 -364
  195. package/src/common/services/PlatformResponse.ts +0 -393
  196. package/src/common/utils/closeServer.spec.ts +0 -11
  197. package/src/common/utils/closeServer.ts +0 -8
  198. package/src/common/utils/createContext.spec.ts +0 -111
  199. package/src/common/utils/createContext.ts +0 -52
  200. package/src/common/utils/createHttpServer.spec.ts +0 -75
  201. package/src/common/utils/createHttpServer.ts +0 -16
  202. package/src/common/utils/createHttpsServer.spec.ts +0 -74
  203. package/src/common/utils/createHttpsServer.ts +0 -17
  204. package/src/common/utils/createInjector.spec.ts +0 -42
  205. package/src/common/utils/createInjector.ts +0 -25
  206. package/src/common/utils/createServer.ts +0 -52
  207. package/src/common/utils/ensureContext.spec.ts +0 -41
  208. package/src/common/utils/ensureContext.ts +0 -17
  209. package/src/common/utils/getConfiguration.spec.ts +0 -163
  210. package/src/common/utils/getConfiguration.ts +0 -34
  211. package/src/common/utils/getStaticsOptions.spec.ts +0 -27
  212. package/src/common/utils/getStaticsOptions.ts +0 -24
  213. package/src/common/utils/listenServer.ts +0 -18
  214. package/src/common/utils/mapReturnedResponse.spec.ts +0 -35
  215. package/src/common/utils/mapReturnedResponse.ts +0 -10
  216. package/src/common/utils/printRoutes.spec.ts +0 -19
  217. package/src/common/utils/printRoutes.ts +0 -40
  218. package/src/common/utils/resolveControllers.spec.ts +0 -40
  219. package/src/common/utils/resolveControllers.ts +0 -54
  220. package/src/common/utils/setResponseHeaders.spec.ts +0 -84
  221. package/src/common/utils/setResponseHeaders.ts +0 -31
  222. package/src/index.ts +0 -2
  223. package/src/testing/FakeAdapter.ts +0 -69
  224. package/src/testing/FakeResponse.ts +0 -115
  225. package/src/testing/PlatformTest.spec.ts +0 -53
  226. package/src/testing/PlatformTest.ts +0 -161
  227. package/src/testing/index.ts +0 -5
  228. package/vitest.config.mts +0 -25
@@ -1,426 +0,0 @@
1
- import type {IncomingMessage, ServerResponse} from "node:http";
2
- import Http from "node:http";
3
- import type Https from "node:https";
4
-
5
- import {type Env, isClass, isFunction, isString, nameOf, Type} from "@tsed/core";
6
- import {
7
- colors,
8
- configuration,
9
- constant,
10
- createContainer,
11
- destroyInjector,
12
- injector,
13
- InjectorService,
14
- logger,
15
- ProviderOpts,
16
- ProviderScope,
17
- setLoggerConfiguration,
18
- TokenProvider
19
- } from "@tsed/di";
20
- import {$asyncAlter, $asyncEmit} from "@tsed/hooks";
21
- import {getMiddlewaresForHook, PlatformMiddlewareLoadingOptions} from "@tsed/platform-middlewares";
22
- import {PlatformLayer} from "@tsed/platform-router";
23
- import Http2 from "http2";
24
-
25
- import {PlatformStaticsSettings} from "../config/interfaces/PlatformStaticsSettings.js";
26
- import {PlatformRouteDetails} from "../domain/PlatformRouteDetails.js";
27
- import {application} from "../fn/application.js";
28
- import {Route} from "../interfaces/Route.js";
29
- import {Platform} from "../services/Platform.js";
30
- import {PlatformAdapter, PlatformBuilderSettings} from "../services/PlatformAdapter.js";
31
- import {PlatformApplication} from "../services/PlatformApplication.js";
32
- import {closeServer} from "../utils/closeServer.js";
33
- import {createInjector} from "../utils/createInjector.js";
34
- import {CreateServerReturn} from "../utils/createServer.js";
35
- import {getConfiguration} from "../utils/getConfiguration.js";
36
- import {getStaticsOptions} from "../utils/getStaticsOptions.js";
37
- import {printRoutes} from "../utils/printRoutes.js";
38
- import {resolveControllers} from "../utils/resolveControllers.js";
39
-
40
- /**
41
- * @platform
42
- */
43
- export class PlatformBuilder<App = TsED.Application> {
44
- protected startedAt = new Date();
45
- protected current = new Date();
46
- readonly #rootModule: Type<any>;
47
- #promise: Promise<this>;
48
- #servers: CreateServerReturn[];
49
- #listeners: (Http.Server | Https.Server | Http2.Http2Server)[] = [];
50
-
51
- protected constructor(module: Type, settings: Partial<TsED.Configuration>) {
52
- this.#rootModule = module;
53
-
54
- const configuration = getConfiguration(settings, module);
55
-
56
- createInjector(configuration);
57
-
58
- this.log(`Loading ${this.name.toUpperCase()} platform adapter...`);
59
-
60
- this.createHttpServers();
61
-
62
- this.log("Injector created...");
63
- }
64
-
65
- get name() {
66
- return this.adapter.NAME;
67
- }
68
-
69
- get rootModule(): any {
70
- return injector().get(this.#rootModule);
71
- }
72
-
73
- get app(): PlatformApplication<App> {
74
- return injector().get<PlatformApplication<App>>(PlatformApplication)!;
75
- }
76
-
77
- get platform() {
78
- return injector().get<Platform>(Platform)!;
79
- }
80
-
81
- get adapter() {
82
- return injector().get(PlatformAdapter);
83
- }
84
-
85
- /**
86
- * Return the settings configured by the decorator @@Configuration@@.
87
- *
88
- * ```typescript
89
- * @Configuration({
90
- * port: 8000,
91
- * httpsPort: 8080,
92
- * mount: {
93
- * "/rest": "${rootDir}/controllers/**\/*.js"
94
- * }
95
- * })
96
- * export class Server {
97
- * $onInit(){
98
- * console.log(this.settings); // {rootDir, port, httpsPort,...}
99
- * }
100
- * }
101
- * ```
102
- *
103
- * @returns {PlatformConfiguration}
104
- */
105
- get settings() {
106
- return configuration();
107
- }
108
-
109
- get logger() {
110
- return logger();
111
- }
112
-
113
- get disableBootstrapLog() {
114
- return constant<boolean>("logger.disableBootstrapLog");
115
- }
116
-
117
- /**
118
- * @deprecated use injector() instead of this method.
119
- */
120
- get injector(): InjectorService {
121
- return injector();
122
- }
123
-
124
- static create<App = TsED.Application>(module: Type<any>, settings: PlatformBuilderSettings<App>) {
125
- return this.build(module, {
126
- httpsPort: false,
127
- httpPort: false,
128
- ...settings
129
- });
130
- }
131
-
132
- static build<App = TsED.Application>(module: Type<any>, settings: PlatformBuilderSettings<App>) {
133
- return new PlatformBuilder(module, settings);
134
- }
135
-
136
- /**
137
- * Bootstrap a server application
138
- * @param module
139
- * @param settings
140
- */
141
- static bootstrap<App = TsED.Application>(module: Type<any>, settings: PlatformBuilderSettings<App>) {
142
- return this.build<App>(module, settings).bootstrap();
143
- }
144
-
145
- callback(): (req: IncomingMessage, res: ServerResponse) => void;
146
- callback(req: IncomingMessage, res: ServerResponse): void;
147
- callback(...args: [IncomingMessage?, ServerResponse?]) {
148
- return (this.adapter.app.callback as any)(...args);
149
- }
150
-
151
- log(...data: any[]) {
152
- return !this.disableBootstrapLog && logger().info(...data, this.diff());
153
- }
154
-
155
- /**
156
- * Add classes decorated by @@Controller@@ to components container.
157
- *
158
- * ### Example
159
- *
160
- * ```typescript
161
- * @Controller('/ctrl')
162
- * class MyController{
163
- * }
164
- *
165
- * platform.addControllers('/rest', [MyController])
166
- * ```
167
- *
168
- * ::: tip
169
- * If the MyController class isn't decorated, the class will be ignored.
170
- * :::
171
- *
172
- * @param {string} endpoint
173
- * @param {any[]} controllers
174
- */
175
- public addControllers(endpoint: string, controllers: TokenProvider | TokenProvider[]) {
176
- [].concat(controllers as never[]).forEach((token: TokenProvider) => {
177
- configuration().routes.push({token, route: endpoint});
178
- });
179
- }
180
-
181
- public async runLifecycle() {
182
- // init adapter (Express, Koa, etc...)
183
- await this.adapter.onInit();
184
-
185
- setLoggerConfiguration();
186
-
187
- // create the middleware mapping to be executed to the expected hook
188
- await this.mapTokenMiddlewares();
189
-
190
- await this.loadInjector();
191
-
192
- // add the context middleware to the application
193
- this.log("Mount app context");
194
- await this.adapter.useContext();
195
-
196
- // init routes (controllers, middlewares, etc...)
197
- this.log("Load routes");
198
- await this.adapter.beforeLoadRoutes();
199
-
200
- if (this.rootModule.$beforeRoutesInit) {
201
- await this.rootModule.$beforeRoutesInit();
202
- // remove method to avoid multiple call and preserve hook order
203
- this.rootModule.$beforeRoutesInit = () => {};
204
- }
205
-
206
- // Hooks execution (adding middlewares, controllers, services, etc...)
207
- await this.loadStatics("$beforeRoutesInit");
208
- await this.callHook("$beforeRoutesInit");
209
-
210
- const routes = configuration().get<Route[]>("routes");
211
-
212
- this.platform.addRoutes(routes);
213
-
214
- await this.callHook("$onRoutesInit");
215
-
216
- await this.loadStatics("$afterRoutesInit");
217
- await this.callHook("$afterRoutesInit");
218
-
219
- await this.adapter.afterLoadRoutes();
220
-
221
- // map routers are loaded after all hooks because it contains all added middlewares/controllers in the virtual Ts.ED layers
222
- // This step will convert all Ts.ED layers to the platform layer (Express or Koa)
223
- await this.mapRouters();
224
-
225
- // Server is bootstrapped and ready to listen
226
-
227
- return this;
228
- }
229
-
230
- async loadInjector() {
231
- this.log("Build providers");
232
- const settings = configuration();
233
-
234
- settings.set("routes", settings.get("routes").concat(resolveControllers(settings)));
235
-
236
- const container = createContainer();
237
- container.delete(this.#rootModule);
238
- container.addProvider(this.#rootModule, {
239
- type: "server:module",
240
- scope: ProviderScope.SINGLETON
241
- });
242
-
243
- await injector().load(container);
244
-
245
- this.log("Settings and injector loaded...");
246
-
247
- await this.callHook("$afterInit");
248
- }
249
-
250
- async listen(network = true) {
251
- if (!this.#promise) {
252
- await this.bootstrap();
253
- }
254
-
255
- await this.callHook("$beforeListen");
256
-
257
- if (network) {
258
- await this.listenServers();
259
- }
260
-
261
- await this.callHook("$afterListen");
262
-
263
- await this.ready();
264
- }
265
-
266
- async stop() {
267
- await destroyInjector();
268
-
269
- this.#listeners.map(closeServer);
270
- }
271
-
272
- public async ready() {
273
- const {startedAt} = this;
274
-
275
- await this.callHook("$onReady");
276
-
277
- this.log(`Started in ${new Date().getTime() - startedAt.getTime()} ms`);
278
- }
279
-
280
- async callHook(hook: string, ...args: any[]) {
281
- if (!this.disableBootstrapLog) {
282
- logger().debug(`\x1B[1mCall hook ${hook}\x1B[22m`);
283
- }
284
-
285
- // Load middlewares for the given hook
286
- this.loadMiddlewaresFor(hook);
287
-
288
- // call hooks added by providers
289
- await $asyncEmit(hook, args);
290
- }
291
-
292
- loadStatics(hook: string) {
293
- const statics = constant<PlatformStaticsSettings>("statics");
294
- const app = application();
295
-
296
- getStaticsOptions(statics).forEach(({path, options}) => {
297
- if (options.hook === hook) {
298
- app.statics(path, options);
299
- }
300
- });
301
- }
302
-
303
- useProvider(token: Type<any>, settings?: Partial<ProviderOpts>) {
304
- injector().addProvider(token, settings);
305
-
306
- return this;
307
- }
308
-
309
- bootstrap(): Promise<this> {
310
- this.#promise = this.#promise || this.runLifecycle();
311
-
312
- return this.#promise;
313
- }
314
-
315
- protected mapRouters() {
316
- const layers = this.platform.getLayers();
317
-
318
- this.adapter.mapLayers(layers);
319
-
320
- const rawBody =
321
- constant("rawBody") ||
322
- layers.some(({handlers}) => {
323
- return handlers.some((handler) => handler.opts?.paramsTypes?.RAW_BODY);
324
- });
325
-
326
- this.settings.set("rawBody", rawBody);
327
-
328
- return this.logRoutes(layers.filter((layer) => layer.isProvider()));
329
- }
330
-
331
- protected diff() {
332
- const ms = colors.yellow(`+${new Date().getTime() - this.current.getTime()}ms`);
333
- this.current = new Date();
334
- return ms;
335
- }
336
-
337
- /**
338
- * Load middlewares from configuration for the given hook
339
- * @param hook
340
- * @protected
341
- */
342
- protected loadMiddlewaresFor(hook: string): void {
343
- return getMiddlewaresForHook(hook, this.settings, "$beforeRoutesInit").forEach(({use}) => {
344
- this.app.use(use);
345
- });
346
- }
347
-
348
- protected createHttpServers() {
349
- this.#servers = this.adapter.getServers();
350
- }
351
-
352
- protected async listenServers(): Promise<void> {
353
- this.#listeners = await Promise.all(this.#servers.map((cb) => cb && cb()));
354
- }
355
-
356
- protected async logRoutes(layers: PlatformLayer[]) {
357
- this.log("Routes mounted...");
358
-
359
- if (!this.settings.get("logger.disableRoutesSummary") && !this.disableBootstrapLog) {
360
- const routes: PlatformRouteDetails[] = layers.map((layer) => {
361
- return {
362
- url: layer.path,
363
- method: layer.method,
364
- name: layer.opts.name || `${layer.provider.className}.constructor()`,
365
- className: layer.opts.className || layer.provider.className,
366
- methodClassName: layer.opts.methodClassName || ""
367
- } as PlatformRouteDetails;
368
- });
369
-
370
- logger().info(printRoutes(await $asyncAlter("$logRoutes", routes)));
371
- }
372
- }
373
-
374
- protected async mapTokenMiddlewares() {
375
- let middlewares = constant<PlatformMiddlewareLoadingOptions[]>("middlewares", []);
376
- const env = constant<Env>("env");
377
- const defaultHook = "$beforeRoutesInit";
378
-
379
- const promises = middlewares.map(async (middleware: PlatformMiddlewareLoadingOptions): Promise<PlatformMiddlewareLoadingOptions> => {
380
- if (isFunction(middleware)) {
381
- return {
382
- env,
383
- hook: defaultHook,
384
- use: middleware
385
- };
386
- }
387
-
388
- if (isString(middleware)) {
389
- middleware = {env, use: middleware, hook: defaultHook};
390
- }
391
-
392
- let {use, options} = middleware;
393
-
394
- if (isString(use)) {
395
- if (["text-parser", "raw-parser", "json-parser", "urlencoded-parser"].includes(use)) {
396
- use = this.adapter.bodyParser(use.replace("-parser", ""), options);
397
- } else {
398
- const mod = await import(use);
399
- use = (mod.default || mod)(options);
400
- }
401
- }
402
-
403
- if (isClass(use) && ["$beforeInit", "$onInit", "$afterInit"].includes(middleware.hook!)) {
404
- throw new Error(
405
- `Ts.ED Middleware "${nameOf(use)}" middleware cannot be added on ${
406
- middleware.hook
407
- } hook. Use one of this hooks instead: $beforeRoutesInit, $onRoutesInit, $afterRoutesInit, $beforeListen, $afterListen, $onReady`
408
- );
409
- }
410
-
411
- return {
412
- env,
413
- hook: defaultHook,
414
- ...middleware,
415
- use
416
- };
417
- });
418
-
419
- middlewares = await Promise.all(promises);
420
-
421
- configuration().set(
422
- "middlewares",
423
- middlewares.filter((middleware) => middleware.use)
424
- );
425
- }
426
- }
@@ -1,39 +0,0 @@
1
- import {Type} from "@tsed/core";
2
-
3
- import {FakeAdapter} from "../../../testing/index.js";
4
- import {PlatformBuilder} from "../PlatformBuilder.js";
5
-
6
- export class PlatformCustom extends FakeAdapter {
7
- readonly NAME = "custom";
8
- readonly providers = [
9
- {
10
- token: class Test {}
11
- }
12
- ];
13
-
14
- static create(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {
15
- return PlatformBuilder.create<any>(module, {
16
- ...settings,
17
- adapter: PlatformCustom
18
- });
19
- }
20
-
21
- static bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {
22
- return PlatformBuilder.build(module, {
23
- ...settings,
24
- adapter: PlatformCustom
25
- }).bootstrap();
26
- }
27
-
28
- afterLoadRoutes(): Promise<any> {
29
- return Promise.resolve(undefined);
30
- }
31
-
32
- beforeLoadRoutes(): Promise<any> {
33
- return Promise.resolve(undefined);
34
- }
35
-
36
- useContext(): any {}
37
-
38
- useRouter(): any {}
39
- }
@@ -1,11 +0,0 @@
1
- import {JsonMapperGlobalOptions} from "@tsed/json-mapper";
2
-
3
- export interface PlatformJsonMapperSettings extends JsonMapperGlobalOptions {}
4
-
5
- declare global {
6
- namespace TsED {
7
- interface Configuration extends Record<string, any> {
8
- jsonMapper?: PlatformJsonMapperSettings;
9
- }
10
- }
11
- }
@@ -1,22 +0,0 @@
1
- import {
2
- PlatformMulter as M1,
3
- PlatformMulterField as M4,
4
- PlatformMulterFile as M3,
5
- PlatformMulterSettings as M2
6
- } from "@tsed/platform-multer";
7
- /**
8
- * @deprecated Since v8.5. Use PlatformMulter from @tsed/platform-multer instead.
9
- */
10
- export type PlatformMulter = M1;
11
- /**
12
- * @deprecated Since v8.5. Use PlatformMulterSettings from @tsed/platform-multer instead.
13
- */
14
- export type PlatformMulterSettings = M2;
15
- /**
16
- * @deprecated Since v8.5. Use PlatformMulterFile from @tsed/platform-multer instead.
17
- */
18
- export type PlatformMulterFile = M3;
19
- /**
20
- * @deprecated Since v8.5. Use PlatformMulterField from @tsed/platform-multer instead.
21
- */
22
- export type PlatformMulterField = M4;
@@ -1,11 +0,0 @@
1
- export interface PlatformRouterSettings {
2
- appendChildrenRoutesFirst?: boolean;
3
- }
4
-
5
- declare global {
6
- namespace TsED {
7
- interface Configuration extends Record<string, any> {
8
- router?: PlatformRouterSettings;
9
- }
10
- }
11
- }
@@ -1,23 +0,0 @@
1
- declare global {
2
- namespace TsED {
3
- // @ts-ignore
4
- export interface StaticsOptions {
5
- root: string;
6
- hook?: "$beforeRoutesInit" | "$afterRoutesInit";
7
- // @ts-ignore
8
- [key: string]: any;
9
- }
10
- }
11
- }
12
-
13
- export interface PlatformStaticsOptions extends TsED.StaticsOptions {
14
- root: string;
15
-
16
- [key: string]: any;
17
- }
18
-
19
- export type PathOrStaticsOptions = string | PlatformStaticsOptions;
20
-
21
- export interface PlatformStaticsSettings {
22
- [endpoint: string]: PathOrStaticsOptions | PathOrStaticsOptions[];
23
- }