cache.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Cache = exports.ExpiringCacheEntry = void 0;
  4. /* 5 minutes in milliseconds */
  5. const EXPIRATION_BUFFER_MS = 300000;
  6. /**
  7. * An entry in a cache that can expire in a certain amount of time.
  8. */
  9. class ExpiringCacheEntry {
  10. /**
  11. * Create a new expiring token entry.
  12. */
  13. constructor(expiration) {
  14. this.expiration = this.expirationTime(expiration);
  15. }
  16. /**
  17. * The entry is still valid if the expiration is more than
  18. * 5 minutes from the expiration time.
  19. */
  20. isValid() {
  21. return this.expiration - Date.now() > EXPIRATION_BUFFER_MS;
  22. }
  23. /**
  24. * Get an expiration time in milliseconds past epoch.
  25. */
  26. expirationTime(expiresInSeconds) {
  27. return Date.now() + expiresInSeconds * 1000;
  28. }
  29. }
  30. exports.ExpiringCacheEntry = ExpiringCacheEntry;
  31. /**
  32. * Base class for OIDC caches.
  33. */
  34. class Cache {
  35. /**
  36. * Create a new cache.
  37. */
  38. constructor() {
  39. this.entries = new Map();
  40. }
  41. /**
  42. * Clear the cache.
  43. */
  44. clear() {
  45. this.entries.clear();
  46. }
  47. /**
  48. * Create a cache key from the address and username.
  49. */
  50. hashedCacheKey(address, username, callbackHash) {
  51. return JSON.stringify([address, username, callbackHash]);
  52. }
  53. }
  54. exports.Cache = Cache;
  55. //# sourceMappingURL=cache.js.map