{"_id":"@capaj/redis-throttle","_rev":"1-4e7f4054856d1957ed0abce0c23d8810","name":"@capaj/redis-throttle","dist-tags":{"latest":"0.1.1"},"versions":{"0.1.0":{"name":"@capaj/redis-throttle","version":"0.1.0","description":"```ts import { redisThrottle } from '@capaj/redis-throttle'","main":"./dist/redis-throttle.js","module":"./dist/redis-throttle.mjs","types":"./dist/redis-throttle.d.ts","exports":{".":{"require":"./dist/redis-throttle.js","import":"./dist/redis-throttle.mjs","types":"./dist/redis-throttle.d.ts"}},"keywords":["redis-throttle","promise","all","limit","batching","concurrency","parallel","async","await","typescript","javascript","nodejs","node"],"author":"","license":"MIT","peerDependencies":{"ioredis":"^5.3.0"},"devDependencies":{"ioredis":"^5.3.0","tsup":"^6.5.0","typescript":"^4.9.5","vitest":"^0.28.3"},"scripts":{"test":"vitest","ts":"tsc --noEmit","build":"tsup redis-throttle.ts --format cjs,esm --dts --sourcemap"},"_id":"@capaj/redis-throttle@0.1.0","_integrity":"sha512-7m69DOgCqjSrNuhBOBgaARzuEa1I15OAYKIT1/L04qY0+WCbg8gQfsVq1H5pgaITUuz/OJOBo7xmU85L1riRSQ==","_resolved":"/tmp/2f9c0740eacef89256f4d8af11293951/capaj-redis-throttle-0.1.0.tgz","_from":"file:capaj-redis-throttle-0.1.0.tgz","_nodeVersion":"16.18.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-7m69DOgCqjSrNuhBOBgaARzuEa1I15OAYKIT1/L04qY0+WCbg8gQfsVq1H5pgaITUuz/OJOBo7xmU85L1riRSQ==","shasum":"671fbadf854f047dde9e436cca2a24f80f15daf5","tarball":"https://registry.npmjs.org/@capaj/redis-throttle/-/redis-throttle-0.1.0.tgz","fileCount":12,"unpackedSize":20679,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDkrCHUGOMhgy/aclATZP99telDHPWhJkMZmCQKjukdOwIhAIoV25tF4iQMDFAX3/RucSSPCyv0s7wa8vf0m9grLkKr"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj2m2+ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmr1Yw/8DNrBnFEmyFOblYzWcxNbQE4I5rT4Gp2DwFmKAhvNU2CRXpA8\r\n3e8a1ixyZ8tDaRZLOPN3RHw9ffKNqSkIRpPLOpoL/g5VIPR61sfdzrunvlTA\r\n44do1DAURkH+LM4BgCcKe7papjuj1rZwuxEq2epu8dDR6Kc/wPToSQGovPPc\r\nOSIbewcXwf5H4aGuvKJQDxE/9stHF/zUdt/cR7euhXB+QGsAzx++/YsrQl2z\r\nhxiQyL1PQ3IPxEJp1O/OgjkRgdFTkGl70kSCbLGPzvfk8pmvtBnifzP9pWyo\r\nxJCebeAkzv5cmGwh3zqWVjrHwjFuY2hYN5nyZh49siwCzfMDpwgw7n9ha0DM\r\na2gimLiP8mzep2Y0u0QXlr515nWSWJNkU/cueFwA3drsUWweQZdPcbfvexr3\r\nIqc9wxo45tYCy4WQy13EtLifZGjafYWhGvF651xNO5uXCqB+nqFP4nhYek8O\r\nPeVtKUks8Kbt4mJmo7SDd3XLmPjDL5f87n/z5NMYSwGbBjRQL2pmQW70NNVa\r\n+c/k/UB5/qzwdTA0wvWoOeuj7VOEoVkJWrpXiHjjrCUwi6arBk3ulF/KAqE9\r\nvy/pbTuWVkkw+Sy3uFVU3cLuGx8y5umY0LS92uB2EL/RBfDu9TpPTIy7iXEU\r\nIdhRwO6AGNpNUzLbpjnEullYZLolNbCT82U=\r\n=gDi7\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"capaj","email":"capajj@gmail.com"},"directories":{},"maintainers":[{"name":"capaj","email":"capajj@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/redis-throttle_0.1.0_1675259325901_0.4034172912557885"},"_hasShrinkwrap":false},"0.1.1":{"name":"@capaj/redis-throttle","version":"0.1.1","description":"```ts import { redisThrottle } from '@capaj/redis-throttle'","main":"./dist/redis-throttle.js","module":"./dist/redis-throttle.mjs","types":"./dist/redis-throttle.d.ts","exports":{".":{"require":"./dist/redis-throttle.js","import":"./dist/redis-throttle.mjs","types":"./dist/redis-throttle.d.ts"}},"keywords":["redis-throttle","promise","all","limit","batching","concurrency","parallel","async","await","typescript","javascript","nodejs","node"],"author":"","license":"MIT","peerDependencies":{"ioredis":"^5.3.0"},"devDependencies":{"ioredis":"^5.3.0","tsup":"^6.5.0","typescript":"^4.9.5","vitest":"^0.28.3"},"scripts":{"test":"vitest","ts":"tsc --noEmit","build":"tsup redis-throttle.ts --format cjs,esm --dts --sourcemap"},"_id":"@capaj/redis-throttle@0.1.1","_integrity":"sha512-qBzJoJJItjE7JWtkI0sOtUyvZCPzG4Qt//zZ4MQrkIzs/NRL5pObzhcZMI8BS1qYRof65tx6PccWBEDgeviyOg==","_resolved":"/tmp/fdbbfd77aa8914ad4c6ccad742ff5cb8/capaj-redis-throttle-0.1.1.tgz","_from":"file:capaj-redis-throttle-0.1.1.tgz","_nodeVersion":"16.18.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-qBzJoJJItjE7JWtkI0sOtUyvZCPzG4Qt//zZ4MQrkIzs/NRL5pObzhcZMI8BS1qYRof65tx6PccWBEDgeviyOg==","shasum":"ba03b5ff636a0acccc0943d174c7a948625ffdd5","tarball":"https://registry.npmjs.org/@capaj/redis-throttle/-/redis-throttle-0.1.1.tgz","fileCount":12,"unpackedSize":21315,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDQZzmu5T/LWHV7eomdG6twnaPdOCwSxoNCnwfiAreMJAIgMVzsMaagRvxXI8kkSB/7vMD5T75DIuJSyEPpQwKE3Y0="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj2puVACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmq8PhAAi8Ww/T/EGcZqnaaMRQWvUiakSxNPPYpsKGtR6E8E16NTDYJW\r\nJp5tvbDgx+/fdi0d2uNcU4WvZM1Q/nBrOe5+fiR4geBFSb/YPBv2eOCh/34z\r\nzZcHriswFi9SCh3BE6Z5N2F6CrIUi68nt1Wbuje4bqpcL2Ftvf/wMMK1TsXJ\r\np9NrJJXvHb19YzHMHZ3fNsmrm+Ggwf1QN8QBtdPKCWrbqdns+DcS0bW4iEI1\r\nG1Dt3iNwCoo9VJHGgoFh2hLo+NZxj63UKUdqfMmTJqtkItrVNw+rWDkwlkfA\r\nlQE85gc4dvKkuw5ASp3YW/suDoMslTFLhdakB9QRBr73sISVCWo595GQip8O\r\nvrinMTf8nS8y7XtybjaqSEZHO4T61V5ZaOvI/pYs2a46v/ZlbiMbD3b/HKRS\r\nMH7nQSs9djxW7xxPWUFBO2Dr6xDPHxWysOphsaYQa/h46h0XOBUtjT7UAL+5\r\nop+PcMIuNwYTJVd4dC1J2e5jibL0YZYeW++B2pSxQT8g8jrT2NRwLPuKo1dm\r\n6gO8FehM+MsJfFi+gVVDOj1afe4WMq9EDL7Gl8xm4x/xezvCLH6bIEhfZlEm\r\n8LPoFg3E0g5TkKW533L3nWl3XEO+6ui48xrKjktZgfWBbZleF/lJZEo9N8RW\r\nCagmqCr7miiNZ/9Z7JrBoEqkHABbHxxNeXc=\r\n=70ff\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"capaj","email":"capajj@gmail.com"},"directories":{},"maintainers":[{"name":"capaj","email":"capajj@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/redis-throttle_0.1.1_1675271061385_0.6574291179126586"},"_hasShrinkwrap":false}},"time":{"created":"2023-02-01T13:48:45.762Z","0.1.0":"2023-02-01T13:48:46.062Z","modified":"2023-02-01T17:04:21.693Z","0.1.1":"2023-02-01T17:04:21.582Z"},"maintainers":[{"name":"capaj","email":"capajj@gmail.com"}],"description":"```ts import { redisThrottle } from '@capaj/redis-throttle'","keywords":["redis-throttle","promise","all","limit","batching","concurrency","parallel","async","await","typescript","javascript","nodejs","node"],"license":"MIT","readme":"## @capaj/redis-throttle\n\n```ts\nimport { redisThrottle } from '@capaj/redis-throttle'\n\nconst throttled = redisThrottle({\n  redis: redisClient,\n  key: 'showcase',\n  limit: 3,\n  durationSeconds: 5,\n  fn: async (n: number): Promise<number[]> => {\n    return new Promise((resolve) => {\n      setTimeout(() => {\n        resolve([n, throttled.runningCount(), throttled.waitingCount()])\n      }, 500)\n    })\n  }\n})\nawait throttled.reset()\nconst before = getTime()\nconst res = await Promise.all(\n  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((n) => throttled.call(n))\n)\n\nconsole.log(res) // [\n//   [ 1, 3, 10 ], [ 2, 3, 10 ],\n//   [ 3, 3, 10 ], [ 4, 3, 7 ],\n//   [ 5, 3, 7 ],  [ 6, 3, 7 ],\n//   [ 7, 3, 4 ],  [ 8, 3, 4 ],\n//   [ 9, 3, 4 ],  [ 10, 1, 1 ]\n// ]\nexpect(getTime() - before).toBeLessThan(5000) // all the call take around 4200 ms\nexpect(getTime() - before).toBeGreaterThan(4000)\nexpect(throttled.runningCount()).toBe(1)\nexpect(throttled.waitingCount()).toBe(1)\n\nexpect(res.length).toBe(10)\nexpect(await throttled.runningRedisCount()).toBe(0)\nexpect(throttled.runningCount()).toBe(0)\nexpect(throttled.waitingCount()).toBe(0)\n```\n\n### Why\n\nFor example when you have a single API key shared on multiple instances and you need to ensure you stay under the API rate limit.\n","readmeFilename":"readme.md"}