@belmonddev/catch-request-express-middleware 3.0.1 → 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 +7 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -58,7 +58,10 @@ const catchHttpResponse = (reqObject, responseCb) => {
58
58
 
59
59
  const overrideHttpModule = (httpModule, requestSentCb) => {
60
60
  override(httpModule, 'request', (reqObject, request) => {
61
- const contentLength = getHeaderValue(request.headers, 'content-length');
61
+ const contentLength = getHeaderValue(
62
+ request.headers || {},
63
+ 'content-length'
64
+ );
62
65
  const requestHasBody = !!contentLength;
63
66
  const params = Object.fromEntries(
64
67
  new URLSearchParams(request.query).entries()
@@ -108,11 +111,11 @@ const callReceivedCbs = (req, res, requestReceivedCb, body) => {
108
111
  });
109
112
 
110
113
  if (responseCb) {
111
- req.socket.setMaxListeners(100);
112
- if (req.socketClosed) {
114
+ res.setMaxListeners(100);
115
+ if (res.writableFinished) {
113
116
  callResponseCb(responseCb, res);
114
117
  } else {
115
- req.socket.on('close', () => {
118
+ res.on('close', () => {
116
119
  callResponseCb(responseCb, res);
117
120
  });
118
121
  }
@@ -132,7 +135,6 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
132
135
  if (!res.bodyWritten.length) {
133
136
  res.bodyWritten = firstData.split('\r\n\r\n')?.[1];
134
137
  }
135
- req.socketClosed = true;
136
138
  }
137
139
  });
138
140
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@belmonddev/catch-request-express-middleware",
3
- "version": "3.0.1",
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"