{"id":18672982,"url":"https://github.com/vicanso/superlimiter","last_synced_at":"2025-11-06T23:30:23.319Z","repository":{"id":57374780,"uuid":"86334090","full_name":"vicanso/superlimiter","owner":"vicanso","description":"frequency limiter for node.js","archived":false,"fork":false,"pushed_at":"2017-08-18T13:14:31.000Z","size":26,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-27T21:24:02.248Z","etag":null,"topics":["express-limit","koa-limit","limit","limiter"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vicanso.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-27T12:42:48.000Z","updated_at":"2022-10-10T01:33:51.000Z","dependencies_parsed_at":"2022-08-29T17:00:23.232Z","dependency_job_id":null,"html_url":"https://github.com/vicanso/superlimiter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fsuperlimiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fsuperlimiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fsuperlimiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fsuperlimiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vicanso","download_url":"https://codeload.github.com/vicanso/superlimiter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239513379,"owners_count":19651322,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express-limit","koa-limit","limit","limiter"],"created_at":"2024-11-07T09:13:37.081Z","updated_at":"2025-11-06T23:30:23.286Z","avatar_url":"https://github.com/vicanso.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# superlimiter\n\n[![Build Status](https://travis-ci.org/vicanso/superlimiter.svg?branch=master)](https://travis-ci.org/vicanso/superlimiter)\n[![Coverage Status](https://img.shields.io/coveralls/vicanso/superlimiter/master.svg?style=flat)](https://coveralls.io/r/vicanso/superlimiter?branch=master)\n[![npm](http://img.shields.io/npm/v/superlimiter.svg?style=flat-square)](https://www.npmjs.org/package/superlimiter)\n[![Github Releases](https://img.shields.io/npm/dm/superlimiter.svg?style=flat-square)](https://github.com/vicanso/superlimiter)\n\n\n## Installation\n\n```js\n$ npm i superlimiter -S\n```\n\n## Example\n\nUse for koa frequency limit\n\n```js\nconst Koa = require('koa');\nconst Limiter = require('superlimiter');\n\nconst limiter = new Limiter(redis, {\n  ttl: 60,\n  max: 10,\n  hash: (ctx) =\u003e {\n    if (!ctx.session || !ctx.session.user) {\n      return '';\n    }\n    return ctx.session.user.account;\n  },\n});\n\nconst app = new Koa();\n\n// user session\napp.use(session());\n\napp.use(limiter.middleware());\n\n// other middlewares\n...\n\napp.listen(8080);\n\n```\n\n\n## API\n\n### constructor\n\n- `client` The redis client\n\n- `options` The options for limiter\n\n- `options.ttl` The ttl for frequency limit, default is `60`\n\n- `options.max` The max count for frequency limit, default is `10`\n\n- `options.expired` The expired for frequency limit, it should be `HH:mm`. If it's set, the ttl will be ignored\n\n- `options.hash` The function to get the hash key, default is `_.identity`, if return `''`, the limit will be ignore\n\n- `options.prefix` The prefix for the cache key\n\n- `options.err` The error will be throw when count max than `options.max`, default is `new Error('Exceeded the limit frequency')`\n\n```js\nconst Redis = require('ioredis');\nconst Limiter = require('superlimiter');\n\nconst redis = new Redis('redis://127.0.0.1:6379');\nconst limiter = new Limiter(redis, {\n  ttl: 10,\n});\n```\n\n### seter\n\n`ttl` `expired` `prefix` can be reset\n\n```js\nconst Redis = require('ioredis');\nconst Limiter = require('superlimiter');\n\nconst redis = new Redis('redis://127.0.0.1:6379');\nconst limiter = new Limiter(redis, {\n  ttl: 10,\n});\nlimiter.ttl = 60;\nlimiter.expired = '23:30';\nlimiter.prefix = 'my-test-';\n```\n\n### getter\n\n`client` `options` `ttl` `expired` `prefix`\n\n```js\nconst Redis = require('ioredis');\nconst assert = require('assert');\nconst Limiter = require('superlimiter');\n\nconst redis = new Redis('redis://127.0.0.1:6379');\nconst limiter = new Limiter(redis, {\n  ttl: 10,\n});\nassert.equal(limiter.client, redis);\n// {ttl : 10, .. ..}\nconsole.info(limiter.options);\nassert.equal(limiter.ttl, 10);\nassert.equal(limiter.expired, '');\nassert.equal(limiter.prefix, 'super-limiter-');\n```\n\n### exec\n\nInc the count of the key, if the count bigger than max, it will be throw an error, otherwise it will be resolve. If the hash function return `''`, it will be resolve without any change of count.\n\n- `...args` The arguments for the hash function\n\n```js\nconst Redis = require('ioredis');\nconst Limiter = require('superlimiter');\n\nconst redis = new Redis('redis://127.0.0.1:6379');\nconst limiter = new Limiter(redis, {\n  ttl: 10,\n});\nlimiter.exec('mykey').then(() =\u003e {\n  console.info('pass');\n}).catch(console.error);\n```\n\n### getCount\n\n- `...args` The arguments for the hash function \n\n```js\nconst Redis = require('ioredis');\nconst Limiter = require('superlimiter');\n\nconst redis = new Redis('redis://127.0.0.1:6379');\nconst limiter = new Limiter(redis, {\n  ttl: 10,\n});\nlimiter.getCount('mykey').then((count) =\u003e {\n  console.info(count);\n}).catch(console.error);\n```\n\n### middleware\n\n- `type` The middleware's type, it can be `koa` or `express`, default is `koa`.\n\nThe middleware for koa and express. For koa, it will use `ctx` for the hash argument. For express, it will use `req`, `res` for the hash argument.\n\n```js\nconst Koa = require('koa');\nconst Limiter = require('superlimiter');\n\nconst limiter = new Limiter(redis, {\n  ttl: 60,\n  max: 10,\n  hash: (ctx) =\u003e {\n    if (!ctx.session || !ctx.session.user) {\n      return '';\n    }\n    return ctx.session.user.account;\n  },\n});\n\nconst app = new Koa();\n\n// user session\napp.use(session());\n\napp.use(limiter.middleware());\n\n// other middlewares\n...\n\napp.listen(8080);\n\n```\n\n### keys\n\nGet the keys of this limiter\n\n- `withTTL` Get the ttl of key if set `true`, default is `false`\n\n```js\nconst Redis = require('ioredis');\nconst Limiter = require('superlimiter');\n\nconst redis = new Redis('redis://127.0.0.1:6379');\nconst limiter = new Limiter(redis, {\n  ttl: 10,\n});\n\n// [\"...\", \"...\"]\nlimiter.keys().then(console.info).catch(console.error);\n\n// [{\"key\": \"...\", \"ttl\": ..}, ...]\nlimiter.keys(true).then(console.info).catch(console.error);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicanso%2Fsuperlimiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvicanso%2Fsuperlimiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicanso%2Fsuperlimiter/lists"}