express-adaptive-rate-limiter 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +250 -16
- package/dist/algorithms/baseAlgorithm.js +1 -1
- package/dist/algorithms/fixedWindow.js +1 -1
- package/dist/algorithms/leakyBucket.js +1 -1
- package/dist/algorithms/slidingWindowCounter.js +1 -1
- package/dist/algorithms/slidingWindowLog.js +1 -1
- package/dist/algorithms/tokenBucket.js +1 -1
- package/dist/core/blockedKeys.js +1 -0
- package/dist/core/coreMiddleware.js +1 -0
- package/dist/core/limiters/clusterLimiter.js +16 -0
- package/dist/core/limiters/dynamoDbLimiter.js +1 -0
- package/dist/core/limiters/etcdLimiter.js +1 -0
- package/dist/core/limiters/memcachedLimiter.js +1 -0
- package/dist/core/limiters/memoryLimiter.js +1 -0
- package/dist/core/limiters/mongoLimiter.js +1 -0
- package/dist/core/limiters/mongooseLimiter.js +1 -0
- package/dist/core/limiters/mySqlLimiter.js +12 -0
- package/dist/core/limiters/nodeClusterLimiter.js +1 -0
- package/dist/core/limiters/postgresLimiter.js +13 -0
- package/dist/core/limiters/prismaLimiter.js +24 -0
- package/dist/core/limiters/redisLimiter.js +16 -0
- package/dist/core/limiters/sqliteLimiter.js +13 -0
- package/dist/core/limiters/valkeyLimiter.js +16 -0
- package/dist/core/rateLimiter.js +1 -0
- package/dist/core/rateLimiterRes.js +1 -0
- package/dist/core/rateLimiterStore.js +1 -0
- package/dist/core/wrappers/blackAndWhiteLimiter.js +1 -0
- package/dist/core/wrappers/burstyRateLimiter.js +1 -0
- package/dist/core/wrappers/rateLimiterQueue.js +1 -0
- package/dist/core/wrappers/rateLimiterUnion.js +1 -0
- package/dist/core/wrappers/timeoutLimiter.js +1 -0
- package/dist/index.js +1 -1
- package/dist/middleware/expressMiddleware.js +1 -1
- package/dist/middleware/headers.js +1 -1
- package/dist/rateLimit.js +1 -1
- package/dist/rateLimitManager.js +1 -1
- package/dist/stores/baseStore.js +1 -1
- package/dist/stores/luaScripts.js +379 -1
- package/dist/stores/memoryStore.js +1 -1
- package/dist/stores/redisStore.js +1 -1
- package/dist/types/index.d.ts +507 -0
- package/dist/utils/errors.js +1 -1
- package/dist/utils/graphQLParser.js +1 -1
- package/dist/utils/ipUtils.js +1 -1
- package/dist/utils/keyGenerator.js +1 -1
- package/dist/utils/redisClient.js +1 -0
- package/dist/utils/retryAfter.js +1 -1
- package/dist/utils/skipUtils.js +1 -1
- package/dist/utils/time.js +1 -1
- package/dist/utils/validation.js +1 -1
- package/package.json +67 -4
package/dist/utils/skipUtils.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const _0x848244=_0x377c;(function(_0x3d44cf,_0x28853c){const _0xda7666=_0x377c,_0x4167b4=_0x3d44cf();while(!![]){try{const _0x4fe2ba=-parseInt(_0xda7666(432))/1*(-parseInt(_0xda7666(474))/2)+parseInt(_0xda7666(465))/3*(-parseInt(_0xda7666(459))/4)+parseInt(_0xda7666(447))/5*(-parseInt(_0xda7666(454))/6)+-parseInt(_0xda7666(443))/7*(-parseInt(_0xda7666(450))/8)+parseInt(_0xda7666(433))/9+parseInt(_0xda7666(446))/10+-parseInt(_0xda7666(452))/11*(parseInt(_0xda7666(471))/12);if(_0x4fe2ba===_0x28853c)break;else _0x4167b4["push"](_0x4167b4["shift"]())}catch(_0x408f63){_0x4167b4["push"](_0x4167b4["shift"]())}}})(_0x49a4,437199);import{compileWhitelist,isIpInWhitelist,validateIpRangeV4,validateSecondaryIps}from"./ipUtils.js";export function getHeader(_0x21f711,_0x2cdb65){const _0x5e2a1a=_0x377c;if(!_0x21f711||!_0x2cdb65)return undefined;if(typeof _0x21f711["get"]===_0x5e2a1a(449)){const _0x3bde0b=_0x21f711["get"](_0x2cdb65);if(_0x3bde0b!==undefined)return _0x3bde0b}if(_0x21f711[_0x5e2a1a(444)]&&typeof _0x21f711[_0x5e2a1a(444)]===_0x5e2a1a(461)){if(_0x21f711["headers"][_0x2cdb65]!==undefined)return _0x21f711["headers"][_0x2cdb65];const _0x4cd707=_0x2cdb65[_0x5e2a1a(455)]();if(_0x21f711[_0x5e2a1a(444)][_0x4cd707]!==undefined)return _0x21f711[_0x5e2a1a(444)][_0x4cd707];for(const _0x36ae3d of Object["keys"](_0x21f711["headers"])){if(_0x36ae3d[_0x5e2a1a(455)]()===_0x4cd707)return _0x21f711[_0x5e2a1a(444)][_0x36ae3d]}}return undefined}function _0x377c(_0x3c6ac6,_0x3850cf){_0x3c6ac6=_0x3c6ac6-427;const _0x49a4a4=_0x49a4();let _0x377c9e=_0x49a4a4[_0x3c6ac6];if(_0x377c["kkBdVQ"]===undefined){var _0x118961=function(_0x465472){const _0x395b41="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=";let _0x42242c="",_0x21f711="";for(let _0x2cdb65=0,_0x3bde0b,_0x4cd707,_0x36ae3d=0;_0x4cd707=_0x465472["charAt"](_0x36ae3d++);~_0x4cd707&&(_0x3bde0b=_0x2cdb65%4?_0x3bde0b*64+_0x4cd707:_0x4cd707,_0x2cdb65++%4)?_0x42242c+=String["fromCharCode"](255&_0x3bde0b>>(-2*_0x2cdb65&6)):0){_0x4cd707=_0x395b41["indexOf"](_0x4cd707)}for(let _0x396b6f=0,_0x482645=_0x42242c["length"];_0x396b6f<_0x482645;_0x396b6f++){_0x21f711+="%"+("00"+_0x42242c["charCodeAt"](_0x396b6f)["toString"](16))["slice"](-2)}return decodeURIComponent(_0x21f711)};_0x377c["YLuKgC"]=_0x118961,_0x377c["VUZsHz"]={},_0x377c["kkBdVQ"]=!![]}const _0x50cbc4=_0x49a4a4[0];_0x377c["hcMGlr"]!==_0x50cbc4&&(_0x377c["VUZsHz"]={},_0x377c["hcMGlr"]=_0x50cbc4);const _0x9be681=_0x377c["VUZsHz"][_0x3c6ac6];return _0x9be681===undefined?(_0x377c9e=_0x377c["YLuKgC"](_0x377c9e),_0x377c["VUZsHz"][_0x3c6ac6]=_0x377c9e):_0x377c9e=_0x9be681,_0x377c9e}export function matchValue(_0x396b6f,_0x482645){const _0x179a26=_0x377c;if(_0x482645===!![])return _0x396b6f!==undefined&&_0x396b6f!==null&&_0x396b6f!=="";if(_0x482645===![])return _0x396b6f===undefined||_0x396b6f===null||_0x396b6f==="";if(typeof _0x482645===_0x179a26(449))return Boolean(_0x482645(_0x396b6f));if(_0x482645 instanceof RegExp)return typeof _0x396b6f===_0x179a26(460)&&_0x482645[_0x179a26(464)](_0x396b6f);if(Array[_0x179a26(438)](_0x482645))return _0x482645["some"](_0x856816=>matchValue(_0x396b6f,_0x856816));if(_0x396b6f===undefined||_0x396b6f===null)return![];return String(_0x396b6f)===String(_0x482645)}export function checkHeaderMap(_0x53baaa,_0x42b156){const _0x5e744f=_0x377c;if(!_0x42b156||typeof _0x42b156!=="object")return![];const _0x112e82=Object[_0x5e744f(436)](_0x42b156);if(_0x112e82[_0x5e744f(439)]===0)return![];for(const[_0x4552cc,_0x283a14]of _0x112e82){const _0x4442a7=getHeader(_0x53baaa,_0x4552cc);if(!matchValue(_0x4442a7,_0x283a14))return![]}return!![]}function _0x49a4(){const _0x2fc253=["AxnbCNjHEq","BgvUz3rO","A2v5","Bw9Kzq","B3jPz2LUCW","ndqZmw9lr2XcAW","AgvHzgvYCW","AxbZ","ndaXmJyZmg9nAuH4qW","ntaYmfbfBMDiCq","AgfZ","zNvUy3rPB24","mtm2uM9pENHX","A2v5CW","mtG3mZqXB3PUDKfg","Bwv0Ag9K","ndy4nMfPwfzvuW","Dg9mB3DLCKnHC2u","Cgf0Ahm","z2v0q2XPzw50sxa","C3rHCNrZv2L0Aa","mJmWnZGYnejvqxbuzW","C3rYAw5N","B2jQzwn0","ChvZAa","DxjS","DgvZDa","m2f5z0jNAG","CMvWBgfJzq","y3vZDg9Tu2TPCa","y3vZDg9TC2TPCa","C2TPCa","CMvZB2X2zq","mtjpD2rHALC","yxbPs2v5","AgvHzgvY","mti4DfnyzwDf","yxbPx2TLEq","C29JA2v0","B3jPz2LU","CMvMzxjLCG","CxvLCNK","Dg9vChbLCKnHC2u","CMvTB3rLqwrKCMvZCW","mtiZotfMDfntAuG","ntq5mJm1oevTDMPItG","C29Tzq","ywXS","zw50CMLLCW","Cgf0Aa"];_0x49a4=function(){return _0x2fc253};return _0x49a4()}export function checkHeaderCriteria(_0x3b7864,_0x1bcae2){const _0x3f2cc4=_0x377c;if(!_0x1bcae2)return![];if(typeof _0x1bcae2==="function")return Boolean(_0x1bcae2(_0x3b7864[_0x3f2cc4(444)]||{},_0x3b7864));if(typeof _0x1bcae2===_0x3f2cc4(460))return matchValue(getHeader(_0x3b7864,_0x1bcae2),!![]);if(Array["isArray"](_0x1bcae2))return _0x1bcae2[_0x3f2cc4(434)](_0x4228a0=>{const _0x3ba95d=_0x3f2cc4;if(typeof _0x4228a0===_0x3ba95d(460))return matchValue(getHeader(_0x3b7864,_0x4228a0),!![]);if(typeof _0x4228a0==="object"&&_0x4228a0!==null)return checkHeaderMap(_0x3b7864,_0x4228a0);return![]});if(typeof _0x1bcae2===_0x3f2cc4(461))return checkHeaderMap(_0x3b7864,_0x1bcae2);return![]}export function checkOriginCriteria(_0x2f85f7,_0x3d47f1){const _0x3044fd=_0x377c;if(!_0x3d47f1)return![];const _0x5acd8a=getHeader(_0x2f85f7,_0x3044fd(427))||getHeader(_0x2f85f7,_0x3044fd(428))||"";if(!_0x5acd8a&&_0x3d47f1!==![])return![];let _0x4765f2="";try{_0x4765f2=new URL(_0x5acd8a)[_0x3044fd(427)]}catch{}const _0x344e1e=[_0x5acd8a];_0x4765f2&&_0x4765f2!==_0x5acd8a&&_0x344e1e[_0x3044fd(462)](_0x4765f2);if(_0x3d47f1==="*")return Boolean(_0x5acd8a);if(typeof _0x3d47f1===_0x3044fd(449))return _0x344e1e["some"](_0x2c0455=>Boolean(_0x3d47f1(_0x2c0455,_0x2f85f7)));if(_0x3d47f1 instanceof RegExp)return _0x344e1e[_0x3044fd(434)](_0x3aed55=>_0x3d47f1[_0x3044fd(464)](_0x3aed55));if(Array[_0x3044fd(438)](_0x3d47f1))return _0x3d47f1[_0x3044fd(434)](_0x160235=>checkOriginCriteria(_0x2f85f7,_0x160235));if(typeof _0x3d47f1===_0x3044fd(460)){const _0x5ed336=_0x3d47f1["replace"](/\/+$/,"");for(const _0x100eb2 of _0x344e1e){const _0x2f7b09=_0x100eb2[_0x3044fd(466)](/\/+$/,"");if(_0x2f7b09===_0x5ed336)return!![];if(_0x100eb2[_0x3044fd(458)](_0x5ed336+"/"))return!![]}return![]}return![]}export function checkIpCriteria(_0x4f1a0b,_0x3cef68,_0x55f92c){const _0x117c0c=_0x377c;if(!_0x3cef68)return![];const _0x3d247c=_0x55f92c||_0x4f1a0b&&(_0x4f1a0b["ip"]||_0x4f1a0b[_0x117c0c(476)]&&_0x4f1a0b[_0x117c0c(476)][_0x117c0c(431)])||"127.0.0.1";if(typeof _0x3cef68===_0x117c0c(449))return Boolean(_0x3cef68(_0x3d247c,_0x4f1a0b));if(_0x3cef68 instanceof RegExp)return _0x3cef68["test"](_0x3d247c);try{const _0x237899=compileWhitelist(_0x3cef68);if(isIpInWhitelist(_0x3d247c,_0x237899))return!![];if(_0x4f1a0b&&_0x4f1a0b["ip"]&&isIpInWhitelist(_0x4f1a0b["ip"],_0x237899))return!![];if(validateSecondaryIps(_0x4f1a0b,_0x3cef68))return!![];if(validateIpRangeV4(_0x3d247c,_0x3cef68))return!![]}catch{}return![]}export function checkKeyCriteria(_0xa82713,_0x5152fb,_0x1cc897){const _0x5ad93b=_0x377c;if(!_0x5152fb)return![];const _0x4bdfd5=[];_0x1cc897!==undefined&&_0x1cc897!==null&&_0x4bdfd5["push"](_0x1cc897);if(_0xa82713){if(_0xa82713[_0x5ad93b(440)]!==undefined&&_0xa82713[_0x5ad93b(440)]!==null)_0x4bdfd5[_0x5ad93b(462)](_0xa82713[_0x5ad93b(440)]);if(_0xa82713[_0x5ad93b(472)]!==undefined&&_0xa82713[_0x5ad93b(472)]!==null)_0x4bdfd5[_0x5ad93b(462)](_0xa82713[_0x5ad93b(472)]);if(_0xa82713["api_key"]!==undefined&&_0xa82713[_0x5ad93b(475)]!==null)_0x4bdfd5[_0x5ad93b(462)](_0xa82713[_0x5ad93b(475)]);const _0x3d8d05=getHeader(_0xa82713,"x-api-key")||getHeader(_0xa82713,"api-key");if(_0x3d8d05!==undefined)_0x4bdfd5[_0x5ad93b(462)](_0x3d8d05);if(_0xa82713[_0x5ad93b(429)]){if(_0xa82713[_0x5ad93b(429)][_0x5ad93b(440)])_0x4bdfd5[_0x5ad93b(462)](_0xa82713[_0x5ad93b(429)][_0x5ad93b(440)]);if(_0xa82713["query"][_0x5ad93b(472)])_0x4bdfd5["push"](_0xa82713[_0x5ad93b(429)]["apiKey"]);if(_0xa82713[_0x5ad93b(429)]["api_key"])_0x4bdfd5[_0x5ad93b(462)](_0xa82713[_0x5ad93b(429)][_0x5ad93b(475)])}}if(typeof _0x5152fb==="function")return _0x4bdfd5[_0x5ad93b(434)](_0x10485c=>Boolean(_0x5152fb(_0x10485c,_0xa82713)));if(_0x5152fb instanceof RegExp)return _0x4bdfd5["some"](_0x23f771=>typeof _0x23f771===_0x5ad93b(460)&&_0x5152fb[_0x5ad93b(464)](_0x23f771));if(Array[_0x5ad93b(438)](_0x5152fb))return _0x5152fb[_0x5ad93b(434)](_0x1f0679=>checkKeyCriteria(_0xa82713,_0x1f0679,_0x1cc897));return _0x4bdfd5["some"](_0x244977=>matchValue(_0x244977,_0x5152fb))}export function checkPathCriteria(_0x155146,_0x1660fa){const _0x3571e2=_0x377c;if(!_0x1660fa||!_0x155146)return![];const _0x5b51e3=_0x155146["path"]||_0x155146[_0x3571e2(463)]||"";if(typeof _0x1660fa===_0x3571e2(449))return Boolean(_0x1660fa(_0x5b51e3,_0x155146));if(_0x1660fa instanceof RegExp)return _0x1660fa[_0x3571e2(464)](_0x5b51e3);if(Array[_0x3571e2(438)](_0x1660fa))return _0x1660fa[_0x3571e2(434)](_0x3a5094=>checkPathCriteria(_0x155146,_0x3a5094));return String(_0x5b51e3)===String(_0x1660fa)}export function checkMethodCriteria(_0x370ef4,_0x3e67c6){const _0xfb687a=_0x377c;if(!_0x3e67c6||!_0x370ef4)return![];const _0x5b63e8=(_0x370ef4[_0xfb687a(453)]||"")["toUpperCase"]();if(typeof _0x3e67c6===_0xfb687a(449))return Boolean(_0x3e67c6(_0x5b63e8,_0x370ef4));if(Array[_0xfb687a(438)](_0x3e67c6))return _0x3e67c6[_0xfb687a(434)](_0x275840=>String(_0x275840)["toUpperCase"]()===_0x5b63e8);return _0x5b63e8===String(_0x3e67c6)[_0xfb687a(430)]()}const KNOWN_CONFIG_KEYS=new Set(["header","headers",_0x848244(440),_0x848244(451),_0x848244(427),_0x848244(442),"ip","ips",_0x848244(437),_0x848244(456),_0x848244(453),"methods","fn","customSkip",_0x848244(468),"skip",_0x848244(441),"match"]);export async function evaluateSkipRule(_0x545588,_0x4273d8,_0x5a746a,_0x35c1a4={}){const _0x4db9b5=_0x848244;if(!_0x545588||!_0x4273d8)return![];if(typeof _0x545588==="function")try{return Boolean(await Promise["resolve"](_0x545588(_0x4273d8,_0x5a746a)))}catch{return![]}if(Array[_0x4db9b5(438)](_0x545588)){for(const _0x3e3b9c of _0x545588){if(await evaluateSkipRule(_0x3e3b9c,_0x4273d8,_0x5a746a,_0x35c1a4))return!![]}return![]}if(typeof _0x545588==="object"){const _0x273c7d=Object[_0x4db9b5(451)](_0x545588);if(_0x273c7d[_0x4db9b5(439)]===0)return![];const _0xf688ab={};for(const _0x4ebd9d of _0x273c7d){!KNOWN_CONFIG_KEYS[_0x4db9b5(448)](_0x4ebd9d)&&(_0xf688ab[_0x4ebd9d]=_0x545588[_0x4ebd9d])}const _0x1779c2=[],_0x1bf8cb=_0x545588["header"]!==undefined?_0x545588[_0x4db9b5(473)]:_0x545588[_0x4db9b5(444)];_0x1bf8cb!==undefined&&_0x1779c2[_0x4db9b5(462)](()=>checkHeaderCriteria(_0x4273d8,_0x1bf8cb));Object[_0x4db9b5(451)](_0xf688ab)[_0x4db9b5(439)]>0&&_0x1779c2[_0x4db9b5(462)](()=>checkHeaderMap(_0x4273d8,_0xf688ab));const _0x20a0d1=_0x545588[_0x4db9b5(440)]!==undefined?_0x545588[_0x4db9b5(440)]:_0x545588[_0x4db9b5(451)];if(_0x20a0d1!==undefined){const _0x3d7c5b=typeof _0x35c1a4["generateKey"]===_0x4db9b5(449)?_0x35c1a4["generateKey"](_0x4273d8):undefined;_0x1779c2[_0x4db9b5(462)](()=>checkKeyCriteria(_0x4273d8,_0x20a0d1,_0x3d7c5b))}const _0x521ae9=_0x545588[_0x4db9b5(427)]!==undefined?_0x545588[_0x4db9b5(427)]:_0x545588["origins"];_0x521ae9!==undefined&&_0x1779c2[_0x4db9b5(462)](()=>checkOriginCriteria(_0x4273d8,_0x521ae9));const _0x41a976=_0x545588["ip"]!==undefined?_0x545588["ip"]:_0x545588[_0x4db9b5(445)];if(_0x41a976!==undefined){const _0x1c3ca9=typeof _0x35c1a4[_0x4db9b5(457)]==="function"?_0x35c1a4[_0x4db9b5(457)](_0x4273d8):_0x4273d8["ip"];_0x1779c2["push"](()=>checkIpCriteria(_0x4273d8,_0x41a976,_0x1c3ca9))}const _0x2ba7cd=_0x545588["path"]!==undefined?_0x545588["path"]:_0x545588[_0x4db9b5(456)];_0x2ba7cd!==undefined&&_0x1779c2[_0x4db9b5(462)](()=>checkPathCriteria(_0x4273d8,_0x2ba7cd));const _0x33d83e=_0x545588["method"]!==undefined?_0x545588[_0x4db9b5(453)]:_0x545588["methods"];_0x33d83e!==undefined&&_0x1779c2[_0x4db9b5(462)](()=>checkMethodCriteria(_0x4273d8,_0x33d83e));const _0x2f8cb9=_0x545588["fn"]||_0x545588[_0x4db9b5(469)]||_0x545588[_0x4db9b5(467)]||_0x545588["customskip"];typeof _0x2f8cb9===_0x4db9b5(449)&&_0x1779c2[_0x4db9b5(462)](async()=>{const _0x10b212=_0x4db9b5;try{return Boolean(await Promise[_0x10b212(470)](_0x2f8cb9(_0x4273d8,_0x5a746a)))}catch{return![]}});if(_0x1779c2[_0x4db9b5(439)]===0)return![];const _0x14d3e6=String(_0x545588["mode"]||_0x545588["match"]||"any")[_0x4db9b5(455)]();if(_0x14d3e6===_0x4db9b5(435)){for(const _0x56844f of _0x1779c2){const _0x5e9a58=await Promise["resolve"](_0x56844f());if(!_0x5e9a58)return![]}return!![]}else{for(const _0x4082df of _0x1779c2){const _0x37c0e5=await Promise[_0x4db9b5(470)](_0x4082df());if(_0x37c0e5)return!![]}return![]}}return![]}
|
|
1
|
+
import{compileWhitelistCached,isIpInWhitelist,validateIpRangeV4,validateSecondaryIps}from'./ipUtils.js';const lowercaseHeadersCache=new WeakMap();function getLowercaseHeaders(headers){let lowercase=lowercaseHeadersCache.get(headers);if(!lowercase){lowercase=new Map();for(const key of Object.keys(headers)){const lower=key.toLowerCase();if(!lowercase.has(lower)){lowercase.set(lower,headers[key]);}}lowercaseHeadersCache.set(headers,lowercase);}return lowercase;}export function getHeader(req,name){if(!req||!name)return undefined;if(typeof req.get==='function'){const val=req.get(name);if(val!==undefined)return val;}if(req.headers&&typeof req.headers==='object'){if(req.headers[name]!==undefined)return req.headers[name];const lower=name.toLowerCase();if(req.headers[lower]!==undefined)return req.headers[lower];const cached=getLowercaseHeaders(req.headers);if(cached.has(lower))return cached.get(lower);}return undefined;}export function matchValue(actual,expected){if(expected===true){return actual!==undefined&&actual!==null&&actual!=='';}if(expected===false){return actual===undefined||actual===null||actual==='';}if(typeof expected==='function'){return Boolean(expected(actual));}if(expected instanceof RegExp){return typeof actual==='string'&&expected.test(actual);}if(Array.isArray(expected)){return expected.some(exp=>matchValue(actual,exp));}if(actual===undefined||actual===null){return false;}return String(actual)===String(expected);}export function checkHeaderMap(req,headerObj){if(!headerObj||typeof headerObj!=='object')return false;const entries=Object.entries(headerObj);if(entries.length===0)return false;for(const[key,exp]of entries){const actual=getHeader(req,key);if(!matchValue(actual,exp)){return false;}}return true;}export function checkHeaderCriteria(req,headerConfig){if(!headerConfig)return false;if(typeof headerConfig==='function'){return Boolean(headerConfig(req.headers||{},req));}if(typeof headerConfig==='string'){return matchValue(getHeader(req,headerConfig),true);}if(Array.isArray(headerConfig)){return headerConfig.some(item=>{if(typeof item==='string'){return matchValue(getHeader(req,item),true);}if(typeof item==='object'&&item!==null){return checkHeaderMap(req,item);}return false;});}if(typeof headerConfig==='object'){return checkHeaderMap(req,headerConfig);}return false;}export function checkOriginCriteria(req,originConfig){if(!originConfig)return false;const rawOrigin=getHeader(req,'origin')||getHeader(req,'referer')||'';if(!rawOrigin&&originConfig!==false)return false;let parsedOrigin='';try{parsedOrigin=new URL(rawOrigin).origin;}catch{}const candidates=[rawOrigin];if(parsedOrigin&&parsedOrigin!==rawOrigin){candidates.push(parsedOrigin);}if(originConfig==='*'){return Boolean(rawOrigin);}if(typeof originConfig==='function'){return candidates.some(cand=>Boolean(originConfig(cand,req)));}if(originConfig instanceof RegExp){return candidates.some(cand=>originConfig.test(cand));}if(Array.isArray(originConfig)){return originConfig.some(item=>checkOriginCriteria(req,item));}if(typeof originConfig==='string'){const normalizedConfig=originConfig.replace(/\/+$/,'');for(const cand of candidates){const normalizedCand=cand.replace(/\/+$/,'');if(normalizedCand===normalizedConfig)return true;if(cand.startsWith(normalizedConfig+'/'))return true;}return false;}return false;}export function checkIpCriteria(req,ipConfig,clientIp,options={}){if(!ipConfig)return false;const ip=clientIp||req&&(req.ip||req.socket&&req.socket.remoteAddress)||'127.0.0.1';if(typeof ipConfig==='function'){return Boolean(ipConfig(ip,req));}if(ipConfig instanceof RegExp){return ipConfig.test(ip);}try{const compiled=compileWhitelistCached(ipConfig);if(isIpInWhitelist(ip,compiled)){return true;}if(req&&req.ip&&isIpInWhitelist(req.ip,compiled)){return true;}if(validateSecondaryIps(req,ipConfig,options)){return true;}if(validateIpRangeV4(ip,ipConfig)){return true;}}catch{}return false;}export function checkKeyCriteria(req,keyConfig,generatedKey){if(!keyConfig)return false;const candidates=[];if(generatedKey!==undefined&&generatedKey!==null){candidates.push(generatedKey);}if(req){if(req.key!==undefined&&req.key!==null)candidates.push(req.key);if(req.apiKey!==undefined&&req.apiKey!==null)candidates.push(req.apiKey);if(req.api_key!==undefined&&req.api_key!==null)candidates.push(req.api_key);}if(typeof keyConfig==='function'){return candidates.some(cand=>Boolean(keyConfig(cand,req)));}if(keyConfig instanceof RegExp){return candidates.some(cand=>typeof cand==='string'&&keyConfig.test(cand));}if(Array.isArray(keyConfig)){return keyConfig.some(item=>checkKeyCriteria(req,item,generatedKey));}return candidates.some(cand=>matchValue(cand,keyConfig));}export function checkPathCriteria(req,pathConfig){if(!pathConfig||!req)return false;const actualPath=req.path||req.url||'';if(typeof pathConfig==='function')return Boolean(pathConfig(actualPath,req));if(pathConfig instanceof RegExp)return pathConfig.test(actualPath);if(Array.isArray(pathConfig))return pathConfig.some(p=>checkPathCriteria(req,p));return String(actualPath)===String(pathConfig);}export function checkMethodCriteria(req,methodConfig){if(!methodConfig||!req)return false;const actualMethod=(req.method||'').toUpperCase();if(typeof methodConfig==='function')return Boolean(methodConfig(actualMethod,req));if(Array.isArray(methodConfig)){return methodConfig.some(m=>String(m).toUpperCase()===actualMethod);}return actualMethod===String(methodConfig).toUpperCase();}const KNOWN_CONFIG_KEYS=new Set(['header','headers','key','keys','origin','origins','ip','ips','path','paths','method','methods','fn','customSkip','customskip','skip','mode','match']);const COMPARE_KEYS=['header','headers','key','keys','origin','origins','ip','ips','path','paths','method','methods','fn','skip','customSkip','customskip','mode','match'];const compiledRuleCache=new WeakMap();function countKeys(obj){let count=0;for(const key in obj){if(Object.prototype.hasOwnProperty.call(obj,key)){count++;}}return count;}function compileObjectRule(rule){const keys=Object.keys(rule);const extraHeaderKeys=keys.filter(key=>!KNOWN_CONFIG_KEYS.has(key));const checks=[];const headerConfig=rule.header!==undefined?rule.header:rule.headers;if(headerConfig!==undefined){checks.push({kind:'header',config:headerConfig});}if(extraHeaderKeys.length>0){checks.push({kind:'extraHeaders',config:extraHeaderKeys});}const keyConfig=rule.key!==undefined?rule.key:rule.keys;if(keyConfig!==undefined){checks.push({kind:'key',config:keyConfig});}const originConfig=rule.origin!==undefined?rule.origin:rule.origins;if(originConfig!==undefined){checks.push({kind:'origin',config:originConfig});}const ipConfig=rule.ip!==undefined?rule.ip:rule.ips;if(ipConfig!==undefined){checks.push({kind:'ip',config:ipConfig});}const pathConfig=rule.path!==undefined?rule.path:rule.paths;if(pathConfig!==undefined){checks.push({kind:'path',config:pathConfig});}const methodConfig=rule.method!==undefined?rule.method:rule.methods;if(methodConfig!==undefined){checks.push({kind:'method',config:methodConfig});}const fnConfig=rule.fn||rule.skip||rule.customSkip||rule.customskip;if(typeof fnConfig==='function'){checks.push({kind:'fn',config:fnConfig});}return{type:'object',mode:String(rule.mode||rule.match||'any').toLowerCase(),checks,extraHeaderKeys,extraHeaderValues:extraHeaderKeys.map(name=>rule[name]),keyCount:keys.length,refs:COMPARE_KEYS.map(name=>({name,value:rule[name]}))};}function isCompiledRuleFresh(compiled,rule){if(compiled.keyCount!==countKeys(rule)){return false;}for(let i=0;i<compiled.refs.length;i++){const ref=compiled.refs[i];if(ref.value!==rule[ref.name]){return false;}}if(compiled.extraHeaderKeys){for(let i=0;i<compiled.extraHeaderKeys.length;i++){const name=compiled.extraHeaderKeys[i];if(compiled.extraHeaderValues[i]!==rule[name]){return false;}}}return true;}async function runCompiledCheck(check,req,res,context,getKey,getIp,rule){switch(check.kind){case'header':return checkHeaderCriteria(req,check.config);case'extraHeaders':return checkExtraHeaders(req,rule,check.config);case'key':return checkKeyCriteria(req,check.config,getKey());case'origin':return checkOriginCriteria(req,check.config);case'ip':return checkIpCriteria(req,check.config,getIp(),context);case'path':return checkPathCriteria(req,check.config);case'method':return checkMethodCriteria(req,check.config);case'fn':try{return Boolean(await Promise.resolve(check.config(req,res)));}catch{return false;}default:return false;}}function checkExtraHeaders(req,rule,headerKeys){if(!headerKeys||headerKeys.length===0)return false;for(const key of headerKeys){if(!matchValue(getHeader(req,key),rule[key])){return false;}}return true;}export async function evaluateSkipRule(rule,req,res,context={}){if(!rule||!req)return false;if(typeof rule==='function'){try{return Boolean(await Promise.resolve(rule(req,res)));}catch{return false;}}if(Array.isArray(rule)){for(const item of rule){if(await evaluateSkipRule(item,req,res,context)){return true;}}return false;}if(typeof rule!=='object'){return false;}const keys=Object.keys(rule);if(keys.length===0)return false;let compiled=compiledRuleCache.get(rule);if(!compiled||!isCompiledRuleFresh(compiled,rule)){compiled=compileObjectRule(rule);compiledRuleCache.set(rule,compiled);}if(compiled.checks.length===0)return false;let generatedKey;let clientIp;const getKey=()=>{if(generatedKey===undefined){generatedKey=typeof context.generateKey==='function'?context.generateKey(req):undefined;}return generatedKey;};const getIp=()=>{if(clientIp===undefined){clientIp=typeof context.getClientIp==='function'?context.getClientIp(req):req.ip;}return clientIp;};const requireAll=compiled.mode==='all';for(const check of compiled.checks){const passed=await runCompiledCheck(check,req,res,context,getKey,getIp,rule);if(requireAll){if(!passed)return false;}else if(passed){return true;}}return requireAll;}
|
package/dist/utils/time.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export function nowMs(){return Date.now();}export function msToSeconds(ms){return Math.ceil(ms/1000);}export function secondsToMs(sec){return sec*1000;}
|
package/dist/utils/validation.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const _0x38e7a6=_0x18c9;function _0x35a9(){const _0xc2496a=["C3rVCMu","CMW6","BgvHA3KTyNvJA2v0igLUihnOyxbPBMCGBw9KzsbYzxf1AxjLCYaIBwf4uxvLDwvtAxPLiIb0BYbIzsbHihbVC2L0AxzLigLUDgvNzxi","BgvNywn5sgvHzgvYCW","AxaTzw5KCg9PBNq","ChjLzML4","A2v5r2vUzxjHDg9Yig11C3qGyMuGysbMDw5JDgLVBIbVCIbVBMuGB2yGiMLWiIWGiMLWlwvUzhbVAw50iIWGiMDYyxbOCwWI","Dg9Rzw4TyNvJA2v0ihjLCxvPCMvZicjYzwzPBgXsyxrLiIb0BYbIzsbHihbVC2L0AxzLig51BwjLCG","BgvHA3KTyNvJA2v0ihjLCxvPCMvZicjSzwfRuMf0zsiGDg8GyMuGysbWB3nPDgL2zsbUDw1Izxi","Bw9Kzq","iI4Gu3vWCg9YDgvKoIa","y3vZDg9Tu2TPCa","ywXNB3jPDgHT","x3n0B3jLuhjLzML4","zNvUy3rPB24","C3rVCMvqCMvMAxG","C2XPzgLUzY13Aw5KB3CTy291BNrLCG","nduZodqXmMDuCfnzqq","Bwf4uxvLDwvtAxPL","B25fCNjVCIbTDxn0igjLigeGzNvUy3rPB24","A2v5r2vUzxjHDg9Y","BgvHA1jHDgu","Dg9Rzw4TyNvJA2v0ihjLCxvPCMvZicjYzwzPBgXjBNrLCNzHBciGDg8GyMuGysbWB3nPDgL2zsbUDw1IzxiGAw4GBwLSBgLZzwnVBMrZ","mZe4mZC5mfjrz3rozG","AgfZ","Cg9SAwnPBMC","BwvTB3j5","ihjLCxvPCMvZicj3Aw5KB3DnCYiGDg8GyMuGysbWB3nPDgL2zsbUDw1Izxi","Bwf4rw50CMLLCW","C2TPCcbTDxn0igjLigeGzNvUy3rPB24Sig9IAMvJDcWGB3iGyxjYyxK","BNvTyMvY","Bwf4","AgfUzgXLCG","y2fWywnPDhK","BgvHA3KTyNvJA2v0igLUihnOyxbPBMCGBw9KzsbYzxf1AxjLCYaICxvLDwvuAw1LB3v0txmIihrVigjLigeGCg9ZAxrPDMuGBNvTyMvY","BgvHA3KTyNvJA2v0ihjLCxvPCMvZicjSzwfRsw50zxj2ywWIihrVigjLigeGCg9ZAxrPDMuGBNvTyMvYigLUig1PBgXPC2vJB25KCW","u3rVCMuGBxvZDcbIzsaIBwvTB3j5iIWGiNjLzgLZiIWGB3iGysbJDxn0B20GC3rVCMuGAw5ZDgfUy2u","A2v5uhjLzML4","ihjLCxvPCMvZicjSAw1PDciGDg8GyMuGysbWB3nPDgL2zsbUDw1Izxi","y3vZDg9TC2TPCa","BwvZC2fNzq","C2HHCgLUzW","yM9VBgvHBG","y29ZDa","BgvHA3KTyNvJA2v0","mJjvA05Lz0S","CxvLDwvuAw1LB3v0txm","AxnjBNrLz2vY","CgfZC09Uu3rVCMvfCNjVCG","vg9Vig1HBNKGCMvXDwvZDhmSihbSzwfZzsb0CNKGywDHAw4GBgf0zxiU","DxnLuMvKAxnuAw1L","y2XLyw51CeLUDgvYDMfStxmGBxvZDcbIzsbHihbVC2L0AxzLig51BwjLCG","CMvMAwXSsw50zxj2ywW","mtC0odqXnuXbzK1QtG","Aw5PDgLHBfrVA2vUCW","CMvKAxm","AgfUzgXLCIbTDxn0igjLigeGzNvUy3rPB24","Dg9Rzw4TyNvJA2v0icjPBML0AwfSvg9Rzw5ZiIbTDxn0igjLigeGBM9Ulw5Lz2f0AxzLig51BwjLCG","mJiWmJqWmdHlsLDABMS","BgLTAxq","mteYmZaZww5QBxPl","mZC2mdq5nhH5yMrhuW","C3rHDhvZq29Kzq","mZqZnJq1ngLJueDqvW","C3rHBMrHCMrizwfKzxjZig11C3qGyMuGysbIB29SzwfUig9YicjKCMfMDc02iIbVCIaIzhjHzNqTnYi","zhjHzNqTnG","C2TPCa","C3rYAw5N","AM9PBG","C3rHBMrHCMrizwfKzxjZ","C2XPzgLUzY13Aw5KB3CTBg9N","q29ZDcbTDxn0igjLigeGCg9ZAxrPDMuGBNvTyMvYig9YigeGzNvUy3rPB24GCMv0DxjUAw5NigeGCg9ZAxrPDMuGBNvTyMvY","B2jQzwn0","CMvMAwXSuMf0zq","BgvHA3KTyNvJA2v0icjTB2rLiIbTDxn0igjLigvPDgHLCIaICg9SAwnPBMCIig9YicjZAgfWAw5NiG","BgvHA0LUDgvYDMfS","zML4zwqTD2LUzg93","Bwf4rw50CMLLCYbTDxn0igjLigeGCg9ZAxrPDMuGAw50zwDLCG","AxngAw5PDgu","DxnLuMvKAxnuAw1Lig11C3qGyMuGysbIB29SzwfU","y2XLyw51CeLUDgvYDMfStxm","D2LUzg93txm","C3rHDhvZq29KzsbTDxn0igjLigeGDMfSAwqGsfruucbZDgf0DxmGy29KzsaOmtaWltu5osK","BgvHA3KTyNvJA2v0ihjLCxvPCMvZicjJyxbHy2L0EsiGDg8GyMuGysbWB3nPDgL2zsbUDw1Izxi","B25fCNjVCG","Dg9Rzw4TyNvJA2v0ihjLCxvPCMvZicjJyxbHy2L0EsiGDg8GyMuGysbWB3nPDgL2zsbUDw1Izxi","zMfPBe9Wzw4"];_0x35a9=function(){return _0xc2496a};return _0x35a9()}function _0x18c9(_0x16248c,_0x4a4083){_0x16248c=_0x16248c-459;const _0x35a907=_0x35a9();let _0x18c9b3=_0x35a907[_0x16248c];if(_0x18c9["qcitDj"]===undefined){var _0x47ffdb=function(_0x19568c){const _0x304e67="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=";let _0x12e8e9="",_0x2d559e="";for(let _0x1994a1=0,_0x195056,_0x3fa70f,_0x59b81a=0;_0x3fa70f=_0x19568c["charAt"](_0x59b81a++);~_0x3fa70f&&(_0x195056=_0x1994a1%4?_0x195056*64+_0x3fa70f:_0x3fa70f,_0x1994a1++%4)?_0x12e8e9+=String["fromCharCode"](255&_0x195056>>(-2*_0x1994a1&6)):0){_0x3fa70f=_0x304e67["indexOf"](_0x3fa70f)}for(let _0x438488=0,_0x564abd=_0x12e8e9["length"];_0x438488<_0x564abd;_0x438488++){_0x2d559e+="%"+("00"+_0x12e8e9["charCodeAt"](_0x438488)["toString"](16))["slice"](-2)}return decodeURIComponent(_0x2d559e)};_0x18c9["EYokFu"]=_0x47ffdb,_0x18c9["aXDMsO"]={},_0x18c9["qcitDj"]=!![]}const _0x357206=_0x35a907[0];_0x18c9["CEHMmR"]!==_0x357206&&(_0x18c9["aXDMsO"]={},_0x18c9["CEHMmR"]=_0x357206);const _0x2b48a1=_0x18c9["aXDMsO"][_0x16248c];return _0x2b48a1===undefined?(_0x18c9b3=_0x18c9["EYokFu"](_0x18c9b3),_0x18c9["aXDMsO"][_0x16248c]=_0x18c9b3):_0x18c9b3=_0x2b48a1,_0x18c9b3}(function(_0x27aa40,_0x5631c9){const _0x2fc90f=_0x18c9,_0x2de4ba=_0x27aa40();while(!![]){try{const _0x49b73f=-parseInt(_0x2fc90f(477))/1*(-parseInt(_0x2fc90f(462))/2)+parseInt(_0x2fc90f(470))/3+parseInt(_0x2fc90f(521))/4+parseInt(_0x2fc90f(527))/5+-parseInt(_0x2fc90f(478))/6+parseInt(_0x2fc90f(480))/7+-parseInt(_0x2fc90f(475))/8;if(_0x49b73f===_0x5631c9)break;else _0x2de4ba["push"](_0x2de4ba["shift"]())}catch(_0x4c652b){_0x2de4ba["push"](_0x2de4ba["shift"]())}}})(_0x35a9,700671);import{ConfigurationError}from"./errors.js";export const VALID_ALGORITHMS=new Set([_0x38e7a6(493),_0x38e7a6(487),_0x38e7a6(520),"token-bucket",_0x38e7a6(461)]);export function validateOptions(_0x2d559e={}){const _0x574d61=_0x38e7a6,_0x1994a1={..._0x2d559e};_0x1994a1[_0x574d61(516)]=_0x1994a1[_0x574d61(516)]||"fixed-window";if(!VALID_ALGORITHMS[_0x574d61(528)](_0x1994a1["algorithm"]))throw new ConfigurationError("Invalid algorithm: \""+_0x1994a1[_0x574d61(516)]+_0x574d61(514)+Array["from"](VALID_ALGORITHMS)[_0x574d61(485)](", "));if(typeof _0x1994a1[_0x574d61(504)]===_0x574d61(484)){if(_0x1994a1["store"]!==_0x574d61(530)&&_0x1994a1[_0x574d61(504)]!==_0x574d61(472))throw new ConfigurationError("Invalid store: \""+_0x1994a1[_0x574d61(504)]+"\". Supported string stores: \"memory\", \"redis\"")}else{if(_0x1994a1[_0x574d61(504)]&&typeof _0x1994a1["store"]!==_0x574d61(489))throw new ConfigurationError(_0x574d61(540));else!_0x1994a1["store"]&&(_0x1994a1[_0x574d61(504)]=_0x574d61(530))}const _0x195056=_0x1994a1[_0x574d61(541)]!==undefined?_0x1994a1[_0x574d61(541)]:_0x1994a1[_0x574d61(509)]!==undefined?_0x1994a1[_0x574d61(509)]:_0x1994a1[_0x574d61(519)]!==undefined?_0x1994a1[_0x574d61(519)]:_0x1994a1[_0x574d61(517)];if(_0x195056!==undefined&&typeof _0x195056!=="string")throw new ConfigurationError("keyPrefix must be a string");_0x1994a1[_0x574d61(541)]=typeof _0x195056===_0x574d61(484)?_0x195056:_0x574d61(505);if(_0x1994a1["cost"]!==undefined){if(typeof _0x1994a1["cost"]!==_0x574d61(518)&&(typeof _0x1994a1["cost"]!==_0x574d61(534)||_0x1994a1[_0x574d61(460)]<=0||!Number["isFinite"](_0x1994a1[_0x574d61(460)])))throw new ConfigurationError(_0x574d61(488))}else _0x1994a1[_0x574d61(460)]=1;if(_0x1994a1["keyGenerator"]!==undefined){if(typeof _0x1994a1[_0x574d61(524)]!==_0x574d61(518)&&_0x1994a1[_0x574d61(524)]!=="ip"&&_0x1994a1[_0x574d61(524)]!==_0x574d61(508)&&_0x1994a1[_0x574d61(524)]!=="graphql")throw new ConfigurationError(_0x574d61(510))}if(_0x1994a1["skip"]===undefined){if(_0x1994a1[_0x574d61(515)]!==undefined)_0x1994a1[_0x574d61(483)]=_0x1994a1[_0x574d61(515)];else _0x1994a1[_0x574d61(543)]!==undefined&&(_0x1994a1[_0x574d61(483)]=_0x1994a1["customskip"])}if(_0x1994a1["skip"]!==undefined&&typeof _0x1994a1[_0x574d61(483)]!==_0x574d61(518)&&(typeof _0x1994a1[_0x574d61(483)]!=="object"||_0x1994a1[_0x574d61(483)]===null))throw new ConfigurationError(_0x574d61(533));if(_0x1994a1["handler"]!==undefined&&typeof _0x1994a1[_0x574d61(536)]!==_0x574d61(518))throw new ConfigurationError(_0x574d61(473));if(_0x1994a1[_0x574d61(501)]!==undefined&&typeof _0x1994a1["onError"]!==_0x574d61(518))throw new ConfigurationError(_0x574d61(523));_0x1994a1[_0x574d61(479)]=_0x1994a1["statusCode"]!==undefined?_0x1994a1[_0x574d61(479)]:429;if(!Number[_0x574d61(464)](_0x1994a1[_0x574d61(479)])||_0x1994a1["statusCode"]<100||_0x1994a1[_0x574d61(479)]>599)throw new ConfigurationError(_0x574d61(499));if(_0x1994a1[_0x574d61(486)]!==undefined&&typeof _0x1994a1[_0x574d61(486)]!=="boolean"&&_0x1994a1[_0x574d61(486)]!==_0x574d61(482)&&_0x1994a1[_0x574d61(486)]!=="draft-7")throw new ConfigurationError(_0x574d61(481));_0x1994a1[_0x574d61(486)]=_0x1994a1[_0x574d61(486)]!==undefined?_0x1994a1[_0x574d61(486)]:!![],_0x1994a1[_0x574d61(507)]=_0x1994a1[_0x574d61(507)]!==undefined?Boolean(_0x1994a1["legacyHeaders"]):![],_0x1994a1[_0x574d61(503)]=Boolean(_0x1994a1[_0x574d61(503)]||_0x1994a1[_0x574d61(465)]),_0x1994a1[_0x574d61(544)]=_0x1994a1[_0x574d61(544)]!==undefined?_0x1994a1[_0x574d61(544)]:_0x574d61(466);if(_0x1994a1[_0x574d61(532)]!==undefined){if(!Number[_0x574d61(464)](_0x1994a1[_0x574d61(532)])||_0x1994a1["maxEntries"]<=0)throw new ConfigurationError(_0x574d61(494))}if(_0x1994a1[_0x574d61(497)]!==undefined){if(typeof _0x1994a1[_0x574d61(497)]!==_0x574d61(534)||_0x1994a1[_0x574d61(497)]<=0||!Number[_0x574d61(495)](_0x1994a1[_0x574d61(497)]))throw new ConfigurationError(_0x574d61(468))}if(_0x1994a1[_0x574d61(467)]!==undefined&&typeof _0x1994a1[_0x574d61(467)]!==_0x574d61(459))throw new ConfigurationError(_0x574d61(496));if(_0x1994a1["algorithm"]==="fixed-window"||_0x1994a1["algorithm"]===_0x574d61(487)||_0x1994a1[_0x574d61(516)]==="sliding-window-counter"){_0x1994a1[_0x574d61(498)]=_0x1994a1[_0x574d61(498)]!==undefined?_0x1994a1["windowMs"]:60000;if(typeof _0x1994a1[_0x574d61(498)]!==_0x574d61(534)||_0x1994a1["windowMs"]<=0||!Number[_0x574d61(495)](_0x1994a1["windowMs"]))throw new ConfigurationError(_0x1994a1[_0x574d61(516)]+_0x574d61(531));_0x1994a1["limit"]=_0x1994a1[_0x574d61(476)]!==undefined?_0x1994a1["limit"]:_0x1994a1[_0x574d61(535)]!==undefined?_0x1994a1[_0x574d61(535)]:60;if(typeof _0x1994a1[_0x574d61(476)]!==_0x574d61(534)||_0x1994a1[_0x574d61(476)]<=0||!Number[_0x574d61(495)](_0x1994a1[_0x574d61(476)]))throw new ConfigurationError(_0x1994a1[_0x574d61(516)]+_0x574d61(542))}if(_0x1994a1[_0x574d61(516)]==="token-bucket"){_0x1994a1[_0x574d61(537)]=_0x1994a1["capacity"]!==undefined?_0x1994a1[_0x574d61(537)]:_0x1994a1[_0x574d61(476)]!==undefined?_0x1994a1[_0x574d61(476)]:_0x1994a1[_0x574d61(535)]!==undefined?_0x1994a1[_0x574d61(535)]:60;if(typeof _0x1994a1[_0x574d61(537)]!==_0x574d61(534)||_0x1994a1[_0x574d61(537)]<=0||!Number[_0x574d61(495)](_0x1994a1[_0x574d61(537)]))throw new ConfigurationError(_0x574d61(502));_0x1994a1[_0x574d61(490)]=_0x1994a1["refillRate"]!==undefined?_0x1994a1[_0x574d61(490)]:1;if(typeof _0x1994a1[_0x574d61(490)]!==_0x574d61(534)||_0x1994a1[_0x574d61(490)]<=0||!Number["isFinite"](_0x1994a1[_0x574d61(490)]))throw new ConfigurationError(_0x574d61(511));_0x1994a1[_0x574d61(469)]=_0x1994a1[_0x574d61(469)]!==undefined?_0x1994a1["refillInterval"]:1000;if(typeof _0x1994a1[_0x574d61(469)]!==_0x574d61(534)||_0x1994a1["refillInterval"]<=0||!Number[_0x574d61(495)](_0x1994a1["refillInterval"]))throw new ConfigurationError(_0x574d61(526));if(_0x1994a1["initialTokens"]!==undefined){if(typeof _0x1994a1["initialTokens"]!=="number"||_0x1994a1[_0x574d61(471)]<0||!Number["isFinite"](_0x1994a1[_0x574d61(471)]))throw new ConfigurationError(_0x574d61(474))}else _0x1994a1[_0x574d61(471)]=_0x1994a1[_0x574d61(537)]}if(_0x1994a1[_0x574d61(516)]===_0x574d61(461)){_0x1994a1[_0x574d61(537)]=_0x1994a1[_0x574d61(537)]!==undefined?_0x1994a1[_0x574d61(537)]:_0x1994a1[_0x574d61(476)]!==undefined?_0x1994a1[_0x574d61(476)]:_0x1994a1[_0x574d61(535)]!==undefined?_0x1994a1[_0x574d61(535)]:60;if(typeof _0x1994a1[_0x574d61(537)]!==_0x574d61(534)||_0x1994a1["capacity"]<=0||!Number[_0x574d61(495)](_0x1994a1["capacity"]))throw new ConfigurationError(_0x574d61(500));_0x1994a1[_0x574d61(525)]=_0x1994a1[_0x574d61(525)]!==undefined?_0x1994a1[_0x574d61(525)]:1;if(typeof _0x1994a1[_0x574d61(525)]!==_0x574d61(534)||_0x1994a1[_0x574d61(525)]<=0||!Number[_0x574d61(495)](_0x1994a1[_0x574d61(525)]))throw new ConfigurationError(_0x574d61(512));_0x1994a1[_0x574d61(492)]=_0x1994a1[_0x574d61(492)]!==undefined?_0x1994a1[_0x574d61(492)]:1000;if(typeof _0x1994a1["leakInterval"]!==_0x574d61(534)||_0x1994a1[_0x574d61(492)]<=0||!Number[_0x574d61(495)](_0x1994a1["leakInterval"]))throw new ConfigurationError(_0x574d61(539));_0x1994a1[_0x574d61(513)]=_0x1994a1[_0x574d61(513)]||_0x574d61(529);if(_0x1994a1[_0x574d61(513)]!==_0x574d61(529)&&_0x1994a1[_0x574d61(513)]!==_0x574d61(545))throw new ConfigurationError(_0x574d61(491));if(_0x1994a1["mode"]===_0x574d61(545)){_0x1994a1[_0x574d61(522)]=_0x1994a1[_0x574d61(522)]!==undefined?_0x1994a1[_0x574d61(522)]:100;if(!Number[_0x574d61(464)](_0x1994a1[_0x574d61(522)])||_0x1994a1[_0x574d61(522)]<=0)throw new ConfigurationError(_0x574d61(506));_0x1994a1["queueTimeoutMs"]=_0x1994a1[_0x574d61(463)]!==undefined?_0x1994a1[_0x574d61(463)]:30000;if(typeof _0x1994a1[_0x574d61(463)]!==_0x574d61(534)||_0x1994a1[_0x574d61(463)]<=0||!Number[_0x574d61(495)](_0x1994a1[_0x574d61(463)]))throw new ConfigurationError(_0x574d61(538))}}return _0x1994a1}
|
|
1
|
+
import{ConfigurationError}from'./errors.js';export const VALID_ALGORITHMS=new Set(['fixed-window','sliding-window-log','sliding-window-counter','token-bucket','leaky-bucket']);export function validateOptions(rawOptions={}){const options={...rawOptions};options.algorithm=options.algorithm||'fixed-window';if(!VALID_ALGORITHMS.has(options.algorithm)){throw new ConfigurationError(`Invalid algorithm: "${options.algorithm}". Supported: ${Array.from(VALID_ALGORITHMS).join(', ')}`);}if(typeof options.store==='string'){if(options.store!=='memory'&&options.store!=='redis'){throw new ConfigurationError(`Invalid store: "${options.store}". Supported string stores: "memory", "redis"`);}}else if(options.store&&typeof options.store!=='object'){throw new ConfigurationError('Store must be "memory", "redis", or a custom store instance');}else if(!options.store){options.store='memory';}const rawKeyPrefix=options.keyPrefix!==undefined?options.keyPrefix:options.prefix!==undefined?options.prefix:options.storePrefix!==undefined?options.storePrefix:options._storePrefix;if(rawKeyPrefix!==undefined&&typeof rawKeyPrefix!=='string'){throw new ConfigurationError('keyPrefix must be a string');}options.keyPrefix=typeof rawKeyPrefix==='string'?rawKeyPrefix:'rl:';if(options.cost!==undefined){if(typeof options.cost!=='function'&&(typeof options.cost!=='number'||options.cost<=0||!Number.isFinite(options.cost))){throw new ConfigurationError('Cost must be a positive number or a function returning a positive number');}}else{options.cost=1;}if(options.keyGenerator!==undefined){if(typeof options.keyGenerator!=='function'&&options.keyGenerator!=='ip'&&options.keyGenerator!=='ip-endpoint'&&options.keyGenerator!=='graphql'){throw new ConfigurationError('keyGenerator must be a function or one of "ip", "ip-endpoint", "graphql"');}}if(options.skip===undefined){if(options.customSkip!==undefined){options.skip=options.customSkip;}else if(options.customskip!==undefined){options.skip=options.customskip;}}if(options.skip!==undefined&&typeof options.skip!=='function'&&(typeof options.skip!=='object'||options.skip===null)){throw new ConfigurationError('skip must be a function, object, or array');}if(options.handler!==undefined&&typeof options.handler!=='function'){throw new ConfigurationError('handler must be a function');}if(options.onError!==undefined&&typeof options.onError!=='function'){throw new ConfigurationError('onError must be a function');}options.statusCode=options.statusCode!==undefined?options.statusCode:429;if(!Number.isInteger(options.statusCode)||options.statusCode<100||options.statusCode>599){throw new ConfigurationError('statusCode must be a valid HTTP status code (100-599)');}if(options.standardHeaders!==undefined&&typeof options.standardHeaders!=='boolean'&&options.standardHeaders!=='draft-6'&&options.standardHeaders!=='draft-7'){throw new ConfigurationError('standardHeaders must be a boolean or "draft-6" or "draft-7"');}options.standardHeaders=options.standardHeaders!==undefined?options.standardHeaders:true;options.legacyHeaders=options.legacyHeaders!==undefined?Boolean(options.legacyHeaders):false;options.failOpen=Boolean(options.failOpen||options.passOnStoreError);options.message=options.message!==undefined?options.message:'Too many requests, please try again later.';if(options.maxEntries!==undefined){if(!Number.isInteger(options.maxEntries)||options.maxEntries<=0){throw new ConfigurationError('maxEntries must be a positive integer');}}if(options.cleanupIntervalMs!==undefined){if(typeof options.cleanupIntervalMs!=='number'||options.cleanupIntervalMs<=0||!Number.isFinite(options.cleanupIntervalMs)){throw new ConfigurationError('cleanupIntervalMs must be a positive number');}}if(options.useRedisTime!==undefined&&typeof options.useRedisTime!=='boolean'){throw new ConfigurationError('useRedisTime must be a boolean');}if(options.algorithm==='fixed-window'||options.algorithm==='sliding-window-log'||options.algorithm==='sliding-window-counter'){options.windowMs=options.windowMs!==undefined?options.windowMs:60000;if(typeof options.windowMs!=='number'||options.windowMs<=0||!Number.isFinite(options.windowMs)){throw new ConfigurationError(`${options.algorithm} requires "windowMs" to be a positive number`);}options.limit=options.limit!==undefined?options.limit:options.max!==undefined?options.max:60;if(typeof options.limit!=='number'||options.limit<=0||!Number.isFinite(options.limit)){throw new ConfigurationError(`${options.algorithm} requires "limit" to be a positive number`);}}if(options.algorithm==='token-bucket'){options.capacity=options.capacity!==undefined?options.capacity:options.limit!==undefined?options.limit:options.max!==undefined?options.max:60;if(typeof options.capacity!=='number'||options.capacity<=0||!Number.isFinite(options.capacity)){throw new ConfigurationError('token-bucket requires "capacity" to be a positive number');}options.refillRate=options.refillRate!==undefined?options.refillRate:1;if(typeof options.refillRate!=='number'||options.refillRate<=0||!Number.isFinite(options.refillRate)){throw new ConfigurationError('token-bucket requires "refillRate" to be a positive number');}options.refillInterval=options.refillInterval!==undefined?options.refillInterval:1000;if(typeof options.refillInterval!=='number'||options.refillInterval<=0||!Number.isFinite(options.refillInterval)){throw new ConfigurationError('token-bucket requires "refillInterval" to be a positive number in milliseconds');}if(options.initialTokens!==undefined){if(typeof options.initialTokens!=='number'||options.initialTokens<0||!Number.isFinite(options.initialTokens)){throw new ConfigurationError('token-bucket "initialTokens" must be a non-negative number');}}else{options.initialTokens=options.capacity;}}if(options.algorithm==='leaky-bucket'){options.capacity=options.capacity!==undefined?options.capacity:options.limit!==undefined?options.limit:options.max!==undefined?options.max:60;if(typeof options.capacity!=='number'||options.capacity<=0||!Number.isFinite(options.capacity)){throw new ConfigurationError('leaky-bucket requires "capacity" to be a positive number');}options.leakRate=options.leakRate!==undefined?options.leakRate:1;if(typeof options.leakRate!=='number'||options.leakRate<=0||!Number.isFinite(options.leakRate)){throw new ConfigurationError('leaky-bucket requires "leakRate" to be a positive number');}options.leakInterval=options.leakInterval!==undefined?options.leakInterval:1000;if(typeof options.leakInterval!=='number'||options.leakInterval<=0||!Number.isFinite(options.leakInterval)){throw new ConfigurationError('leaky-bucket requires "leakInterval" to be a positive number in milliseconds');}options.mode=options.mode||'policing';if(options.mode!=='policing'&&options.mode!=='shaping'){throw new ConfigurationError('leaky-bucket "mode" must be either "policing" or "shaping"');}if(options.mode==='shaping'){options.maxQueueSize=options.maxQueueSize!==undefined?options.maxQueueSize:100;if(!Number.isInteger(options.maxQueueSize)||options.maxQueueSize<=0){throw new ConfigurationError('leaky-bucket in shaping mode requires "maxQueueSize" to be a positive integer');}options.queueTimeoutMs=options.queueTimeoutMs!==undefined?options.queueTimeoutMs:30000;if(typeof options.queueTimeoutMs!=='number'||options.queueTimeoutMs<=0||!Number.isFinite(options.queueTimeoutMs)){throw new ConfigurationError('leaky-bucket in shaping mode requires "queueTimeoutMs" to be a positive number');}}}return options;}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-adaptive-rate-limiter",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Production-ready
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Production-ready rate limiting for Node.js: five rate limiting algorithms plus a framework-agnostic points-based core with Memory, Redis, Mongo, Mongoose, MySQL, Postgres, SQLite, Prisma, Memcached, DynamoDB, Valkey, etcd and Redis Cluster backends.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/types/index.d.ts",
|
|
@@ -29,23 +29,38 @@
|
|
|
29
29
|
"lint:fix": "eslint . --fix",
|
|
30
30
|
"format": "prettier --check \"src/**/*.js\" \"test/**/*.js\" \"benchmarks/**/*.js\" \"eslint.config.js\"",
|
|
31
31
|
"format:write": "prettier --write \"src/**/*.js\" \"test/**/*.js\" \"benchmarks/**/*.js\" \"eslint.config.js\"",
|
|
32
|
+
"clean": "node -e \"fs.rmSync('dist', { recursive: true, force: true })\"",
|
|
33
|
+
"prebuild": "npm run clean",
|
|
32
34
|
"build": "npx babel src --out-dir dist && node scripts/copy-types.mjs",
|
|
33
35
|
"build:dev": "npx babel src --out-dir dist --watch",
|
|
36
|
+
"prepack": "npm run build",
|
|
37
|
+
"prepublishOnly": "npm run build",
|
|
34
38
|
"check": "npm run test:types && npm run lint && npm run format && npm test"
|
|
35
39
|
},
|
|
36
40
|
"keywords": [
|
|
37
41
|
"express",
|
|
38
42
|
"rate-limit",
|
|
39
43
|
"rate-limiting",
|
|
44
|
+
"rate-limiter",
|
|
40
45
|
"token-bucket",
|
|
41
46
|
"leaky-bucket",
|
|
42
47
|
"sliding-window",
|
|
43
48
|
"fixed-window",
|
|
44
49
|
"redis",
|
|
50
|
+
"mongodb",
|
|
51
|
+
"mongoose",
|
|
52
|
+
"mysql",
|
|
53
|
+
"postgres",
|
|
54
|
+
"sqlite",
|
|
55
|
+
"prisma",
|
|
56
|
+
"memcached",
|
|
57
|
+
"dynamodb",
|
|
58
|
+
"valkey",
|
|
59
|
+
"etcd",
|
|
45
60
|
"middleware",
|
|
46
61
|
"graphql"
|
|
47
62
|
],
|
|
48
|
-
"author": "
|
|
63
|
+
"author": "RAVI N D",
|
|
49
64
|
"license": "MIT",
|
|
50
65
|
"repository": {
|
|
51
66
|
"type": "git",
|
|
@@ -58,7 +73,19 @@
|
|
|
58
73
|
"peerDependencies": {
|
|
59
74
|
"graphql": ">=16.0.0",
|
|
60
75
|
"ioredis": ">=4.0.0",
|
|
61
|
-
"redis": ">=4.0.0"
|
|
76
|
+
"redis": ">=4.0.0",
|
|
77
|
+
"mongodb": ">=4.0.0",
|
|
78
|
+
"mongoose": ">=6.0.0",
|
|
79
|
+
"mysql2": ">=2.0.0",
|
|
80
|
+
"pg": ">=8.0.0",
|
|
81
|
+
"better-sqlite3": ">=7.0.0",
|
|
82
|
+
"@prisma/client": ">=3.0.0",
|
|
83
|
+
"memjs": ">=1.0.0",
|
|
84
|
+
"memcached": ">=2.0.0",
|
|
85
|
+
"@aws-sdk/client-dynamodb": ">=3.0.0",
|
|
86
|
+
"iovalkey": ">=0.1.0",
|
|
87
|
+
"@valkey/valkey-glide": ">=1.0.0",
|
|
88
|
+
"etcd3": ">=1.0.0"
|
|
62
89
|
},
|
|
63
90
|
"peerDependenciesMeta": {
|
|
64
91
|
"ioredis": {
|
|
@@ -69,6 +96,42 @@
|
|
|
69
96
|
},
|
|
70
97
|
"graphql": {
|
|
71
98
|
"optional": true
|
|
99
|
+
},
|
|
100
|
+
"mongodb": {
|
|
101
|
+
"optional": true
|
|
102
|
+
},
|
|
103
|
+
"mongoose": {
|
|
104
|
+
"optional": true
|
|
105
|
+
},
|
|
106
|
+
"mysql2": {
|
|
107
|
+
"optional": true
|
|
108
|
+
},
|
|
109
|
+
"pg": {
|
|
110
|
+
"optional": true
|
|
111
|
+
},
|
|
112
|
+
"better-sqlite3": {
|
|
113
|
+
"optional": true
|
|
114
|
+
},
|
|
115
|
+
"@prisma/client": {
|
|
116
|
+
"optional": true
|
|
117
|
+
},
|
|
118
|
+
"memjs": {
|
|
119
|
+
"optional": true
|
|
120
|
+
},
|
|
121
|
+
"memcached": {
|
|
122
|
+
"optional": true
|
|
123
|
+
},
|
|
124
|
+
"@aws-sdk/client-dynamodb": {
|
|
125
|
+
"optional": true
|
|
126
|
+
},
|
|
127
|
+
"iovalkey": {
|
|
128
|
+
"optional": true
|
|
129
|
+
},
|
|
130
|
+
"@valkey/valkey-glide": {
|
|
131
|
+
"optional": true
|
|
132
|
+
},
|
|
133
|
+
"etcd3": {
|
|
134
|
+
"optional": true
|
|
72
135
|
}
|
|
73
136
|
},
|
|
74
137
|
"devDependencies": {
|