superagent-cache
Advanced tools
Comparing version 1.3.2 to 1.3.3
{ | ||
"name": "superagent-cache", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Superagent with flexible built-in caching.", | ||
@@ -5,0 +5,0 @@ "main": "superagentCache.js", |
@@ -0,1 +1,3 @@ | ||
var utils = require('./utils'); | ||
/** | ||
@@ -16,3 +18,3 @@ * superagentCache constructor | ||
var Request = superagent.Request; | ||
var props = resetProps(superagent.defaults); | ||
var props = utils.resetProps(superagent.defaults); | ||
var supportedMethods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE']; | ||
@@ -103,3 +105,3 @@ var cacheableMethods = ['GET', 'HEAD']; | ||
Request.prototype._end = function(cb){ | ||
props = resetProps(superagent.defaults); | ||
props = utils.resetProps(superagent.defaults); | ||
this.execute(cb); | ||
@@ -114,10 +116,10 @@ } | ||
var curProps = props; | ||
props = resetProps(superagent.defaults); | ||
props = utils.resetProps(superagent.defaults); | ||
if(~supportedMethods.indexOf(this.method)){ | ||
var _this = this; | ||
var key = keygen(this, curProps); | ||
var key = utils.keygen(superagent, this, curProps); | ||
if(~cacheableMethods.indexOf(this.method)){ | ||
superagent.cache.get(key, function (err, response){ | ||
if(!err && response){ | ||
callbackExecutor(cb, err, response, key); | ||
utils.callbackExecutor(cb, err, response, key); | ||
} | ||
@@ -128,3 +130,3 @@ else{ | ||
if(err) { | ||
return callbackExecutor(cb, err, response, key); | ||
return utils.callbackExecutor(cb, err, response, key); | ||
} | ||
@@ -139,15 +141,15 @@ else if(!err && response){ | ||
else{ | ||
response = gutResponse(response); | ||
response = utils.gutResponse(response); | ||
} | ||
if(!isEmpty(response) || curProps.cacheWhenEmpty){ | ||
if(!utils.isEmpty(response) || curProps.cacheWhenEmpty){ | ||
var refresh = curProps.backgroundRefresh || null; | ||
if(typeof refresh == 'boolean'){ | ||
refresh = getBackgroundRefreshFunction(curProps); | ||
refresh = utils.getBackgroundRefreshFunction(superagent, curProps); | ||
} | ||
superagent.cache.set(key, response, curProps.expiration, refresh, function (){ | ||
callbackExecutor(cb, err, response, key); | ||
utils.callbackExecutor(cb, err, response, key); | ||
}); | ||
} | ||
else{ | ||
callbackExecutor(cb, err, response, key); | ||
utils.callbackExecutor(cb, err, response, key); | ||
} | ||
@@ -158,3 +160,3 @@ } | ||
else{ | ||
callbackExecutor(cb, null, null, key); | ||
utils.callbackExecutor(cb, null, null, key); | ||
} | ||
@@ -167,3 +169,3 @@ } | ||
if(err){ | ||
return callbackExecutor(cb, err, response, key); | ||
return utils.callbackExecutor(cb, err, response, key); | ||
} | ||
@@ -174,3 +176,3 @@ if(!err && response){ | ||
superagent.cache.del([keyGet, keyHead], function (){ | ||
callbackExecutor(cb, err, response, key); | ||
utils.callbackExecutor(cb, err, response, key); | ||
}); | ||
@@ -183,220 +185,6 @@ } | ||
this._end(function (err, response){ | ||
callbackExecutor(cb, err, response, undefined); | ||
utils.callbackExecutor(cb, err, response, undefined); | ||
}); | ||
} | ||
} | ||
/** | ||
* Set this.req to null so that future http calls get a branc new req object | ||
*/ | ||
Request.prototype.reset = function(){ | ||
this.req = null; | ||
} | ||
/** | ||
* Generate a cache key unique to this query | ||
* @param {object} reg | ||
* @param {object} cProps | ||
*/ | ||
function keygen(req, cProps){ | ||
var cleanParams = null; | ||
var cleanOptions = null; | ||
var params = getQueryParams(req); | ||
var options = getHeaderOptions(req); | ||
if(cProps.pruneParams || cProps.pruneOptions){ | ||
cleanParams = (cProps.pruneParams) ? pruneObj(cloneObject(params), cProps.pruneParams) : params; | ||
cleanOptions = (cProps.pruneOptions) ? pruneObj(cloneObject(options), cProps.pruneOptions, true) : options; | ||
} | ||
return JSON.stringify({ | ||
nameSpace: superagent.cache.nameSpace, | ||
method: req.method, | ||
uri: req.url, | ||
params: cleanParams || params || null, | ||
options: cleanOptions || options || null | ||
}); | ||
} | ||
function getQueryParams(req){ | ||
if(req && req.qs && !isEmpty(req.qs)){ | ||
return req.qs; | ||
} | ||
else if(req && req.qsRaw){ | ||
return arrayToObj(req.qsRaw); | ||
} | ||
else if(req && req.req){ | ||
return stringToObj(req.req.path); | ||
} | ||
else if(req && req._query){ | ||
return stringToObj(req._query.join('&')); | ||
} | ||
return null; | ||
} | ||
function getHeaderOptions(req){ | ||
if(req && req.req && req.req._headers){ | ||
return req.req._headers; | ||
} | ||
else if(req && req._header){ | ||
return req._header; | ||
} | ||
return null; | ||
} | ||
/** | ||
* Convert an array to an object | ||
* @param {array} arr | ||
*/ | ||
function arrayToObj(arr){ | ||
if(arr && arr.length){ | ||
var obj = {}; | ||
for(var i = 0; i < arr.length; i++){ | ||
var str = arr[i]; | ||
var kvArray = str.split('&'); | ||
for(var j = 0; j < kvArray.length; j++){ | ||
var kvString = kvArray[j].split('='); | ||
obj[kvString[0]] = kvString[1]; | ||
} | ||
} | ||
return obj; | ||
} | ||
return null; | ||
} | ||
/** | ||
* Convert a string to an object | ||
* @param {string} str | ||
*/ | ||
function stringToObj(str){ | ||
if(str){ | ||
var obj = {}; | ||
if(~str.indexOf('?')){ | ||
var strs = str.split('?'); | ||
str = strs[1]; | ||
} | ||
var kvArray = str.split('&'); | ||
for(var i = 0; i < kvArray.length; i++){ | ||
var kvString = kvArray[i].split('='); | ||
obj[kvString[0]] = kvString[1]; | ||
} | ||
return obj; | ||
} | ||
return null; | ||
} | ||
/** | ||
* Remove properties from an object | ||
* @param {object} obj | ||
* @param {array} props | ||
* @param {boolean} isOptions | ||
*/ | ||
function pruneObj(obj, props, isOptions){ | ||
for(var i = 0; i < props.length; i++){ | ||
var prop = props[i]; | ||
if(isOptions){ | ||
prop = prop.toLowerCase(); | ||
} | ||
delete obj[prop] | ||
} | ||
return obj; | ||
} | ||
/** | ||
* Simplify superagent's http response object | ||
* @param {object} r | ||
*/ | ||
function gutResponse(r){ | ||
var newResponse = {}; | ||
newResponse.body = r.body; | ||
newResponse.text = r.text; | ||
newResponse.headers = r.headers; | ||
newResponse.statusCode = r.statusCode; | ||
newResponse.status = r.status; | ||
newResponse.ok = r.ok; | ||
return newResponse; | ||
} | ||
/** | ||
* Determine whether a value is considered empty | ||
* @param {*} val | ||
*/ | ||
function isEmpty(val){ | ||
return (val === false || val === null || (typeof val == 'object' && Object.keys(val).length == 0)); | ||
} | ||
/** | ||
* Return a cloneof an object | ||
* @param {object} obj | ||
*/ | ||
function cloneObject(obj){ | ||
var newObj = {}; | ||
for(var attr in obj) { | ||
if (obj.hasOwnProperty(attr)){ | ||
newObj[attr] = obj[attr]; | ||
} | ||
} | ||
return newObj; | ||
} | ||
/** | ||
* Reset superagent-cache's default query properties using the options defaults object | ||
* @param {object} d | ||
*/ | ||
function resetProps(d){ | ||
return { | ||
doQuery: (typeof d.doQuery === 'boolean') ? d.doQuery : true, | ||
cacheWhenEmpty: (typeof d.cacheWhenEmpty === 'boolean') ? d.cacheWhenEmpty : true, | ||
prune: d.prune, | ||
pruneParams: d.pruneParams, | ||
pruneOptions: d.pruneOptions, | ||
responseProp: d.responseProp, | ||
expiration: d.expiration, | ||
backgroundRefresh: d.backgroundRefresh | ||
}; | ||
} | ||
/** | ||
* Generate a background refresh query identical to the current query | ||
* @param {object} curProps | ||
*/ | ||
function getBackgroundRefreshFunction(curProps){ | ||
return function(key, cb){ | ||
key = JSON.parse(key); | ||
var method = key.method.toLowerCase(); | ||
var request = superagent | ||
[method](key.uri) | ||
.doQuery(curProps.doQuery) | ||
.pruneParams(curProps.pruneParams) | ||
.pruneOptions(curProps.pruneOptions) | ||
.prune(curProps.prune) | ||
.responseProp(curProps.responseProp) | ||
.expiration(curProps.expiration) | ||
.cacheWhenEmpty(curProps.cacheWhenEmpty); | ||
if(key.params){ | ||
request.query(key.params) | ||
} | ||
if(key.options){ | ||
request.set(key.options); | ||
} | ||
request.end(cb); | ||
} | ||
} | ||
/** | ||
* Handle the varying number of callback output params | ||
* @param {function} cb | ||
* @param {object} err | ||
* @param {object} response | ||
* @param {string} key | ||
*/ | ||
function callbackExecutor(cb, err, response, key){ | ||
if(cb.length === 1){ | ||
cb(response); | ||
} | ||
else if(cb.length > 1){ | ||
cb(err, response, key); | ||
} | ||
else{ | ||
throw new Error('UnsupportedCallbackException: Your .end() callback must pass at least one argument.'); | ||
} | ||
} | ||
} | ||
@@ -403,0 +191,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11
1034
55711