@naturalcycles/backend-lib 9.42.4 → 9.43.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.
@@ -33,6 +33,10 @@ export async function createDefaultApp(cfg) {
33
33
  // accepts application/json
34
34
  app.use(express.json({
35
35
  limit: '1mb',
36
+ verify(req, _res, buf) {
37
+ // Store the raw Buffer body
38
+ req.rawBody = buf;
39
+ },
36
40
  ...cfg.bodyParserJsonOptions,
37
41
  }));
38
42
  app.use(express.urlencoded({
@@ -44,6 +48,10 @@ export async function createDefaultApp(cfg) {
44
48
  app.use(express.raw({
45
49
  // inflate: true, // default is `true`
46
50
  limit: '1mb',
51
+ verify(req, _res, buf) {
52
+ // Store the raw Buffer body
53
+ req.rawBody = buf;
54
+ },
47
55
  ...cfg.bodyParserRawOptions,
48
56
  }));
49
57
  app.use(cookieParser());
@@ -18,6 +18,15 @@ export interface BackendRequest extends Request {
18
18
  * Only used for request logging purposes.
19
19
  */
20
20
  userId?: string;
21
+ /**
22
+ * Raw Buffer of the `req.body`, before it's stringified and json-parsed.
23
+ * Useful for when something mutates `req.body` json (e.g j validation), and you
24
+ * want access to the original input.
25
+ *
26
+ * For `req.rawBody` to exist - you need to use `createDefaultApp`, or use the
27
+ * `verify` option of the json parser (copy-paste it from `createDefaultApp`).
28
+ */
29
+ rawBody?: Buffer;
21
30
  /**
22
31
  * Set by requestTimeoutMiddleware.
23
32
  * Can be used to cancel/override the timeout.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.42.4",
4
+ "version": "9.43.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -10,7 +10,11 @@ import { logMiddleware } from '../server/logMiddleware.js'
10
10
  import { methodOverrideMiddleware } from '../server/methodOverrideMiddleware.js'
11
11
  import { notFoundMiddleware } from '../server/notFoundMiddleware.js'
12
12
  import { requestTimeoutMiddleware } from '../server/requestTimeoutMiddleware.js'
13
- import type { BackendApplication, BackendRequestHandler } from '../server/server.model.js'
13
+ import type {
14
+ BackendApplication,
15
+ BackendRequest,
16
+ BackendRequestHandler,
17
+ } from '../server/server.model.js'
14
18
  import { simpleRequestLoggerMiddleware } from '../server/simpleRequestLoggerMiddleware.js'
15
19
 
16
20
  const isTest = process.env['APP_ENV'] === 'test'
@@ -53,6 +57,10 @@ export async function createDefaultApp(cfg: DefaultAppCfg): Promise<BackendAppli
53
57
  app.use(
54
58
  express.json({
55
59
  limit: '1mb',
60
+ verify(req: BackendRequest, _res, buf) {
61
+ // Store the raw Buffer body
62
+ req.rawBody = buf
63
+ },
56
64
  ...cfg.bodyParserJsonOptions,
57
65
  }),
58
66
  )
@@ -70,6 +78,10 @@ export async function createDefaultApp(cfg: DefaultAppCfg): Promise<BackendAppli
70
78
  express.raw({
71
79
  // inflate: true, // default is `true`
72
80
  limit: '1mb',
81
+ verify(req: BackendRequest, _res, buf) {
82
+ // Store the raw Buffer body
83
+ req.rawBody = buf
84
+ },
73
85
  ...cfg.bodyParserRawOptions,
74
86
  }),
75
87
  )
@@ -20,6 +20,15 @@ export interface BackendRequest extends Request {
20
20
  * Only used for request logging purposes.
21
21
  */
22
22
  userId?: string
23
+ /**
24
+ * Raw Buffer of the `req.body`, before it's stringified and json-parsed.
25
+ * Useful for when something mutates `req.body` json (e.g j validation), and you
26
+ * want access to the original input.
27
+ *
28
+ * For `req.rawBody` to exist - you need to use `createDefaultApp`, or use the
29
+ * `verify` option of the json parser (copy-paste it from `createDefaultApp`).
30
+ */
31
+ rawBody?: Buffer
23
32
 
24
33
  /**
25
34
  * Set by requestTimeoutMiddleware.