@belmonddev/catch-request-express-middleware 1.0.2 → 1.2.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 +48 -17
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -97,7 +97,18 @@ const overrideHttpModule = (httpModule, requestSentCb) => {
97
97
  });
98
98
  };
99
99
 
100
- const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
100
+ const callResponseCb = (responseCb, res) => {
101
+ responseCb({
102
+ headers: res.getHeaders(),
103
+ statusCode: res.statusCode,
104
+ statusMessage: res.statusMessage,
105
+ body: /application\/json;?.*?$/.test(res.getHeaders()['content-type'])
106
+ ? JSON.parse(res.bodyWritten)
107
+ : res.bodyWritten,
108
+ });
109
+ };
110
+
111
+ const callReceivedCbs = (req, res, requestReceivedCb) => {
101
112
  const responseCb = requestReceivedCb({
102
113
  headers: req.headers,
103
114
  method: req.method,
@@ -108,26 +119,46 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
108
119
  body: req.body,
109
120
  });
110
121
  if (responseCb) {
111
- let body = '';
112
- let writeCount = 0;
113
- override(req.socket, 'write', (result, data) => {
114
- writeCount++;
115
- if (writeCount === 2) {
116
- body = data.toString();
122
+ req.socket.setMaxListeners(100);
123
+ if (req.socketClosed) {
124
+ callResponseCb(responseCb, res);
125
+ } else {
126
+ req.socket.on('close', () => {
127
+ callResponseCb(responseCb, res);
128
+ });
129
+ }
130
+ }
131
+ };
132
+
133
+ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
134
+ // we override write method ASAP cause otherwise if the request finishes too fast the response callback is not called
135
+ let writeCount = 0;
136
+ let firstData = '';
137
+ override(req.socket, 'write', (result, data) => {
138
+ writeCount++;
139
+ if (writeCount === 1) {
140
+ firstData = data;
141
+ } else if (writeCount === 2) {
142
+ res.bodyWritten = data.toString();
143
+ if (!res.bodyWritten.length) {
144
+ res.bodyWritten = firstData.split('\r\n\r\n')?.[1];
117
145
  }
146
+ req.socketClosed = true;
147
+ }
148
+ });
149
+
150
+ if (req.headers['content-type'] !== 'application/json') {
151
+ let body = '';
152
+ req.on('data', function (data) {
153
+ body += data;
118
154
  });
119
155
 
120
- req.socket.setMaxListeners(100);
121
- req.socket.on('close', () => {
122
- responseCb({
123
- headers: res.getHeaders(),
124
- statusCode: res.statusCode,
125
- statusMessage: res.statusMessage,
126
- body: /application\/json;?.*?$/.test(res.getHeaders()['content-type'])
127
- ? JSON.parse(body)
128
- : body,
129
- });
156
+ req.on('end', function () {
157
+ req.body = body;
158
+ callReceivedCbs(req, res, requestReceivedCb);
130
159
  });
160
+ } else {
161
+ callReceivedCbs(req, res, requestReceivedCb);
131
162
  }
132
163
  };
133
164
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@belmonddev/catch-request-express-middleware",
3
- "version": "1.0.2",
3
+ "version": "1.2.0",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "nodemon -L --experimental-specifier-resolution=node ./src/index.js"