-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(loggers): allow to ignore urls by regex (#3592)
- Loading branch information
Showing
5 changed files
with
125 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
'@commercetools-backend/loggers': minor | ||
--- | ||
|
||
In the access logger the option `ignoreUrls` supports both strings and regular expressions. | ||
|
||
For strings, the value is matched as-is. You can now use the regular expression to match a certain path structure, for example to ignore requests to load assets. | ||
|
||
```ts | ||
const access = createAccessLoggerMiddleware({ | ||
level: 'info', | ||
json: true, | ||
ignoreUrls: ['/', '/health', /^\/static\/(.*)/], | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { parseIps, createAccessLogSkipper } from './helpers'; | ||
|
||
describe('parseIps', () => { | ||
describe.each` | ||
header | expectedIps | ||
${undefined} | ${[]} | ||
${'1.1.1.1'} | ${['1.1.1.1']} | ||
${'1.1.1.1,2.2.2.2'} | ${['1.1.1.1', '2.2.2.2']} | ||
${['1.1.1.1']} | ${['1.1.1.1']} | ||
${['1.1.1.1', '2.2.2.2']} | ${['1.1.1.1', '2.2.2.2']} | ||
`(`given the header x-forwarded-for "$header"`, ({ header, expectedIps }) => { | ||
it(`it should extract list of IPs as ${expectedIps}`, () => { | ||
expect( | ||
parseIps( | ||
// @ts-ignore: this should be a Request object | ||
{ headers: { 'x-forwarded-for': header } } | ||
) | ||
).toEqual(expectedIps); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('createAccessLogSkipper', () => { | ||
describe.each` | ||
originalUrl | ignoreUrl | shouldSkip | ||
${'/'} | ${'/'} | ${true} | ||
${'/favicon.ico'} | ${'/favicon.ico'} | ${true} | ||
${'/favicon.ico'} | ${/favicon/} | ${true} | ||
${'/static/favicon.ico'} | ${'/favicon.ico'} | ${false} | ||
${'/static/favicon.ico'} | ${/^\/static\/(.*)/} | ${true} | ||
${'/static/chunks/webpack-123.js'} | ${'/static'} | ${false} | ||
${'/static/chunks/webpack-123.js'} | ${/^\/static\/chunks\/(.*)/} | ${true} | ||
`(`given "$originalUrl"`, ({ originalUrl, ignoreUrl, shouldSkip }) => { | ||
it(`when ignoring "${ignoreUrl}" it should result in skip: ${shouldSkip}`, () => { | ||
const skip = createAccessLogSkipper({ ignoreUrls: [ignoreUrl] }); | ||
expect( | ||
skip( | ||
// @ts-ignore: this should be a Request object | ||
{ originalUrl } | ||
) | ||
).toEqual(shouldSkip); | ||
}); | ||
}); | ||
|
||
it('should skip if option "silent" is true', () => { | ||
const skip = createAccessLogSkipper({ silent: true }); | ||
expect( | ||
skip( | ||
// @ts-ignore: this should be a Request object | ||
{} | ||
) | ||
).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Request } from 'express'; | ||
import type { TAccessLoggerOptions } from './types'; | ||
|
||
const parseIps = (request: Request) => { | ||
const forwardedFor = request.headers['x-forwarded-for']; | ||
|
||
if (!forwardedFor) { | ||
return []; | ||
} | ||
|
||
const remoteAddresses = Array.isArray(forwardedFor) | ||
? forwardedFor | ||
: forwardedFor.split(','); | ||
|
||
return remoteAddresses; | ||
}; | ||
|
||
const createAccessLogSkipper = | ||
(options: TAccessLoggerOptions) => (request: Request) => { | ||
if (Boolean(options.silent)) { | ||
return true; | ||
} | ||
const hasMatchingIgnoreUrl = (options.ignoreUrls ?? []).some( | ||
(uriPathOrRegex) => { | ||
if (typeof uriPathOrRegex === 'string') { | ||
return request.originalUrl === uriPathOrRegex; | ||
} | ||
return request.originalUrl.match(uriPathOrRegex); | ||
} | ||
); | ||
return hasMatchingIgnoreUrl; | ||
}; | ||
|
||
const mapRequestMetadata = (request: Request) => { | ||
try { | ||
const remoteAddress = request.socket?.remoteAddress; | ||
const proxyIps = parseIps(request); | ||
const [clientIp] = proxyIps; | ||
return { | ||
clientIp: clientIp ?? remoteAddress, | ||
proxyIps, | ||
hostname: request.socket ? request.hostname : undefined, | ||
...(remoteAddress ? { remoteAddress } : {}), | ||
}; | ||
} catch (error) { | ||
console.error(`Failed to parse request metadata`, error); | ||
return {}; | ||
} | ||
}; | ||
|
||
export { parseIps, createAccessLogSkipper, mapRequestMetadata }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters