@tramvai/module-server 2.31.0 → 2.32.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/lib/modules/papi/server/executor.d.ts +1 -2
- package/lib/server/webApp.d.ts +3 -11
- package/lib/server.es.js +43 -182
- package/lib/server.js +47 -190
- package/package.json +17 -20
- package/lib/server/express-compatibility.d.ts +0 -24
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export declare const papiExecutorProvider: import("@tinkoff/dippy").Provider<{
|
|
2
2
|
di: import("@tinkoff/dippy").BaseTokenInterface<import("@tinkoff/dippy").Container>;
|
|
3
3
|
logger: import("@tinkoff/dippy").BaseTokenInterface<import("@tramvai/tokens-common").LoggerFactory>;
|
|
4
|
-
fastifyRequest: import("@tinkoff/dippy").BaseTokenInterface<import("fastify").FastifyRequest<import("fastify
|
|
4
|
+
fastifyRequest: import("@tinkoff/dippy").BaseTokenInterface<import("fastify").FastifyRequest<import("fastify").RouteGenericInterface, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, import("fastify").RouteGenericInterface>> & {
|
|
5
5
|
cookies: Record<string, string>;
|
|
6
|
-
query: Record<string, string>;
|
|
7
6
|
}>;
|
|
8
7
|
requestManager: import("@tinkoff/dippy").BaseTokenInterface<import("@tramvai/tokens-common").RequestManager>;
|
|
9
8
|
responseManager: import("@tinkoff/dippy").BaseTokenInterface<import("@tramvai/tokens-common").ResponseManager>;
|
package/lib/server/webApp.d.ts
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
import type { EXECUTION_CONTEXT_MANAGER_TOKEN, LOGGER_TOKEN } from '@tramvai/tokens-common';
|
|
2
2
|
import type { COMMAND_LINE_RUNNER_TOKEN } from '@tramvai/core';
|
|
3
|
-
import type {
|
|
3
|
+
import type { SERVER_TOKEN } from '@tramvai/tokens-server';
|
|
4
4
|
import type { WEB_FASTIFY_APP_TOKEN, WEB_FASTIFY_APP_AFTER_INIT_TOKEN, WEB_FASTIFY_APP_BEFORE_INIT_TOKEN, WEB_FASTIFY_APP_INIT_TOKEN, WEB_FASTIFY_APP_LIMITER_TOKEN, WEB_FASTIFY_APP_BEFORE_ERROR_TOKEN, WEB_FASTIFY_APP_AFTER_ERROR_TOKEN, WEB_FASTIFY_APP_PROCESS_ERROR_TOKEN, WEB_FASTIFY_APP_METRICS_TOKEN } from '@tramvai/tokens-server-private';
|
|
5
5
|
import type { ExtractDependencyType } from '@tinkoff/dippy';
|
|
6
6
|
export declare const webAppFactory: ({ server }: {
|
|
7
7
|
server: typeof SERVER_TOKEN;
|
|
8
|
-
}) => import("fastify").FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").
|
|
9
|
-
export declare const
|
|
10
|
-
webApp: typeof WEB_FASTIFY_APP_TOKEN;
|
|
11
|
-
}) => import("express-serve-static-core").Express;
|
|
12
|
-
export declare const webAppInitCommand: ({ app, expressApp, logger, commandLineRunner, executionContextManager, beforeInit, requestMetrics, limiterRequest, init, afterInit, expressBeforeInit, expressInit, expressAfterInit, expressLimiterRequest, beforeError, processError, afterError, }: {
|
|
8
|
+
}) => import("fastify").FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<import("fastify").FastifyInstance<import("http").Server<typeof import("http").IncomingMessage, typeof import("http").ServerResponse>, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>>;
|
|
9
|
+
export declare const webAppInitCommand: ({ app, logger, commandLineRunner, executionContextManager, beforeInit, requestMetrics, limiterRequest, init, afterInit, beforeError, processError, afterError, }: {
|
|
13
10
|
app: ExtractDependencyType<typeof WEB_FASTIFY_APP_TOKEN>;
|
|
14
|
-
expressApp: ExtractDependencyType<typeof WEB_APP_TOKEN>;
|
|
15
11
|
logger: ExtractDependencyType<typeof LOGGER_TOKEN>;
|
|
16
12
|
commandLineRunner: ExtractDependencyType<typeof COMMAND_LINE_RUNNER_TOKEN>;
|
|
17
13
|
executionContextManager: ExtractDependencyType<typeof EXECUTION_CONTEXT_MANAGER_TOKEN>;
|
|
@@ -20,10 +16,6 @@ export declare const webAppInitCommand: ({ app, expressApp, logger, commandLineR
|
|
|
20
16
|
limiterRequest: ExtractDependencyType<typeof WEB_FASTIFY_APP_LIMITER_TOKEN>;
|
|
21
17
|
init: ExtractDependencyType<typeof WEB_FASTIFY_APP_INIT_TOKEN>;
|
|
22
18
|
afterInit: ExtractDependencyType<typeof WEB_FASTIFY_APP_AFTER_INIT_TOKEN>;
|
|
23
|
-
expressBeforeInit: ExtractDependencyType<typeof WEB_APP_BEFORE_INIT_TOKEN>;
|
|
24
|
-
expressInit: ExtractDependencyType<typeof WEB_APP_INIT_TOKEN>;
|
|
25
|
-
expressAfterInit: ExtractDependencyType<typeof WEB_APP_AFTER_INIT_TOKEN>;
|
|
26
|
-
expressLimiterRequest: ExtractDependencyType<typeof WEB_APP_LIMITER_TOKEN>;
|
|
27
19
|
beforeError: ExtractDependencyType<typeof WEB_FASTIFY_APP_BEFORE_ERROR_TOKEN>;
|
|
28
20
|
processError: ExtractDependencyType<typeof WEB_FASTIFY_APP_PROCESS_ERROR_TOKEN>;
|
|
29
21
|
afterError: ExtractDependencyType<typeof WEB_FASTIFY_APP_AFTER_ERROR_TOKEN>;
|
package/lib/server.es.js
CHANGED
|
@@ -2,24 +2,21 @@ import { __decorate } from 'tslib';
|
|
|
2
2
|
import { setDefaultResultOrder } from 'dns';
|
|
3
3
|
import EventEmitter$1, { EventEmitter } from 'events';
|
|
4
4
|
import { Scope, APP_INFO_TOKEN, Module, provide as provide$1, DI_TOKEN as DI_TOKEN$1, COMMAND_LINE_RUNNER_TOKEN, commandLineListTokens, createToken } from '@tramvai/core';
|
|
5
|
-
import { SERVER_MODULE_PAPI_PUBLIC_ROUTE, SERVER_MODULE_PAPI_PUBLIC_URL, SERVER_MODULE_PAPI_PRIVATE_URL, SERVER_MODULE_PAPI_PRIVATE_ROUTE, SERVER_MODULE_STATICS_OPTIONS, SERVER_TOKEN, READINESS_PROBE_TOKEN, LIVENESS_PROBE_TOKEN, UTILITY_SERVER_PATHS,
|
|
5
|
+
import { SERVER_MODULE_PAPI_PUBLIC_ROUTE, SERVER_MODULE_PAPI_PUBLIC_URL, SERVER_MODULE_PAPI_PRIVATE_URL, SERVER_MODULE_PAPI_PRIVATE_ROUTE, SERVER_MODULE_STATICS_OPTIONS, SERVER_TOKEN, READINESS_PROBE_TOKEN, LIVENESS_PROBE_TOKEN, UTILITY_SERVER_PATHS, PROXY_CONFIG_TOKEN, DEPENDENCIES_VERSION_FILTER_TOKEN, UTILITY_SERVER_PORT_TOKEN } from '@tramvai/tokens-server';
|
|
6
6
|
export * from '@tramvai/tokens-server';
|
|
7
|
-
import { PAPI_EXECUTOR, WEB_FASTIFY_APP_BEFORE_INIT_TOKEN, UTILITY_WEB_FASTIFY_APP_TOKEN, UTILITY_SERVER_TOKEN, SERVER_FACTORY_TOKEN, WEB_FASTIFY_APP_TOKEN, WEB_FASTIFY_APP_FACTORY_TOKEN,
|
|
8
|
-
import { ROOT_EXECUTION_CONTEXT_TOKEN,
|
|
7
|
+
import { FASTIFY_REQUEST, FASTIFY_RESPONSE, PAPI_EXECUTOR, WEB_FASTIFY_APP_BEFORE_INIT_TOKEN, UTILITY_WEB_FASTIFY_APP_TOKEN, WEB_FASTIFY_APP_INIT_TOKEN, UTILITY_SERVER_TOKEN, SERVER_FACTORY_TOKEN, WEB_FASTIFY_APP_TOKEN, WEB_FASTIFY_APP_FACTORY_TOKEN, WEB_FASTIFY_APP_AFTER_INIT_TOKEN, WEB_FASTIFY_APP_METRICS_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';
|
|
8
|
+
import { ROOT_EXECUTION_CONTEXT_TOKEN, RESPONSE_MANAGER_TOKEN, LOGGER_TOKEN, REQUEST_MANAGER_TOKEN, ENV_MANAGER_TOKEN, ENV_USED_TOKEN, EXECUTION_CONTEXT_MANAGER_TOKEN } from '@tramvai/tokens-common';
|
|
9
9
|
import { MetricsModule } from '@tramvai/module-metrics';
|
|
10
10
|
import { CacheWarmupModule } from '@tramvai/module-cache-warmup';
|
|
11
11
|
import http from 'http';
|
|
12
12
|
import fastify from 'fastify';
|
|
13
|
-
import express from 'express';
|
|
14
13
|
import { fastifyCookie } from '@fastify/cookie';
|
|
15
14
|
import fastifyFormBody from '@fastify/formbody';
|
|
16
15
|
import { provide, createChildContainer, Scope as Scope$1, DI_TOKEN } from '@tinkoff/dippy';
|
|
17
|
-
import fp from 'fastify-plugin';
|
|
18
|
-
import symbols from 'fastify/lib/symbols';
|
|
19
16
|
import isNil from '@tinkoff/utils/is/nil';
|
|
20
17
|
import { isRedirectFoundError, isNotFoundError, isHttpError, HttpError } from '@tinkoff/errors';
|
|
21
|
-
import
|
|
22
|
-
import
|
|
18
|
+
import fastifyCompress from '@fastify/compress';
|
|
19
|
+
import FastifyStatic, { fastifyStatic } from '@fastify/static';
|
|
23
20
|
import os from 'os';
|
|
24
21
|
import filterObj from '@tinkoff/utils/object/filter';
|
|
25
22
|
import flatten from '@tinkoff/utils/array/flatten';
|
|
@@ -27,7 +24,6 @@ import toArray from '@tinkoff/utils/array/toArray';
|
|
|
27
24
|
import { getPapiParameters, isPapiMethod, createPapiMethod } from '@tramvai/papi';
|
|
28
25
|
import eachObj from '@tinkoff/utils/object/each';
|
|
29
26
|
import { resolve } from 'path';
|
|
30
|
-
import FastifyStatic from '@fastify/static';
|
|
31
27
|
import { createTerminus } from '@tinkoff/terminus';
|
|
32
28
|
import { parse } from '@tinkoff/url';
|
|
33
29
|
import monkeypatch from '@tinkoff/monkeypatch';
|
|
@@ -51,91 +47,6 @@ const serverListenCommand = ({ server, logger, envManager, }) => {
|
|
|
51
47
|
};
|
|
52
48
|
};
|
|
53
49
|
|
|
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('preHandler', enhanceRequest)
|
|
67
|
-
.addHook('preHandler', 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);
|
|
74
|
-
}
|
|
75
|
-
this[kMiddlewares].push([path, fn]);
|
|
76
|
-
if (fn == null) {
|
|
77
|
-
this.express.use(path);
|
|
78
|
-
}
|
|
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
|
-
if (req.body) {
|
|
94
|
-
req.raw.body = req.body;
|
|
95
|
-
}
|
|
96
|
-
if (req.cookies) {
|
|
97
|
-
req.raw.cookies = req.cookies;
|
|
98
|
-
}
|
|
99
|
-
const originalProtocol = req.raw.protocol;
|
|
100
|
-
// Make it lazy as it does a bit of work
|
|
101
|
-
Object.defineProperty(req.raw, 'protocol', {
|
|
102
|
-
get() {
|
|
103
|
-
// added in Fastify@3.5, so handle it missing
|
|
104
|
-
return req.protocol || originalProtocol;
|
|
105
|
-
},
|
|
106
|
-
});
|
|
107
|
-
next();
|
|
108
|
-
}
|
|
109
|
-
function runConnect(req, reply, next) {
|
|
110
|
-
if (this[kMiddlewares].length > 0) {
|
|
111
|
-
for (const [headerName, headerValue] of Object.entries(reply.getHeaders())) {
|
|
112
|
-
reply.raw.setHeader(headerName, headerValue);
|
|
113
|
-
}
|
|
114
|
-
this.express(req.raw, reply.raw, next);
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
next();
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
function onRegister(instance) {
|
|
121
|
-
var _a, _b;
|
|
122
|
-
const middlewares = instance[kMiddlewares].slice();
|
|
123
|
-
// eslint-disable-next-line no-param-reassign
|
|
124
|
-
instance[kMiddlewares] = [];
|
|
125
|
-
instance.decorate('express', (_b = (_a = options.express) === null || _a === void 0 ? void 0 : _a.instance) !== null && _b !== void 0 ? _b : express());
|
|
126
|
-
instance.express.disable('x-powered-by');
|
|
127
|
-
instance.decorate('use', use);
|
|
128
|
-
for (const middleware of middlewares) {
|
|
129
|
-
instance.use(...middleware);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
next();
|
|
133
|
-
};
|
|
134
|
-
const fastifyExpressCompatibility = fp(expressPlugin, {
|
|
135
|
-
fastify: '>=3.0.0',
|
|
136
|
-
name: 'fastify-express',
|
|
137
|
-
});
|
|
138
|
-
|
|
139
50
|
const errorHandler = (app, { log, beforeError, processError, afterError, }) => {
|
|
140
51
|
app.setErrorHandler(async (error, request, reply) => {
|
|
141
52
|
const runHandlers = async (handlers) => {
|
|
@@ -196,39 +107,28 @@ const webAppFactory = ({ server }) => {
|
|
|
196
107
|
});
|
|
197
108
|
return app;
|
|
198
109
|
};
|
|
199
|
-
const
|
|
200
|
-
const app = express();
|
|
201
|
-
app.disable('etag');
|
|
202
|
-
app.disable('x-powered-by');
|
|
203
|
-
return app;
|
|
204
|
-
};
|
|
205
|
-
const webAppInitCommand = ({ app, expressApp, logger, commandLineRunner, executionContextManager, beforeInit, requestMetrics, limiterRequest, init, afterInit, expressBeforeInit, expressInit, expressAfterInit, expressLimiterRequest, beforeError, processError, afterError, }) => {
|
|
110
|
+
const webAppInitCommand = ({ app, logger, commandLineRunner, executionContextManager, beforeInit, requestMetrics, limiterRequest, init, afterInit, beforeError, processError, afterError, }) => {
|
|
206
111
|
const log = logger('server:webapp');
|
|
207
|
-
const runHandlers = (instance, handlers
|
|
208
|
-
return Promise.all([
|
|
209
|
-
handlers && Promise.all(handlers.map((handler) => handler(instance))),
|
|
210
|
-
expressHandlers && Promise.all(expressHandlers.map((handler) => handler(expressApp))),
|
|
211
|
-
]);
|
|
112
|
+
const runHandlers = (instance, handlers) => {
|
|
113
|
+
return Promise.all([handlers && Promise.all(handlers.map((handler) => handler(instance)))]);
|
|
212
114
|
};
|
|
213
115
|
return async function webAppInit() {
|
|
214
116
|
errorHandler(app, { log, beforeError, processError, afterError });
|
|
215
|
-
await app.register(fastifyExpressCompatibility, {
|
|
216
|
-
express: {
|
|
217
|
-
instance: expressApp,
|
|
218
|
-
},
|
|
219
|
-
});
|
|
220
117
|
await app.register(async (instance) => {
|
|
221
|
-
await runHandlers(instance, beforeInit
|
|
118
|
+
await runHandlers(instance, beforeInit);
|
|
222
119
|
});
|
|
223
120
|
await app.register(async (instance) => {
|
|
224
|
-
await runHandlers(instance, requestMetrics
|
|
225
|
-
await runHandlers(instance, limiterRequest
|
|
121
|
+
await runHandlers(instance, requestMetrics);
|
|
122
|
+
await runHandlers(instance, limiterRequest);
|
|
226
123
|
await app.register(fastifyCookie);
|
|
227
124
|
await app.register(fastifyFormBody, { bodyLimit: 2097152 }); // 2mb
|
|
228
|
-
await runHandlers(instance, init
|
|
229
|
-
//
|
|
230
|
-
|
|
231
|
-
|
|
125
|
+
await runHandlers(instance, init);
|
|
126
|
+
// break the cycle of event loop to allow server to handle other requests
|
|
127
|
+
// while current on is in processing
|
|
128
|
+
// mainly to prevent problems and response hanging in case the response process
|
|
129
|
+
// uses only sync and microtask code
|
|
130
|
+
instance.addHook('preHandler', (req, res, next) => {
|
|
131
|
+
setImmediate(next);
|
|
232
132
|
});
|
|
233
133
|
instance.all('*', async (request, reply) => {
|
|
234
134
|
try {
|
|
@@ -243,18 +143,6 @@ const webAppInitCommand = ({ app, expressApp, logger, commandLineRunner, executi
|
|
|
243
143
|
provide: ROOT_EXECUTION_CONTEXT_TOKEN,
|
|
244
144
|
useValue: rootExecutionContext,
|
|
245
145
|
}),
|
|
246
|
-
{
|
|
247
|
-
provide: REQUEST,
|
|
248
|
-
scope: Scope.REQUEST,
|
|
249
|
-
useValue: request.raw,
|
|
250
|
-
},
|
|
251
|
-
{
|
|
252
|
-
provide: RESPONSE,
|
|
253
|
-
scope: Scope.REQUEST,
|
|
254
|
-
useValue: reply.raw,
|
|
255
|
-
},
|
|
256
|
-
// TODO: перевести использование на новые
|
|
257
|
-
// TODO: добавить для papi
|
|
258
146
|
{
|
|
259
147
|
provide: FASTIFY_REQUEST,
|
|
260
148
|
scope: Scope.REQUEST,
|
|
@@ -295,7 +183,7 @@ const webAppInitCommand = ({ app, expressApp, logger, commandLineRunner, executi
|
|
|
295
183
|
});
|
|
296
184
|
});
|
|
297
185
|
await app.register(async (instance) => {
|
|
298
|
-
await runHandlers(
|
|
186
|
+
await runHandlers(instance, afterInit);
|
|
299
187
|
});
|
|
300
188
|
await app.ready();
|
|
301
189
|
};
|
|
@@ -308,25 +196,23 @@ const staticAppCommand = ({ logger, envManager, appInfo, }) => {
|
|
|
308
196
|
const log = logger('server:static');
|
|
309
197
|
const port = +envManager.get('PORT_STATIC');
|
|
310
198
|
const appVersion = envManager.get('APP_VERSION');
|
|
311
|
-
return function staticApp() {
|
|
312
|
-
const appStatic =
|
|
313
|
-
appStatic.
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
}
|
|
326
|
-
next();
|
|
199
|
+
return async function staticApp() {
|
|
200
|
+
const appStatic = fastify();
|
|
201
|
+
await appStatic.register(fastifyCompress);
|
|
202
|
+
await appStatic.register(fastifyStatic, {
|
|
203
|
+
root: process.cwd(),
|
|
204
|
+
prefix: '/',
|
|
205
|
+
setHeaders: (res) => {
|
|
206
|
+
res.headers({
|
|
207
|
+
'Access-Control-Allow-Origin': '*',
|
|
208
|
+
'Timing-Allow-Origin': '*',
|
|
209
|
+
'X-App-Id': appInfo.appName,
|
|
210
|
+
'X-App-Version': appVersion,
|
|
211
|
+
'X-Host': os.hostname(),
|
|
212
|
+
});
|
|
213
|
+
},
|
|
327
214
|
});
|
|
328
|
-
appStatic.
|
|
329
|
-
appStatic.listen(port, () => log.info(`Running static server on port: ${port}`));
|
|
215
|
+
appStatic.listen({ port }, () => log.info(`Running static server on port: ${port}`));
|
|
330
216
|
return appStatic;
|
|
331
217
|
};
|
|
332
218
|
};
|
|
@@ -394,17 +280,6 @@ function createApi(rootApp, papiList, { baseUrl, di, logger }) {
|
|
|
394
280
|
scope: Scope$1.REQUEST,
|
|
395
281
|
useValue: res,
|
|
396
282
|
},
|
|
397
|
-
// TODO: remove deprecated after removing express compatibility layer
|
|
398
|
-
{
|
|
399
|
-
provide: REQUEST,
|
|
400
|
-
scope: Scope$1.REQUEST,
|
|
401
|
-
useValue: req.raw,
|
|
402
|
-
},
|
|
403
|
-
{
|
|
404
|
-
provide: RESPONSE,
|
|
405
|
-
scope: Scope$1.REQUEST,
|
|
406
|
-
useValue: res.raw,
|
|
407
|
-
},
|
|
408
283
|
]);
|
|
409
284
|
const papiExecutor = childDi.get(PAPI_EXECUTOR);
|
|
410
285
|
// TODO: use abortSignal
|
|
@@ -887,11 +762,9 @@ ServerProxyModule = __decorate([
|
|
|
887
762
|
Module({
|
|
888
763
|
providers: [
|
|
889
764
|
{
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
useFactory: ({ app, defaultProxies }) => {
|
|
894
|
-
return () => {
|
|
765
|
+
provide: WEB_FASTIFY_APP_INIT_TOKEN,
|
|
766
|
+
useFactory: ({ defaultProxies }) => {
|
|
767
|
+
return (app) => {
|
|
895
768
|
const proxyConfig = safeNodeRequire(resolve(process.cwd(), 'proxy.conf'));
|
|
896
769
|
const proxies = defaultProxies !== null && defaultProxies !== void 0 ? defaultProxies : [];
|
|
897
770
|
if (!proxyConfig && proxies.length === 0) {
|
|
@@ -910,19 +783,21 @@ ServerProxyModule = __decorate([
|
|
|
910
783
|
}, proxyConfig);
|
|
911
784
|
}
|
|
912
785
|
proxies.forEach((proxy) => {
|
|
913
|
-
|
|
786
|
+
const middleware = createProxyMiddleware(proxy.context, {
|
|
914
787
|
changeOrigin: true,
|
|
915
788
|
onProxyRes: (proxyRes) => {
|
|
916
789
|
// eslint-disable-next-line no-param-reassign
|
|
917
790
|
proxyRes.headers['x-tramvai-proxied-response'] = '1';
|
|
918
791
|
},
|
|
919
792
|
...proxy,
|
|
920
|
-
})
|
|
793
|
+
});
|
|
794
|
+
app.addHook('onRequest', (req, res, next) => {
|
|
795
|
+
middleware(req.raw, res.raw, next);
|
|
796
|
+
});
|
|
921
797
|
});
|
|
922
798
|
};
|
|
923
799
|
},
|
|
924
800
|
deps: {
|
|
925
|
-
app: WEB_APP_TOKEN,
|
|
926
801
|
defaultProxies: {
|
|
927
802
|
token: PROXY_CONFIG_TOKEN,
|
|
928
803
|
optional: true,
|
|
@@ -1159,22 +1034,12 @@ ServerModule = __decorate([
|
|
|
1159
1034
|
factory: WEB_FASTIFY_APP_FACTORY_TOKEN,
|
|
1160
1035
|
},
|
|
1161
1036
|
}),
|
|
1162
|
-
provide$1({
|
|
1163
|
-
// BACKWARD: provide the express app as before
|
|
1164
|
-
provide: WEB_APP_TOKEN,
|
|
1165
|
-
scope: Scope.SINGLETON,
|
|
1166
|
-
useFactory: webAppExpressFactory,
|
|
1167
|
-
deps: {
|
|
1168
|
-
webApp: WEB_FASTIFY_APP_TOKEN,
|
|
1169
|
-
},
|
|
1170
|
-
}),
|
|
1171
1037
|
{
|
|
1172
1038
|
provide: commandLineListTokens.init,
|
|
1173
1039
|
multi: true,
|
|
1174
1040
|
useFactory: webAppInitCommand,
|
|
1175
1041
|
deps: {
|
|
1176
1042
|
app: WEB_FASTIFY_APP_TOKEN,
|
|
1177
|
-
expressApp: WEB_APP_TOKEN,
|
|
1178
1043
|
logger: LOGGER_TOKEN,
|
|
1179
1044
|
commandLineRunner: COMMAND_LINE_RUNNER_TOKEN,
|
|
1180
1045
|
executionContextManager: EXECUTION_CONTEXT_MANAGER_TOKEN,
|
|
@@ -1183,10 +1048,6 @@ ServerModule = __decorate([
|
|
|
1183
1048
|
afterInit: { token: WEB_FASTIFY_APP_AFTER_INIT_TOKEN, optional: true },
|
|
1184
1049
|
requestMetrics: { token: WEB_FASTIFY_APP_METRICS_TOKEN, optional: true },
|
|
1185
1050
|
limiterRequest: { token: WEB_FASTIFY_APP_LIMITER_TOKEN, optional: true },
|
|
1186
|
-
expressBeforeInit: { token: WEB_APP_BEFORE_INIT_TOKEN, optional: true },
|
|
1187
|
-
expressInit: { token: WEB_APP_INIT_TOKEN, optional: true },
|
|
1188
|
-
expressAfterInit: { token: WEB_APP_AFTER_INIT_TOKEN, optional: true },
|
|
1189
|
-
expressLimiterRequest: { token: WEB_APP_LIMITER_TOKEN, optional: true },
|
|
1190
1051
|
beforeError: { token: WEB_FASTIFY_APP_BEFORE_ERROR_TOKEN, optional: true },
|
|
1191
1052
|
processError: { token: WEB_FASTIFY_APP_PROCESS_ERROR_TOKEN, optional: true },
|
|
1192
1053
|
afterError: { token: WEB_FASTIFY_APP_AFTER_ERROR_TOKEN, optional: true },
|
package/lib/server.js
CHANGED
|
@@ -13,16 +13,13 @@ var moduleMetrics = require('@tramvai/module-metrics');
|
|
|
13
13
|
var moduleCacheWarmup = require('@tramvai/module-cache-warmup');
|
|
14
14
|
var http = require('http');
|
|
15
15
|
var fastify = require('fastify');
|
|
16
|
-
var express = require('express');
|
|
17
16
|
var cookie = require('@fastify/cookie');
|
|
18
17
|
var fastifyFormBody = require('@fastify/formbody');
|
|
19
18
|
var dippy = require('@tinkoff/dippy');
|
|
20
|
-
var fp = require('fastify-plugin');
|
|
21
|
-
var symbols = require('fastify/lib/symbols');
|
|
22
19
|
var isNil = require('@tinkoff/utils/is/nil');
|
|
23
20
|
var errors = require('@tinkoff/errors');
|
|
24
|
-
var
|
|
25
|
-
var
|
|
21
|
+
var fastifyCompress = require('@fastify/compress');
|
|
22
|
+
var FastifyStatic = require('@fastify/static');
|
|
26
23
|
var os = require('os');
|
|
27
24
|
var filterObj = require('@tinkoff/utils/object/filter');
|
|
28
25
|
var flatten = require('@tinkoff/utils/array/flatten');
|
|
@@ -30,7 +27,6 @@ var toArray = require('@tinkoff/utils/array/toArray');
|
|
|
30
27
|
var papi = require('@tramvai/papi');
|
|
31
28
|
var eachObj = require('@tinkoff/utils/object/each');
|
|
32
29
|
var path = require('path');
|
|
33
|
-
var FastifyStatic = require('@fastify/static');
|
|
34
30
|
var terminus = require('@tinkoff/terminus');
|
|
35
31
|
var url = require('@tinkoff/url');
|
|
36
32
|
var monkeypatch = require('@tinkoff/monkeypatch');
|
|
@@ -45,19 +41,15 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
45
41
|
var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter);
|
|
46
42
|
var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
|
|
47
43
|
var fastify__default = /*#__PURE__*/_interopDefaultLegacy(fastify);
|
|
48
|
-
var express__default = /*#__PURE__*/_interopDefaultLegacy(express);
|
|
49
44
|
var fastifyFormBody__default = /*#__PURE__*/_interopDefaultLegacy(fastifyFormBody);
|
|
50
|
-
var fp__default = /*#__PURE__*/_interopDefaultLegacy(fp);
|
|
51
|
-
var symbols__default = /*#__PURE__*/_interopDefaultLegacy(symbols);
|
|
52
45
|
var isNil__default = /*#__PURE__*/_interopDefaultLegacy(isNil);
|
|
53
|
-
var
|
|
54
|
-
var
|
|
46
|
+
var fastifyCompress__default = /*#__PURE__*/_interopDefaultLegacy(fastifyCompress);
|
|
47
|
+
var FastifyStatic__default = /*#__PURE__*/_interopDefaultLegacy(FastifyStatic);
|
|
55
48
|
var os__default = /*#__PURE__*/_interopDefaultLegacy(os);
|
|
56
49
|
var filterObj__default = /*#__PURE__*/_interopDefaultLegacy(filterObj);
|
|
57
50
|
var flatten__default = /*#__PURE__*/_interopDefaultLegacy(flatten);
|
|
58
51
|
var toArray__default = /*#__PURE__*/_interopDefaultLegacy(toArray);
|
|
59
52
|
var eachObj__default = /*#__PURE__*/_interopDefaultLegacy(eachObj);
|
|
60
|
-
var FastifyStatic__default = /*#__PURE__*/_interopDefaultLegacy(FastifyStatic);
|
|
61
53
|
var monkeypatch__default = /*#__PURE__*/_interopDefaultLegacy(monkeypatch);
|
|
62
54
|
var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
|
|
63
55
|
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
|
|
@@ -77,91 +69,6 @@ const serverListenCommand = ({ server, logger, envManager, }) => {
|
|
|
77
69
|
};
|
|
78
70
|
};
|
|
79
71
|
|
|
80
|
-
/**
|
|
81
|
-
* Fork of https://github.com/fastify/fastify-express
|
|
82
|
-
*/
|
|
83
|
-
const kMiddlewares = Symbol('fastify-express-middlewares');
|
|
84
|
-
const expressPlugin = (fastify, options, next) => {
|
|
85
|
-
var _a, _b;
|
|
86
|
-
fastify.decorate('use', use);
|
|
87
|
-
// eslint-disable-next-line no-param-reassign
|
|
88
|
-
fastify[kMiddlewares] = [];
|
|
89
|
-
fastify.decorate('express', (_b = (_a = options.express) === null || _a === void 0 ? void 0 : _a.instance) !== null && _b !== void 0 ? _b : express__default["default"]());
|
|
90
|
-
fastify.express.disable('x-powered-by');
|
|
91
|
-
fastify
|
|
92
|
-
.addHook('preHandler', enhanceRequest)
|
|
93
|
-
.addHook('preHandler', runConnect)
|
|
94
|
-
.addHook('onRegister', onRegister);
|
|
95
|
-
function use(path, fn) {
|
|
96
|
-
if (typeof path === 'string') {
|
|
97
|
-
const prefix = this[symbols__default["default"].kRoutePrefix];
|
|
98
|
-
// eslint-disable-next-line no-param-reassign
|
|
99
|
-
path = prefix + (path === '/' && prefix.length > 0 ? '' : path);
|
|
100
|
-
}
|
|
101
|
-
this[kMiddlewares].push([path, fn]);
|
|
102
|
-
if (fn == null) {
|
|
103
|
-
this.express.use(path);
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
this.express.use(path, fn);
|
|
107
|
-
}
|
|
108
|
-
return this;
|
|
109
|
-
}
|
|
110
|
-
function enhanceRequest(req, reply, next) {
|
|
111
|
-
req.raw.originalUrl = req.raw.url;
|
|
112
|
-
req.raw.id = req.id;
|
|
113
|
-
req.raw.hostname = req.hostname;
|
|
114
|
-
req.raw.ip = req.ip;
|
|
115
|
-
req.raw.ips = req.ips;
|
|
116
|
-
req.raw.log = req.log;
|
|
117
|
-
// eslint-disable-next-line no-param-reassign
|
|
118
|
-
reply.raw.log = req.log;
|
|
119
|
-
if (req.body) {
|
|
120
|
-
req.raw.body = req.body;
|
|
121
|
-
}
|
|
122
|
-
if (req.cookies) {
|
|
123
|
-
req.raw.cookies = req.cookies;
|
|
124
|
-
}
|
|
125
|
-
const originalProtocol = req.raw.protocol;
|
|
126
|
-
// Make it lazy as it does a bit of work
|
|
127
|
-
Object.defineProperty(req.raw, 'protocol', {
|
|
128
|
-
get() {
|
|
129
|
-
// added in Fastify@3.5, so handle it missing
|
|
130
|
-
return req.protocol || originalProtocol;
|
|
131
|
-
},
|
|
132
|
-
});
|
|
133
|
-
next();
|
|
134
|
-
}
|
|
135
|
-
function runConnect(req, reply, next) {
|
|
136
|
-
if (this[kMiddlewares].length > 0) {
|
|
137
|
-
for (const [headerName, headerValue] of Object.entries(reply.getHeaders())) {
|
|
138
|
-
reply.raw.setHeader(headerName, headerValue);
|
|
139
|
-
}
|
|
140
|
-
this.express(req.raw, reply.raw, next);
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
next();
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
function onRegister(instance) {
|
|
147
|
-
var _a, _b;
|
|
148
|
-
const middlewares = instance[kMiddlewares].slice();
|
|
149
|
-
// eslint-disable-next-line no-param-reassign
|
|
150
|
-
instance[kMiddlewares] = [];
|
|
151
|
-
instance.decorate('express', (_b = (_a = options.express) === null || _a === void 0 ? void 0 : _a.instance) !== null && _b !== void 0 ? _b : express__default["default"]());
|
|
152
|
-
instance.express.disable('x-powered-by');
|
|
153
|
-
instance.decorate('use', use);
|
|
154
|
-
for (const middleware of middlewares) {
|
|
155
|
-
instance.use(...middleware);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
next();
|
|
159
|
-
};
|
|
160
|
-
const fastifyExpressCompatibility = fp__default["default"](expressPlugin, {
|
|
161
|
-
fastify: '>=3.0.0',
|
|
162
|
-
name: 'fastify-express',
|
|
163
|
-
});
|
|
164
|
-
|
|
165
72
|
const errorHandler = (app, { log, beforeError, processError, afterError, }) => {
|
|
166
73
|
app.setErrorHandler(async (error, request, reply) => {
|
|
167
74
|
const runHandlers = async (handlers) => {
|
|
@@ -222,39 +129,28 @@ const webAppFactory = ({ server }) => {
|
|
|
222
129
|
});
|
|
223
130
|
return app;
|
|
224
131
|
};
|
|
225
|
-
const
|
|
226
|
-
const app = express__default["default"]();
|
|
227
|
-
app.disable('etag');
|
|
228
|
-
app.disable('x-powered-by');
|
|
229
|
-
return app;
|
|
230
|
-
};
|
|
231
|
-
const webAppInitCommand = ({ app, expressApp, logger, commandLineRunner, executionContextManager, beforeInit, requestMetrics, limiterRequest, init, afterInit, expressBeforeInit, expressInit, expressAfterInit, expressLimiterRequest, beforeError, processError, afterError, }) => {
|
|
132
|
+
const webAppInitCommand = ({ app, logger, commandLineRunner, executionContextManager, beforeInit, requestMetrics, limiterRequest, init, afterInit, beforeError, processError, afterError, }) => {
|
|
232
133
|
const log = logger('server:webapp');
|
|
233
|
-
const runHandlers = (instance, handlers
|
|
234
|
-
return Promise.all([
|
|
235
|
-
handlers && Promise.all(handlers.map((handler) => handler(instance))),
|
|
236
|
-
expressHandlers && Promise.all(expressHandlers.map((handler) => handler(expressApp))),
|
|
237
|
-
]);
|
|
134
|
+
const runHandlers = (instance, handlers) => {
|
|
135
|
+
return Promise.all([handlers && Promise.all(handlers.map((handler) => handler(instance)))]);
|
|
238
136
|
};
|
|
239
137
|
return async function webAppInit() {
|
|
240
138
|
errorHandler(app, { log, beforeError, processError, afterError });
|
|
241
|
-
await app.register(fastifyExpressCompatibility, {
|
|
242
|
-
express: {
|
|
243
|
-
instance: expressApp,
|
|
244
|
-
},
|
|
245
|
-
});
|
|
246
139
|
await app.register(async (instance) => {
|
|
247
|
-
await runHandlers(instance, beforeInit
|
|
140
|
+
await runHandlers(instance, beforeInit);
|
|
248
141
|
});
|
|
249
142
|
await app.register(async (instance) => {
|
|
250
|
-
await runHandlers(instance, requestMetrics
|
|
251
|
-
await runHandlers(instance, limiterRequest
|
|
143
|
+
await runHandlers(instance, requestMetrics);
|
|
144
|
+
await runHandlers(instance, limiterRequest);
|
|
252
145
|
await app.register(cookie.fastifyCookie);
|
|
253
146
|
await app.register(fastifyFormBody__default["default"], { bodyLimit: 2097152 }); // 2mb
|
|
254
|
-
await runHandlers(instance, init
|
|
255
|
-
//
|
|
256
|
-
|
|
257
|
-
|
|
147
|
+
await runHandlers(instance, init);
|
|
148
|
+
// break the cycle of event loop to allow server to handle other requests
|
|
149
|
+
// while current on is in processing
|
|
150
|
+
// mainly to prevent problems and response hanging in case the response process
|
|
151
|
+
// uses only sync and microtask code
|
|
152
|
+
instance.addHook('preHandler', (req, res, next) => {
|
|
153
|
+
setImmediate(next);
|
|
258
154
|
});
|
|
259
155
|
instance.all('*', async (request, reply) => {
|
|
260
156
|
try {
|
|
@@ -270,24 +166,12 @@ const webAppInitCommand = ({ app, expressApp, logger, commandLineRunner, executi
|
|
|
270
166
|
useValue: rootExecutionContext,
|
|
271
167
|
}),
|
|
272
168
|
{
|
|
273
|
-
provide:
|
|
274
|
-
scope: core.Scope.REQUEST,
|
|
275
|
-
useValue: request.raw,
|
|
276
|
-
},
|
|
277
|
-
{
|
|
278
|
-
provide: tokensCommon.RESPONSE,
|
|
279
|
-
scope: core.Scope.REQUEST,
|
|
280
|
-
useValue: reply.raw,
|
|
281
|
-
},
|
|
282
|
-
// TODO: перевести использование на новые
|
|
283
|
-
// TODO: добавить для papi
|
|
284
|
-
{
|
|
285
|
-
provide: tokensCommon.FASTIFY_REQUEST,
|
|
169
|
+
provide: tokensServerPrivate.FASTIFY_REQUEST,
|
|
286
170
|
scope: core.Scope.REQUEST,
|
|
287
171
|
useValue: request,
|
|
288
172
|
},
|
|
289
173
|
{
|
|
290
|
-
provide:
|
|
174
|
+
provide: tokensServerPrivate.FASTIFY_RESPONSE,
|
|
291
175
|
scope: core.Scope.REQUEST,
|
|
292
176
|
useValue: reply,
|
|
293
177
|
},
|
|
@@ -321,7 +205,7 @@ const webAppInitCommand = ({ app, expressApp, logger, commandLineRunner, executi
|
|
|
321
205
|
});
|
|
322
206
|
});
|
|
323
207
|
await app.register(async (instance) => {
|
|
324
|
-
await runHandlers(
|
|
208
|
+
await runHandlers(instance, afterInit);
|
|
325
209
|
});
|
|
326
210
|
await app.ready();
|
|
327
211
|
};
|
|
@@ -334,25 +218,23 @@ const staticAppCommand = ({ logger, envManager, appInfo, }) => {
|
|
|
334
218
|
const log = logger('server:static');
|
|
335
219
|
const port = +envManager.get('PORT_STATIC');
|
|
336
220
|
const appVersion = envManager.get('APP_VERSION');
|
|
337
|
-
return function staticApp() {
|
|
338
|
-
const appStatic =
|
|
339
|
-
appStatic.
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
}
|
|
352
|
-
next();
|
|
221
|
+
return async function staticApp() {
|
|
222
|
+
const appStatic = fastify__default["default"]();
|
|
223
|
+
await appStatic.register(fastifyCompress__default["default"]);
|
|
224
|
+
await appStatic.register(FastifyStatic.fastifyStatic, {
|
|
225
|
+
root: process.cwd(),
|
|
226
|
+
prefix: '/',
|
|
227
|
+
setHeaders: (res) => {
|
|
228
|
+
res.headers({
|
|
229
|
+
'Access-Control-Allow-Origin': '*',
|
|
230
|
+
'Timing-Allow-Origin': '*',
|
|
231
|
+
'X-App-Id': appInfo.appName,
|
|
232
|
+
'X-App-Version': appVersion,
|
|
233
|
+
'X-Host': os__default["default"].hostname(),
|
|
234
|
+
});
|
|
235
|
+
},
|
|
353
236
|
});
|
|
354
|
-
appStatic.
|
|
355
|
-
appStatic.listen(port, () => log.info(`Running static server on port: ${port}`));
|
|
237
|
+
appStatic.listen({ port }, () => log.info(`Running static server on port: ${port}`));
|
|
356
238
|
return appStatic;
|
|
357
239
|
};
|
|
358
240
|
};
|
|
@@ -411,26 +293,15 @@ function createApi(rootApp, papiList, { baseUrl, di, logger }) {
|
|
|
411
293
|
}, async (req, res) => {
|
|
412
294
|
const childDi = dippy.createChildContainer(di, [
|
|
413
295
|
{
|
|
414
|
-
provide:
|
|
296
|
+
provide: tokensServerPrivate.FASTIFY_REQUEST,
|
|
415
297
|
scope: dippy.Scope.REQUEST,
|
|
416
298
|
useValue: req,
|
|
417
299
|
},
|
|
418
300
|
{
|
|
419
|
-
provide:
|
|
301
|
+
provide: tokensServerPrivate.FASTIFY_RESPONSE,
|
|
420
302
|
scope: dippy.Scope.REQUEST,
|
|
421
303
|
useValue: res,
|
|
422
304
|
},
|
|
423
|
-
// TODO: remove deprecated after removing express compatibility layer
|
|
424
|
-
{
|
|
425
|
-
provide: tokensCommon.REQUEST,
|
|
426
|
-
scope: dippy.Scope.REQUEST,
|
|
427
|
-
useValue: req.raw,
|
|
428
|
-
},
|
|
429
|
-
{
|
|
430
|
-
provide: tokensCommon.RESPONSE,
|
|
431
|
-
scope: dippy.Scope.REQUEST,
|
|
432
|
-
useValue: res.raw,
|
|
433
|
-
},
|
|
434
305
|
]);
|
|
435
306
|
const papiExecutor = childDi.get(tokensServerPrivate.PAPI_EXECUTOR);
|
|
436
307
|
// TODO: use abortSignal
|
|
@@ -542,7 +413,7 @@ const papiExecutorProvider = dippy.provide({
|
|
|
542
413
|
deps: {
|
|
543
414
|
di: dippy.DI_TOKEN,
|
|
544
415
|
logger: tokensCommon.LOGGER_TOKEN,
|
|
545
|
-
fastifyRequest:
|
|
416
|
+
fastifyRequest: tokensServerPrivate.FASTIFY_REQUEST,
|
|
546
417
|
requestManager: tokensCommon.REQUEST_MANAGER_TOKEN,
|
|
547
418
|
responseManager: tokensCommon.RESPONSE_MANAGER_TOKEN,
|
|
548
419
|
},
|
|
@@ -913,11 +784,9 @@ ServerProxyModule = tslib.__decorate([
|
|
|
913
784
|
core.Module({
|
|
914
785
|
providers: [
|
|
915
786
|
{
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
useFactory: ({ app, defaultProxies }) => {
|
|
920
|
-
return () => {
|
|
787
|
+
provide: tokensServerPrivate.WEB_FASTIFY_APP_INIT_TOKEN,
|
|
788
|
+
useFactory: ({ defaultProxies }) => {
|
|
789
|
+
return (app) => {
|
|
921
790
|
const proxyConfig = safeNodeRequire(path.resolve(process.cwd(), 'proxy.conf'));
|
|
922
791
|
const proxies = defaultProxies !== null && defaultProxies !== void 0 ? defaultProxies : [];
|
|
923
792
|
if (!proxyConfig && proxies.length === 0) {
|
|
@@ -936,19 +805,21 @@ ServerProxyModule = tslib.__decorate([
|
|
|
936
805
|
}, proxyConfig);
|
|
937
806
|
}
|
|
938
807
|
proxies.forEach((proxy) => {
|
|
939
|
-
|
|
808
|
+
const middleware = httpProxyMiddleware.createProxyMiddleware(proxy.context, {
|
|
940
809
|
changeOrigin: true,
|
|
941
810
|
onProxyRes: (proxyRes) => {
|
|
942
811
|
// eslint-disable-next-line no-param-reassign
|
|
943
812
|
proxyRes.headers['x-tramvai-proxied-response'] = '1';
|
|
944
813
|
},
|
|
945
814
|
...proxy,
|
|
946
|
-
})
|
|
815
|
+
});
|
|
816
|
+
app.addHook('onRequest', (req, res, next) => {
|
|
817
|
+
middleware(req.raw, res.raw, next);
|
|
818
|
+
});
|
|
947
819
|
});
|
|
948
820
|
};
|
|
949
821
|
},
|
|
950
822
|
deps: {
|
|
951
|
-
app: tokensServer.WEB_APP_TOKEN,
|
|
952
823
|
defaultProxies: {
|
|
953
824
|
token: tokensServer.PROXY_CONFIG_TOKEN,
|
|
954
825
|
optional: true,
|
|
@@ -1185,22 +1056,12 @@ exports.ServerModule = tslib.__decorate([
|
|
|
1185
1056
|
factory: tokensServerPrivate.WEB_FASTIFY_APP_FACTORY_TOKEN,
|
|
1186
1057
|
},
|
|
1187
1058
|
}),
|
|
1188
|
-
core.provide({
|
|
1189
|
-
// BACKWARD: provide the express app as before
|
|
1190
|
-
provide: tokensServer.WEB_APP_TOKEN,
|
|
1191
|
-
scope: core.Scope.SINGLETON,
|
|
1192
|
-
useFactory: webAppExpressFactory,
|
|
1193
|
-
deps: {
|
|
1194
|
-
webApp: tokensServerPrivate.WEB_FASTIFY_APP_TOKEN,
|
|
1195
|
-
},
|
|
1196
|
-
}),
|
|
1197
1059
|
{
|
|
1198
1060
|
provide: core.commandLineListTokens.init,
|
|
1199
1061
|
multi: true,
|
|
1200
1062
|
useFactory: webAppInitCommand,
|
|
1201
1063
|
deps: {
|
|
1202
1064
|
app: tokensServerPrivate.WEB_FASTIFY_APP_TOKEN,
|
|
1203
|
-
expressApp: tokensServer.WEB_APP_TOKEN,
|
|
1204
1065
|
logger: tokensCommon.LOGGER_TOKEN,
|
|
1205
1066
|
commandLineRunner: core.COMMAND_LINE_RUNNER_TOKEN,
|
|
1206
1067
|
executionContextManager: tokensCommon.EXECUTION_CONTEXT_MANAGER_TOKEN,
|
|
@@ -1209,10 +1070,6 @@ exports.ServerModule = tslib.__decorate([
|
|
|
1209
1070
|
afterInit: { token: tokensServerPrivate.WEB_FASTIFY_APP_AFTER_INIT_TOKEN, optional: true },
|
|
1210
1071
|
requestMetrics: { token: tokensServerPrivate.WEB_FASTIFY_APP_METRICS_TOKEN, optional: true },
|
|
1211
1072
|
limiterRequest: { token: tokensServerPrivate.WEB_FASTIFY_APP_LIMITER_TOKEN, optional: true },
|
|
1212
|
-
expressBeforeInit: { token: tokensServer.WEB_APP_BEFORE_INIT_TOKEN, optional: true },
|
|
1213
|
-
expressInit: { token: tokensServer.WEB_APP_INIT_TOKEN, optional: true },
|
|
1214
|
-
expressAfterInit: { token: tokensServer.WEB_APP_AFTER_INIT_TOKEN, optional: true },
|
|
1215
|
-
expressLimiterRequest: { token: tokensServer.WEB_APP_LIMITER_TOKEN, optional: true },
|
|
1216
1073
|
beforeError: { token: tokensServerPrivate.WEB_FASTIFY_APP_BEFORE_ERROR_TOKEN, optional: true },
|
|
1217
1074
|
processError: { token: tokensServerPrivate.WEB_FASTIFY_APP_PROCESS_ERROR_TOKEN, optional: true },
|
|
1218
1075
|
afterError: { token: tokensServerPrivate.WEB_FASTIFY_APP_AFTER_ERROR_TOKEN, optional: true },
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-server",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.32.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -26,33 +26,30 @@
|
|
|
26
26
|
"@tinkoff/monkeypatch": "2.0.2",
|
|
27
27
|
"@tinkoff/terminus": "0.1.3",
|
|
28
28
|
"@tinkoff/url": "0.8.3",
|
|
29
|
-
"@tramvai/module-cache-warmup": "2.
|
|
30
|
-
"@tramvai/module-metrics": "2.
|
|
31
|
-
"@tramvai/papi": "2.
|
|
32
|
-
"@tramvai/tokens-server": "2.
|
|
33
|
-
"@tramvai/tokens-server-private": "2.
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"fastify": "^
|
|
37
|
-
"fastify
|
|
38
|
-
"@fastify/
|
|
39
|
-
"@fastify/
|
|
40
|
-
"@fastify/static": "^5.0.2",
|
|
29
|
+
"@tramvai/module-cache-warmup": "2.32.0",
|
|
30
|
+
"@tramvai/module-metrics": "2.32.0",
|
|
31
|
+
"@tramvai/papi": "2.32.0",
|
|
32
|
+
"@tramvai/tokens-server": "2.32.0",
|
|
33
|
+
"@tramvai/tokens-server-private": "2.32.0",
|
|
34
|
+
"fastify": "^4.6.0",
|
|
35
|
+
"fastify-plugin": "^4.2.1",
|
|
36
|
+
"@fastify/cookie": "^8.1.0",
|
|
37
|
+
"@fastify/compress": "^6.1.1",
|
|
38
|
+
"@fastify/formbody": "^7.2.0",
|
|
39
|
+
"@fastify/static": "^6.5.0",
|
|
41
40
|
"http-proxy-middleware": "^2.0.2"
|
|
42
41
|
},
|
|
43
42
|
"peerDependencies": {
|
|
44
43
|
"@tinkoff/dippy": "0.8.6",
|
|
45
44
|
"@tinkoff/utils": "^2.1.2",
|
|
46
|
-
"@tramvai/cli": "2.
|
|
47
|
-
"@tramvai/core": "2.
|
|
48
|
-
"@tramvai/module-common": "2.
|
|
49
|
-
"@tramvai/module-environment": "2.
|
|
50
|
-
"@tramvai/tokens-common": "2.
|
|
45
|
+
"@tramvai/cli": "2.32.0",
|
|
46
|
+
"@tramvai/core": "2.32.0",
|
|
47
|
+
"@tramvai/module-common": "2.32.0",
|
|
48
|
+
"@tramvai/module-environment": "2.32.0",
|
|
49
|
+
"@tramvai/tokens-common": "2.32.0",
|
|
51
50
|
"tslib": "^2.4.0"
|
|
52
51
|
},
|
|
53
52
|
"devDependencies": {
|
|
54
|
-
"@types/compression": "^1.7.0",
|
|
55
|
-
"@types/express": "^4.17.9",
|
|
56
53
|
"@types/http-proxy-middleware": "^0.19.3"
|
|
57
54
|
},
|
|
58
55
|
"module": "lib/server.es.js",
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Fork of https://github.com/fastify/fastify-express
|
|
3
|
-
*/
|
|
4
|
-
import express from 'express';
|
|
5
|
-
import type { FastifyPluginCallback } from 'fastify';
|
|
6
|
-
declare module 'fastify' {
|
|
7
|
-
interface FastifyInstance {
|
|
8
|
-
/**
|
|
9
|
-
* Express middleware function
|
|
10
|
-
*/
|
|
11
|
-
use: express.Application['use'];
|
|
12
|
-
/**
|
|
13
|
-
* Express application instance
|
|
14
|
-
*/
|
|
15
|
-
express: express.Application;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
interface Options {
|
|
19
|
-
express: {
|
|
20
|
-
instance: express.Application;
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
export declare const fastifyExpressCompatibility: FastifyPluginCallback<Options, import("fastify").RawServerDefault>;
|
|
24
|
-
export {};
|