@naturalcycles/backend-lib 9.44.1 → 9.45.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.
@@ -35,7 +35,6 @@ export class BaseAdminService {
35
35
  if (!email)
36
36
  return;
37
37
  console.log(`getEmailPermissions (${dimGrey(email)}) returning undefined (please override the implementation)`);
38
- return;
39
38
  }
40
39
  /**
41
40
  * To be extended.
@@ -62,7 +61,6 @@ export class BaseAdminService {
62
61
  return; // skip logging, expected error
63
62
  }
64
63
  req.error(`getEmailByToken error:`, err);
65
- return;
66
64
  }
67
65
  }
68
66
  /**
@@ -5,6 +5,7 @@ import { methodOverrideMiddleware } from '../server/methodOverrideMiddleware.js'
5
5
  import { notFoundMiddleware } from '../server/notFoundMiddleware.js';
6
6
  import { requestTimeoutMiddleware } from '../server/requestTimeoutMiddleware.js';
7
7
  import { simpleRequestLoggerMiddleware } from '../server/simpleRequestLoggerMiddleware.js';
8
+ import { isGAE } from '../util.js';
8
9
  const isTest = process.env['APP_ENV'] === 'test';
9
10
  const isDev = process.env['APP_ENV'] === 'dev';
10
11
  export async function createDefaultApp(cfg) {
@@ -29,6 +30,25 @@ export async function createDefaultApp(cfg) {
29
30
  if (isDev) {
30
31
  app.use(simpleRequestLoggerMiddleware());
31
32
  }
33
+ if (!isTest) {
34
+ // leaks, load lazily
35
+ const { default: helmet } = await import('helmet');
36
+ app.use(helmet({
37
+ contentSecurityPolicy: false, // to allow "admin 401 auto-redirect"
38
+ }));
39
+ }
40
+ app.use(cors({
41
+ origin: true,
42
+ credentials: true,
43
+ // methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // default
44
+ maxAge: 86400,
45
+ ...cfg.corsOptions,
46
+ }));
47
+ if (!isGAE() && isTest) {
48
+ // compression is not used in AppEngine, because AppEngine provides it by default
49
+ const { default: compression } = await import('compression');
50
+ app.use(compression());
51
+ }
32
52
  // app.use(safeJsonMiddleware()) // optional
33
53
  // accepts application/json
34
54
  app.use(express.json({
@@ -55,20 +75,6 @@ export async function createDefaultApp(cfg) {
55
75
  ...cfg.bodyParserRawOptions,
56
76
  }));
57
77
  app.use(cookieParser());
58
- if (!isTest) {
59
- // leaks, load lazily
60
- const { default: helmet } = await import('helmet');
61
- app.use(helmet({
62
- contentSecurityPolicy: false, // to allow "admin 401 auto-redirect"
63
- }));
64
- }
65
- app.use(cors({
66
- origin: true,
67
- credentials: true,
68
- // methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // default
69
- maxAge: 86400,
70
- ...cfg.corsOptions,
71
- }));
72
78
  // app.use(clearBodyParserTimeout()) // removed by default
73
79
  // Static is now disabled by default due to performance
74
80
  // Without: 6500 rpsAvg
@@ -8,7 +8,7 @@ import type { Application, IRouter, NextFunction, Request, Response } from 'expr
8
8
  * `BackendRequest` seems to not conflict with anything right now.
9
9
  * Previous name `ExpressRequest` was clashing with Sentry.
10
10
  */
11
- export interface BackendRequest extends Request {
11
+ export interface BackendRequest<BODY = unknown> extends Request {
12
12
  debug: CommonLogFunction;
13
13
  log: CommonLogFunction;
14
14
  warn: CommonLogFunction;
@@ -19,10 +19,10 @@ export interface BackendRequest extends Request {
19
19
  */
20
20
  userId?: string;
21
21
  /**
22
- * It's set to unknown (instead of `any`) to prevent implicit use of any
22
+ * It defaults to unknown (instead of `any`) to prevent implicit use of any
23
23
  * in unexpected places.
24
24
  */
25
- body: unknown;
25
+ body: BODY;
26
26
  /**
27
27
  * Raw Buffer of the `req.body`, before it's stringified and json-parsed.
28
28
  * Useful for when something mutates `req.body` json (e.g j validation), and you
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.44.1",
4
+ "version": "9.45.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -10,10 +10,12 @@
10
10
  "@naturalcycles/js-lib": "^15",
11
11
  "@naturalcycles/nodejs-lib": "^15",
12
12
  "@types/body-parser": "^1",
13
+ "@types/compression": "^1",
13
14
  "@types/cookie-parser": "^1",
14
15
  "@types/cors": "^2",
15
16
  "@types/express": "^5",
16
17
  "@types/on-finished": "^2",
18
+ "compression": "^1",
17
19
  "cookie-parser": "^1",
18
20
  "cors": "^2",
19
21
  "dotenv": "^17",
@@ -29,7 +31,7 @@
29
31
  "@sentry/node": "^10",
30
32
  "@types/ejs": "^3",
31
33
  "fastify": "^5",
32
- "@naturalcycles/dev-lib": "20.12.11"
34
+ "@naturalcycles/dev-lib": "20.15.0"
33
35
  },
34
36
  "exports": {
35
37
  ".": "./dist/index.js",
@@ -65,7 +65,6 @@ export class BaseAdminService {
65
65
  email,
66
66
  )}) returning undefined (please override the implementation)`,
67
67
  )
68
- return
69
68
  }
70
69
 
71
70
  /**
@@ -107,7 +106,6 @@ export class BaseAdminService {
107
106
  }
108
107
 
109
108
  req.error(`getEmailByToken error:`, err)
110
- return
111
109
  }
112
110
  }
113
111
 
@@ -16,6 +16,7 @@ import type {
16
16
  BackendRequestHandler,
17
17
  } from '../server/server.model.js'
18
18
  import { simpleRequestLoggerMiddleware } from '../server/simpleRequestLoggerMiddleware.js'
19
+ import { isGAE } from '../util.js'
19
20
 
20
21
  const isTest = process.env['APP_ENV'] === 'test'
21
22
  const isDev = process.env['APP_ENV'] === 'dev'
@@ -51,6 +52,32 @@ export async function createDefaultApp(cfg: DefaultAppCfg): Promise<BackendAppli
51
52
  app.use(simpleRequestLoggerMiddleware())
52
53
  }
53
54
 
55
+ if (!isTest) {
56
+ // leaks, load lazily
57
+ const { default: helmet } = await import('helmet')
58
+ app.use(
59
+ helmet({
60
+ contentSecurityPolicy: false, // to allow "admin 401 auto-redirect"
61
+ }),
62
+ )
63
+ }
64
+
65
+ app.use(
66
+ cors({
67
+ origin: true,
68
+ credentials: true,
69
+ // methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // default
70
+ maxAge: 86400,
71
+ ...cfg.corsOptions,
72
+ }),
73
+ )
74
+
75
+ if (!isGAE() && isTest) {
76
+ // compression is not used in AppEngine, because AppEngine provides it by default
77
+ const { default: compression } = await import('compression')
78
+ app.use(compression())
79
+ }
80
+
54
81
  // app.use(safeJsonMiddleware()) // optional
55
82
 
56
83
  // accepts application/json
@@ -88,26 +115,6 @@ export async function createDefaultApp(cfg: DefaultAppCfg): Promise<BackendAppli
88
115
 
89
116
  app.use(cookieParser())
90
117
 
91
- if (!isTest) {
92
- // leaks, load lazily
93
- const { default: helmet } = await import('helmet')
94
- app.use(
95
- helmet({
96
- contentSecurityPolicy: false, // to allow "admin 401 auto-redirect"
97
- }),
98
- )
99
- }
100
-
101
- app.use(
102
- cors({
103
- origin: true,
104
- credentials: true,
105
- // methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', // default
106
- maxAge: 86400,
107
- ...cfg.corsOptions,
108
- }),
109
- )
110
-
111
118
  // app.use(clearBodyParserTimeout()) // removed by default
112
119
 
113
120
  // Static is now disabled by default due to performance
@@ -9,7 +9,7 @@ import type { Application, IRouter, NextFunction, Request, Response } from 'expr
9
9
  * `BackendRequest` seems to not conflict with anything right now.
10
10
  * Previous name `ExpressRequest` was clashing with Sentry.
11
11
  */
12
- export interface BackendRequest extends Request {
12
+ export interface BackendRequest<BODY = unknown> extends Request {
13
13
  debug: CommonLogFunction
14
14
  log: CommonLogFunction
15
15
  warn: CommonLogFunction
@@ -22,10 +22,10 @@ export interface BackendRequest extends Request {
22
22
  userId?: string
23
23
 
24
24
  /**
25
- * It's set to unknown (instead of `any`) to prevent implicit use of any
25
+ * It defaults to unknown (instead of `any`) to prevent implicit use of any
26
26
  * in unexpected places.
27
27
  */
28
- body: unknown
28
+ body: BODY
29
29
 
30
30
  /**
31
31
  * Raw Buffer of the `req.body`, before it's stringified and json-parsed.