@belmonddev/catch-request-express-middleware 3.0.0 → 3.1.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.
Files changed (2) hide show
  1. package/index.js +10 -20
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -43,20 +43,12 @@ const catchHttpResponse = (reqObject, responseCb) => {
43
43
  zlibFunc(buffer, (_, decoded) => {
44
44
  const stringBody = decoded.toString('utf8');
45
45
  const request = { ...responseObj, body: stringBody };
46
- if (responseCb?.then) {
47
- responseCb.then(() => request);
48
- } else {
49
- responseCb(request);
50
- }
46
+ responseCb(request);
51
47
  });
52
48
  } else {
53
49
  const stringBody = chunks.map((c) => c.toString('utf8')).join('');
54
50
  const request = { ...responseObj, body: stringBody };
55
- if (responseCb?.then) {
56
- responseCb.then(() => request);
57
- } else {
58
- responseCb(request);
59
- }
51
+ responseCb(request);
60
52
  }
61
53
  });
62
54
  }
@@ -66,7 +58,10 @@ const catchHttpResponse = (reqObject, responseCb) => {
66
58
 
67
59
  const overrideHttpModule = (httpModule, requestSentCb) => {
68
60
  override(httpModule, 'request', (reqObject, request) => {
69
- const contentLength = getHeaderValue(request.headers, 'content-length');
61
+ const contentLength = getHeaderValue(
62
+ request.headers || {},
63
+ 'content-length'
64
+ );
70
65
  const requestHasBody = !!contentLength;
71
66
  const params = Object.fromEntries(
72
67
  new URLSearchParams(request.query).entries()
@@ -100,11 +95,7 @@ const callResponseCb = (responseCb, res) => {
100
95
  statusMessage: res.statusMessage,
101
96
  body: res.bodyWritten,
102
97
  };
103
- if (responseCb?.then) {
104
- responseCb.then(() => response);
105
- } else {
106
- responseCb(response);
107
- }
98
+ responseCb(response);
108
99
  };
109
100
 
110
101
  const callReceivedCbs = (req, res, requestReceivedCb, body) => {
@@ -120,11 +111,11 @@ const callReceivedCbs = (req, res, requestReceivedCb, body) => {
120
111
  });
121
112
 
122
113
  if (responseCb) {
123
- req.socket.setMaxListeners(100);
124
- if (req.socketClosed) {
114
+ res.setMaxListeners(100);
115
+ if (res.writableFinished) {
125
116
  callResponseCb(responseCb, res);
126
117
  } else {
127
- req.socket.on('close', () => {
118
+ res.on('close', () => {
128
119
  callResponseCb(responseCb, res);
129
120
  });
130
121
  }
@@ -144,7 +135,6 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
144
135
  if (!res.bodyWritten.length) {
145
136
  res.bodyWritten = firstData.split('\r\n\r\n')?.[1];
146
137
  }
147
- req.socketClosed = true;
148
138
  }
149
139
  });
150
140
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@belmonddev/catch-request-express-middleware",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "nodemon -L --experimental-specifier-resolution=node ./src/index.js"