diff --git a/lib/request.js b/lib/request.js index 63e0c1cd607..c580cc9d70f 100644 --- a/lib/request.js +++ b/lib/request.js @@ -225,7 +225,7 @@ defineGetter(req, 'query', function query(){ var querystring = parse(this).query; return queryparse(querystring); -}); +}, true); /** * Check if the incoming request contains the "Content-Type" @@ -299,7 +299,7 @@ defineGetter(req, 'protocol', function protocol(){ return index !== -1 ? header.substring(0, index).trim() : header.trim() -}); +}, true); /** * Short-hand for: @@ -312,7 +312,7 @@ defineGetter(req, 'protocol', function protocol(){ defineGetter(req, 'secure', function secure(){ return this.protocol === 'https'; -}); +}, true); /** * Return the remote address from the trusted proxy. @@ -327,7 +327,7 @@ defineGetter(req, 'secure', function secure(){ defineGetter(req, 'ip', function ip(){ var trust = this.app.get('trust proxy fn'); return proxyaddr(this, trust); -}); +}, true); /** * When "trust proxy" is set, trusted proxy addresses + client. @@ -350,7 +350,7 @@ defineGetter(req, 'ips', function ips() { addrs.reverse().pop() return addrs -}); +}, true); /** * Return subdomains as an array. @@ -378,7 +378,7 @@ defineGetter(req, 'subdomains', function subdomains() { : [hostname]; return subdomains.slice(offset); -}); +}, true); /** * Short-hand for `url.parse(req.url).pathname`. @@ -389,7 +389,7 @@ defineGetter(req, 'subdomains', function subdomains() { defineGetter(req, 'path', function path() { return parse(this).pathname; -}); +}, true); /** * Parse the "Host" header field to a host. @@ -415,7 +415,7 @@ defineGetter(req, 'host', function host(){ } return val || undefined; -}); +}, true); /** * Parse the "Host" header field to a hostname. @@ -442,7 +442,7 @@ defineGetter(req, 'hostname', function hostname(){ return index !== -1 ? host.substring(0, index) : host; -}); +}, true); /** * Check if the request is fresh, aka @@ -470,7 +470,7 @@ defineGetter(req, 'fresh', function(){ } return false; -}); +}, true); /** * Check if the request is stale, aka @@ -483,7 +483,7 @@ defineGetter(req, 'fresh', function(){ defineGetter(req, 'stale', function stale(){ return !this.fresh; -}); +}, true); /** * Check if the request was an _XMLHttpRequest_. @@ -495,20 +495,34 @@ defineGetter(req, 'stale', function stale(){ defineGetter(req, 'xhr', function xhr(){ var val = this.get('X-Requested-With') || ''; return val.toLowerCase() === 'xmlhttprequest'; -}); +}, true); /** * Helper function for creating a getter on an object. * - * @param {Object} obj - * @param {String} name - * @param {Function} getter + * @param {Object} obj - The object to define the property on. + * @param {String} name - The name of the property. + * @param {Function} getter - The function to call to get the value. + * @param {boolean} enableCache - Whether to cache the value. * @private */ -function defineGetter(obj, name, getter) { +function defineGetter(obj, name, getter, enableCache) { + let cache; + let cacheValid = false; + Object.defineProperty(obj, name, { configurable: true, enumerable: true, - get: getter + get: function () { + if (!enableCache || !cacheValid) { + cache = getter.call(this); + cacheValid = true; + } + return cache; + }, + set: function (value) { + cache = value; + cacheValid = true; + } }); }