-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:Restocks/http-config-cache
- Loading branch information
Showing
4 changed files
with
1,099 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
var request = require('request'); | ||
var Timedcache = require('timed-cache'); | ||
|
||
var cache = new Timedcache(); | ||
|
||
var fetchObject = function fetchObject(url) { | ||
return new Promise(function (resolve, reject) { | ||
var retries = 0; | ||
|
||
var URLResponseHandler = function URLResponseHandler(err, res, body) { | ||
if (err) { | ||
setTimeout(function () { | ||
retries += 1; | ||
if (retries > 3) { | ||
reject('Unable to fetch data'); | ||
} else { | ||
/* eslint-disable no-console */ | ||
console.warn('[http-config-cache]: reattempting JSON fetch, after failed attempt #' + retries); | ||
/* eslint-enable no-console */ | ||
request(url, URLResponseHandler); | ||
} | ||
}, 750); | ||
} else { | ||
var configObject = null; | ||
try { | ||
configObject = JSON.parse(body); | ||
} catch (e) { | ||
reject('Data not valid JSON'); | ||
} | ||
resolve(configObject); | ||
} | ||
}; | ||
|
||
request(url, URLResponseHandler); | ||
}); | ||
}; | ||
|
||
module.exports = function (url, time, ignoreCache) { | ||
return new Promise(function (resolve, reject) { | ||
var cacheResponse = cache.get(url); | ||
if (typeof cacheResponse === 'undefined' || ignoreCache) { | ||
fetchObject(url).then(function (cacheObject) { | ||
var cacheConfig = {}; | ||
|
||
if (typeof time === 'number') { | ||
cacheConfig.ttl = time; | ||
} else { | ||
cacheConfig.ttl = 600000; | ||
} | ||
|
||
cache.put(url, cacheObject, cacheConfig); | ||
resolve(cacheObject); | ||
}).catch(function (err) { | ||
reject(err); | ||
}); | ||
} else { | ||
resolve(cacheResponse); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.