mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			48 lines
		
	
	
		
			958 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			958 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
"use strict";
 | 
						|
 | 
						|
function calculateNextResetTime(windowMs) {
 | 
						|
  const d = new Date();
 | 
						|
  d.setMilliseconds(d.getMilliseconds() + windowMs);
 | 
						|
  return d;
 | 
						|
}
 | 
						|
 | 
						|
function MemoryStore(windowMs) {
 | 
						|
  let hits = {};
 | 
						|
  let resetTime = calculateNextResetTime(windowMs);
 | 
						|
 | 
						|
  this.incr = function (key, cb) {
 | 
						|
    if (hits[key]) {
 | 
						|
      hits[key]++;
 | 
						|
    } else {
 | 
						|
      hits[key] = 1;
 | 
						|
    }
 | 
						|
 | 
						|
    cb(null, hits[key], resetTime);
 | 
						|
  };
 | 
						|
 | 
						|
  this.decrement = function (key) {
 | 
						|
    if (hits[key]) {
 | 
						|
      hits[key]--;
 | 
						|
    }
 | 
						|
  };
 | 
						|
 | 
						|
  // export an API to allow hits all IPs to be reset
 | 
						|
  this.resetAll = function () {
 | 
						|
    hits = {};
 | 
						|
    resetTime = calculateNextResetTime(windowMs);
 | 
						|
  };
 | 
						|
 | 
						|
  // export an API to allow hits from one IP to be reset
 | 
						|
  this.resetKey = function (key) {
 | 
						|
    delete hits[key];
 | 
						|
  };
 | 
						|
 | 
						|
  // simply reset ALL hits every windowMs
 | 
						|
  const interval = setInterval(this.resetAll, windowMs);
 | 
						|
  if (interval.unref) {
 | 
						|
    interval.unref();
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
module.exports = MemoryStore;
 |