@tramvai/module-server 1.85.0 → 1.90.2

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.
@@ -1,8 +1,8 @@
1
1
  import type { APP_INFO_TOKEN } from '@tramvai/core';
2
- import type { WEB_APP_TOKEN } from '@tramvai/tokens-server';
3
2
  import type { ENV_MANAGER_TOKEN } from '@tramvai/module-common';
3
+ import type { WEB_FASTIFY_APP_TOKEN } from '@tramvai/tokens-server-private';
4
4
  export declare const xHeadersFactory: ({ app, envManager, appInfo, }: {
5
- app: typeof WEB_APP_TOKEN;
5
+ app: typeof WEB_FASTIFY_APP_TOKEN;
6
6
  envManager: typeof ENV_MANAGER_TOKEN;
7
7
  appInfo: typeof APP_INFO_TOKEN;
8
8
  }) => () => Promise<void>;
package/lib/server.es.js CHANGED
@@ -26,6 +26,8 @@ import toArray from '@tinkoff/utils/array/toArray';
26
26
  import { create, middlewares, getPapiParameters, createPapiMethod } from '@tramvai/papi';
27
27
  import { createChildContainer } from '@tinkoff/dippy';
28
28
  import eachObj from '@tinkoff/utils/object/each';
29
+ import { resolve } from 'path';
30
+ import FastifyStatic from 'fastify-static';
29
31
  import { createTerminus } from '@tinkoff/terminus';
30
32
  import { parse } from '@tinkoff/url';
31
33
  import { EventEmitter } from 'events';
@@ -33,7 +35,6 @@ import monkeypatch from '@tinkoff/monkeypatch';
33
35
  import https from 'https';
34
36
  import isArray from '@tinkoff/utils/is/array';
35
37
  import isObject from '@tinkoff/utils/is/object';
36
- import { resolve } from 'path';
37
38
  import { createProxyMiddleware } from 'http-proxy-middleware';
38
39
 
39
40
  const serverFactory = () => {
@@ -323,9 +324,8 @@ const xHeadersFactory = ({ app, envManager, appInfo, }) => {
323
324
  'x-deploy-repository': envManager.get('DEPLOY_REPOSITORY'),
324
325
  });
325
326
  return async () => {
326
- app.use((req, res, next) => {
327
- res.set(xHeaders);
328
- next();
327
+ app.addHook('preHandler', async (_, reply) => {
328
+ reply.headers(xHeaders);
329
329
  });
330
330
  };
331
331
  };
@@ -369,10 +369,20 @@ try {
369
369
  Api = require('@tramvai/cli/lib/external/api').default; // eslint-disable-line import/no-unresolved
370
370
  }
371
371
  catch (e) { }
372
- const getFileApi = () => {
372
+ const getFileApi = ({ logger }) => {
373
+ const log = logger('papi:fileApi');
373
374
  const result = [];
374
375
  eachObj((v, k) => {
375
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
+ }
376
386
  const papiParameters = getPapiParameters(handler);
377
387
  result.push(createPapiMethod({
378
388
  ...v,
@@ -386,7 +396,10 @@ const getFileApi = () => {
386
396
  const fileApiProvider = {
387
397
  provide: SERVER_MODULE_PAPI_PUBLIC_ROUTE,
388
398
  multi: true,
389
- useValue: getFileApi(),
399
+ useFactory: getFileApi,
400
+ deps: {
401
+ logger: LOGGER_TOKEN,
402
+ },
390
403
  };
391
404
 
392
405
  const sharedProviders = [
@@ -521,29 +534,32 @@ let ServerStaticsModule = class ServerStaticsModule {
521
534
  ServerStaticsModule = __decorate([
522
535
  Module({
523
536
  providers: [
524
- {
525
- provide: WEB_APP_BEFORE_INIT_TOKEN,
537
+ provide({
538
+ provide: WEB_FASTIFY_APP_BEFORE_INIT_TOKEN,
526
539
  useFactory: ({ app, options }) => {
527
540
  const path = (options === null || options === void 0 ? void 0 : options.path) || 'public';
528
541
  return () => {
529
- app.use(express.static(path, {
542
+ app.register(FastifyStatic, {
543
+ decorateReply: false,
544
+ prefix: `/${path}`,
545
+ root: resolve(process.cwd(), path),
530
546
  setHeaders: (res) => {
531
547
  const oneYearForward = new Date(Date.now() + ONE_YEAR * 1000);
532
- res.set('cache-control', `public, max-age=${ONE_YEAR}`);
533
- res.set('expires', oneYearForward.toUTCString());
548
+ res.setHeader('cache-control', `public, max-age=${ONE_YEAR}`);
549
+ res.setHeader('expires', oneYearForward.toUTCString());
534
550
  },
535
- }));
551
+ });
536
552
  };
537
553
  },
538
554
  deps: {
539
- app: WEB_APP_TOKEN,
555
+ app: WEB_FASTIFY_APP_TOKEN,
540
556
  options: {
541
557
  token: SERVER_MODULE_STATICS_OPTIONS,
542
558
  optional: true,
543
559
  },
544
560
  },
545
561
  multi: true,
546
- },
562
+ }),
547
563
  ],
548
564
  })
549
565
  ], ServerStaticsModule);
@@ -768,6 +784,8 @@ ServerProxyModule = __decorate([
768
784
  Module({
769
785
  providers: [
770
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
771
789
  provide: WEB_APP_BEFORE_INIT_TOKEN,
772
790
  useFactory: ({ app, defaultProxies }) => {
773
791
  return () => {
@@ -956,11 +974,11 @@ ServerModule = __decorate([
956
974
  ],
957
975
  },
958
976
  {
959
- provide: WEB_APP_BEFORE_INIT_TOKEN,
977
+ provide: WEB_FASTIFY_APP_INIT_TOKEN,
960
978
  multi: true,
961
979
  useFactory: xHeadersFactory,
962
980
  deps: {
963
- app: WEB_APP_TOKEN,
981
+ app: WEB_FASTIFY_APP_TOKEN,
964
982
  envManager: ENV_MANAGER_TOKEN,
965
983
  appInfo: APP_INFO_TOKEN,
966
984
  },
package/lib/server.js CHANGED
@@ -29,6 +29,8 @@ var toArray = require('@tinkoff/utils/array/toArray');
29
29
  var papi = require('@tramvai/papi');
30
30
  var dippy = require('@tinkoff/dippy');
31
31
  var eachObj = require('@tinkoff/utils/object/each');
32
+ var path = require('path');
33
+ var FastifyStatic = require('fastify-static');
32
34
  var terminus = require('@tinkoff/terminus');
33
35
  var url = require('@tinkoff/url');
34
36
  var events = require('events');
@@ -36,7 +38,6 @@ var monkeypatch = require('@tinkoff/monkeypatch');
36
38
  var https = require('https');
37
39
  var isArray = require('@tinkoff/utils/is/array');
38
40
  var isObject = require('@tinkoff/utils/is/object');
39
- var path = require('path');
40
41
  var httpProxyMiddleware = require('http-proxy-middleware');
41
42
 
42
43
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -57,6 +58,7 @@ var filterObj__default = /*#__PURE__*/_interopDefaultLegacy(filterObj);
57
58
  var flatten__default = /*#__PURE__*/_interopDefaultLegacy(flatten);
58
59
  var toArray__default = /*#__PURE__*/_interopDefaultLegacy(toArray);
59
60
  var eachObj__default = /*#__PURE__*/_interopDefaultLegacy(eachObj);
61
+ var FastifyStatic__default = /*#__PURE__*/_interopDefaultLegacy(FastifyStatic);
60
62
  var monkeypatch__default = /*#__PURE__*/_interopDefaultLegacy(monkeypatch);
61
63
  var https__default = /*#__PURE__*/_interopDefaultLegacy(https);
62
64
  var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
@@ -349,9 +351,8 @@ const xHeadersFactory = ({ app, envManager, appInfo, }) => {
349
351
  'x-deploy-repository': envManager.get('DEPLOY_REPOSITORY'),
350
352
  });
351
353
  return async () => {
352
- app.use((req, res, next) => {
353
- res.set(xHeaders);
354
- next();
354
+ app.addHook('preHandler', async (_, reply) => {
355
+ reply.headers(xHeaders);
355
356
  });
356
357
  };
357
358
  };
@@ -395,10 +396,20 @@ try {
395
396
  Api = require('@tramvai/cli/lib/external/api').default; // eslint-disable-line import/no-unresolved
396
397
  }
397
398
  catch (e) { }
398
- const getFileApi = () => {
399
+ const getFileApi = ({ logger }) => {
400
+ const log = logger('papi:fileApi');
399
401
  const result = [];
400
402
  eachObj__default["default"]((v, k) => {
401
403
  const handler = (v.handler || v.default);
404
+ if (!handler) {
405
+ log.error({
406
+ message: `Cannot resolve a papi handler.
407
+ Check that you are using file based papi right way by docs https://tramvai.dev/docs/how-to/how-create-papi#automatic-handler-creation
408
+ 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
409
+ change settings application.commands.build.options.serverApiDir in tramvai.json`,
410
+ });
411
+ throw new Error('Not a papi');
412
+ }
402
413
  const papiParameters = papi.getPapiParameters(handler);
403
414
  result.push(papi.createPapiMethod({
404
415
  ...v,
@@ -412,7 +423,10 @@ const getFileApi = () => {
412
423
  const fileApiProvider = {
413
424
  provide: tokensServer.SERVER_MODULE_PAPI_PUBLIC_ROUTE,
414
425
  multi: true,
415
- useValue: getFileApi(),
426
+ useFactory: getFileApi,
427
+ deps: {
428
+ logger: tokensCommon.LOGGER_TOKEN,
429
+ },
416
430
  };
417
431
 
418
432
  const sharedProviders = [
@@ -547,29 +561,32 @@ let ServerStaticsModule = class ServerStaticsModule {
547
561
  ServerStaticsModule = tslib.__decorate([
548
562
  core.Module({
549
563
  providers: [
550
- {
551
- provide: tokensServer.WEB_APP_BEFORE_INIT_TOKEN,
564
+ core.provide({
565
+ provide: tokensServerPrivate.WEB_FASTIFY_APP_BEFORE_INIT_TOKEN,
552
566
  useFactory: ({ app, options }) => {
553
- const path = (options === null || options === void 0 ? void 0 : options.path) || 'public';
567
+ const path$1 = (options === null || options === void 0 ? void 0 : options.path) || 'public';
554
568
  return () => {
555
- app.use(express__default["default"].static(path, {
569
+ app.register(FastifyStatic__default["default"], {
570
+ decorateReply: false,
571
+ prefix: `/${path$1}`,
572
+ root: path.resolve(process.cwd(), path$1),
556
573
  setHeaders: (res) => {
557
574
  const oneYearForward = new Date(Date.now() + ONE_YEAR * 1000);
558
- res.set('cache-control', `public, max-age=${ONE_YEAR}`);
559
- res.set('expires', oneYearForward.toUTCString());
575
+ res.setHeader('cache-control', `public, max-age=${ONE_YEAR}`);
576
+ res.setHeader('expires', oneYearForward.toUTCString());
560
577
  },
561
- }));
578
+ });
562
579
  };
563
580
  },
564
581
  deps: {
565
- app: tokensServer.WEB_APP_TOKEN,
582
+ app: tokensServerPrivate.WEB_FASTIFY_APP_TOKEN,
566
583
  options: {
567
584
  token: tokensServer.SERVER_MODULE_STATICS_OPTIONS,
568
585
  optional: true,
569
586
  },
570
587
  },
571
588
  multi: true,
572
- },
589
+ }),
573
590
  ],
574
591
  })
575
592
  ], ServerStaticsModule);
@@ -794,6 +811,8 @@ ServerProxyModule = tslib.__decorate([
794
811
  core.Module({
795
812
  providers: [
796
813
  {
814
+ // TODO: tramvai@2 migrate to `fastify` and `fastify-http-proxy`
815
+ // interfaces for the proxies are not compatible so some migration from the app is needed
797
816
  provide: tokensServer.WEB_APP_BEFORE_INIT_TOKEN,
798
817
  useFactory: ({ app, defaultProxies }) => {
799
818
  return () => {
@@ -982,11 +1001,11 @@ exports.ServerModule = tslib.__decorate([
982
1001
  ],
983
1002
  },
984
1003
  {
985
- provide: tokensServer.WEB_APP_BEFORE_INIT_TOKEN,
1004
+ provide: tokensServerPrivate.WEB_FASTIFY_APP_INIT_TOKEN,
986
1005
  multi: true,
987
1006
  useFactory: xHeadersFactory,
988
1007
  deps: {
989
- app: tokensServer.WEB_APP_TOKEN,
1008
+ app: tokensServerPrivate.WEB_FASTIFY_APP_TOKEN,
990
1009
  envManager: tokensCommon.ENV_MANAGER_TOKEN,
991
1010
  appInfo: core.APP_INFO_TOKEN,
992
1011
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-server",
3
- "version": "1.85.0",
3
+ "version": "1.90.2",
4
4
  "description": "",
5
5
  "browser": "lib/browser.js",
6
6
  "main": "lib/server.js",
@@ -20,14 +20,14 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@tinkoff/errors": "0.2.20",
23
- "@tinkoff/terminus": "0.0.1",
24
23
  "@tinkoff/monkeypatch": "1.3.3",
24
+ "@tinkoff/terminus": "0.0.1",
25
25
  "@tinkoff/url": "0.7.37",
26
- "@tramvai/module-cache-warmup": "1.85.0",
27
- "@tramvai/module-metrics": "1.85.0",
28
- "@tramvai/papi": "1.85.0",
29
- "@tramvai/tokens-server": "1.85.0",
30
- "@tramvai/tokens-server-private": "1.85.0",
26
+ "@tramvai/module-cache-warmup": "1.90.2",
27
+ "@tramvai/module-metrics": "1.90.2",
28
+ "@tramvai/papi": "1.90.2",
29
+ "@tramvai/tokens-server": "1.90.2",
30
+ "@tramvai/tokens-server-private": "1.90.2",
31
31
  "body-parser": "^1.19.0",
32
32
  "compression": "^1.7.4",
33
33
  "cookie-parser": "^1.4.3",
@@ -36,16 +36,17 @@
36
36
  "fastify-cookie": "^5.6.0",
37
37
  "fastify-formbody": "^5.2.0",
38
38
  "fastify-plugin": "^3.0.1",
39
+ "fastify-static": "^4.6.1",
39
40
  "http-proxy-middleware": "^2.0.2"
40
41
  },
41
42
  "peerDependencies": {
42
43
  "@tinkoff/dippy": "0.7.39",
43
44
  "@tinkoff/utils": "^2.1.2",
44
- "@tramvai/cli": "1.85.0",
45
- "@tramvai/core": "1.85.0",
46
- "@tramvai/module-common": "1.85.0",
47
- "@tramvai/module-environment": "1.85.0",
48
- "@tramvai/tokens-common": "1.85.0",
45
+ "@tramvai/cli": "1.90.2",
46
+ "@tramvai/core": "1.90.2",
47
+ "@tramvai/module-common": "1.90.2",
48
+ "@tramvai/module-environment": "1.90.2",
49
+ "@tramvai/tokens-common": "1.90.2",
49
50
  "tslib": "^2.0.3"
50
51
  },
51
52
  "devDependencies": {