@belmonddev/catch-request-express-middleware 1.2.4 → 2.0.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.
- package/index.js +35 -53
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -14,6 +14,9 @@ const override = (module, attr, fn) => {
|
|
|
14
14
|
module[attr] = wrapper;
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
+
export const getHeaderValue = (headers, key) =>
|
|
18
|
+
Object.entries(headers).find(([k]) => k.toLowerCase() === key)?.[1];
|
|
19
|
+
|
|
17
20
|
const catchHttpResponse = (reqObject, responseCb) => {
|
|
18
21
|
override(reqObject, 'emit', (result, eventName, response) => {
|
|
19
22
|
const chunks = [];
|
|
@@ -29,26 +32,17 @@ const catchHttpResponse = (reqObject, responseCb) => {
|
|
|
29
32
|
statusCode: response.statusCode,
|
|
30
33
|
statusMessage: response.statusMessage,
|
|
31
34
|
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
) {
|
|
35
|
+
const contentEncoding = getHeaderValue(
|
|
36
|
+
response.headers,
|
|
37
|
+
'content-encoding'
|
|
38
|
+
);
|
|
39
|
+
if (['gzip', 'deflate'].indexOf(contentEncoding) !== -1) {
|
|
37
40
|
const buffer = Buffer.concat(chunks);
|
|
38
41
|
const zlibFunc =
|
|
39
|
-
|
|
40
|
-
? zlib.gunzip
|
|
41
|
-
: zlib.inflate;
|
|
42
|
+
contentEncoding === 'gzip' ? zlib.gunzip : zlib.inflate;
|
|
42
43
|
zlibFunc(buffer, (_, decoded) => {
|
|
43
44
|
const stringBody = decoded.toString('utf8');
|
|
44
|
-
const request = {
|
|
45
|
-
...responseObj,
|
|
46
|
-
body: /application\/json;?.*?$/.test(
|
|
47
|
-
response.headers['content-type']
|
|
48
|
-
)
|
|
49
|
-
? JSON.parse(stringBody)
|
|
50
|
-
: stringBody,
|
|
51
|
-
};
|
|
45
|
+
const request = { ...responseObj, body: stringBody };
|
|
52
46
|
if (responseCb?.then) {
|
|
53
47
|
responseCb.then(() => request);
|
|
54
48
|
} else {
|
|
@@ -57,14 +51,7 @@ const catchHttpResponse = (reqObject, responseCb) => {
|
|
|
57
51
|
});
|
|
58
52
|
} else {
|
|
59
53
|
const stringBody = chunks.map((c) => c.toString('utf8')).join('');
|
|
60
|
-
const request = {
|
|
61
|
-
...responseObj,
|
|
62
|
-
body: /application\/json;?.*?$/.test(
|
|
63
|
-
response.headers['content-type']
|
|
64
|
-
)
|
|
65
|
-
? JSON.parse(stringBody)
|
|
66
|
-
: stringBody,
|
|
67
|
-
};
|
|
54
|
+
const request = { ...responseObj, body: stringBody };
|
|
68
55
|
if (responseCb?.then) {
|
|
69
56
|
responseCb.then(() => request);
|
|
70
57
|
} else {
|
|
@@ -79,7 +66,8 @@ const catchHttpResponse = (reqObject, responseCb) => {
|
|
|
79
66
|
|
|
80
67
|
const overrideHttpModule = (httpModule, requestSentCb) => {
|
|
81
68
|
override(httpModule, 'request', (reqObject, request) => {
|
|
82
|
-
const
|
|
69
|
+
const contentLength = getHeaderValue(request.headers, 'content-length');
|
|
70
|
+
const requestHasBody = !!contentLength;
|
|
83
71
|
const params = Object.fromEntries(
|
|
84
72
|
new URLSearchParams(request.query).entries()
|
|
85
73
|
);
|
|
@@ -90,11 +78,7 @@ const overrideHttpModule = (httpModule, requestSentCb) => {
|
|
|
90
78
|
const responseCb = requestSentCb({
|
|
91
79
|
...request,
|
|
92
80
|
params,
|
|
93
|
-
body
|
|
94
|
-
request.headers?.['Content-Type']?.[0]
|
|
95
|
-
)
|
|
96
|
-
? JSON.parse(body)
|
|
97
|
-
: body,
|
|
81
|
+
body,
|
|
98
82
|
});
|
|
99
83
|
if (responseCb) {
|
|
100
84
|
catchHttpResponse(reqObject, responseCb);
|
|
@@ -102,26 +86,19 @@ const overrideHttpModule = (httpModule, requestSentCb) => {
|
|
|
102
86
|
});
|
|
103
87
|
} else {
|
|
104
88
|
const responseCb = requestSentCb({ ...request, params, body: null });
|
|
105
|
-
|
|
89
|
+
if (responseCb) {
|
|
90
|
+
catchHttpResponse(reqObject, responseCb);
|
|
91
|
+
}
|
|
106
92
|
}
|
|
107
93
|
});
|
|
108
94
|
};
|
|
109
95
|
|
|
110
96
|
const callResponseCb = (responseCb, res) => {
|
|
111
|
-
let body;
|
|
112
|
-
try {
|
|
113
|
-
body = /application\/json;?.*?$/.test(res.getHeaders()['content-type'])
|
|
114
|
-
? JSON.parse(res.bodyWritten)
|
|
115
|
-
: res.bodyWritten;
|
|
116
|
-
} catch (e) {
|
|
117
|
-
body = res.bodyWritten;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
97
|
const response = {
|
|
121
98
|
headers: res.getHeaders(),
|
|
122
99
|
statusCode: res.statusCode,
|
|
123
100
|
statusMessage: res.statusMessage,
|
|
124
|
-
body,
|
|
101
|
+
body: res.bodyWritten,
|
|
125
102
|
};
|
|
126
103
|
if (responseCb?.then) {
|
|
127
104
|
responseCb.then(() => response);
|
|
@@ -130,15 +107,16 @@ const callResponseCb = (responseCb, res) => {
|
|
|
130
107
|
}
|
|
131
108
|
};
|
|
132
109
|
|
|
133
|
-
const callReceivedCbs = (req, res, requestReceivedCb) => {
|
|
110
|
+
const callReceivedCbs = (req, res, requestReceivedCb, body) => {
|
|
134
111
|
const responseCb = requestReceivedCb({
|
|
112
|
+
originalRequest: req,
|
|
135
113
|
headers: req.headers,
|
|
136
114
|
method: req.method,
|
|
137
115
|
host: req.headers.host,
|
|
138
116
|
pathname: req._parsedUrl.pathname,
|
|
139
117
|
params: req.params,
|
|
140
118
|
query: req.query,
|
|
141
|
-
body
|
|
119
|
+
body,
|
|
142
120
|
});
|
|
143
121
|
|
|
144
122
|
if (responseCb) {
|
|
@@ -170,17 +148,21 @@ const catchExpressReceivedRequest = (req, res, requestReceivedCb) => {
|
|
|
170
148
|
}
|
|
171
149
|
});
|
|
172
150
|
|
|
173
|
-
if (req.
|
|
174
|
-
|
|
175
|
-
req.on('data', function (data) {
|
|
176
|
-
body += data;
|
|
177
|
-
});
|
|
178
|
-
req.on('end', function () {
|
|
179
|
-
req.body = body;
|
|
180
|
-
callReceivedCbs(req, res, requestReceivedCb);
|
|
181
|
-
});
|
|
151
|
+
if (req.body !== undefined) {
|
|
152
|
+
callReceivedCbs(req, res, requestReceivedCb, req.body);
|
|
182
153
|
} else {
|
|
183
|
-
|
|
154
|
+
const contentLength = getHeaderValue(req.headers, 'content-length');
|
|
155
|
+
if (contentLength) {
|
|
156
|
+
let body = '';
|
|
157
|
+
req.on('data', function (data) {
|
|
158
|
+
body += data;
|
|
159
|
+
});
|
|
160
|
+
req.on('end', function () {
|
|
161
|
+
callReceivedCbs(req, res, requestReceivedCb, body);
|
|
162
|
+
});
|
|
163
|
+
} else {
|
|
164
|
+
callReceivedCbs(req, res, requestReceivedCb, '');
|
|
165
|
+
}
|
|
184
166
|
}
|
|
185
167
|
};
|
|
186
168
|
|