@abejarano/ts-express-server 1.7.1 → 1.7.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.
@@ -339,12 +339,19 @@ const createJsonBodyParser = (app) => {
339
339
  if (!req.raw || req.body !== undefined) {
340
340
  return next();
341
341
  }
342
+ const method = String(req.method || "").toUpperCase();
343
+ if (method === "GET" || method === "HEAD") {
344
+ return next();
345
+ }
342
346
  const contentType = String(req.headers["content-type"] || "");
343
347
  if (!contentType.includes("application/json")) {
344
348
  return next();
345
349
  }
346
350
  const limit = getBodyLimit(app);
347
351
  const contentLength = parseContentLength(req.headers["content-length"]);
352
+ if (contentLength === 0) {
353
+ return next();
354
+ }
348
355
  if (contentLength !== undefined && contentLength > limit) {
349
356
  res.status(413).json({ message: "Payload too large" });
350
357
  return;
@@ -26,7 +26,13 @@ class ExpressAdapter {
26
26
  const express = getExpressModule();
27
27
  const bodyParser = require("body-parser");
28
28
  const cookieParser = require("cookie-parser");
29
- expressApp.use(express.json());
29
+ const jsonParser = express.json();
30
+ expressApp.use((req, res, next) => {
31
+ if (!shouldParseJson(req)) {
32
+ return next();
33
+ }
34
+ return jsonParser(req, res, next);
35
+ });
30
36
  expressApp.use(bodyParser.urlencoded({ extended: true }));
31
37
  expressApp.use(cookieParser());
32
38
  expressApp.set("port", port);
@@ -38,3 +44,22 @@ class ExpressAdapter {
38
44
  }
39
45
  }
40
46
  exports.ExpressAdapter = ExpressAdapter;
47
+ const shouldParseJson = (req) => {
48
+ const method = String(req.method || "").toUpperCase();
49
+ if (method === "GET" || method === "HEAD") {
50
+ return false;
51
+ }
52
+ const lengthHeader = req.headers["content-length"];
53
+ if (lengthHeader === undefined) {
54
+ return true;
55
+ }
56
+ const value = Array.isArray(lengthHeader) ? lengthHeader[0] : lengthHeader;
57
+ if (!value) {
58
+ return true;
59
+ }
60
+ const parsed = Number.parseInt(value, 10);
61
+ if (Number.isNaN(parsed)) {
62
+ return true;
63
+ }
64
+ return parsed > 0;
65
+ };
@@ -44,7 +44,9 @@ const createCorsMiddleware = (options) => {
44
44
  res.set("access-control-allow-credentials", "true");
45
45
  }
46
46
  if (options.methods) {
47
- res.set("access-control-allow-methods", Array.isArray(options.methods) ? options.methods.join(",") : options.methods);
47
+ res.set("access-control-allow-methods", Array.isArray(options.methods)
48
+ ? options.methods.join(",")
49
+ : options.methods);
48
50
  }
49
51
  if (options.allowedHeaders) {
50
52
  res.set("access-control-allow-headers", Array.isArray(options.allowedHeaders)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@abejarano/ts-express-server",
3
3
  "author": "angel bejarano / angel.bejarano@jaspesoft.com",
4
- "version": "1.7.1",
4
+ "version": "1.7.2",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [