UNPKG

koa-util-limit

Version:
2 lines (1 loc) 4.86 kB
function t(t,e,r,n){return new(r=r||Promise)((function(a,o){function i(t){try{c(n.next(t))}catch(t){o(t)}}function s(t){try{c(n.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?a(t.value):((e=t.value)instanceof r?e:new r((function(t){t(e)}))).then(i,s)}c((n=n.apply(t,e||[])).next())}))}function e(t,e){var r,n,a,o={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=s(0),i.throw=s(1),i.return=s(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(s){return function(c){var u=[s,c];if(r)throw new TypeError("Generator is already executing.");for(;o=i&&u[i=0]?0:o;)try{if(r=1,n&&(a=2&u[0]?n.return:u[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,u[1])).done)return a;switch(n=0,(u=a?[2&u[0],a.value]:u)[0]){case 0:case 1:a=u;break;case 4:return o.label++,{value:u[1],done:!1};case 5:o.label++,n=u[1],u=[0];continue;case 7:u=o.ops.pop(),o.trys.pop();continue;default:if(!(a=0<(a=o.trys).length&&a[a.length-1])&&(6===u[0]||2===u[0])){o=0;continue}if(3===u[0]&&(!a||u[1]>a[0]&&u[1]<a[3]))o.label=u[1];else if(6===u[0]&&o.label<a[1])o.label=a[1],a=u;else{if(!(a&&o.label<a[2])){a[2]&&o.ops.pop(),o.trys.pop();continue}o.label=a[2],o.ops.push(u)}}u=e.call(t,o)}catch(c){u=[6,c],n=0}finally{r=a=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}}}var r=(()=>{function r(t){var e=t.db,r=t.max,n=t.duration,a=t.namespace;t=t.error;this.db=e,this.max=r,this.duration=n,this.namespace=a,this.error=t}return r.prototype.set=function(r){return t(this,void 0,void 0,(function(){var t,n;return e(this,(function(e){switch(e.label){case 0:return t="".concat(this.namespace,":").concat(r),n=Date.now(),[4,this.db.zcard(this.namespace)];case 1:return e.sent(),[4,this.db.zadd(t,n,n)];case 2:return e.sent(),[4,this.db.ttl(t)];case 3:return e.sent()<=-1?[4,this.db.pexpire(t,this.duration)]:[3,5];case 4:e.sent(),e.label=5;case 5:return[2]}}))}))},r.prototype.get=function(r){return t(this,void 0,void 0,(function(){var t;return e(this,(function(e){switch(e.label){case 0:return t="".concat(this.namespace,":").concat(r),[4,this.db.zrange(t,0,-1)];case 1:return e.sent().length<this.max?[4,this.set(r)]:[3,3];case 2:return[2,e.sent()];case 3:return[2,{status:this.error.code,msg:"".concat(this.duration,"ms内超过最大限制").concat(this.max,"次")}]}}))}))},r})(),n=(()=>{function t(t){var e=t.db,r=t.max,n=t.duration,a=t.namespace;t=t.error;this.db=e,this.max=r,this.duration=n,this.namespace=a,this.error=t}return t.prototype.set=function(t,e){t="".concat(this.namespace,":").concat(t);var r=Date.now(),n={time:r,data:[]},a=this.db.get(t);(a="reset"!==e&&a?a:n).data.push(r),this.db.set(t,a)},t.prototype.get=function(t){var e="".concat(this.namespace,":").concat(t),r=Date.now(),n=(e=this.db.get(e)||{time:r,data:[]}).time;return r-n<=this.duration&&e.data.length<this.max?this.set(t):r-n>this.duration?this.set(t,"reset"):{status:this.error.code,msg:"".concat(this.duration,"ms内超过最大限制").concat(this.max,"次")}},t})();module.exports=function(a){var o=a.id,i=a.max,s=a.duration,c=a.namespace,u=void 0===(u=a.driver)?"redis":u,d=a.error;o={id:o,db:a.db,max:void 0===i?100:i,duration:void 0===s?6e4:s,namespace:void 0===c?"limit":c,error:void 0===d?{code:429,msg:"too many requests"}:d,black:a.black,white:a.white};return"redis"===u?function(n){var a,o=n.id,i=n.db,s=n.max,c=n.duration,u=n.namespace,d=n.error,h=n.white,l=n.black;if(!o)throw new Error("id function is required");if(i)return a=new r({db:i,max:s,duration:c,namespace:u,error:d}),function(r,n){return t(void 0,void 0,void 0,(function(){var t,i,s,c,u,p;return e(this,(function(e){switch(e.label){case 0:return t=o(r),s=Boolean,[4,h(r)];case 1:return i=s.apply(void 0,[e.sent()]),c=Boolean,[4,l(r)];case 2:return p=c.apply(void 0,[e.sent()]),u=d.code,d.msg,p?(u=403,[2,r.body={code:u,msg:"forbidden"}]):p||!i?[3,4]:[4,n()];case 3:case 6:return[2,e.sent()];case 4:return[4,a.get(t)];case 5:return(p=e.sent())&&p.status===u?[2,r.body={code:u,msg:p.msg}]:[4,n()]}}))}))};throw new Error("db is required")}(o):"memory"===u?function(r){var a,o=r.id,i=r.max,s=r.duration,c=r.namespace,u=r.error,d=r.white,h=r.black;if(o)return a=new n({db:new Map,max:i,duration:s,namespace:c,error:u}),function(r,n){return t(void 0,void 0,void 0,(function(){var t,i,s,c,l,p;return e(this,(function(e){switch(e.label){case 0:return t=o(r),s=Boolean,[4,d(r)];case 1:return i=s.apply(void 0,[e.sent()]),c=Boolean,[4,h(r)];case 2:return p=c.apply(void 0,[e.sent()]),l=u.code,u.msg,p?(l=403,[2,r.body={code:l,msg:"forbidden"}]):p||!i?[3,4]:[4,n()];case 3:case 6:return[2,e.sent()];case 4:return[4,a.get(t)];case 5:return(p=e.sent())&&p.status===l?[2,r.body={code:l,msg:p.msg}]:[4,n()]}}))}))};throw new Error("id function is required")}(o):void 0};