@belmonddev/catch-request-express-middleware 3.3.4 → 3.3.6
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 +51 -19
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -17,6 +17,24 @@ const override = (module, attr, fn) => {
|
|
|
17
17
|
export const getHeaderValue = (headers, key) =>
|
|
18
18
|
Object.entries(headers || {}).find(([k]) => k.toLowerCase() === key)?.[1];
|
|
19
19
|
|
|
20
|
+
function tryDecompress(buffer) {
|
|
21
|
+
if (zlib.brotliDecompressSync) {
|
|
22
|
+
try {
|
|
23
|
+
return zlib.brotliDecompressSync(buffer);
|
|
24
|
+
} catch {
|
|
25
|
+
try {
|
|
26
|
+
return zlib.gunzipSync(buffer);
|
|
27
|
+
} catch {
|
|
28
|
+
try {
|
|
29
|
+
return zlib.inflateSync(buffer);
|
|
30
|
+
} catch {
|
|
31
|
+
return buffer;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
20
38
|
const catchHttpResponse = (reqObject, responseCb) => {
|
|
21
39
|
override(reqObject, 'emit', (result, eventName, response) => {
|
|
22
40
|
const chunks = [];
|
|
@@ -32,24 +50,10 @@ const catchHttpResponse = (reqObject, responseCb) => {
|
|
|
32
50
|
statusCode: response.statusCode,
|
|
33
51
|
statusMessage: response.statusMessage,
|
|
34
52
|
};
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
);
|
|
39
|
-
if (['gzip', 'deflate'].indexOf(contentEncoding) !== -1) {
|
|
40
|
-
const buffer = Buffer.concat(chunks);
|
|
41
|
-
const zlibFunc =
|
|
42
|
-
contentEncoding === 'gzip' ? zlib.gunzip : zlib.inflate;
|
|
43
|
-
zlibFunc(buffer, (_, decoded) => {
|
|
44
|
-
const stringBody = decoded.toString('utf8');
|
|
45
|
-
const request = { ...responseObj, body: stringBody };
|
|
46
|
-
responseCb(request);
|
|
47
|
-
});
|
|
48
|
-
} else {
|
|
49
|
-
const stringBody = chunks.map((c) => c.toString('utf8')).join('');
|
|
50
|
-
const request = { ...responseObj, body: stringBody };
|
|
51
|
-
responseCb(request);
|
|
52
|
-
}
|
|
53
|
+
const buffer = Buffer.concat(chunks);
|
|
54
|
+
const stringBody = tryDecompress(buffer).toString('utf8');
|
|
55
|
+
const request = { ...responseObj, body: stringBody };
|
|
56
|
+
responseCb(request);
|
|
53
57
|
});
|
|
54
58
|
}
|
|
55
59
|
}
|
|
@@ -109,11 +113,39 @@ const overrideHttpModule = (httpModule, outgoingRequestCb) => {
|
|
|
109
113
|
|
|
110
114
|
reqObject.on('error', (err) => {
|
|
111
115
|
if (responseCb) {
|
|
116
|
+
// Serializar el error con todos sus detalles
|
|
117
|
+
let errorBody;
|
|
118
|
+
if (err && typeof err === 'object') {
|
|
119
|
+
if (err.name === 'AggregateError' && Array.isArray(err.errors)) {
|
|
120
|
+
errorBody = JSON.stringify({
|
|
121
|
+
name: err.name,
|
|
122
|
+
message: err.message,
|
|
123
|
+
stack: err.stack,
|
|
124
|
+
errors: err.errors.map((e) => ({
|
|
125
|
+
name: e?.name,
|
|
126
|
+
message: e?.message,
|
|
127
|
+
code: e?.code,
|
|
128
|
+
stack: e?.stack,
|
|
129
|
+
})),
|
|
130
|
+
});
|
|
131
|
+
} else {
|
|
132
|
+
errorBody = JSON.stringify({
|
|
133
|
+
name: err.name,
|
|
134
|
+
message: err.message,
|
|
135
|
+
code: err.code,
|
|
136
|
+
stack: err.stack,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
// Fallback para errores no estándar (string, número, etc.)
|
|
141
|
+
errorBody = JSON.stringify({ message: String(err) });
|
|
142
|
+
}
|
|
143
|
+
|
|
112
144
|
const response = {
|
|
113
145
|
headers: {},
|
|
114
146
|
statusCode: null,
|
|
115
147
|
statusMessage: null,
|
|
116
|
-
body:
|
|
148
|
+
body: errorBody,
|
|
117
149
|
};
|
|
118
150
|
responseCb(response);
|
|
119
151
|
}
|