a zip code crypto-currency system good for red ONLY

UnsafeCachePlugin.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var createInnerCallback = require("./createInnerCallback");
  6. function UnsafeCachePlugin(source, filterPredicate, cache, withContext, target) {
  7. this.source = source;
  8. this.filterPredicate = filterPredicate;
  9. this.withContext = withContext;
  10. this.cache = cache || {};
  11. this.target = target;
  12. }
  13. module.exports = UnsafeCachePlugin;
  14. function getCacheId(request, withContext) {
  15. return JSON.stringify({
  16. context: withContext ? request.context : "",
  17. path: request.path,
  18. query: request.query,
  19. request: request.request
  20. });
  21. }
  22. UnsafeCachePlugin.prototype.apply = function(resolver) {
  23. var filterPredicate = this.filterPredicate;
  24. var cache = this.cache;
  25. var target = this.target;
  26. var withContext = this.withContext;
  27. resolver.plugin(this.source, function(request, callback) {
  28. if(!filterPredicate(request)) return callback();
  29. var cacheId = getCacheId(request, withContext);
  30. var cacheEntry = cache[cacheId];
  31. if(cacheEntry) {
  32. return callback(null, cacheEntry);
  33. }
  34. resolver.doResolve(target, request, null, createInnerCallback(function(err, result) {
  35. if(err) return callback(err);
  36. if(result) return callback(null, cache[cacheId] = result);
  37. callback();
  38. }, callback));
  39. });
  40. };