From ad1efeaf2315c1ccd25c1e5bccb21df626c1d12c Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Thu, 14 Sep 2017 21:12:36 -0400 Subject: [PATCH] perf: reduce overhead when no X-Forwarded-For header --- HISTORY.md | 5 +++++ index.js | 25 ++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index afbb768..7d8b47a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +unreleased +========== + + * perf: reduce overhead when no `X-Forwarded-For` header + 0.1.1 / 2017-09-10 ================== diff --git a/index.js b/index.js index e62605d..5739d36 100644 --- a/index.js +++ b/index.js @@ -34,14 +34,29 @@ function forwarded (req) { } // simple header parsing - var proxyAddrs = (req.headers['x-forwarded-for'] || '') - .trim() - .split(TOKEN_LIST_REGEXP) - .filter(Boolean) - .reverse() + var proxyAddrs = parse(req.headers['x-forwarded-for'] || '') var socketAddr = req.connection.remoteAddress var addrs = [socketAddr].concat(proxyAddrs) // return all addresses return addrs } + +/** + * Parse the X-Forwarded-For header. + * + * @param {string} header + * @private + */ + +function parse (header) { + if (!header) { + return [] + } + + return header + .trim() + .split(TOKEN_LIST_REGEXP) + .filter(Boolean) + .reverse() +}