@tramvai/module-server 1.84.2 → 1.90.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.
- package/lib/server/error.d.ts +6 -3
- package/lib/server/express-compatibility.d.ts +25 -0
- package/lib/server/server.d.ts +2 -4
- package/lib/server/webApp.d.ts +25 -9
- package/lib/server/xHeaders.d.ts +2 -2
- package/lib/server.es.js +265 -96
- package/lib/server.js +276 -102
- package/package.json +18 -14
package/lib/server.es.js
CHANGED
|
@@ -2,14 +2,20 @@ import { __decorate } from 'tslib';
|
|
|
2
2
|
import { Scope, APP_INFO_TOKEN, Module, provide, DI_TOKEN, COMMAND_LINE_RUNNER_TOKEN, commandLineListTokens } from '@tramvai/core';
|
|
3
3
|
import { SERVER_MODULE_PAPI_PUBLIC_ROUTE, SERVER_MODULE_PAPI_PUBLIC_URL, SERVER_MODULE_PAPI_PRIVATE_URL, WEB_APP_BEFORE_INIT_TOKEN, WEB_APP_TOKEN, SERVER_MODULE_PAPI_PRIVATE_ROUTE, SERVER_MODULE_STATICS_OPTIONS, SERVER_TOKEN, READINESS_PROBE_TOKEN, LIVENESS_PROBE_TOKEN, SPECIAL_SERVER_PATHS, PROXY_CONFIG_TOKEN, DEPENDENCIES_VERSION_FILTER_TOKEN, WEB_APP_INIT_TOKEN, WEB_APP_AFTER_INIT_TOKEN, WEB_APP_LIMITER_TOKEN } from '@tramvai/tokens-server';
|
|
4
4
|
export * from '@tramvai/tokens-server';
|
|
5
|
-
import {
|
|
5
|
+
import { WEB_FASTIFY_APP_BEFORE_INIT_TOKEN, WEB_FASTIFY_APP_TOKEN, WEB_FASTIFY_APP_INIT_TOKEN, WEB_FASTIFY_APP_AFTER_INIT_TOKEN, WEB_FASTIFY_APP_LIMITER_TOKEN, WEB_FASTIFY_APP_BEFORE_ERROR_TOKEN, WEB_FASTIFY_APP_PROCESS_ERROR_TOKEN, WEB_FASTIFY_APP_AFTER_ERROR_TOKEN } from '@tramvai/tokens-server-private';
|
|
6
|
+
import { REQUEST, RESPONSE, FASTIFY_REQUEST, FASTIFY_RESPONSE, RESPONSE_MANAGER_TOKEN, LOGGER_TOKEN, ENV_MANAGER_TOKEN, ENV_USED_TOKEN } from '@tramvai/tokens-common';
|
|
6
7
|
import { MetricsModule } from '@tramvai/module-metrics';
|
|
7
8
|
import { CacheWarmupModule } from '@tramvai/module-cache-warmup';
|
|
8
9
|
import http from 'http';
|
|
10
|
+
import fastify from 'fastify';
|
|
9
11
|
import express from 'express';
|
|
10
12
|
import cookieParser from 'cookie-parser';
|
|
11
13
|
import bodyParser from 'body-parser';
|
|
12
|
-
import
|
|
14
|
+
import { fastifyCookie } from 'fastify-cookie';
|
|
15
|
+
import fastifyFormBody from 'fastify-formbody';
|
|
16
|
+
import fp from 'fastify-plugin';
|
|
17
|
+
import symbols from 'fastify/lib/symbols';
|
|
18
|
+
import isNil from '@tinkoff/utils/is/nil';
|
|
13
19
|
import { isRedirectFoundError, isNotFoundError, isHttpError } from '@tinkoff/errors';
|
|
14
20
|
import zlib from 'zlib';
|
|
15
21
|
import compression from 'compression';
|
|
@@ -17,24 +23,22 @@ import os from 'os';
|
|
|
17
23
|
import filterObj from '@tinkoff/utils/object/filter';
|
|
18
24
|
import flatten from '@tinkoff/utils/array/flatten';
|
|
19
25
|
import toArray from '@tinkoff/utils/array/toArray';
|
|
20
|
-
import { REQUEST as REQUEST$1, RESPONSE as RESPONSE$1, LOGGER_TOKEN, ENV_MANAGER_TOKEN } from '@tramvai/tokens-common';
|
|
21
26
|
import { create, middlewares, getPapiParameters, createPapiMethod } from '@tramvai/papi';
|
|
22
27
|
import { createChildContainer } from '@tinkoff/dippy';
|
|
23
28
|
import eachObj from '@tinkoff/utils/object/each';
|
|
24
|
-
import {
|
|
29
|
+
import { resolve } from 'path';
|
|
30
|
+
import FastifyStatic from 'fastify-static';
|
|
31
|
+
import { createTerminus } from '@tinkoff/terminus';
|
|
25
32
|
import { parse } from '@tinkoff/url';
|
|
26
33
|
import { EventEmitter } from 'events';
|
|
27
34
|
import monkeypatch from '@tinkoff/monkeypatch';
|
|
28
35
|
import https from 'https';
|
|
29
36
|
import isArray from '@tinkoff/utils/is/array';
|
|
30
37
|
import isObject from '@tinkoff/utils/is/object';
|
|
31
|
-
import { resolve } from 'path';
|
|
32
38
|
import { createProxyMiddleware } from 'http-proxy-middleware';
|
|
33
39
|
|
|
34
|
-
const serverFactory = (
|
|
35
|
-
|
|
36
|
-
server.on('request', webApp);
|
|
37
|
-
return server;
|
|
40
|
+
const serverFactory = () => {
|
|
41
|
+
return http.createServer();
|
|
38
42
|
};
|
|
39
43
|
const serverListenCommand = ({ server, logger, envManager, }) => {
|
|
40
44
|
const log = logger('server');
|
|
@@ -47,88 +51,220 @@ const serverListenCommand = ({ server, logger, envManager, }) => {
|
|
|
47
51
|
};
|
|
48
52
|
};
|
|
49
53
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Fork of https://github.com/fastify/fastify-express
|
|
56
|
+
*/
|
|
57
|
+
const kMiddlewares = Symbol('fastify-express-middlewares');
|
|
58
|
+
const expressPlugin = (fastify, options, next) => {
|
|
59
|
+
var _a, _b;
|
|
60
|
+
fastify.decorate('use', use);
|
|
61
|
+
// eslint-disable-next-line no-param-reassign
|
|
62
|
+
fastify[kMiddlewares] = [];
|
|
63
|
+
fastify.decorate('express', (_b = (_a = options.express) === null || _a === void 0 ? void 0 : _a.instance) !== null && _b !== void 0 ? _b : express());
|
|
64
|
+
fastify.express.disable('x-powered-by');
|
|
65
|
+
fastify
|
|
66
|
+
.addHook('onRequest', enhanceRequest)
|
|
67
|
+
.addHook('onRequest', runConnect)
|
|
68
|
+
.addHook('onRegister', onRegister);
|
|
69
|
+
function use(path, fn) {
|
|
70
|
+
if (typeof path === 'string') {
|
|
71
|
+
const prefix = this[symbols.kRoutePrefix];
|
|
72
|
+
// eslint-disable-next-line no-param-reassign
|
|
73
|
+
path = prefix + (path === '/' && prefix.length > 0 ? '' : path);
|
|
55
74
|
}
|
|
56
|
-
|
|
57
|
-
|
|
75
|
+
this[kMiddlewares].push([path, fn]);
|
|
76
|
+
if (fn == null) {
|
|
77
|
+
this.express.use(path);
|
|
58
78
|
}
|
|
59
|
-
|
|
60
|
-
|
|
79
|
+
else {
|
|
80
|
+
this.express.use(path, fn);
|
|
81
|
+
}
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
function enhanceRequest(req, reply, next) {
|
|
85
|
+
req.raw.originalUrl = req.raw.url;
|
|
86
|
+
req.raw.id = req.id;
|
|
87
|
+
req.raw.hostname = req.hostname;
|
|
88
|
+
req.raw.ip = req.ip;
|
|
89
|
+
req.raw.ips = req.ips;
|
|
90
|
+
req.raw.log = req.log;
|
|
91
|
+
// eslint-disable-next-line no-param-reassign
|
|
92
|
+
reply.raw.log = req.log;
|
|
93
|
+
const originalProtocol = req.raw.protocol;
|
|
94
|
+
// Make it lazy as it does a bit of work
|
|
95
|
+
Object.defineProperty(req.raw, 'protocol', {
|
|
96
|
+
get() {
|
|
97
|
+
// added in Fastify@3.5, so handle it missing
|
|
98
|
+
return req.protocol || originalProtocol;
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
next();
|
|
102
|
+
}
|
|
103
|
+
function runConnect(req, reply, next) {
|
|
104
|
+
if (this[kMiddlewares].length > 0) {
|
|
105
|
+
for (const [headerName, headerValue] of Object.entries(reply.getHeaders())) {
|
|
106
|
+
reply.raw.setHeader(headerName, headerValue);
|
|
107
|
+
}
|
|
108
|
+
this.express(req.raw, reply.raw, next);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
next();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function onRegister(instance) {
|
|
115
|
+
const middlewares = instance[kMiddlewares].slice();
|
|
116
|
+
// eslint-disable-next-line no-param-reassign
|
|
117
|
+
instance[kMiddlewares] = [];
|
|
118
|
+
instance.decorate('express', express());
|
|
119
|
+
instance.express.disable('x-powered-by');
|
|
120
|
+
instance.decorate('use', use);
|
|
121
|
+
for (const middleware of middlewares) {
|
|
122
|
+
instance.use(...middleware);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
next();
|
|
61
126
|
};
|
|
62
|
-
const
|
|
63
|
-
|
|
127
|
+
const fastifyExpressCompatibility = fp(expressPlugin, {
|
|
128
|
+
fastify: '>=3.0.0',
|
|
129
|
+
name: 'fastify-express',
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const errorHandler = (app, { log, beforeError, processError, afterError, }) => {
|
|
133
|
+
app.setErrorHandler(async (error, request, reply) => {
|
|
134
|
+
const runHandlers = async (handlers) => {
|
|
135
|
+
if (handlers) {
|
|
136
|
+
for (const handler of handlers) {
|
|
137
|
+
const result = await handler(error, request, reply);
|
|
138
|
+
if (result) {
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
64
144
|
const requestInfo = {
|
|
65
|
-
ip:
|
|
66
|
-
requestId:
|
|
67
|
-
url:
|
|
145
|
+
ip: request.ip,
|
|
146
|
+
requestId: request.headers['x-request-id'],
|
|
147
|
+
url: request.url,
|
|
68
148
|
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
149
|
+
const beforeErrorResult = await runHandlers(beforeError);
|
|
150
|
+
if (!isNil(beforeErrorResult)) {
|
|
151
|
+
return beforeErrorResult;
|
|
152
|
+
}
|
|
153
|
+
if (isRedirectFoundError(error)) {
|
|
154
|
+
reply.header('cache-control', 'no-cache, no-store, must-revalidate');
|
|
155
|
+
reply.redirect(error.httpStatus || 307, error.nextUrl);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (isNotFoundError(error)) {
|
|
159
|
+
reply.status(404);
|
|
160
|
+
return '';
|
|
161
|
+
}
|
|
162
|
+
const processErrorResult = await runHandlers(processError);
|
|
163
|
+
if (!isNil(processErrorResult)) {
|
|
164
|
+
return processErrorResult;
|
|
165
|
+
}
|
|
166
|
+
if (isHttpError(error)) {
|
|
167
|
+
if (error.httpStatus >= 500) {
|
|
168
|
+
log.error({ event: 'send-server-error', error, requestInfo });
|
|
72
169
|
}
|
|
73
|
-
|
|
170
|
+
reply.status(error.httpStatus);
|
|
171
|
+
return '';
|
|
172
|
+
}
|
|
173
|
+
log.error({ event: 'send-server-error', error, requestInfo });
|
|
174
|
+
const afterErrorResult = await runHandlers(afterError);
|
|
175
|
+
if (!isNil(afterErrorResult)) {
|
|
176
|
+
return afterErrorResult;
|
|
74
177
|
}
|
|
75
|
-
|
|
76
|
-
finalhandler(req, res)(err);
|
|
178
|
+
throw error;
|
|
77
179
|
});
|
|
78
180
|
};
|
|
79
181
|
|
|
80
|
-
const webAppFactory = () => {
|
|
182
|
+
const webAppFactory = ({ server, expressApp, }) => {
|
|
183
|
+
const app = fastify({
|
|
184
|
+
serverFactory: (handler) => {
|
|
185
|
+
server.on('request', handler);
|
|
186
|
+
return server;
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
return app;
|
|
190
|
+
};
|
|
191
|
+
const webAppExpressFactory = ({ webApp }) => {
|
|
81
192
|
const app = express();
|
|
82
193
|
app.disable('etag');
|
|
83
194
|
app.disable('x-powered-by');
|
|
84
195
|
return app;
|
|
85
196
|
};
|
|
86
|
-
const webAppInitCommand = ({ app, logger, commandLineRunner, beforeInit, init, afterInit, limiterRequest, }) => {
|
|
197
|
+
const webAppInitCommand = ({ app, expressApp, logger, commandLineRunner, beforeInit, init, afterInit, limiterRequest, expressBeforeInit, expressInit, expressAfterInit, expressLimiterRequest, beforeError, processError, afterError, }) => {
|
|
87
198
|
const log = logger('server:webapp');
|
|
88
|
-
const runHandlers = (handlers) => {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
199
|
+
const runHandlers = (handlers, expressHandlers) => {
|
|
200
|
+
return Promise.all([
|
|
201
|
+
handlers && Promise.all(handlers.map((handler) => handler(app))),
|
|
202
|
+
expressHandlers && Promise.all(expressHandlers.map((handler) => handler(expressApp))),
|
|
203
|
+
]);
|
|
92
204
|
};
|
|
93
205
|
return async function webAppInit() {
|
|
94
|
-
|
|
95
|
-
await
|
|
96
|
-
|
|
206
|
+
errorHandler(app, { log, beforeError, processError, afterError });
|
|
207
|
+
await app.register(fastifyExpressCompatibility, {
|
|
208
|
+
express: {
|
|
209
|
+
instance: expressApp,
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
await runHandlers(beforeInit, expressBeforeInit);
|
|
213
|
+
await runHandlers(limiterRequest, expressLimiterRequest);
|
|
214
|
+
await app.register(fastifyCookie);
|
|
215
|
+
await app.register(fastifyFormBody);
|
|
216
|
+
expressApp.use(bodyParser.urlencoded({
|
|
97
217
|
limit: '2mb',
|
|
98
218
|
extended: false,
|
|
99
219
|
}), cookieParser());
|
|
100
|
-
await runHandlers(init);
|
|
101
|
-
|
|
220
|
+
await runHandlers(init, expressInit);
|
|
221
|
+
// force express to execute to update server's request and response instances
|
|
222
|
+
app.use((req, res, next) => {
|
|
223
|
+
next();
|
|
224
|
+
});
|
|
225
|
+
app.all('*', async (request, reply) => {
|
|
102
226
|
try {
|
|
103
227
|
log.debug({
|
|
104
228
|
event: 'start:request',
|
|
105
229
|
message: 'Клиент зашел на страницу',
|
|
106
|
-
url:
|
|
230
|
+
url: request.url,
|
|
107
231
|
});
|
|
108
232
|
const di = await commandLineRunner.run('server', 'customer', [
|
|
109
233
|
{
|
|
110
234
|
provide: REQUEST,
|
|
111
235
|
scope: Scope.REQUEST,
|
|
112
|
-
useValue:
|
|
236
|
+
useValue: request.raw,
|
|
113
237
|
},
|
|
114
238
|
{
|
|
115
239
|
provide: RESPONSE,
|
|
116
240
|
scope: Scope.REQUEST,
|
|
117
|
-
useValue:
|
|
241
|
+
useValue: reply.raw,
|
|
242
|
+
},
|
|
243
|
+
// TODO: перевести использование на новые
|
|
244
|
+
// TODO: добавить для papi
|
|
245
|
+
{
|
|
246
|
+
provide: FASTIFY_REQUEST,
|
|
247
|
+
scope: Scope.REQUEST,
|
|
248
|
+
useValue: request,
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
provide: FASTIFY_RESPONSE,
|
|
252
|
+
scope: Scope.REQUEST,
|
|
253
|
+
useValue: reply,
|
|
118
254
|
},
|
|
119
255
|
]);
|
|
120
256
|
const responseManager = di.get(RESPONSE_MANAGER_TOKEN);
|
|
121
|
-
if (
|
|
257
|
+
if (reply.sent) {
|
|
122
258
|
log.debug({
|
|
123
259
|
event: 'response-ended',
|
|
124
260
|
message: 'Response was already ended.',
|
|
125
|
-
url:
|
|
261
|
+
url: request.url,
|
|
126
262
|
});
|
|
127
263
|
}
|
|
128
264
|
else {
|
|
129
|
-
|
|
130
|
-
.
|
|
131
|
-
.
|
|
265
|
+
reply
|
|
266
|
+
.header('content-type', 'text/html')
|
|
267
|
+
.headers(responseManager.getHeaders())
|
|
132
268
|
.status(responseManager.getStatus())
|
|
133
269
|
.send(responseManager.getBody());
|
|
134
270
|
}
|
|
@@ -136,16 +272,15 @@ const webAppInitCommand = ({ app, logger, commandLineRunner, beforeInit, init, a
|
|
|
136
272
|
catch (err) {
|
|
137
273
|
if (err.di) {
|
|
138
274
|
const responseManager = err.di.get(RESPONSE_MANAGER_TOKEN);
|
|
139
|
-
if (responseManager && !
|
|
140
|
-
|
|
275
|
+
if (responseManager && !reply.sent) {
|
|
276
|
+
reply.headers(responseManager.getHeaders());
|
|
141
277
|
}
|
|
142
278
|
}
|
|
143
|
-
|
|
279
|
+
throw err;
|
|
144
280
|
}
|
|
145
281
|
});
|
|
146
|
-
|
|
147
|
-
await
|
|
148
|
-
errorHandler(app, { log });
|
|
282
|
+
await runHandlers(afterInit, expressAfterInit);
|
|
283
|
+
await app.ready();
|
|
149
284
|
};
|
|
150
285
|
};
|
|
151
286
|
|
|
@@ -189,9 +324,8 @@ const xHeadersFactory = ({ app, envManager, appInfo, }) => {
|
|
|
189
324
|
'x-deploy-repository': envManager.get('DEPLOY_REPOSITORY'),
|
|
190
325
|
});
|
|
191
326
|
return async () => {
|
|
192
|
-
app.
|
|
193
|
-
|
|
194
|
-
next();
|
|
327
|
+
app.addHook('preHandler', async (_, reply) => {
|
|
328
|
+
reply.headers(xHeaders);
|
|
195
329
|
});
|
|
196
330
|
};
|
|
197
331
|
};
|
|
@@ -213,11 +347,11 @@ function createApi(papiList, { di, logger }) {
|
|
|
213
347
|
var _a;
|
|
214
348
|
const childDI = createChildContainer(di);
|
|
215
349
|
childDI.register({
|
|
216
|
-
provide: REQUEST
|
|
350
|
+
provide: REQUEST,
|
|
217
351
|
useValue: req,
|
|
218
352
|
});
|
|
219
353
|
childDI.register({
|
|
220
|
-
provide: RESPONSE
|
|
354
|
+
provide: RESPONSE,
|
|
221
355
|
useValue: res,
|
|
222
356
|
});
|
|
223
357
|
return { ...rootDeps, ...childDI.getOfDeps((_a = papi.deps) !== null && _a !== void 0 ? _a : {}), req, res };
|
|
@@ -235,10 +369,20 @@ try {
|
|
|
235
369
|
Api = require('@tramvai/cli/lib/external/api').default; // eslint-disable-line import/no-unresolved
|
|
236
370
|
}
|
|
237
371
|
catch (e) { }
|
|
238
|
-
const getFileApi = () => {
|
|
372
|
+
const getFileApi = ({ logger }) => {
|
|
373
|
+
const log = logger('papi:fileApi');
|
|
239
374
|
const result = [];
|
|
240
375
|
eachObj((v, k) => {
|
|
241
376
|
const handler = (v.handler || v.default);
|
|
377
|
+
if (!handler) {
|
|
378
|
+
log.error({
|
|
379
|
+
message: `Cannot resolve a papi handler.
|
|
380
|
+
Check that you are using file based papi right way by docs https://tramvai.dev/docs/how-to/how-create-papi#automatic-handler-creation
|
|
381
|
+
In case you have not added any file papi handler, consider renaming directory ./src/api (by default) to the other name to resolve conflicts with papi, or
|
|
382
|
+
change settings application.commands.build.options.serverApiDir in tramvai.json`,
|
|
383
|
+
});
|
|
384
|
+
throw new Error('Not a papi');
|
|
385
|
+
}
|
|
242
386
|
const papiParameters = getPapiParameters(handler);
|
|
243
387
|
result.push(createPapiMethod({
|
|
244
388
|
...v,
|
|
@@ -252,7 +396,10 @@ const getFileApi = () => {
|
|
|
252
396
|
const fileApiProvider = {
|
|
253
397
|
provide: SERVER_MODULE_PAPI_PUBLIC_ROUTE,
|
|
254
398
|
multi: true,
|
|
255
|
-
|
|
399
|
+
useFactory: getFileApi,
|
|
400
|
+
deps: {
|
|
401
|
+
logger: LOGGER_TOKEN,
|
|
402
|
+
},
|
|
256
403
|
};
|
|
257
404
|
|
|
258
405
|
const sharedProviders = [
|
|
@@ -387,29 +534,32 @@ let ServerStaticsModule = class ServerStaticsModule {
|
|
|
387
534
|
ServerStaticsModule = __decorate([
|
|
388
535
|
Module({
|
|
389
536
|
providers: [
|
|
390
|
-
{
|
|
391
|
-
provide:
|
|
537
|
+
provide({
|
|
538
|
+
provide: WEB_FASTIFY_APP_BEFORE_INIT_TOKEN,
|
|
392
539
|
useFactory: ({ app, options }) => {
|
|
393
540
|
const path = (options === null || options === void 0 ? void 0 : options.path) || 'public';
|
|
394
541
|
return () => {
|
|
395
|
-
app.
|
|
542
|
+
app.register(FastifyStatic, {
|
|
543
|
+
decorateReply: false,
|
|
544
|
+
prefix: `/${path}`,
|
|
545
|
+
root: resolve(process.cwd(), path),
|
|
396
546
|
setHeaders: (res) => {
|
|
397
547
|
const oneYearForward = new Date(Date.now() + ONE_YEAR * 1000);
|
|
398
|
-
res.
|
|
399
|
-
res.
|
|
548
|
+
res.setHeader('cache-control', `public, max-age=${ONE_YEAR}`);
|
|
549
|
+
res.setHeader('expires', oneYearForward.toUTCString());
|
|
400
550
|
},
|
|
401
|
-
})
|
|
551
|
+
});
|
|
402
552
|
};
|
|
403
553
|
},
|
|
404
554
|
deps: {
|
|
405
|
-
app:
|
|
555
|
+
app: WEB_FASTIFY_APP_TOKEN,
|
|
406
556
|
options: {
|
|
407
557
|
token: SERVER_MODULE_STATICS_OPTIONS,
|
|
408
558
|
optional: true,
|
|
409
559
|
},
|
|
410
560
|
},
|
|
411
561
|
multi: true,
|
|
412
|
-
},
|
|
562
|
+
}),
|
|
413
563
|
],
|
|
414
564
|
})
|
|
415
565
|
], ServerStaticsModule);
|
|
@@ -425,9 +575,9 @@ ServerGracefulShutdownModule = __decorate([
|
|
|
425
575
|
Module({
|
|
426
576
|
providers: [
|
|
427
577
|
{
|
|
428
|
-
provide:
|
|
578
|
+
provide: WEB_FASTIFY_APP_BEFORE_INIT_TOKEN,
|
|
429
579
|
multi: true,
|
|
430
|
-
useFactory: ({
|
|
580
|
+
useFactory: ({ app, server, logger, commandLineRunner, livenessProbe, readinessProbe, }) => {
|
|
431
581
|
const log = logger('server');
|
|
432
582
|
return function serverListen() {
|
|
433
583
|
createTerminus(server, app, {
|
|
@@ -470,8 +620,8 @@ ServerGracefulShutdownModule = __decorate([
|
|
|
470
620
|
};
|
|
471
621
|
},
|
|
472
622
|
deps: {
|
|
623
|
+
app: WEB_FASTIFY_APP_TOKEN,
|
|
473
624
|
server: SERVER_TOKEN,
|
|
474
|
-
app: WEB_APP_TOKEN,
|
|
475
625
|
logger: LOGGER_TOKEN,
|
|
476
626
|
commandLineRunner: COMMAND_LINE_RUNNER_TOKEN,
|
|
477
627
|
readinessProbe: { token: READINESS_PROBE_TOKEN, optional: true },
|
|
@@ -634,6 +784,8 @@ ServerProxyModule = __decorate([
|
|
|
634
784
|
Module({
|
|
635
785
|
providers: [
|
|
636
786
|
{
|
|
787
|
+
// TODO: tramvai@2 migrate to `fastify` and `fastify-http-proxy`
|
|
788
|
+
// interfaces for the proxies are not compatible so some migration from the app is needed
|
|
637
789
|
provide: WEB_APP_BEFORE_INIT_TOKEN,
|
|
638
790
|
useFactory: ({ app, defaultProxies }) => {
|
|
639
791
|
return () => {
|
|
@@ -737,31 +889,48 @@ ServerModule = __decorate([
|
|
|
737
889
|
process.env.NODE_ENV !== 'production' && DebugHttpRequestsModule,
|
|
738
890
|
].filter(Boolean),
|
|
739
891
|
providers: [
|
|
740
|
-
{
|
|
741
|
-
provide:
|
|
892
|
+
provide({
|
|
893
|
+
provide: SERVER_TOKEN,
|
|
742
894
|
scope: Scope.SINGLETON,
|
|
895
|
+
useFactory: serverFactory,
|
|
896
|
+
}),
|
|
897
|
+
provide({
|
|
898
|
+
provide: WEB_FASTIFY_APP_TOKEN,
|
|
743
899
|
useFactory: webAppFactory,
|
|
744
|
-
|
|
900
|
+
scope: Scope.SINGLETON,
|
|
901
|
+
deps: {
|
|
902
|
+
server: SERVER_TOKEN,
|
|
903
|
+
},
|
|
904
|
+
}),
|
|
905
|
+
provide({
|
|
906
|
+
// BACKWARD: provide the express app as before
|
|
907
|
+
provide: WEB_APP_TOKEN,
|
|
908
|
+
scope: Scope.SINGLETON,
|
|
909
|
+
useFactory: webAppExpressFactory,
|
|
910
|
+
deps: {
|
|
911
|
+
webApp: WEB_FASTIFY_APP_TOKEN,
|
|
912
|
+
},
|
|
913
|
+
}),
|
|
745
914
|
{
|
|
746
915
|
provide: commandLineListTokens.init,
|
|
747
916
|
multi: true,
|
|
748
917
|
useFactory: webAppInitCommand,
|
|
749
918
|
deps: {
|
|
750
|
-
app:
|
|
751
|
-
|
|
919
|
+
app: WEB_FASTIFY_APP_TOKEN,
|
|
920
|
+
expressApp: WEB_APP_TOKEN,
|
|
921
|
+
logger: LOGGER_TOKEN,
|
|
752
922
|
commandLineRunner: COMMAND_LINE_RUNNER_TOKEN,
|
|
753
|
-
beforeInit: { token:
|
|
754
|
-
init: { token:
|
|
755
|
-
afterInit: { token:
|
|
756
|
-
limiterRequest: { token:
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
webApp: WEB_APP_TOKEN,
|
|
923
|
+
beforeInit: { token: WEB_FASTIFY_APP_BEFORE_INIT_TOKEN, optional: true },
|
|
924
|
+
init: { token: WEB_FASTIFY_APP_INIT_TOKEN, optional: true },
|
|
925
|
+
afterInit: { token: WEB_FASTIFY_APP_AFTER_INIT_TOKEN, optional: true },
|
|
926
|
+
limiterRequest: { token: WEB_FASTIFY_APP_LIMITER_TOKEN, optional: true },
|
|
927
|
+
expressBeforeInit: { token: WEB_APP_BEFORE_INIT_TOKEN, optional: true },
|
|
928
|
+
expressInit: { token: WEB_APP_INIT_TOKEN, optional: true },
|
|
929
|
+
expressAfterInit: { token: WEB_APP_AFTER_INIT_TOKEN, optional: true },
|
|
930
|
+
expressLimiterRequest: { token: WEB_APP_LIMITER_TOKEN, optional: true },
|
|
931
|
+
beforeError: { token: WEB_FASTIFY_APP_BEFORE_ERROR_TOKEN, optional: true },
|
|
932
|
+
processError: { token: WEB_FASTIFY_APP_PROCESS_ERROR_TOKEN, optional: true },
|
|
933
|
+
afterError: { token: WEB_FASTIFY_APP_AFTER_ERROR_TOKEN, optional: true },
|
|
765
934
|
},
|
|
766
935
|
},
|
|
767
936
|
{
|
|
@@ -770,8 +939,8 @@ ServerModule = __decorate([
|
|
|
770
939
|
useFactory: serverListenCommand,
|
|
771
940
|
deps: {
|
|
772
941
|
server: SERVER_TOKEN,
|
|
773
|
-
logger: LOGGER_TOKEN
|
|
774
|
-
envManager: ENV_MANAGER_TOKEN
|
|
942
|
+
logger: LOGGER_TOKEN,
|
|
943
|
+
envManager: ENV_MANAGER_TOKEN,
|
|
775
944
|
},
|
|
776
945
|
},
|
|
777
946
|
{
|
|
@@ -779,8 +948,8 @@ ServerModule = __decorate([
|
|
|
779
948
|
multi: true,
|
|
780
949
|
useFactory: staticAppCommand,
|
|
781
950
|
deps: {
|
|
782
|
-
logger: LOGGER_TOKEN
|
|
783
|
-
envManager: ENV_MANAGER_TOKEN
|
|
951
|
+
logger: LOGGER_TOKEN,
|
|
952
|
+
envManager: ENV_MANAGER_TOKEN,
|
|
784
953
|
appInfo: APP_INFO_TOKEN,
|
|
785
954
|
},
|
|
786
955
|
},
|
|
@@ -805,12 +974,12 @@ ServerModule = __decorate([
|
|
|
805
974
|
],
|
|
806
975
|
},
|
|
807
976
|
{
|
|
808
|
-
provide:
|
|
977
|
+
provide: WEB_FASTIFY_APP_INIT_TOKEN,
|
|
809
978
|
multi: true,
|
|
810
979
|
useFactory: xHeadersFactory,
|
|
811
980
|
deps: {
|
|
812
|
-
app:
|
|
813
|
-
envManager: ENV_MANAGER_TOKEN
|
|
981
|
+
app: WEB_FASTIFY_APP_TOKEN,
|
|
982
|
+
envManager: ENV_MANAGER_TOKEN,
|
|
814
983
|
appInfo: APP_INFO_TOKEN,
|
|
815
984
|
},
|
|
816
985
|
},
|