Skip to content

Commit 91e8beb

Browse files
authored
feat(middleware): (#31)
- allow to call the middleware without a response object
1 parent 8bef5f1 commit 91e8beb

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

packages/crowdsec-http-middleware/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const middlewareOptions: ICrowdSecHTTPMiddlewareOptions = {
104104
},
105105
// here, an optional function to extract Ip from request
106106
// you can also use a scenario with "extractIp" capability
107+
// getCurrentIp is prior to scenarios extractIp . If you want to use a default function, create a scenario with only extractIp
107108
getCurrentIp: (req: IncomingMessage) => req.socket.remoteAddress || '0.0.0.0',
108109
//we will see this configurations later
109110
watcher: watcherOptions,

packages/crowdsec-http-middleware/src/CrowdSecHTTPMiddleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class CrowdSecHTTPMiddleware extends CommonsMiddleware {
8888
return this.ipObjectCache.getIpObjectWithCache(ip);
8989
}
9090

91-
protected middlewareFunction = (req: IncomingMessage, res: ServerResponse) => {
91+
protected middlewareFunction = (req: IncomingMessage, res?: ServerResponse) => {
9292
const currentIp = this.getIpObjectFromReq(req);
9393

9494
if (!currentIp.addressMinusSuffix) {
@@ -101,7 +101,7 @@ export class CrowdSecHTTPMiddleware extends CommonsMiddleware {
101101

102102
this.watcher?.middleware(currentIp.addressMinusSuffix, req);
103103

104-
if (this.options.protectedByHeader) {
104+
if (this.options.protectedByHeader && res) {
105105
res.appendHeader('X-Protected-By', 'CrowdSec');
106106
}
107107
};

0 commit comments

Comments
 (0)