Skip to content
Open
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
8 changes: 5 additions & 3 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,11 @@ export function getRequestIP(
if (opts.xForwardedFor) {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For#syntax
const _header = event.req.headers.get("x-forwarded-for");
const xForwardedFor = (_header || "")?.split(",").shift()?.trim();
if (xForwardedFor) {
return xForwardedFor;
if (_header) {
const xForwardedFor = _header.split(",")[0].trim();
if (xForwardedFor) {
return xForwardedFor;
}
}
Comment on lines +377 to 382
Copy link
Author

Choose a reason for hiding this comment

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

If we want to code-golf this we can use &&, do you prefer that I write it like this?

Suggested change
if (_header) {
const xForwardedFor = _header.split(",")[0].trim();
if (xForwardedFor) {
return xForwardedFor;
}
}
const xForwardedFor = _header && _header.split(",")[0].trim();
if (xForwardedFor) {
return xForwardedFor;
}

(this may have a slight, but not noticeable impact, as in case _header is falsy it will have to be evaluated as falsy twice)

}

Expand Down