sadbot
Version:
SadBot is a tooling _to thwart_ (someone/"somebot") from accomplishing an action in a given resource, it blocks, throttles and time-restricts the access like a security guard guy when the party is packed or you're not so sober to be welcome.
3 lines (2 loc) • 17.6 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("moment"))&&"object"==typeof t&&"default"in t?t.default:t,r=require("redis");function n(t){var e,r,n,o=2;for("undefined"!=typeof Symbol&&(r=Symbol.asyncIterator,n=Symbol.iterator);o--;){if(r&&null!=(e=t[r]))return e.call(t);if(n&&null!=(e=t[n]))return new i(e.call(t));r="@@asyncIterator",n="@@iterator"}throw new TypeError("Object is not async iterable")}function i(t){function e(t){if(Object(t)!==t)return Promise.reject(new TypeError(t+" is not an object."));var e=t.done;return Promise.resolve(t.value).then((function(t){return{value:t,done:e}}))}return(i=function(t){this.s=t,this.n=t.next}).prototype={s:null,n:null,next:function(){return e(this.n.apply(this.s,arguments))},return:function(t){var r=this.s.return;return void 0===r?Promise.resolve({value:t,done:!0}):e(r.apply(this.s,arguments))},throw:function(t){var r=this.s.return;return void 0===r?Promise.reject(t):e(r.apply(this.s,arguments))}},new i(t)}function o(t,e,r,n,i,o,u){try{var a=t[o](u),c=a.value}catch(t){return void r(t)}a.done?e(c):Promise.resolve(c).then(n,i)}function u(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var u=t.apply(e,r);function a(t){o(u,n,i,a,c,"next",t)}function c(t){o(u,n,i,a,c,"throw",t)}a(void 0)}))}}function a(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,s(t,e)}function c(t){return(c=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function s(t,e){return(s=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function f(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function p(t,e,r){return(p=f()?Reflect.construct:function(t,e,r){var n=[null];n.push.apply(n,e);var i=new(Function.bind.apply(t,n));return r&&s(i,r.prototype),i}).apply(null,arguments)}function h(t){var e="function"==typeof Map?new Map:void 0;return(h=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,r)}function r(){return p(t,arguments,c(this).constructor)}return r.prototype=Object.create(t.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),s(r,t)})(t)}var l,d=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},i=n.iterator||"@@iterator",o=n.asyncIterator||"@@asyncIterator",u=n.toStringTag||"@@toStringTag";function a(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{a({},"")}catch(t){a=function(t,e,r){return t[e]=r}}function c(t,e,r,n){var i=Object.create((e&&e.prototype instanceof p?e:p).prototype),o=new P(n||[]);return i._invoke=function(t,e,r){var n="suspendedStart";return function(i,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===i)throw o;return{value:void 0,done:!0}}for(r.method=i,r.arg=o;;){var u=r.delegate;if(u){var a=b(u,r);if(a){if(a===f)continue;return a}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=s(t,e,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===f)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}(t,r,o),i}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var f={};function p(){}function h(){}function l(){}var d={};a(d,i,(function(){return this}));var v=Object.getPrototypeOf,y=v&&v(v(R([])));y&&y!==e&&r.call(y,i)&&(d=y);var m=l.prototype=p.prototype=Object.create(d);function x(t){["next","throw","return"].forEach((function(e){a(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(i,o){function u(){return new e((function(n,u){!function n(i,o,u,a){var c=s(t[i],t,o);if("throw"!==c.type){var f=c.arg,p=f.value;return p&&"object"==typeof p&&r.call(p,"__await")?e.resolve(p.__await).then((function(t){n("next",t,u,a)}),(function(t){n("throw",t,u,a)})):e.resolve(p).then((function(t){f.value=t,u(f)}),(function(t){return n("throw",t,u,a)}))}a(c.arg)}(i,o,n,u)}))}return n=n?n.then(u,u):u()}}function b(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,b(t,e),"throw"===e.method))return f;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,f;var i=n.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,f):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,f)}function g(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function k(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function P(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(g,this),this.reset(!0)}function R(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:L}}function L(){return{value:void 0,done:!0}}return h.prototype=l,a(m,"constructor",l),a(l,"constructor",h),h.displayName=a(l,u,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,l):(t.__proto__=l,a(t,u,"GeneratorFunction")),t.prototype=Object.create(m),t},t.awrap=function(t){return{__await:t}},x(w.prototype),a(w.prototype,o,(function(){return this})),t.AsyncIterator=w,t.async=function(e,r,n,i,o){void 0===o&&(o=Promise);var u=new w(c(e,r,n,i),o);return t.isGeneratorFunction(r)?u:u.next().then((function(t){return t.done?t.value:u.next()}))},x(m),a(m,u,"Generator"),a(m,i,(function(){return this})),a(m,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=R,P.prototype={constructor:P,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return u.type="throw",u.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var i=this.tryEntries.length-1;i>=0;--i){var o=this.tryEntries[i],u=o.completion;if("root"===o.tryLoc)return n("end");if(o.tryLoc<=this.prev){var a=r.call(o,"catchLoc"),c=r.call(o,"finallyLoc");if(a&&c){if(this.prev<o.catchLoc)return n(o.catchLoc,!0);if(this.prev<o.finallyLoc)return n(o.finallyLoc)}else if(a){if(this.prev<o.catchLoc)return n(o.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return n(o.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n];if(i.tryLoc<=this.prev&&r.call(i,"finallyLoc")&&this.prev<i.finallyLoc){var o=i;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var u=o?o.completion:{};return u.type=t,u.arg=e,o?(this.method="next",this.next=o.finallyLoc,f):this.complete(u)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),f},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),k(r),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;k(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:R(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=e}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=e:Function("r","regeneratorRuntime = r")(e)}}(l={exports:{}}),l.exports),v=function(t){var e=this;void 0===t&&(t={}),this.namespace="sadbot",this.prefix="sadbot",this.connectedRedisClient=u(d.mark((function t(){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e.redisClient.isOpen){t.next=3;break}return t.next=3,e.redisClient.connect();case 3:return t.abrupt("return",e.redisClient);case 4:case"end":return t.stop()}}),t)}))),this.basePrefix=function(){return[e.namespace,e.prefix]},this.basePrefixKey=function(){return e.basePrefix().join(":")},this.resourcePrefixKey=function(t){return[].concat(e.basePrefix(),[t]).join(":")},this.redisClient=t.redisClient||r.createClient(),this.namespace=t.namespace||this.namespace,this.prefix=t.prefix||this.prefix},y=function(t){function r(){var r;return(r=t.apply(this,arguments)||this).prefix="restricted",r.restrict=function(){var t=u(d.mark((function t(e,n){var i,o,u;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r.connectedRedisClient();case 2:return i=t.sent,o=r.basePrefixKey(),u=JSON.stringify({s:n.startTime,e:n.endTime}),t.next=7,i.hSet(o,e,u);case 7:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}(),r.unrestrict=function(){var t=u(d.mark((function t(e){var n,i;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r.connectedRedisClient();case 2:return n=t.sent,i=r.basePrefixKey(),t.next=6,n.hDel(i,e);case 6:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),r.getRestrictedTime=function(){var t=u(d.mark((function t(e){var n,i,o;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r.connectedRedisClient();case 2:return n=t.sent,i=r.basePrefixKey(),t.prev=4,t.next=7,n.hGet(i,e);case 7:return o=JSON.parse(t.sent),t.abrupt("return",{startTime:o.s,endTime:o.e});case 12:return t.prev=12,t.t0=t.catch(4),t.abrupt("return",null);case 15:case"end":return t.stop()}}),t,null,[[4,12]])})));return function(e){return t.apply(this,arguments)}}(),r.isRestricted=function(){var t=u(d.mark((function t(n){var i,o,u;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r.getRestrictedTime(n);case 2:if(i=t.sent){t.next=5;break}return t.abrupt("return",!1);case 5:return o=e(i.startTime,"HH:mm:ss"),u=e(i.endTime,"HH:mm:ss"),t.abrupt("return",e().isBetween(o,u));case 8:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),r}return a(r,t),r}(v),m=function(t){function e(){var e;return(e=t.apply(this,arguments)||this).prefix="blocked",e.isBlocked=function(){var t=u(d.mark((function t(r,n){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",e.connectedRedisClient().then((function(t){return t.sIsMember(e.resourcePrefixKey(r),n)})));case 1:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}(),e.block=function(){var t=u(d.mark((function t(r,n){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",e.connectedRedisClient().then((function(t){return t.sAdd(e.resourcePrefixKey(r),n)})));case 1:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}(),e.unblock=function(){var t=u(d.mark((function t(r,n){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",e.connectedRedisClient().then((function(t){return t.sRem(e.resourcePrefixKey(r),n)})));case 1:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}(),e.getBlockedList=function(){var t=u(d.mark((function t(r){var n;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,e.connectedRedisClient();case 2:return n=t.sent,t.next=5,n.sMembers(e.resourcePrefixKey(r));case 5:return t.abrupt("return",{name:r,values:t.sent});case 7:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),e.getAllBlockedLists=u(d.mark((function t(){var r,i,o,a,c,s,f,p;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,e.connectedRedisClient();case 2:r=t.sent,i=e.resourcePrefixKey("*"),o=[],a=!1,c=!1,t.prev=7,f=n(r.scanIterator({MATCH:i}));case 9:return t.next=11,f.next();case 11:if(!(a=!(p=t.sent).done)){t.next=17;break}o.push(p.value);case 14:a=!1,t.next=9;break;case 17:t.next=23;break;case 19:t.prev=19,t.t0=t.catch(7),c=!0,s=t.t0;case 23:if(t.prev=23,t.prev=24,!a||null==f.return){t.next=28;break}return t.next=28,f.return();case 28:if(t.prev=28,!c){t.next=31;break}throw s;case 31:return t.finish(28);case 32:return t.finish(23);case 33:return t.abrupt("return",Promise.all(o.map(function(){var t=u(d.mark((function t(e){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.t0=e.split(/:/)[2],t.next=3,r.sMembers(e);case 3:return t.t1=t.sent,t.abrupt("return",{name:t.t0,values:t.t1});case 5:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}())));case 34:case"end":return t.stop()}}),t,null,[[7,19,23,33],[24,,28,32]])}))),e}return a(e,t),e}(v),x=function(){var t=this;this.constraint=function(){return""},this.maxRequests=10,this.timeValue=10,this.timeSpan=1,this.by=function(e){return t.constraint=e,t},this.limitRequestsTo=function(e){return t.maxRequests=e,t},this.duration=function(e){return t.timeValue=e,t},this.seconds=function(){return t.timeSpan=1,t},this.second=this.seconds,this.minutes=function(){return t.timeSpan=60,t},this.minute=this.minutes,this.hours=function(){return t.timeSpan=3600,t},this.hour=this.hours,this.days=function(){return t.timeSpan=86400,t},this.day=this.days,this.build=function(){return{constraint:t.constraint,maxRequests:t.maxRequests,timeSpan:t.timeSpan,timeValue:t.timeValue}}},w=function(t){function e(e,r){var n;return(n=t.call(this,"Current rate of "+r+" for value: '"+e+"'")||this).name="RequestLimitExceeded",n}return a(e,t),e}(h(Error)),b=function(t){function e(e,r){var i;return void 0===e&&(e={}),(i=t.call(this,e)||this).prefix="throttling",i.maxKeys=1e4,i.hashedResourceId=function(t){return Buffer.from(t).toString("base64")},i.hashedPrefix=function(t){return i.resourcePrefixKey(i.hashedResourceId(t))},i.touchInputRequest=function(){var t=u(d.mark((function t(e,r){var n,o,u,a,c;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return void 0===r&&(r=60),t.next=3,i.connectedRedisClient();case 3:return n=t.sent,o=i.hashedPrefix(e),u=Math.floor(999999*Math.random()),a=Date.now(),c=[o,a,u].join(":"),t.abrupt("return",n.set(c,1,{EX:r}));case 9:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}(),i.rate=function(){var t=u(d.mark((function t(e){var r,o,u,a,c,s,f,p;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,i.connectedRedisClient();case 2:r=t.sent,o=i.hashedPrefix(e),u=[o,":*"].join(""),a=0,c=!1,s=!1,t.prev=8,p=n(r.scanIterator({MATCH:u,COUNT:i.maxKeys}));case 10:return t.next=12,p.next();case 12:if(!(c=!t.sent.done)){t.next=18;break}a+=1;case 15:c=!1,t.next=10;break;case 18:t.next=24;break;case 20:t.prev=20,t.t0=t.catch(8),s=!0,f=t.t0;case 24:if(t.prev=24,t.prev=25,!c||null==p.return){t.next=29;break}return t.next=29,p.return();case 29:if(t.prev=29,!s){t.next=32;break}throw f;case 32:return t.finish(29);case 33:return t.finish(24);case 34:return t.abrupt("return",a);case 35:case"end":return t.stop()}}),t,null,[[8,20,24,34],[25,,29,33]])})));return function(e){return t.apply(this,arguments)}}(),i.throttleBy=function(){var t=u(d.mark((function t(e){return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i.configure.by(e),t.abrupt("return",i.throttle());case 2:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),i.throttle=u(d.mark((function t(){var e,r,n,o,u,a,c;return d.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=i.configure.build(),r=e.maxRequests,n=e.timeSpan,o=e.timeValue,u=(0,e.constraint)()||"unknown",a=o*n,t.next=5,i.rate(u);case 5:if(!((c=t.sent)+1>r)){t.next=10;break}throw new w(u,c);case 10:return t.next=12,i.touchInputRequest(u,a);case 12:case"end":return t.stop()}}),t)}))),i.configure=new x,r&&r(i.configure),i}return a(e,t),e}(v);exports.Blocking=m,exports.RequestLimitExceeded=w,exports.Throttling=b,exports.ThrottlingConstraintBuilder=x,exports.TimeRestricted=y;
//# sourceMappingURL=sadbot.cjs.production.min.js.map