@belmonddev/catch-request-express-middleware 3.3.5 → 5.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/dist/incoming.d.ts +3 -0
- package/dist/incoming.d.ts.map +1 -0
- package/dist/incoming.js +76 -0
- package/dist/incoming.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/outgoing.d.ts +3 -0
- package/dist/outgoing.d.ts.map +1 -0
- package/dist/outgoing.js +111 -0
- package/dist/outgoing.js.map +1 -0
- package/dist/types.d.ts +52 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +33 -0
- package/dist/utils.js.map +1 -0
- package/package.json +19 -3
- package/tsconfig.build.json +10 -0
- package/index.js +0 -220
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { IncomingRequestCallback, ExpressReqExtended, ExpressResExtended } from './types.js';
|
|
2
|
+
export declare const catchExpressIncomingRequest: (req: ExpressReqExtended, res: ExpressResExtended, incomingRequestCb: IncomingRequestCallback) => void;
|
|
3
|
+
//# sourceMappingURL=incoming.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"incoming.d.ts","sourceRoot":"","sources":["../src/incoming.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,uBAAuB,EACvB,kBAAkB,EAClB,kBAAkB,EACnB,MAAM,YAAY,CAAC;AAgDpB,eAAO,MAAM,2BAA2B,GACtC,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACvB,mBAAmB,uBAAuB,KACzC,IAqCF,CAAC"}
|
package/dist/incoming.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { override, getHeaderValue } from './utils.js';
|
|
2
|
+
const callResponseCb = (responseCb, res) => {
|
|
3
|
+
const response = {
|
|
4
|
+
headers: res.getHeaders(),
|
|
5
|
+
statusCode: res.statusCode,
|
|
6
|
+
statusMessage: res.statusMessage,
|
|
7
|
+
body: res.bodyWritten || '',
|
|
8
|
+
};
|
|
9
|
+
responseCb(response);
|
|
10
|
+
};
|
|
11
|
+
const callIncomingRequestsCbs = (req, res, incomingRequestsCb, body) => {
|
|
12
|
+
const responseCb = incomingRequestsCb({
|
|
13
|
+
requestDirection: 'incoming',
|
|
14
|
+
originalRequest: req,
|
|
15
|
+
headers: req.headers,
|
|
16
|
+
protocol: req.protocol,
|
|
17
|
+
method: req.method,
|
|
18
|
+
host: req.headers.host,
|
|
19
|
+
search: req._parsedUrl.search,
|
|
20
|
+
pathname: req._parsedUrl.pathname,
|
|
21
|
+
query: req.query,
|
|
22
|
+
body,
|
|
23
|
+
});
|
|
24
|
+
if (responseCb) {
|
|
25
|
+
req.socket.setMaxListeners(100);
|
|
26
|
+
if (req.socketClosed) {
|
|
27
|
+
callResponseCb(responseCb, res);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
res.on('timeout', () => {
|
|
31
|
+
res.statusCode = 408;
|
|
32
|
+
res.end();
|
|
33
|
+
});
|
|
34
|
+
req.socket.on('close', () => {
|
|
35
|
+
callResponseCb(responseCb, res);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
export const catchExpressIncomingRequest = (req, res, incomingRequestCb) => {
|
|
41
|
+
let writeCount = 0;
|
|
42
|
+
let firstData = '';
|
|
43
|
+
res.bodyWritten = '';
|
|
44
|
+
override(req.socket, 'write', (_result, data) => {
|
|
45
|
+
writeCount++;
|
|
46
|
+
if (writeCount === 1) {
|
|
47
|
+
firstData = data;
|
|
48
|
+
}
|
|
49
|
+
else if (writeCount === 2) {
|
|
50
|
+
res.bodyWritten = data.toString();
|
|
51
|
+
if (!res.bodyWritten.length) {
|
|
52
|
+
res.bodyWritten = firstData.split('\r\n\r\n')?.[1];
|
|
53
|
+
}
|
|
54
|
+
req.socketClosed = true;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
if (req.body !== undefined) {
|
|
58
|
+
callIncomingRequestsCbs(req, res, incomingRequestCb, req.body);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const contentLength = getHeaderValue(req.headers, 'content-length');
|
|
62
|
+
if (contentLength) {
|
|
63
|
+
let body = '';
|
|
64
|
+
req.on('data', function (data) {
|
|
65
|
+
body += data;
|
|
66
|
+
});
|
|
67
|
+
req.on('end', function () {
|
|
68
|
+
callIncomingRequestsCbs(req, res, incomingRequestCb, body);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
callIncomingRequestsCbs(req, res, incomingRequestCb, '');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
//# sourceMappingURL=incoming.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"incoming.js","sourceRoot":"","sources":["../src/incoming.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEtD,MAAM,cAAc,GAAG,CAAC,UAA4B,EAAE,GAAuB,EAAQ,EAAE;IACrF,MAAM,QAAQ,GAAqB;QACjC,OAAO,EAAE,GAAG,CAAC,UAAU,EAA6B;QACpD,UAAU,EAAE,GAAG,CAAC,UAAU;QAC1B,aAAa,EAAE,GAAG,CAAC,aAAa;QAChC,IAAI,EAAE,GAAG,CAAC,WAAW,IAAI,EAAE;KAC5B,CAAC;IACF,UAAU,CAAC,QAAQ,CAAC,CAAC;AACvB,CAAC,CAAC;AAEF,MAAM,uBAAuB,GAAG,CAC9B,GAAuB,EACvB,GAAuB,EACvB,kBAA2C,EAC3C,IAAsC,EAChC,EAAE;IACR,MAAM,UAAU,GAAG,kBAAkB,CAAC;QACpC,gBAAgB,EAAE,UAAU;QAC5B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|
|
2
|
+
export { getHeaderValue } from './utils.js';
|
|
3
|
+
declare const catchRequestExpressMiddleware: (incomingRequestCb?: IncomingRequestCallback | null, outgoingRequestCb?: OutgoingRequestCallback | null) => CatchRequestMiddleware;
|
|
4
|
+
export default catchRequestExpressMiddleware;
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,sBAAsB,EAIvB,MAAM,YAAY,CAAC;AAIpB,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,QAAA,MAAM,6BAA6B,GACjC,oBAAmB,uBAAuB,GAAG,IAAW,EACxD,oBAAmB,uBAAuB,GAAG,IAAW,KACvD,sBAeF,CAAC;AAEF,eAAe,6BAA6B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import https from 'https';
|
|
3
|
+
import { overrideHttpModule } from './outgoing.js';
|
|
4
|
+
import { catchExpressIncomingRequest } from './incoming.js';
|
|
5
|
+
export { getHeaderValue } from './utils.js';
|
|
6
|
+
const catchRequestExpressMiddleware = (incomingRequestCb = null, outgoingRequestCb = null) => {
|
|
7
|
+
if (outgoingRequestCb) {
|
|
8
|
+
overrideHttpModule(http, outgoingRequestCb);
|
|
9
|
+
overrideHttpModule(https, outgoingRequestCb);
|
|
10
|
+
}
|
|
11
|
+
return (req, res, next) => {
|
|
12
|
+
if (incomingRequestCb) {
|
|
13
|
+
catchExpressIncomingRequest(req, res, incomingRequestCb);
|
|
14
|
+
}
|
|
15
|
+
next();
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export default catchRequestExpressMiddleware;
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,KAAK,MAAM,OAAO,CAAC;AAU1B,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAE5D,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,MAAM,6BAA6B,GAAG,CACpC,oBAAoD,IAAI,EACxD,oBAAoD,IAAI,EAChC,EAAE;IAC1B,IAAI,iBAAiB,EAAE,CAAC;QACtB,kBAAkB,CAAC,IAA6B,EAAE,iBAAiB,CAAC,CAAC;QACrE,kBAAkB,CAAC,KAA8B,EAAE,iBAAiB,CAAC,CAAC;IACxE,CAAC;IACD,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAQ,EAAE;QAC/D,IAAI,iBAAiB,EAAE,CAAC;YACtB,2BAA2B,CACzB,GAAyB,EACzB,GAAyB,EACzB,iBAAiB,CAClB,CAAC;QACJ,CAAC;QACD,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,eAAe,6BAA6B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outgoing.d.ts","sourceRoot":"","sources":["../src/outgoing.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,uBAAuB,EACvB,UAAU,EAGX,MAAM,YAAY,CAAC;AAoCpB,eAAO,MAAM,kBAAkB,GAC7B,YAAY,UAAU,EACtB,mBAAmB,uBAAuB,KACzC,IAqGF,CAAC"}
|
package/dist/outgoing.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { override, getHeaderValue, tryDecompress } from './utils.js';
|
|
2
|
+
const catchHttpResponse = (reqObject, responseCb) => {
|
|
3
|
+
override(reqObject, 'emit', (result, eventName, response) => {
|
|
4
|
+
const chunks = [];
|
|
5
|
+
switch (eventName) {
|
|
6
|
+
case 'response': {
|
|
7
|
+
const res = response;
|
|
8
|
+
res.on('data', (d) => {
|
|
9
|
+
chunks.push(d);
|
|
10
|
+
});
|
|
11
|
+
res.on('end', () => {
|
|
12
|
+
const responseObj = {
|
|
13
|
+
headers: res.headers,
|
|
14
|
+
statusCode: res.statusCode ?? null,
|
|
15
|
+
statusMessage: res.statusMessage ?? null,
|
|
16
|
+
};
|
|
17
|
+
const buffer = Buffer.concat(chunks);
|
|
18
|
+
const stringBody = tryDecompress(buffer).toString('utf8');
|
|
19
|
+
const capturedResponse = { ...responseObj, body: stringBody };
|
|
20
|
+
responseCb(capturedResponse);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
export const overrideHttpModule = (httpModule, outgoingRequestCb) => {
|
|
27
|
+
override(httpModule, 'request', async (reqObject, urlOrRequest, request) => {
|
|
28
|
+
const opts = (typeof urlOrRequest === 'string' ? request : urlOrRequest);
|
|
29
|
+
const contentLength = getHeaderValue(opts.headers, 'content-length');
|
|
30
|
+
const requestHasBody = !!contentLength;
|
|
31
|
+
const [pathname, search] = opts.path.split('?');
|
|
32
|
+
const query = search
|
|
33
|
+
? Object.fromEntries(new URLSearchParams(search).entries())
|
|
34
|
+
: {};
|
|
35
|
+
const capturedRequest = {
|
|
36
|
+
requestDirection: 'outgoing',
|
|
37
|
+
originalRequest: opts,
|
|
38
|
+
headers: opts.headers,
|
|
39
|
+
protocol: reqObject.protocol?.replace(':', ''),
|
|
40
|
+
method: opts.method,
|
|
41
|
+
host: reqObject.host,
|
|
42
|
+
search: search ? `?${search}` : null,
|
|
43
|
+
pathname,
|
|
44
|
+
query,
|
|
45
|
+
body: {},
|
|
46
|
+
};
|
|
47
|
+
let responseCb;
|
|
48
|
+
if (requestHasBody) {
|
|
49
|
+
let requestBody = '';
|
|
50
|
+
override(reqObject, 'write', (_result, data) => {
|
|
51
|
+
const body = data.toString();
|
|
52
|
+
requestBody += body;
|
|
53
|
+
});
|
|
54
|
+
override(reqObject, 'end', async () => {
|
|
55
|
+
responseCb = await outgoingRequestCb({
|
|
56
|
+
...capturedRequest,
|
|
57
|
+
body: requestBody,
|
|
58
|
+
});
|
|
59
|
+
if (responseCb) {
|
|
60
|
+
catchHttpResponse(reqObject, responseCb);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
responseCb = await outgoingRequestCb({ ...capturedRequest, body: {} });
|
|
66
|
+
if (responseCb) {
|
|
67
|
+
catchHttpResponse(reqObject, responseCb);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
reqObject.on('error', (err) => {
|
|
71
|
+
if (responseCb) {
|
|
72
|
+
let errorBody;
|
|
73
|
+
if (err && typeof err === 'object') {
|
|
74
|
+
const errObj = err;
|
|
75
|
+
if (errObj.name === 'AggregateError' && Array.isArray(errObj.errors)) {
|
|
76
|
+
errorBody = JSON.stringify({
|
|
77
|
+
name: errObj.name,
|
|
78
|
+
message: errObj.message,
|
|
79
|
+
stack: errObj.stack,
|
|
80
|
+
errors: errObj.errors.map((e) => ({
|
|
81
|
+
name: e?.name,
|
|
82
|
+
message: e?.message,
|
|
83
|
+
code: e?.code,
|
|
84
|
+
stack: e?.stack,
|
|
85
|
+
})),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
errorBody = JSON.stringify({
|
|
90
|
+
name: errObj.name,
|
|
91
|
+
message: errObj.message,
|
|
92
|
+
code: errObj.code,
|
|
93
|
+
stack: errObj.stack,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
errorBody = JSON.stringify({ message: String(err) });
|
|
99
|
+
}
|
|
100
|
+
const response = {
|
|
101
|
+
headers: {},
|
|
102
|
+
statusCode: null,
|
|
103
|
+
statusMessage: null,
|
|
104
|
+
body: errorBody,
|
|
105
|
+
};
|
|
106
|
+
responseCb(response);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
//# sourceMappingURL=outgoing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outgoing.js","sourceRoot":"","sources":["../src/outgoing.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAErE,MAAM,iBAAiB,GAAG,CACxB,SAAoB,EACpB,UAA4B,EACtB,EAAE;IACR,QAAQ,CACN,SAA+C,EAC/C,MAAM,EACN,CAAC,MAAe,EAAE,SAAkB,EAAE,QAAiB,EAAE,EAAE;QACzD,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,QAAQ,SAAS,EAAE,CAAC;YAClB,KAAK,UAAU,CAAC,CAAC,CAAC;gBAChB,MAAM,GAAG,GAAG,QAA2B,CAAC;gBACxC,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAS,EAAE,EAAE;oBAC3B,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;gBACjB,CAAC,CAAC,CAAC;gBAEH,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;oBACjB,MAAM,WAAW,GAAmC;wBAClD,OAAO,EAAE,GAAG,CAAC,OAAkC;wBAC/C,UAAU,EAAE,GAAG,CAAC,UAAU,IAAI,IAAI;wBAClC,aAAa,EAAE,GAAG,CAAC,aAAa,IAAI,IAAI;qBACzC,CAAC;oBACF,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;oBACrC,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;oBAC1D,MAAM,gBAAgB,GAAqB,EAAE,GAAG,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;oBAChF,UAAU,CAAC,gBAAgB,CAAC,CAAC;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AK;4BACnB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;gCAChC,IAAI,EAAE,CAAC,EAAE,IAAI;gCACb,OAAO,EAAE,CAAC,EAAE,OAAO;gCACnB,IAAI,EAAE,CAAC,EAAE,IAAI;gCACb,KAAK,EAAE,CAAC,EAAE,KAAK;6BAChB,CAAC,CAAC;yBACJ,CAAC,CAAC;oBACL,CAAC;yBAAM,CAAC;wBACN,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;4BACzB,IAAI,EAAE,MAAM,CAAC,IAAI;4BACjB,OAAO,EAAE,MAAM,CAAC,OAAO;4BACvB,IAAI,EAAE,MAAM,CAAC,IAAI;4BACjB,KAAK,EAAE,MAAM,CAAC,KAAK;yBACpB,CAAC,CAAC;oBACL,CAAC;gBACH,CAAC;qBAAM,CAAC;oBACN,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;gBACvD,CAAC;gBAED,MAAM,QAAQ,GAAqB;oBACjC,OAAO,EAAE,EAAE;oBACX,UAAU,EAAE,IAAI;oBAChB,aAAa,EAAE,IAAI;oBACnB,IAAI,EAAE,SAAS;iBAChB,CAAC;gBACF,UAAU,CAAC,QAAQ,CAAC,CAAC;YACvB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CACF,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import http, { IncomingHttpHeaders } from 'http';
|
|
2
|
+
import { Request, Response, NextFunction } from 'express';
|
|
3
|
+
export interface CapturedRequest {
|
|
4
|
+
requestDirection: 'incoming' | 'outgoing';
|
|
5
|
+
originalRequest: Request | Record<string, string>;
|
|
6
|
+
headers: IncomingHttpHeaders | Record<string, string>;
|
|
7
|
+
protocol: string | undefined;
|
|
8
|
+
method: string | undefined;
|
|
9
|
+
host: string | undefined;
|
|
10
|
+
search: string | null;
|
|
11
|
+
pathname: string;
|
|
12
|
+
query: Record<string, string>;
|
|
13
|
+
body: string | Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
export interface CapturedResponse {
|
|
16
|
+
headers: Record<string, unknown>;
|
|
17
|
+
statusCode: number | null;
|
|
18
|
+
statusMessage: string | null;
|
|
19
|
+
body: string;
|
|
20
|
+
}
|
|
21
|
+
export type ResponseCallback = (response: CapturedResponse) => void;
|
|
22
|
+
export type IncomingRequestCallback = (request: CapturedRequest) => ResponseCallback | null | undefined;
|
|
23
|
+
export type OutgoingRequestCallback = (request: CapturedRequest) => Promise<ResponseCallback | null | undefined>;
|
|
24
|
+
export type OverrideFn<T> = (result: T, ...args: unknown[]) => void;
|
|
25
|
+
export type CatchRequestMiddleware = (req: Request, res: Response, next: NextFunction) => void;
|
|
26
|
+
export interface HttpModule {
|
|
27
|
+
request: typeof http.request;
|
|
28
|
+
}
|
|
29
|
+
export interface RequestOptions {
|
|
30
|
+
headers: Record<string, string>;
|
|
31
|
+
path: string;
|
|
32
|
+
method: string;
|
|
33
|
+
}
|
|
34
|
+
export interface ReqObject {
|
|
35
|
+
protocol?: string;
|
|
36
|
+
host?: string;
|
|
37
|
+
emit: (...args: unknown[]) => boolean;
|
|
38
|
+
write: (data: unknown) => boolean;
|
|
39
|
+
end: (...args: unknown[]) => void;
|
|
40
|
+
on: (event: string, listener: (...args: unknown[]) => void) => void;
|
|
41
|
+
}
|
|
42
|
+
export interface ExpressReqExtended extends Request {
|
|
43
|
+
_parsedUrl: {
|
|
44
|
+
search: string | null;
|
|
45
|
+
pathname: string;
|
|
46
|
+
};
|
|
47
|
+
socketClosed?: boolean;
|
|
48
|
+
}
|
|
49
|
+
export interface ExpressResExtended extends Response {
|
|
50
|
+
bodyWritten?: string;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version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0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { IncomingHttpHeaders } from 'http';
|
|
2
|
+
import type { OverrideFn } from './types.js';
|
|
3
|
+
export declare const override: <T>(module: Record<string, unknown>, attr: string, fn: OverrideFn<T>) => void;
|
|
4
|
+
export declare const getHeaderValue: (headers: IncomingHttpHeaders | Record<string, string> | null | undefined, key: string) => string | string[] | undefined;
|
|
5
|
+
export declare function tryDecompress(buffer: Buffer): Buffer;
|
|
6
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,MAAM,CAAC;AAC3C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,eAAO,MAAM,QAAQ,GAAI,CAAC,EACxB,QAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,MAAM,MAAM,EACZ,IAAI,UAAU,CAAC,CAAC,CAAC,KAChB,IAUF,CAAC;AAEF,eAAO,MAAM,cAAc,GACzB,SAAS,mBAAmB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,GAAG,SAAS,EACxE,KAAK,MAAM,KACV,MAAM,GAAG,MAAM,EAAE,GAAG,SACoD,CAAC;AAE5E,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAiBpD"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import zlib from 'zlib';
|
|
2
|
+
export const override = (module, attr, fn) => {
|
|
3
|
+
const original = module[attr];
|
|
4
|
+
function wrapper(...args) {
|
|
5
|
+
const result = original.apply(this, args);
|
|
6
|
+
fn.apply(this, [result, ...args]);
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
module[attr] = wrapper;
|
|
10
|
+
};
|
|
11
|
+
export const getHeaderValue = (headers, key) => Object.entries(headers || {}).find(([k]) => k.toLowerCase() === key)?.[1];
|
|
12
|
+
export function tryDecompress(buffer) {
|
|
13
|
+
if (zlib.brotliDecompressSync) {
|
|
14
|
+
try {
|
|
15
|
+
return zlib.brotliDecompressSync(buffer);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
try {
|
|
19
|
+
return zlib.gunzipSync(buffer);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
try {
|
|
23
|
+
return zlib.inflateSync(buffer);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return buffer;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return buffer;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAIxB,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,MAA+B,EAC/B,IAAY,EACZ,EAAiB,EACX,EAAE;IACR,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAA8B,CAAC;IAE3D,SAAS,OAAO,CAAgB,GAAG,IAAe;QAChD,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC1C,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;QAClC,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;AACzB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,OAAwE,EACxE,GAAW,EACoB,EAAE,CACjC,MAAM,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAE5E,MAAM,UAAU,aAAa,CAAC,MAAc;IAC1C,IAAI,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC9B,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;QAC3C,CAAC;QAAC,MAAM,CAAC;YACP,IAAI,CAAC;gBACH,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;YACjC,CAAC;YAAC,MAAM,CAAC;gBACP,IAAI,CAAC;oBACH,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;gBAClC,CAAC;gBAAC,MAAM,CAAC;oBACP,OAAO,MAAM,CAAC;gBAChB,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@belmonddev/catch-request-express-middleware",
|
|
3
|
-
"version": "
|
|
4
|
-
"main": "index.js",
|
|
3
|
+
"version": "5.0.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
5
6
|
"author": "David Escalera",
|
|
6
7
|
"license": "ISC",
|
|
7
8
|
"description": "",
|
|
8
9
|
"type": "module",
|
|
9
|
-
"dependencies": {}
|
|
10
|
+
"dependencies": {},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc -p tsconfig.build.json",
|
|
13
|
+
"dev": "tsc -p tsconfig.build.json -w",
|
|
14
|
+
"test": "NODE_OPTIONS=\"--experimental-vm-modules\" jest",
|
|
15
|
+
"test:coverage": "NODE_OPTIONS=\"--experimental-vm-modules\" jest --coverage",
|
|
16
|
+
"prepublishOnly": "npm run build"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/express": "^4.17.21",
|
|
20
|
+
"@types/jest": "^29.5.14",
|
|
21
|
+
"@types/node": "^20.0.0",
|
|
22
|
+
"jest": "^29.7.0",
|
|
23
|
+
"ts-jest": "^29.2.1",
|
|
24
|
+
"typescript": "^5.0.0"
|
|
25
|
+
}
|
|
10
26
|
}
|
package/index.js
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import https from 'https';
|
|
3
|
-
import zlib from 'zlib';
|
|
4
|
-
|
|
5
|
-
const override = (module, attr, fn) => {
|
|
6
|
-
let original = module[attr];
|
|
7
|
-
|
|
8
|
-
function wrapper(...args) {
|
|
9
|
-
const result = original.apply(this, args);
|
|
10
|
-
fn.apply(this, [result, ...args]);
|
|
11
|
-
return result;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module[attr] = wrapper;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export const getHeaderValue = (headers, key) =>
|
|
18
|
-
Object.entries(headers || {}).find(([k]) => k.toLowerCase() === key)?.[1];
|
|
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
|
-
|
|
38
|
-
const catchHttpResponse = (reqObject, responseCb) => {
|
|
39
|
-
override(reqObject, 'emit', (result, eventName, response) => {
|
|
40
|
-
const chunks = [];
|
|
41
|
-
switch (eventName) {
|
|
42
|
-
case 'response': {
|
|
43
|
-
response.on('data', (d) => {
|
|
44
|
-
chunks.push(d);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
response.on('end', () => {
|
|
48
|
-
const responseObj = {
|
|
49
|
-
headers: response.headers,
|
|
50
|
-
statusCode: response.statusCode,
|
|
51
|
-
statusMessage: response.statusMessage,
|
|
52
|
-
};
|
|
53
|
-
const buffer = Buffer.concat(chunks);
|
|
54
|
-
const stringBody = tryDecompress(buffer).toString('utf8');
|
|
55
|
-
const request = { ...responseObj, body: stringBody };
|
|
56
|
-
responseCb(request);
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const overrideHttpModule = (httpModule, outgoingRequestCb) => {
|
|
64
|
-
override(httpModule, 'request', async (reqObject, urlOrRequest, request) => {
|
|
65
|
-
// node-fetch v3+ will return the 2nd arg as url, so correctly identify the request object
|
|
66
|
-
request = typeof urlOrRequest === 'string' ? request : urlOrRequest;
|
|
67
|
-
|
|
68
|
-
const contentLength = getHeaderValue(request.headers, 'content-length');
|
|
69
|
-
const requestHasBody = !!contentLength;
|
|
70
|
-
|
|
71
|
-
const [pathname, search] = request.path.split('?');
|
|
72
|
-
const query = search
|
|
73
|
-
? Object.fromEntries(new URLSearchParams(search).entries())
|
|
74
|
-
: {};
|
|
75
|
-
|
|
76
|
-
// add values not included in node-fetch v3+
|
|
77
|
-
request = {
|
|
78
|
-
requestDirection: 'outgoing',
|
|
79
|
-
originalRequest: request,
|
|
80
|
-
headers: request.headers,
|
|
81
|
-
protocol: reqObject.protocol?.replace(':', ''),
|
|
82
|
-
method: request.method,
|
|
83
|
-
host: reqObject.host,
|
|
84
|
-
search: search ? `?${search}` : null,
|
|
85
|
-
pathname,
|
|
86
|
-
query,
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
let responseCb;
|
|
90
|
-
if (requestHasBody) {
|
|
91
|
-
let requestBody = '';
|
|
92
|
-
override(reqObject, 'write', (result, data) => {
|
|
93
|
-
const body = data.toString();
|
|
94
|
-
requestBody += body;
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
override(reqObject, 'end', async () => {
|
|
98
|
-
responseCb = await outgoingRequestCb({
|
|
99
|
-
...request,
|
|
100
|
-
body: requestBody,
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
if (responseCb) {
|
|
104
|
-
catchHttpResponse(reqObject, responseCb);
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
} else {
|
|
108
|
-
responseCb = await outgoingRequestCb({ ...request, body: {} });
|
|
109
|
-
if (responseCb) {
|
|
110
|
-
catchHttpResponse(reqObject, responseCb);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
reqObject.on('error', (err) => {
|
|
115
|
-
if (responseCb) {
|
|
116
|
-
const response = {
|
|
117
|
-
headers: {},
|
|
118
|
-
statusCode: null,
|
|
119
|
-
statusMessage: null,
|
|
120
|
-
body: err.toString(),
|
|
121
|
-
};
|
|
122
|
-
responseCb(response);
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const callResponseCb = (responseCb, res) => {
|
|
129
|
-
const response = {
|
|
130
|
-
headers: res.getHeaders(),
|
|
131
|
-
statusCode: res.statusCode,
|
|
132
|
-
statusMessage: res.statusMessage,
|
|
133
|
-
body: res.bodyWritten,
|
|
134
|
-
};
|
|
135
|
-
responseCb(response);
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const callIncomingRequestsCbs = (req, res, incomingRequestsCb, body) => {
|
|
139
|
-
const responseCb = incomingRequestsCb({
|
|
140
|
-
requestDirection: 'incoming',
|
|
141
|
-
originalRequest: req,
|
|
142
|
-
headers: req.headers,
|
|
143
|
-
protocol: req.protocol,
|
|
144
|
-
method: req.method,
|
|
145
|
-
host: req.headers.host,
|
|
146
|
-
search: req._parsedUrl.search,
|
|
147
|
-
pathname: req._parsedUrl.pathname,
|
|
148
|
-
query: req.query,
|
|
149
|
-
body,
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
if (responseCb) {
|
|
153
|
-
req.socket.setMaxListeners(100);
|
|
154
|
-
if (req.socketClosed) {
|
|
155
|
-
callResponseCb(responseCb, res);
|
|
156
|
-
} else {
|
|
157
|
-
res.on('timeout', () => {
|
|
158
|
-
res.statusCode = 408;
|
|
159
|
-
res.end();
|
|
160
|
-
});
|
|
161
|
-
req.socket.on('close', () => {
|
|
162
|
-
callResponseCb(responseCb, res);
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
const catchExpressIncomingRequest = (req, res, incomingRequestCb) => {
|
|
169
|
-
// we override write method ASAP cause otherwise if the request finishes too fast the response callback is not called
|
|
170
|
-
let writeCount = 0;
|
|
171
|
-
let firstData = '';
|
|
172
|
-
res.bodyWritten = '';
|
|
173
|
-
override(req.socket, 'write', (result, data) => {
|
|
174
|
-
writeCount++;
|
|
175
|
-
if (writeCount === 1) {
|
|
176
|
-
firstData = data;
|
|
177
|
-
} else if (writeCount === 2) {
|
|
178
|
-
res.bodyWritten = data.toString();
|
|
179
|
-
if (!res.bodyWritten.length) {
|
|
180
|
-
res.bodyWritten = firstData.split('\r\n\r\n')?.[1];
|
|
181
|
-
}
|
|
182
|
-
req.socketClosed = true;
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
if (req.body !== undefined) {
|
|
187
|
-
callIncomingRequestsCbs(req, res, incomingRequestCb, req.body);
|
|
188
|
-
} else {
|
|
189
|
-
const contentLength = getHeaderValue(req.headers, 'content-length');
|
|
190
|
-
if (contentLength) {
|
|
191
|
-
let body = '';
|
|
192
|
-
req.on('data', function (data) {
|
|
193
|
-
body += data;
|
|
194
|
-
});
|
|
195
|
-
req.on('end', function () {
|
|
196
|
-
callIncomingRequestsCbs(req, res, incomingRequestCb, body);
|
|
197
|
-
});
|
|
198
|
-
} else {
|
|
199
|
-
callIncomingRequestsCbs(req, res, incomingRequestCb, '');
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const catchRequestExpressMiddleware = (
|
|
205
|
-
incomingRequestCb = null,
|
|
206
|
-
outgoingRequestCb = null
|
|
207
|
-
) => {
|
|
208
|
-
if (outgoingRequestCb) {
|
|
209
|
-
overrideHttpModule(http, outgoingRequestCb);
|
|
210
|
-
overrideHttpModule(https, outgoingRequestCb);
|
|
211
|
-
}
|
|
212
|
-
return (req, res, next) => {
|
|
213
|
-
if (incomingRequestCb) {
|
|
214
|
-
catchExpressIncomingRequest(req, res, incomingRequestCb);
|
|
215
|
-
}
|
|
216
|
-
next();
|
|
217
|
-
};
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
export default catchRequestExpressMiddleware;
|