c2-http 1.0.169 → 1.0.171

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,5 +1,6 @@
1
1
  import express from "express";
2
2
  declare class MiddlewareBody {
3
+ private readonly LIMIT_BYTES;
3
4
  config(expressApplication: express.Application): void;
4
5
  }
5
6
  declare const _default: MiddlewareBody;
@@ -5,14 +5,59 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const body_parser_1 = __importDefault(require("body-parser"));
7
7
  const c2_util_1 = require("c2-util");
8
+ const http_status_1 = require("http-status");
8
9
  // import moment from "moment";
9
10
  class MiddlewareBody {
11
+ LIMIT_BYTES = 1024 * 1024; // 1MB
10
12
  config(expressApplication) {
11
- expressApplication.use(body_parser_1.default.json({ limit: "25mb" }));
12
- expressApplication.use(body_parser_1.default.urlencoded({ limit: "25mb", extended: true })); // Aumentar de 5mb para 25mb
13
- expressApplication.use(body_parser_1.default.text({ limit: "25mb" }));
13
+ // Middleware para verificar tamanho da requisição ANTES do body-parser processar
14
+ expressApplication.use((request, response, next) => {
15
+ const contentLength = request.headers["content-length"];
16
+ if (contentLength && parseInt(contentLength) > this.LIMIT_BYTES) {
17
+ // Logar informações da requisição antes de retornar o erro
18
+ const requestInfo = {
19
+ method: request.method,
20
+ url: request.originalUrl || request.url,
21
+ headers: request.headers,
22
+ contentLength: contentLength,
23
+ contentLengthMB: (parseInt(contentLength) / (1024 * 1024)).toFixed(2) + " MB",
24
+ limitMB: (this.LIMIT_BYTES / (1024 * 1024)).toFixed(2) + " MB",
25
+ ip: request.ip || request.socket.remoteAddress,
26
+ userAgent: request.headers["user-agent"]
27
+ };
28
+ (0, c2_util_1.log)("LOG_REQUEST_TOO_LARGE", JSON.stringify(requestInfo, null, 2));
29
+ return response.status(http_status_1.FORBIDDEN).json({
30
+ message: "Request too large",
31
+ detail: "O tamanho da requisição excede o limite permitido"
32
+ });
33
+ }
34
+ next();
35
+ });
36
+ expressApplication.use(body_parser_1.default.json({ limit: "1mb" }));
37
+ expressApplication.use(body_parser_1.default.urlencoded({ limit: "1mb", extended: true })); // Aumentar de 5mb para 25mb
38
+ expressApplication.use(body_parser_1.default.text({ limit: "1mb" }));
14
39
  // Adicionar parser para tipos não especificados
15
- expressApplication.use(body_parser_1.default.raw({ limit: "25mb", type: "*/*" }));
40
+ expressApplication.use(body_parser_1.default.raw({ limit: "1mb", type: "*/*" }));
41
+ // Error handler para capturar erros do body-parser (caso o content-length não esteja disponível)
42
+ expressApplication.use((error, request, response, next) => {
43
+ if (error && (error.status === 413 || error.type === "entity.too.large" || error.message?.includes("request entity too large"))) {
44
+ // Logar informações da requisição antes de retornar o erro
45
+ const requestInfo = {
46
+ method: request.method,
47
+ url: request.originalUrl || request.url,
48
+ headers: request.headers,
49
+ contentLength: request.headers["content-length"] || "unknown",
50
+ ip: request.ip || request.socket.remoteAddress,
51
+ userAgent: request.headers["user-agent"]
52
+ };
53
+ (0, c2_util_1.log)("LOG_REQUEST_TOO_LARGE", JSON.stringify(requestInfo, null, 2));
54
+ return response.status(http_status_1.FORBIDDEN).json({
55
+ message: "Request too large",
56
+ detail: "O tamanho da requisição excede o limite permitido"
57
+ });
58
+ }
59
+ next(error);
60
+ });
16
61
  expressApplication.use((request, response, next) => {
17
62
  // log("LOG_BODY", "Body middleware enabled")
18
63
  const str = JSON.stringify(request.body);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-http",
3
- "version": "1.0.169",
3
+ "version": "1.0.171",
4
4
  "description": "Biblioteca Typescript para API NodeJS",
5
5
  "repository": "https://cabralsilva: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@github.com/cabralsilva/c2-http.git",
6
6
  "author": "Daniel Cabral <cabralconsultoriaemsoftware@gmail.com>",