Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Optimize resolving X-Forwarded-For addresses #5458

Merged
merged 4 commits into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function getTrustProxyFn (tp) {
return tp
}
if (tp === true) {
// Support plain true/false
return function () { return true }
// Support trusting everything
return null
}
if (typeof tp === 'number') {
// Support trusting hop count
Expand Down Expand Up @@ -115,7 +115,8 @@ function buildRequestWithTrustProxy (R, trustProxy) {
Object.defineProperties(_Request.prototype, {
ip: {
get () {
return proxyAddr(this.raw, proxyFn)
const addrs = proxyAddr.all(this.raw, proxyFn)
return addrs[addrs.length - 1]
}
},
Comment on lines 116 to 121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leaving a note here that this won't slow down the performance if only one IP exists, as proxyAddr itself also calls proxyAddr.all, and returns the last element of the returned array, so it does the same thing

ips: {
Expand Down
Loading