{"_id":"@aditya.soni/ratelimiter","_rev":"2-66e4c01b39aa4bec2895976f601670b6","name":"@aditya.soni/ratelimiter","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"@aditya.soni/ratelimiter","version":"1.0.0","description":"Rate limiter package","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["rate","limiter"],"author":{"name":"Aditya Soni"},"license":"ISC","_id":"@aditya.soni/ratelimiter@1.0.0","_npmVersion":"6.4.1","_nodeVersion":"10.13.0","_npmUser":{"name":"aditya.soni","email":"aditya.soni@slicepay.in"},"dist":{"integrity":"sha512-F6mOK+91DMJjbF9UnfHP9IAH7Y2Dn5lZeN/uNugongU/AgN/r3fXXJ0J65fFILs6AOuue0teSprKynFYLijMIw==","shasum":"6be7af5e0aab9124ae6fbe0a347edde699ef2e49","tarball":"https://registry.npmjs.org/@aditya.soni/ratelimiter/-/ratelimiter-1.0.0.tgz","fileCount":4,"unpackedSize":7236,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdCcpZCRA9TVsSAnZWagAAtG4QAIiAguIEUY41tc8a2Xgo\nyolZnGJQHAi8lrynuP5jEwgr6nvWSKM1DDHyp5DD24OhH8kvLK7mf5iawtIH\nPjYIKfDkcWxNLYXg+Dto/rtbPZS7ey7r3Li5oJBdHwH7oAdEalSvgOQDXIbH\n2reep6LmoBbGQntUCyzmKqaQ1cbfq81TpPvl1N9czsA4569CvCUMDuS16+bV\n5rASwTqCzB+Tdt4Lgvo8R78EiyUBCsJ/ODuWFPCCQYC7ecKGd/RSt2j9u5Ly\nPEw4gRzF1hlAAsM9KqehJhEM2rMg4j81o8wtFauroRGzDa98GE8XdEe5vYG5\nMlt8WaNti0X2RPp7zYUi/OGjJmrR5suF1/KddlvqUTqVBZeH0CUkVWN7h22P\nWWcwLwDhHhGWO8E473eb2ptnpUsWrUYlKiYZ1s0Kd7PdW2ZMXXMetdsSPn/X\nVUZf+bjc/CuRsTjxr+cBxgDkw6z6qdn5/6draR1O+kKRE96cjrCUMOgDLAPQ\nV2H2WiDnS9+54B6pGrWVNkVB8sRUu/ipqokjNIc/XYPEgY7FhJKWRr5qo0UM\nT5G3kA9267GP6FL5jPtfrSDVJwzpkWF5GW7FHvtnfnshZNEPUw+8EgvEsTch\n33Zbz+6m0aiDYO3aW2V3eZmTp4Whq3cc21ObI3fDHo4CwnARz170s5hzrXNs\naJGx\r\n=ub+J\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDAAd+n8SUyB5At43GudI0WIlW6TArkT3Y0MrASnbKqNAIhAIx43fKcRgPbxXdFQO2oqQihKq0PcYujb0Vt3R9qJp2Q"}]},"maintainers":[{"name":"aditya.soni","email":"aditya.soni@slicepay.in"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ratelimiter_1.0.0_1560922713032_0.20240082059995523"},"_hasShrinkwrap":false},"1.0.1":{"name":"@aditya.soni/ratelimiter","version":"1.0.1","description":"Rate limiter package","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":["rate","limiter"],"author":{"name":"Aditya Soni"},"license":"ISC","_id":"@aditya.soni/ratelimiter@1.0.1","_npmVersion":"6.4.1","_nodeVersion":"10.13.0","_npmUser":{"name":"aditya.soni","email":"aditya.soni@slicepay.in"},"dist":{"integrity":"sha512-ru0Y5IfktS5MrgQ/kIxyffyk4doFHX4B70HbAb4YzEPUzF6idxQnR/PqgCuDVFYiStEwKA35TLhL+ev6VxZUjQ==","shasum":"0910a6f67af4524a490632f438c708a421291480","tarball":"https://registry.npmjs.org/@aditya.soni/ratelimiter/-/ratelimiter-1.0.1.tgz","fileCount":4,"unpackedSize":7236,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdCcryCRA9TVsSAnZWagAAvq8P/3clwjZdyl9TwoTb0RxS\n41kmVTbc3DFLA/G1IJlIWbclQdwhDMTClSpVql4cTqTWhM2PjMBpTEFoLoAu\n+jofzlill7BqlhrZ4BmZCDNz9nASyLEM2A2/031ygiywl4hwa6DKJh1XNFPI\nk6gXfwX5VlKQAws/JfZP4uu6bbjx2nT9fzobUkaAx+si8tFmI9WvMQWLvqGg\ni5FjIBv0Iiy4NL28/O8H7KGI1XTQIvFXZJ+TKmxRc1sdd3wHncQekAoOdJU6\naD5oXdJRH24QhsLbVOa7b31feSGvImHi0XjlRPAXUC1rvWO9cxAhpkZB6B2C\n1TgwBgSBIMw44IptYcaNy6LxqO6S3QqrUspOGO8AX2IB29UlHBX73qE5ppow\njAUHy7xZ7Jf1WeDM3BlqePHmHsf2T4e0/hI56qUvVdA1XhmhSL5MIQ6yyngj\nneyxI9XkAFolG9kfxkT/EXBTaOMzn0SIqn7tSNdjVVRgQz7vLaBVE14CXv+t\njgcHXnOMSXWHALfsOnMGh7ajWiQTS4uU48n389YnDyW4Lu+zzix/1zwae7I/\nZ2IjBzGkmW3FmXCRa7WjDXzYAiR/QbtQX5PyGJS4lBPHdVWPeLKUWXOwe+4k\nOjlmEzVLjStN0kNS7DEq6zu+8cQk1NcBU4qHodZfjFQc0LuxQgaw02Q/QF9j\n9/jn\r\n=NhNr\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDmnwYOow5WBEI7DDVi4RqtKXY+UBPITqiH+3nRrqCIDAiEAijiAps1SFb8CJIp1oBBMafhcgkfAz87VhQxhkgKLuT4="}]},"maintainers":[{"name":"aditya.soni","email":"aditya.soni@slicepay.in"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/ratelimiter_1.0.1_1560922865736_0.27205140098287606"},"_hasShrinkwrap":false}},"time":{"created":"2019-06-19T05:38:32.994Z","1.0.0":"2019-06-19T05:38:33.171Z","modified":"2022-04-04T11:30:50.862Z","1.0.1":"2019-06-19T05:41:05.893Z"},"maintainers":[{"name":"aditya.soni","email":"aditya.soni@slicepay.in"}],"description":"Rate limiter package","keywords":["rate","limiter"],"author":{"name":"Aditya Soni"},"license":"ISC","readme":"# Rate Limiter\n\n## Use\nA rate limiter is implemented to limit repeated requests to public APIs and/or endpoints such as OTP generation. This helps us to avoid brute force attacks by limiting attacker request per time span.\n\n## Pre-requisites & Packages used\n\n1. [express-rate-limit](https://www.npmjs.com/package/express-rate-limit) & [rate-limit-redis](https://npmjs.com/package/rate-limit-redis) npm packages.\n2. We have implemented a [Redis](https://redis.io/) based key-value store for our rate limiting functionality. So there **must** be a redis server instance running in the background at default `PORT=6379`.\n\n## How to use\n\nThe rate limiter can be utilized in two ways.\n\n1. Reusing predefined limiters for more generic cases like OTP, Transaction etc.\n2. Creating New rate limiters for more dynamic cases which don't or cannot fall under the predefinded cases.\n\n### **Using Predefined Limiters**\n\nYou can use the predefined limiters by importing the `rateLimiter` module from the utils folder.\nFor example :\n\n```javascript\nconst { otpLimiter } = require(\"../../utils/rateLimiter\");\n```\n\nor\n\n```javascript\nconst rateLimiter = require(\"../../utils/rateLimiter\");\nconst otpLimiter = rateLimiter.otpLimiter;\n```\n\nand use it in a route like :\n\n```javascript\nrouter.post(\"/otp/request\", [otpLimiter], function(req, res) {\n    // your code here\n}\n```\n\n### **Creating New Limiters**\n\nYou can also create a new limiter as per a use case using the `createNewRateLimiter` fuction from `rateLimiter` module. <br /><br />\nFirst, import the `createNewRateLimiter` from rateLimiter module.\n\n```javascript\nconst { createNewRateLimiter } = require(\"../../utils/rateLimiter\");\n```\n\nThen, define a new limiter globally or inline\n\n```javascript\nconst FileUploadLimiter = createNewRateLimiter(6000, 5, \"Limit Exeeded!\");\n```\n\nFinally, use it in the route \n```javascript\nrouter.post(\"/fle/upload\", [FileUploadLimiter], function(req, res) {\n    // your code here\n}\n```\nor you can directly create one in the route\n```javascript\nrouter.post(\"/fle/upload\", createNewRateLimiter(6000, 5, \"Limit Exeeded!\"), function(req, res) {\n    // your code here\n}\n```\n\n## The **createNewRateLimiter** function\n\nThe `createNewRateLimiter` function takes in configuration options like `windowMs`, `max`, `message` and `keyGenerator` \n\n\n### Configuration Options\n\n### **max**\n\nMax number of connections during `windowMs` milliseconds before sending a 429 response.\n\n### **windowMs**\n\nHow long in milliseconds to keep records of requests in memory.\n\n### **message**\n\nError message sent to user when max is exceeded. Accepts String and JSON object.\n\n### **keyGenerator**\n\nFunction used to generate keys.\nCurrently we are using the device id to generate keys.\n\n\n## Latency Report\n\n### With Limiter\n\n```\nConnection rate: 10.1 conn/s (99.4 ms/conn, <=1 concurrent connections)\nConnection time [ms]: min 41.0 avg 41.7 max 45.6 median 41.5 stddev 0.9\nConnection time [ms]: connect 0.0\nConnection length [replies/conn]: 1.000\n\nRequest rate: 10.1 req/s (99.4 ms/req)\nRequest size [B]: 544.0\n\nReply rate [replies/s]: min 10.0 avg 10.0 max 10.0 stddev 0.0 (1 samples)\nReply time [ms]: response 41.7 transfer 0.0\nReply size [B]: header 349.0 content 77.0 footer 0.0 (total 426.0)\nReply status: 1xx=0 2xx=3 3xx=0 4xx=97 5xx=0\n\nCPU time [s]: user 4.48 system 5.46 (user 45.1% system 54.9% total 100.0%)\nNet I/O: 9.5 KB/s (0.1*10^6 bps)\n\nErrors: total 0 client-timo 0 socket-timo 0 connrefused 0 connreset 0\nErrors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0\n```\n\n### Without Limiter\n\n```\nTotal: connections 100 requests 100 replies 100 test-duration 9.902 s\n\nConnection rate: 10.1 conn/s (99.0 ms/conn, <=1 concurrent connections)\nConnection time [ms]: min 1.1 avg 1.8 max 47.3 median 1.5 stddev 4.6\nConnection time [ms]: connect 0.0\nConnection length [replies/conn]: 1.000\n\nRequest rate: 10.1 req/s (99.0 ms/req)\nRequest size [B]: 544.0\n\nReply rate [replies/s]: min 10.0 avg 10.0 max 10.0 stddev 0.0 (1 samples)\nReply time [ms]: response 1.8 transfer 0.0\nReply size [B]: header 239.0 content 16.0 footer 0.0 (total 255.0)\nReply status: 1xx=0 2xx=100 3xx=0 4xx=0 5xx=0\n\nCPU time [s]: user 4.72 system 5.18 (user 47.7% system 52.3% total 100.0%)\nNet I/O: 7.9 KB/s (0.1*10^6 bps)\n\nErrors: total 0 client-timo 0 socket-timo 0 connrefused 0 connreset 0\nErrors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0\n```\n\n## Author\n\nThis was originally created by [Aditya Soni](www.slicepay.in).\n","readmeFilename":"README.md"}