UNPKG

auto-tasker

Version:

A Node.js scheduling utility that simplifies task scheduling with human-readable syntax. Convert intuitive time formats into cron expressions and schedule tasks effortlessly using node-schedule

2 lines (1 loc) 20.6 kB
import e from"crypto";import t from"events";import r from"path";import n from"child_process";var i="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function s(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function o(e){if(e.__esModule)return e;var t=e.default;if("function"==typeof t){var r=function e(){return this instanceof e?Reflect.construct(t,arguments,this.constructor):t.apply(this,arguments)};r.prototype=t.prototype}else r={};return Object.defineProperty(r,"__esModule",{value:!0}),Object.keys(e).forEach((function(t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})})),r}var a,u,c,h,d,l,f,p,m,v,y,w,g,k,x,b,E,I,T,D,C,_,M={};function U(){return d||(d=1,h=(()=>{const e=["january","february","march","april","may","june","july","august","september","october","november","december"],t=["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"];function r(e,t){for(let r=0;r<t.length;r++)e=e.replace(new RegExp(t[r],"gi"),parseInt(r,10)+1);return e}return function(n){return n=r(n,e),n=r(n,t)}})()),h}function $(){return f||(f=1,l=(()=>{const e=["sunday","monday","tuesday","wednesday","thursday","friday","saturday"],t=["sun","mon","tue","wed","thu","fri","sat"];function r(e,t){for(let r=0;r<t.length;r++)e=e.replace(new RegExp(t[r],"gi"),parseInt(r,10));return e}return function(n){return n=r(n=n.replace("7","0"),e),r(n,t)}})()),l}function A(){if(x)return k;x=1;const e=U(),t=$(),r=m?p:(m=1,p=(()=>{function e(e,t){return-1!==e.indexOf("*")?e.replace("*",t):e}return function(t){return t[0]=e(t[0],"0-59"),t[1]=e(t[1],"0-59"),t[2]=e(t[2],"0-23"),t[3]=e(t[3],"1-31"),t[4]=e(t[4],"1-12"),t[5]=e(t[5],"0-6"),t}})()),n=(y||(y=1,v=(()=>{function e(e,t,r,n){const i=[];let s=parseInt(n),o=parseInt(r);o>s&&(s=parseInt(r),o=parseInt(n));for(let e=o;e<=s;e++)i.push(e);return e.replace(new RegExp(t,"i"),i.join())}function t(t){const r=/(\d+)-(\d+)/;let n=r.exec(t);for(;null!==n&&n.length>0;)t=e(t,n[0],n[1],n[2]),n=r.exec(t);return t}return function(e){for(let r=0;r<e.length;r++)e[r]=t(e[r]);return e}})()),v),i=(g||(g=1,w=function(e){for(var t=/^(.+)\/(\w+)$/,r=0;r<e.length;r++){var n=t.exec(e[r]);if(null!==n&&n.length>0){var i=n[2];if(isNaN(i))throw i+" is not a valid step value";for(var s=n[1].split(","),o=[],a=parseInt(i,10),u=0;u<=s.length;u++){var c=parseInt(s[u],10);c%a==0&&o.push(c)}e[r]=o.join(",")}}return e}),w);return k=function(s){let o=(a=s,a.replace(/\s{2,}/g," ").trim()).split(" ");var a;return o=function(e){return 5===e.length?["0"].concat(e):e}(o),o[4]=e(o[4]),o[5]=t(o[5]),o=r(o),o=n(o),o=i(o),o=function(e){for(let t=0;t<e.length;t++){const r=e[t].split(",");for(let e=0;e<r.length;e++)r[e]=parseInt(r[e]);e[t]=r}return e}(o),o.join(" ")},k}function P(){if(E)return b;E=1;const e=A(),t=/^(?:\d+|\*|\*\/\d+)$/;function r(e,r,n){const i=e.split(",");for(const e of i){const i=parseInt(e,10);if(!Number.isNaN(i)&&(i<r||i>n)||!t.test(e))return!1}return!0}function n(e,t){if(!r(t[0],0,59))throw new Error(`${e[0]} is a invalid expression for second`);if(function(e){return!r(e,0,59)}(t[1]))throw new Error(`${e[1]} is a invalid expression for minute`);if(function(e){return!r(e,0,23)}(t[2]))throw new Error(`${e[2]} is a invalid expression for hour`);if(function(e){return!r(e,1,31)}(t[3]))throw new Error(`${e[3]} is a invalid expression for day of month`);if(function(e){return!r(e,1,12)}(t[4]))throw new Error(`${e[4]} is a invalid expression for month`);if(function(e){return!r(e,0,7)}(t[5]))throw new Error(`${e[5]} is a invalid expression for week day`)}return b=function(t){if("string"!=typeof t)throw new TypeError("pattern must be a string!");const r=t.split(" "),i=e(t).split(" ");5===r.length&&r.unshift("0"),n(r,i)}}function j(){if(C)return D;C=1;const e=t,r=function(){if(T)return I;T=1;const e=P(),t=A();function r(e,t){return-1!==e.indexOf(",")?-1!==e.split(",").indexOf(t.toString()):e===t.toString()}return I=class{constructor(r,n){e(r),this.pattern=t(r),this.timezone=n,this.expressions=this.pattern.split(" "),this.dtf=this.timezone?new Intl.DateTimeFormat("en-US",{year:"numeric",month:"2-digit",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit",hourCycle:"h23",fractionalSecondDigits:3,timeZone:this.timezone}):null}match(e){e=this.apply(e);const t=r(this.expressions[0],e.getSeconds()),n=r(this.expressions[1],e.getMinutes()),i=r(this.expressions[2],e.getHours()),s=r(this.expressions[3],e.getDate()),o=r(this.expressions[4],e.getMonth()+1),a=r(this.expressions[5],e.getDay());return t&&n&&i&&s&&o&&a}apply(e){return this.dtf?new Date(this.dtf.format(e)):e}}}();return D=class extends e{constructor(e,t,n){super(),this.timeMatcher=new r(e,t),this.autorecover=n}start(){this.stop();let e=process.hrtime(),t=this.timeMatcher.apply(new Date);const r=()=>{const n=process.hrtime(e),i=(1e9*n[0]+n[1])/1e6;for(let e=Math.floor(i/1e3);e>=0;e--){const r=new Date((new Date).getTime()-1e3*e);let n=this.timeMatcher.apply(r);t.getTime()<n.getTime()&&(0===e||this.autorecover)&&this.timeMatcher.match(r)&&(this.emit("scheduled-time-matched",n),n.setMilliseconds(0),t=n)}e=process.hrtime(),this.timeout=setTimeout(r,1e3)};r()}stop(){this.timeout&&clearTimeout(this.timeout),this.timeout=null}},D}var S=new Uint8Array(16);function N(){if(!_&&!(_="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return _(S)}var R=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;function L(e){return"string"==typeof e&&R.test(e)}for(var O,F,B=[],V=0;V<256;++V)B.push((V+256).toString(16).substr(1));function q(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=(B[e[t+0]]+B[e[t+1]]+B[e[t+2]]+B[e[t+3]]+"-"+B[e[t+4]]+B[e[t+5]]+"-"+B[e[t+6]]+B[e[t+7]]+"-"+B[e[t+8]]+B[e[t+9]]+"-"+B[e[t+10]]+B[e[t+11]]+B[e[t+12]]+B[e[t+13]]+B[e[t+14]]+B[e[t+15]]).toLowerCase();if(!L(r))throw TypeError("Stringified UUID is invalid");return r}var z=0,W=0;function H(e){if(!L(e))throw TypeError("Invalid UUID");var t,r=new Uint8Array(16);return r[0]=(t=parseInt(e.slice(0,8),16))>>>24,r[1]=t>>>16&255,r[2]=t>>>8&255,r[3]=255&t,r[4]=(t=parseInt(e.slice(9,13),16))>>>8,r[5]=255&t,r[6]=(t=parseInt(e.slice(14,18),16))>>>8,r[7]=255&t,r[8]=(t=parseInt(e.slice(19,23),16))>>>8,r[9]=255&t,r[10]=(t=parseInt(e.slice(24,36),16))/1099511627776&255,r[11]=t/4294967296&255,r[12]=t>>>24&255,r[13]=t>>>16&255,r[14]=t>>>8&255,r[15]=255&t,r}function Z(e,t,r){function n(e,n,i,s){if("string"==typeof e&&(e=function(e){e=unescape(encodeURIComponent(e));for(var t=[],r=0;r<e.length;++r)t.push(e.charCodeAt(r));return t}(e)),"string"==typeof n&&(n=H(n)),16!==n.length)throw TypeError("Namespace must be array-like (16 iterable integer values, 0-255)");var o=new Uint8Array(16+e.length);if(o.set(n),o.set(e,n.length),(o=r(o))[6]=15&o[6]|t,o[8]=63&o[8]|128,i){s=s||0;for(var a=0;a<16;++a)i[s+a]=o[a];return i}return q(o)}try{n.name=e}catch(e){}return n.DNS="6ba7b810-9dad-11d1-80b4-00c04fd430c8",n.URL="6ba7b811-9dad-11d1-80b4-00c04fd430c8",n}function G(e){return 14+(e+64>>>9<<4)+1}function J(e,t){var r=(65535&e)+(65535&t);return(e>>16)+(t>>16)+(r>>16)<<16|65535&r}function K(e,t,r,n,i,s){return J((o=J(J(t,e),J(n,s)))<<(a=i)|o>>>32-a,r);var o,a}function Q(e,t,r,n,i,s,o){return K(t&r|~t&n,e,t,i,s,o)}function X(e,t,r,n,i,s,o){return K(t&n|r&~n,e,t,i,s,o)}function Y(e,t,r,n,i,s,o){return K(t^r^n,e,t,i,s,o)}function ee(e,t,r,n,i,s,o){return K(r^(t|~n),e,t,i,s,o)}var te=Z("v3",48,(function(e){if("string"==typeof e){var t=unescape(encodeURIComponent(e));e=new Uint8Array(t.length);for(var r=0;r<t.length;++r)e[r]=t.charCodeAt(r)}return function(e){for(var t=[],r=32*e.length,n="0123456789abcdef",i=0;i<r;i+=8){var s=e[i>>5]>>>i%32&255,o=parseInt(n.charAt(s>>>4&15)+n.charAt(15&s),16);t.push(o)}return t}(function(e,t){e[t>>5]|=128<<t%32,e[G(t)-1]=t;for(var r=1732584193,n=-271733879,i=-1732584194,s=271733878,o=0;o<e.length;o+=16){var a=r,u=n,c=i,h=s;r=Q(r,n,i,s,e[o],7,-680876936),s=Q(s,r,n,i,e[o+1],12,-389564586),i=Q(i,s,r,n,e[o+2],17,606105819),n=Q(n,i,s,r,e[o+3],22,-1044525330),r=Q(r,n,i,s,e[o+4],7,-176418897),s=Q(s,r,n,i,e[o+5],12,1200080426),i=Q(i,s,r,n,e[o+6],17,-1473231341),n=Q(n,i,s,r,e[o+7],22,-45705983),r=Q(r,n,i,s,e[o+8],7,1770035416),s=Q(s,r,n,i,e[o+9],12,-1958414417),i=Q(i,s,r,n,e[o+10],17,-42063),n=Q(n,i,s,r,e[o+11],22,-1990404162),r=Q(r,n,i,s,e[o+12],7,1804603682),s=Q(s,r,n,i,e[o+13],12,-40341101),i=Q(i,s,r,n,e[o+14],17,-1502002290),r=X(r,n=Q(n,i,s,r,e[o+15],22,1236535329),i,s,e[o+1],5,-165796510),s=X(s,r,n,i,e[o+6],9,-1069501632),i=X(i,s,r,n,e[o+11],14,643717713),n=X(n,i,s,r,e[o],20,-373897302),r=X(r,n,i,s,e[o+5],5,-701558691),s=X(s,r,n,i,e[o+10],9,38016083),i=X(i,s,r,n,e[o+15],14,-660478335),n=X(n,i,s,r,e[o+4],20,-405537848),r=X(r,n,i,s,e[o+9],5,568446438),s=X(s,r,n,i,e[o+14],9,-1019803690),i=X(i,s,r,n,e[o+3],14,-187363961),n=X(n,i,s,r,e[o+8],20,1163531501),r=X(r,n,i,s,e[o+13],5,-1444681467),s=X(s,r,n,i,e[o+2],9,-51403784),i=X(i,s,r,n,e[o+7],14,1735328473),r=Y(r,n=X(n,i,s,r,e[o+12],20,-1926607734),i,s,e[o+5],4,-378558),s=Y(s,r,n,i,e[o+8],11,-2022574463),i=Y(i,s,r,n,e[o+11],16,1839030562),n=Y(n,i,s,r,e[o+14],23,-35309556),r=Y(r,n,i,s,e[o+1],4,-1530992060),s=Y(s,r,n,i,e[o+4],11,1272893353),i=Y(i,s,r,n,e[o+7],16,-155497632),n=Y(n,i,s,r,e[o+10],23,-1094730640),r=Y(r,n,i,s,e[o+13],4,681279174),s=Y(s,r,n,i,e[o],11,-358537222),i=Y(i,s,r,n,e[o+3],16,-722521979),n=Y(n,i,s,r,e[o+6],23,76029189),r=Y(r,n,i,s,e[o+9],4,-640364487),s=Y(s,r,n,i,e[o+12],11,-421815835),i=Y(i,s,r,n,e[o+15],16,530742520),r=ee(r,n=Y(n,i,s,r,e[o+2],23,-995338651),i,s,e[o],6,-198630844),s=ee(s,r,n,i,e[o+7],10,1126891415),i=ee(i,s,r,n,e[o+14],15,-1416354905),n=ee(n,i,s,r,e[o+5],21,-57434055),r=ee(r,n,i,s,e[o+12],6,1700485571),s=ee(s,r,n,i,e[o+3],10,-1894986606),i=ee(i,s,r,n,e[o+10],15,-1051523),n=ee(n,i,s,r,e[o+1],21,-2054922799),r=ee(r,n,i,s,e[o+8],6,1873313359),s=ee(s,r,n,i,e[o+15],10,-30611744),i=ee(i,s,r,n,e[o+6],15,-1560198380),n=ee(n,i,s,r,e[o+13],21,1309151649),r=ee(r,n,i,s,e[o+4],6,-145523070),s=ee(s,r,n,i,e[o+11],10,-1120210379),i=ee(i,s,r,n,e[o+2],15,718787259),n=ee(n,i,s,r,e[o+9],21,-343485551),r=J(r,a),n=J(n,u),i=J(i,c),s=J(s,h)}return[r,n,i,s]}(function(e){if(0===e.length)return[];for(var t=8*e.length,r=new Uint32Array(G(t)),n=0;n<t;n+=8)r[n>>5]|=(255&e[n/8])<<n%32;return r}(e),8*e.length))}));function re(e,t,r,n){switch(e){case 0:return t&r^~t&n;case 1:case 3:return t^r^n;case 2:return t&r^t&n^r&n}}function ne(e,t){return e<<t|e>>>32-t}var ie=Z("v5",80,(function(e){var t=[1518500249,1859775393,2400959708,3395469782],r=[1732584193,4023233417,2562383102,271733878,3285377520];if("string"==typeof e){var n=unescape(encodeURIComponent(e));e=[];for(var i=0;i<n.length;++i)e.push(n.charCodeAt(i))}else Array.isArray(e)||(e=Array.prototype.slice.call(e));e.push(128);for(var s=e.length/4+2,o=Math.ceil(s/16),a=new Array(o),u=0;u<o;++u){for(var c=new Uint32Array(16),h=0;h<16;++h)c[h]=e[64*u+4*h]<<24|e[64*u+4*h+1]<<16|e[64*u+4*h+2]<<8|e[64*u+4*h+3];a[u]=c}a[o-1][14]=8*(e.length-1)/Math.pow(2,32),a[o-1][14]=Math.floor(a[o-1][14]),a[o-1][15]=8*(e.length-1)&4294967295;for(var d=0;d<o;++d){for(var l=new Uint32Array(80),f=0;f<16;++f)l[f]=a[d][f];for(var p=16;p<80;++p)l[p]=ne(l[p-3]^l[p-8]^l[p-14]^l[p-16],1);for(var m=r[0],v=r[1],y=r[2],w=r[3],g=r[4],k=0;k<80;++k){var x=Math.floor(k/20),b=ne(m,5)+re(x,v,y,w)+g+t[x]+l[k]>>>0;g=w,w=y,y=ne(v,30)>>>0,v=m,m=b}r[0]=r[0]+m>>>0,r[1]=r[1]+v>>>0,r[2]=r[2]+y>>>0,r[3]=r[3]+w>>>0,r[4]=r[4]+g>>>0}return[r[0]>>24&255,r[0]>>16&255,r[0]>>8&255,255&r[0],r[1]>>24&255,r[1]>>16&255,r[1]>>8&255,255&r[1],r[2]>>24&255,r[2]>>16&255,r[2]>>8&255,255&r[2],r[3]>>24&255,r[3]>>16&255,r[3]>>8&255,255&r[3],r[4]>>24&255,r[4]>>16&255,r[4]>>8&255,255&r[4]]}));var se,oe,ae,ue,ce,he,de,le,fe,pe,me,ve,ye,we,ge,ke,xe,be,Ee,Ie,Te,De,Ce,_e,Me,Ue,$e,Ae,Pe=Object.freeze({__proto__:null,NIL:"00000000-0000-0000-0000-000000000000",parse:H,stringify:q,v1:function(e,t,r){var n=t&&r||0,i=t||new Array(16),s=(e=e||{}).node||O,o=void 0!==e.clockseq?e.clockseq:F;if(null==s||null==o){var a=e.random||(e.rng||N)();null==s&&(s=O=[1|a[0],a[1],a[2],a[3],a[4],a[5]]),null==o&&(o=F=16383&(a[6]<<8|a[7]))}var u=void 0!==e.msecs?e.msecs:Date.now(),c=void 0!==e.nsecs?e.nsecs:W+1,h=u-z+(c-W)/1e4;if(h<0&&void 0===e.clockseq&&(o=o+1&16383),(h<0||u>z)&&void 0===e.nsecs&&(c=0),c>=1e4)throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");z=u,W=c,F=o;var d=(1e4*(268435455&(u+=122192928e5))+c)%4294967296;i[n++]=d>>>24&255,i[n++]=d>>>16&255,i[n++]=d>>>8&255,i[n++]=255&d;var l=u/4294967296*1e4&268435455;i[n++]=l>>>8&255,i[n++]=255&l,i[n++]=l>>>24&15|16,i[n++]=l>>>16&255,i[n++]=o>>>8|128,i[n++]=255&o;for(var f=0;f<6;++f)i[n+f]=s[f];return t||q(i)},v3:te,v4:function(e,t,r){var n=(e=e||{}).random||(e.rng||N)();if(n[6]=15&n[6]|64,n[8]=63&n[8]|128,t){r=r||0;for(var i=0;i<16;++i)t[r+i]=n[i];return t}return q(n)},v5:ie,validate:L,version:function(e){if(!L(e))throw TypeError("Invalid UUID");return parseInt(e.substr(14,1),16)}}),je=o(Pe);function Se(){if(oe)return se;oe=1;const e=t,r=function(){if(c)return u;c=1;const e=t;return u=class extends e{constructor(e){if(super(),"function"!=typeof e)throw"execution must be a function";this._execution=e}execute(e){let t;try{t=this._execution(e)}catch(e){return this.emit("task-failed",e)}return t instanceof Promise?t.then((()=>this.emit("task-finished"))).catch((e=>this.emit("task-failed",e))):(this.emit("task-finished"),t)}}}(),n=j(),i=je;return se=class extends e{constructor(e,t,s){super(),s||(s={scheduled:!0,recoverMissedExecutions:!1}),this.options=s,this.options.name=this.options.name||i.v4(),this._task=new r(t),this._scheduler=new n(e,s.timezone,s.recoverMissedExecutions),this._scheduler.on("scheduled-time-matched",(e=>{this.now(e)})),!1!==s.scheduled&&this._scheduler.start(),!0===s.runOnInit&&this.now("init")}now(e="manual"){let t=this._task.execute(e);this.emit("task-done",t)}start(){this._scheduler.start()}stop(){this._scheduler.stop()}}}function Ne(){return he||(he=1,i.scheduledTasks||(i.scheduledTasks=new Map),ce={save:e=>{if(!e.options){const t=je;e.options={},e.options.name=t.v4()}i.scheduledTasks.set(e.options.name,e)},getTasks:()=>i.scheduledTasks}),ce}function Re(){if(le)return de;le=1;const e=Se(),i=function(){if(ue)return ae;ue=1;const e=t,i=r,{fork:s}=n,o=je,a=`${__dirname}/daemon.js`;return ae=class extends e{constructor(e,t,r){super(),r||(r={scheduled:!0,recoverMissedExecutions:!1}),this.cronExpression=e,this.taskPath=t,this.options=r,this.options.name=this.options.name||o.v4(),r.scheduled&&this.start()}start(){this.stop(),this.forkProcess=s(a),this.forkProcess.on("message",(e=>{"task-done"===e.type&&this.emit("task-done",e.result)}));let e=this.options;e.scheduled=!0,this.forkProcess.send({type:"register",path:i.resolve(this.taskPath),cron:this.cronExpression,options:e})}stop(){this.forkProcess&&this.forkProcess.kill()}pid(){if(this.forkProcess)return this.forkProcess.pid}isRunning(){return!this.forkProcess.killed}}}(),s=P(),o=Ne();return de={schedule:function(t,r,n){const s=function(t,r,n){return"string"==typeof r?new i(t,r,n):new e(t,r,n)}(t,r,n);return o.save(s),s},validate:function(e){try{return s(e),!0}catch(e){return!1}},getTasks:function(){return o.getTasks()}},de}function Le(){if(pe)return fe;pe=1;const e=Re();return fe=class{constructor(){this.cronExcuter=e}task(e){if(!e)throw new Error("callBackTask is required");return this.callBackTask=e,this}excute(e=this.cronExcuter){if(!this.callBackTask)throw new Error("task() method must be called before excute()");e.schedule(this.toCronExp(),(()=>{this.callBackTask()}))}}}function Oe(){if(ve)return me;ve=1;return me={DEAFULTS:{every:"week",on:"monday",at:"00:00",period:"am"},validTimeUnits:["day","week","hour"],weekDays:["sunday","monday","tuesday","wednesday","thursday","friday","saturday"]}}function Fe(){if(we)return ye;we=1;const{DEAFULTS:e,weekDays:t}=Oe();return ye={validateWeekDays:e=>!!t.includes(e?.toLowerCase())&&t.indexOf(e.toLowerCase())+1,validateTimeFormat:e=>!!e.match(/^(0?[0-9]|1[0-2]):([0-5]\d)$/),validateTimePeriod:e=>!("string"!=typeof e||!["am","pm"].includes(e?.toLowerCase()))}}function Be(){if(De)return Te;De=1;const e=function(){if(ke)return ge;ke=1;const e=Le(),{validateTimeFormat:t,validateWeekDays:r,validateTimePeriod:n}=Fe();return ge=class extends e{constructor(e,t,r){if(super(),!e||!t||!r)throw new Error("Invalid schedule data");this.day=e.toLowerCase(),this.time=t,this.period=r.toLowerCase()}toCronExp(){if(!1===t(this.time))throw new Error("Invalid time format");if(!1===n(this.period))throw new Error("Invalid schedule data");if(this.time){let[e,t]=this.time.split(":").map(Number);"am"===this.period&&12===e&&(e=0),"pm"===this.period&&12!==e&&(e+=12);const n=r(this.day);if(!n)throw new Error("Invalid day of week");return`${t?`${t}`:0} ${e?`${e}`:0} * * ${n-1}`}throw new Error("Invalid time format")}}}(),t=function(){if(be)return xe;be=1;const e=Le(),{validateTimeFormat:t,validateTimePeriod:r}=Fe();return xe=class extends e{constructor(e,t){if(super(),!e)throw new Error("Invalid schedule data");this.time=e,this.period=t?.toLowerCase()??"am"}toCronExp(){if(!t(this.time)||!r(this.period))throw new Error("Invalid time format");if(this.time){let[e,t]=this.time.split(":").map(Number);return"am"===this.period&&12===e&&(e=0),"pm"===this.period&&12!==e&&(e+=12),`${t?`${t} `:"0 "}${e?`${e}`:""} * * *`}return"* * * * *"}}}(),r=function(){if(Ie)return Ee;Ie=1;const e=Le(),{validateWeekDays:t}=Fe();return Ee=class extends e{constructor(e){if(super(),this.everyMinute=!e,this.day=t(e),!this.day&&!this.everyMinute)throw new Error("Invalid day")}toCronExp(){if(this.day)return"0 * * * "+(this.day-1);if(this.everyMinute)return"* * * * *";throw new Error("Invalid Cron Operation")}}}();return Te=class{constructor(){this.schedules=[]}static createSchedule(n){const{every:i,at:s,period:o,on:a,callBackTask:u}=n;let c;switch(i){case"week":c=new e(a,s,o);break;case"day":c=new t(s,o);break;case"hour":c=new r(a);break;default:throw new Error("Invalid time unit, every must be set to week or day or hour")}return c.task(u).excute()}}}function Ve(){if(Ue)return Me;Ue=1;const e=Be(),{validateTimeFormat:t,validateWeekDays:r,validateTimePeriod:n}=Fe(),{mapNumberToTimeFormat:i}=_e?Ce:(_e=1,Ce={mapNumberToTimeFormat:e=>{if("string"==typeof e)throw new Error("Invalid time format");if(e>=0&&e<=12)return 1===(e=e.toString()).length&&(e=`0${e}`),`${e}:00`;throw new Error("Invalid time format")}}),{DEAFULTS:s,validTimeUnits:o}=Oe();return Me=class{constructor(){this.scheduleData={},this._excutionChain=[],this.scheduleData.callBackTask=null}every(e=s.every){if("string"!=typeof e)throw new Error("Invalid time unit, every must be set to string.");if(!o.includes(e.toLowerCase()))throw new Error("Invalid time unit, every must be set to 'week' or 'day' or 'hour'");return this.scheduleData.every=e.toLowerCase(),this._excutionChain.push("every"),this.scheduleData.on=s.on,this.scheduleData.at=s.at,this.scheduleData.period=s.period,this}on(e){if(this._excutionChain.includes("every")||this.every(),"week"!==this.scheduleData.every)throw new Error("Invalid time unit, every must be set to week");if(!r(e))throw new Error("Invalid day of week");return this.scheduleData.on=e,this._excutionChain.push("on"),this}at(e,r){try{if("string"!=typeof e||isNaN(Number(e))||(e=i(Number(e))),"number"==typeof e&&(e=i(e)),this._excutionChain.includes("every")||this.every("day"),(this._excutionChain.includes("on")||this._excutionChain.includes("every"))&&(e||(e=s.at),r||(r=s.period)),"week"!==this.scheduleData.every&&"day"!==this.scheduleData.every)throw new Error("Invalid time unit, every must be set to week or day");if(!t(e)||!n(r))throw new Error("Invalid time format or time period");return this.scheduleData.at=e,this.scheduleData.period=r?.toLowerCase(),this._excutionChain.push("at"),this}catch(e){throw console.log(e),e}}do(t){if(!t)throw new Error("callBackTask is required");return this.scheduleData.callBackTask=t,e.createSchedule(this.scheduleData)}}}var qe=s(function(){if(Ae)return $e;Ae=1,function(){if(a)return M;a=1;const t=e;t.getRandomValues||(t.getRandomValues=function(e){return t.randomFillSync(e)})}();const t=Ve();return $e={ScheduleBuilder:t}}());export{qe as default};