@belmonddev/catch-request-express-middleware 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/index.js +55 -8
  2. package/package.json +2 -3
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import http from 'http';
2
2
  import https from 'https';
3
+ import zlib from 'zlib';
3
4
 
4
5
  const override = (module, attr, fn) => {
5
6
  let original = module[attr];
@@ -15,20 +16,51 @@ const override = (module, attr, fn) => {
15
16
 
16
17
  const catchHttpResponse = (reqObject, responseCb) => {
17
18
  override(reqObject, 'emit', (result, eventName, response) => {
18
- let body = '';
19
+ const chunks = [];
19
20
  switch (eventName) {
20
21
  case 'response': {
21
22
  response.on('data', (d) => {
22
- body += d.toString('utf8');
23
+ chunks.push(d);
23
24
  });
24
25
 
25
26
  response.on('end', () => {
26
- responseCb({
27
+ const responseObj = {
27
28
  headers: response.headers,
28
29
  statusCode: response.statusCode,
29
30
  statusMessage: response.statusMessage,
30
- body,
31
- });
31
+ };
32
+ if (
33
+ ['gzip', 'deflate'].indexOf(
34
+ response.headers['content-encoding']
35
+ ) !== -1
36
+ ) {
37
+ const buffer = Buffer.concat(chunks);
38
+ const zlibFunc =
39
+ response.headers['content-encoding'] === 'gzip'
40
+ ? zlib.gunzip
41
+ : zlib.inflate;
42
+ zlibFunc(buffer, (_, decoded) => {
43
+ const stringBody = decoded.toString('utf8');
44
+ responseCb({
45
+ ...responseObj,
46
+ body: /application\/json;?.*?$/.test(
47
+ response.headers['content-type']
48
+ )
49
+ ? JSON.parse(stringBody)
50
+ : stringBody,
51
+ });
52
+ });
53
+ } else {
54
+ const stringBody = chunks.toString('utf8');
55
+ responseCb({
56
+ ...responseObj,
57
+ body: /application\/json;?.*?$/.test(
58
+ response.headers['content-type']
59
+ )
60
+ ? JSON.parse(stringBody)
61
+ : stringBody,
62
+ });
63
+ }
32
64
  });
33
65
  }
34
66
  }
@@ -38,17 +70,28 @@ const catchHttpResponse = (reqObject, responseCb) => {
38
70
  const overrideHttpModule = (httpModule, requestSentCb) => {
39
71
  override(httpModule, 'request', (reqObject, request) => {
40
72
  const requestHasBody = request.headers?.['Content-Length']?.[0] > 0;
73
+ const params = Object.fromEntries(
74
+ new URLSearchParams(request.query).entries()
75
+ );
41
76
 
42
77
  if (requestHasBody) {
43
78
  override(reqObject, 'write', (result, data) => {
44
79
  const body = data.toString();
45
- const responseCb = requestSentCb({ ...request, body });
80
+ const responseCb = requestSentCb({
81
+ ...request,
82
+ params,
83
+ body: /application\/json;?.*?$/.test(
84
+ request.headers?.['Content-Type']?.[0]
85
+ )
86
+ ? JSON.parse(body)
87
+ : body,
88
+ });
46
89
  if (responseCb) {
47
90
  catchHttpResponse(reqObject, responseCb);
48
91
  }
49
92
  });
50
93
  } else {
51
- const responseCb = requestSentCb({ ...request, body: null });
94
+ const responseCb = requestSentCb({ ...request, params, body: null });
52
95
  catchHttpResponse(reqObject, responseCb);
53
96
  }
54
97
  });
@@ -73,12 +116,16 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
73
116
  body = data.toString();
74
117
  }
75
118
  });
119
+
120
+ req.socket.setMaxListeners(100);
76
121
  req.socket.on('close', () => {
77
122
  responseCb({
78
123
  headers: res.getHeaders(),
79
124
  statusCode: res.statusCode,
80
125
  statusMessage: res.statusMessage,
81
- body,
126
+ body: /application\/json;?.*?$/.test(res.getHeaders()['content-type'])
127
+ ? JSON.parse(body)
128
+ : body,
82
129
  });
83
130
  });
84
131
  }
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@belmonddev/catch-request-express-middleware",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "nodemon -L --experimental-specifier-resolution=node ./src/index.js"
7
7
  },
8
8
  "author": "David Escalera",
9
9
  "license": "ISC",
10
- "dependencies": {
11
- },
10
+ "dependencies": {},
12
11
  "devDependencies": {
13
12
  "nodemon": "^2.0.7"
14
13
  },