Comparing version 5.0.1 to 5.0.2
@@ -1,1 +0,1 @@ | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.Cron=factory())})(this,function(){"use strict";function minitz(year,month,day,hour,minute,second,timezone,throwOnInvalidTime){return minitz.fromTZ(minitz.tp(year,month,day,hour,minute,second,timezone),throwOnInvalidTime)}minitz.fromTZISO=(localTimeString,timezone,throwOnInvalidTime)=>{return minitz.fromTZ(parseISOLocal(localTimeString,timezone),throwOnInvalidTime)};minitz.fromTZ=function(timePoint,throwOnInvalidTime){const inputDate=new Date(Date.UTC(timePoint.year,timePoint.month-1,timePoint.day,timePoint.hour,timePoint.minute,timePoint.second)),offset=getTimezoneOffset(timePoint.timezone,inputDate),guessedLocalDate=new Date(inputDate.getTime()-offset),guessedInputDateOffset=getTimezoneOffset(timePoint.timezone,guessedLocalDate);if(guessedInputDateOffset-offset===0){return guessedLocalDate}else{const guessedLocalDate2=new Date(inputDate.getTime()-guessedInputDateOffset),guessedInputDateOffset2=getTimezoneOffset(timePoint.timezone,guessedLocalDate2);if(guessedInputDateOffset2-guessedInputDateOffset===0){return guessedLocalDate2}else if(!throwOnInvalidTime){return guessedLocalDate}else{throw new Error("Invalid date passed to fromTZ()")}}};minitz.toTZ=function(date,tzString){const target=new Date(date.toLocaleString("sv-SE",{timeZone:tzString}));return{year:target.getFullYear(),month:target.getMonth()+1,day:target.getDate(),hour:target.getHours(),minute:target.getMinutes(),second:target.getSeconds(),timezone:tzString}};minitz.tp=(y,m,d,h,i,s,t)=>{return{year:y,month:m,day:d,hour:h,minute:i,second:s,timezone:t}};function getTimezoneOffset(timeZone,date=new Date){const tz=date.toLocaleString("en",{timeZone:timeZone,timeStyle:"long"}).split(" ").slice(-1)[0];const dateString=date.toString();return Date.parse(`${dateString} UTC`)-Date.parse(`${dateString} ${tz}`)}function parseISOLocal(dateTimeString,timezone){const dateTimeStringSplit=dateTimeString.split(/\D/);if(dateTimeStringSplit.length<6){throw new Error("minitz: Incomplete ISO8601 passed to parser.")}const year=parseInt(dateTimeStringSplit[0],10),month=parseInt(dateTimeStringSplit[1],10),day=parseInt(dateTimeStringSplit[2],10),hour=parseInt(dateTimeStringSplit[3],10),minute=parseInt(dateTimeStringSplit[4],10),second=parseInt(dateTimeStringSplit[5],10);if(isNaN(year)||isNaN(month)||isNaN(day)||isNaN(hour)||isNaN(minute)||isNaN(second)){throw new Error("minitz: Could not parse ISO8601 string.")}else{const generatedDate=new Date(Date.UTC(year,month-1,day,hour,minute,second));if(!(year==generatedDate.getUTCFullYear()&&month==generatedDate.getUTCMonth()+1&&day==generatedDate.getUTCDate()&&hour==generatedDate.getUTCHours()&&minute==generatedDate.getUTCMinutes()&&second==generatedDate.getUTCSeconds())){throw new Error("minitz: ISO8601 string contains invalid date or time")}if(dateTimeString.indexOf("Z")>0){return minitz.tp(year,month,day,hour,minute,second,"Etc/UTC")}else{return minitz.tp(year,month,day,hour,minute,second,timezone)}}}minitz.minitz=minitz;function CronOptions(options){if(options===void 0){options={}}options.legacyMode=options.legacyMode===void 0?true:options.legacyMode;options.paused=options.paused===void 0?false:options.paused;options.maxRuns=options.maxRuns===void 0?Infinity:options.maxRuns;options.catch=options.catch===void 0?false:options.catch;options.interval=options.interval===void 0?0:parseInt(options.interval,10);options.kill=false;if(options.startAt){options.startAt=new CronDate(options.startAt,options.timezone)}if(options.stopAt){options.stopAt=new CronDate(options.stopAt,options.timezone)}if(options.interval!==null){if(isNaN(options.interval)){throw new Error("CronOptions: Supplied value for interval is not a number")}else if(options.interval<0){throw new Error("CronOptions: Supplied value for interval can not be negative")}}return options}function CronDate(date,timezone){this.timezone=timezone;if(date&&date instanceof Date){if(!isNaN(date)){this.fromDate(date)}else{throw new TypeError("CronDate: Invalid date passed as parameter to CronDate constructor")}}else if(date===void 0){this.fromDate(new Date)}else if(date&&typeof date==="string"){this.fromString(date)}else if(date instanceof CronDate){this.fromCronDate(date)}else{throw new TypeError("CronDate: Invalid type ("+typeof date+") passed as parameter to CronDate constructor")}}CronDate.prototype.fromDate=function(inputDate){const date=minitz.toTZ(inputDate,this.timezone);this.milliseconds=inputDate.getMilliseconds();this.seconds=date.second;this.minutes=date.minute;this.hours=date.hour;this.days=date.day;this.months=date.month-1;this.years=date.year};CronDate.prototype.fromCronDate=function(date){this.timezone=date.timezone;this.milliseconds=date.milliseconds;this.seconds=date.seconds;this.minutes=date.minutes;this.hours=date.hours;this.days=date.days;this.months=date.months;this.years=date.years};CronDate.prototype.apply=function(){const newDate=new Date(Date.UTC(this.years,this.months,this.days,this.hours,this.minutes,this.seconds,this.milliseconds));this.milliseconds=newDate.getUTCMilliseconds();this.seconds=newDate.getUTCSeconds();this.minutes=newDate.getUTCMinutes();this.hours=newDate.getUTCHours();this.days=newDate.getUTCDate();this.months=newDate.getUTCMonth();this.years=newDate.getUTCFullYear()};CronDate.prototype.fromString=function(str){return this.fromDate(minitz.fromTZISO(str,this.timezone))};CronDate.prototype.increment=function(pattern,options,hasPreviousRun){if(options.interval>1&&hasPreviousRun){this.seconds+=options.interval}else{this.seconds+=1}this.milliseconds=0;this.apply();const findNext=(target,pattern,offset,override)=>{const startPos=override===void 0?this[target]+offset:0;for(let i=startPos;i<pattern[target].length;i++){let match=pattern[target][i];if(target==="days"){const targetDate=this.getDate(true);targetDate.setDate(i-offset);if(pattern.lastDayOfMonth){const targetDateCopy=new Date(targetDate);targetDateCopy.setDate(i-offset+1);if(targetDateCopy.getMonth()!==this.months){match=true}}const dowMatch=pattern.daysOfWeek[targetDate.getDay()];if(options.legacyMode){if(!pattern.starDayOfWeek&&pattern.starDayOfMonth){match=dowMatch}else if(!pattern.starDayOfWeek&&!pattern.starDayOfMonth){match=match||dowMatch}}else{match=match&&dowMatch}}if(match){this[target]=i-offset;return true}}return false},resetPrevious=offset=>{while(doing+offset>=0){findNext(toDo[doing+offset][0],pattern,toDo[doing+offset][2],0);doing--}};const toDo=[["seconds","minutes",0],["minutes","hours",0],["hours","days",0],["days","months",-1],["months","years",0]];let doing=0;while(doing<5){const currentValue=this[toDo[doing][0]];if(!findNext(toDo[doing][0],pattern,toDo[doing][2])){this[toDo[doing][1]]++;resetPrevious(0);this.apply()}else if(currentValue!==this[toDo[doing][0]]){resetPrevious(-1)}if(this.years>=4e3){return null}doing++}return this};CronDate.prototype.getDate=function(internal){if(internal||!this.timezone){return new Date(this.years,this.months,this.days,this.hours,this.minutes,this.seconds,this.milliseconds)}else{return minitz(this.years,this.months+1,this.days,this.hours,this.minutes,this.seconds,this.timezone)}};CronDate.prototype.getTime=function(){return this.getDate().getTime()};function CronPattern(pattern,timezone){this.pattern=pattern;this.timezone=timezone;this.seconds=Array(60).fill(0);this.minutes=Array(60).fill(0);this.hours=Array(24).fill(0);this.days=Array(31).fill(0);this.months=Array(12).fill(0);this.daysOfWeek=Array(8).fill(0);this.lastDayOfMonth=false;this.starDayOfMonth=false;this.starDayOfWeek=false;this.parse()}CronPattern.prototype.parse=function(){if(!(typeof this.pattern==="string"||this.pattern.constructor===String)){throw new TypeError("CronPattern: Pattern has to be of type string.")}this.pattern=this.handleNicknames(this.pattern);const parts=this.pattern.trim().replace(/\s+/g," ").split(" ");if(parts.length<5||parts.length>6){throw new TypeError("CronPattern: invalid configuration format ('"+this.pattern+"'), exacly five or six space separated parts required.")}if(parts.length===5){parts.unshift("0")}if(parts[3].indexOf("L")>=0){parts[3]=parts[3].replace("L","");this.lastDayOfMonth=true}if(parts[3].toUpperCase()=="*"){this.starDayOfMonth=true}parts[4]=this.replaceAlphaMonths(parts[4]);parts[5]=this.replaceAlphaDays(parts[5]);if(parts[5].toUpperCase()=="*"){this.starDayOfWeek=true}const initDate=new CronDate(new Date,this.timezone).getDate(true);parts[0]=parts[0].replace("?",initDate.getSeconds());parts[1]=parts[1].replace("?",initDate.getMinutes());parts[2]=parts[2].replace("?",initDate.getHours());parts[3]=parts[3].replace("?",initDate.getDate());parts[4]=parts[4].replace("?",initDate.getMonth()+1);parts[5]=parts[5].replace("?",initDate.getDay());this.throwAtIllegalCharacters(parts);this.partToArray("seconds",parts[0],0);this.partToArray("minutes",parts[1],0);this.partToArray("hours",parts[2],0);this.partToArray("days",parts[3],-1);this.partToArray("months",parts[4],-1);this.partToArray("daysOfWeek",parts[5],0);if(this.daysOfWeek[7]){this.daysOfWeek[0]=1}};CronPattern.prototype.partToArray=function(type,conf,valueIndexOffset){const arr=this[type];if(conf==="*"){for(let i=0;i<arr.length;i++){arr[i]=1}return}const split=conf.split(",");if(split.length>1){for(let i=0;i<split.length;i++){this.partToArray(type,split[i],valueIndexOffset)}}else if(conf.indexOf("-")!==-1&&conf.indexOf("/")!==-1){this.handleRangeWithStepping(conf,type,valueIndexOffset)}else if(conf.indexOf("-")!==-1){this.handleRange(conf,type,valueIndexOffset)}else if(conf.indexOf("/")!==-1){this.handleStepping(conf,type,valueIndexOffset)}else if(conf!==""){this.handleNumber(conf,type,valueIndexOffset)}};CronPattern.prototype.throwAtIllegalCharacters=function(parts){const reValidCron=/[^/*0-9,-]+/;for(let i=0;i<parts.length;i++){if(reValidCron.test(parts[i])){throw new TypeError("CronPattern: configuration entry "+i+" ("+parts[i]+") contains illegal characters.")}}};CronPattern.prototype.handleNumber=function(conf,type,valueIndexOffset){const i=parseInt(conf,10)+valueIndexOffset;if(isNaN(i)){throw new TypeError("CronPattern: "+type+" is not a number: '"+conf+"'")}if(i<0||i>=this[type].length){throw new TypeError("CronPattern: "+type+" value out of range: '"+conf+"'")}this[type][i]=1};CronPattern.prototype.handleRangeWithStepping=function(conf,type,valueIndexOffset){const matches=conf.match(/^(\d+)-(\d+)\/(\d+)$/);if(matches===null)throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '"+conf+"'");let[,lower,upper,steps]=matches;lower=parseInt(lower,10)+valueIndexOffset;upper=parseInt(upper,10)+valueIndexOffset;steps=parseInt(steps,10);if(isNaN(lower))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(upper))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(isNaN(steps))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(steps===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(steps>this[type].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[type].length+")");if(lower<0||upper>=this[type].length)throw new TypeError("CronPattern: Value out of range: '"+conf+"'");if(lower>upper)throw new TypeError("CronPattern: From value is larger than to value: '"+conf+"'");for(let i=lower;i<=upper;i+=steps){this[type][i]=1}};CronPattern.prototype.handleRange=function(conf,type,valueIndexOffset){const split=conf.split("-");if(split.length!==2){throw new TypeError("CronPattern: Syntax error, illegal range: '"+conf+"'")}const lower=parseInt(split[0],10)+valueIndexOffset,upper=parseInt(split[1],10)+valueIndexOffset;if(isNaN(lower)){throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)")}else if(isNaN(upper)){throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)")}if(lower<0||upper>=this[type].length){throw new TypeError("CronPattern: Value out of range: '"+conf+"'")}if(lower>upper){throw new TypeError("CronPattern: From value is larger than to value: '"+conf+"'")}for(let i=lower;i<=upper;i++){this[type][i]=1}};CronPattern.prototype.handleStepping=function(conf,type){const split=conf.split("/");if(split.length!==2){throw new TypeError("CronPattern: Syntax error, illegal stepping: '"+conf+"'")}let start=0;if(split[0]!=="*"){start=parseInt(split[0],10)}const steps=parseInt(split[1],10);if(isNaN(steps))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(steps===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(steps>this[type].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[type].length+")");for(let i=start;i<this[type].length;i+=steps){this[type][i]=1}};CronPattern.prototype.replaceAlphaDays=function(conf){return conf.replace(/-sun/gi,"-7").replace(/sun/gi,"0").replace(/mon/gi,"1").replace(/tue/gi,"2").replace(/wed/gi,"3").replace(/thu/gi,"4").replace(/fri/gi,"5").replace(/sat/gi,"6")};CronPattern.prototype.replaceAlphaMonths=function(conf){return conf.replace(/jan/gi,"1").replace(/feb/gi,"2").replace(/mar/gi,"3").replace(/apr/gi,"4").replace(/may/gi,"5").replace(/jun/gi,"6").replace(/jul/gi,"7").replace(/aug/gi,"8").replace(/sep/gi,"9").replace(/oct/gi,"10").replace(/nov/gi,"11").replace(/dec/gi,"12")};CronPattern.prototype.handleNicknames=function(pattern){const cleanPattern=pattern.trim().toLowerCase();if(cleanPattern==="@yearly"||cleanPattern==="@annually"){return"0 0 1 1 *"}else if(cleanPattern==="@monthly"){return"0 0 1 * *"}else if(cleanPattern==="@weekly"){return"0 0 * * 0"}else if(cleanPattern==="@daily"){return"0 0 * * *"}else if(cleanPattern==="@hourly"){return"0 * * * *"}else{return pattern}};const maxDelay=Math.pow(2,32-1)-1;function Cron(pattern,fnOrOptions1,fnOrOptions2){if(!(this instanceof Cron)){return new Cron(pattern,fnOrOptions1,fnOrOptions2)}let options,func;if(typeof fnOrOptions1==="function"){func=fnOrOptions1}else if(typeof fnOrOptions1==="object"){options=fnOrOptions1}else if(fnOrOptions1!==void 0){throw new Error("Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options).")}if(typeof fnOrOptions2==="function"){func=fnOrOptions2}else if(typeof fnOrOptions2==="object"){options=fnOrOptions2}else if(fnOrOptions2!==void 0){throw new Error("Cron: Invalid argument passed for funcIn. Should be one of function, or object (options).")}this.options=CronOptions(options);this.once=void 0;this.pattern=void 0;if(pattern&&(pattern instanceof Date||typeof pattern==="string"&&pattern.indexOf(":")>0)){this.once=new CronDate(pattern,this.options.timezone)}else{this.pattern=new CronPattern(pattern,this.options.timezone)}if(func!==void 0){this.fn=func;this.schedule()}return this}Cron.prototype.next=function(prev){const next=this._next(prev);return next?next.getDate():null};Cron.prototype.enumerate=function(n,previous){const enumeration=[];let prev=previous||this.previousrun;while(n--&&(prev=this.next(prev))){enumeration.push(prev)}return enumeration};Cron.prototype.running=function(){const msLeft=this.msToNext(this.previousrun);const running=!this.options.paused&&this.fn!==void 0;return msLeft!==null&&running};Cron.prototype.previous=function(){return this.previousrun?this.previousrun.getDate():null};Cron.prototype.msToNext=function(prev){const next=this._next(prev||this.previousrun);prev=new CronDate(prev,this.options.timezone);if(next){return next.getTime(true)-prev.getTime(true)}else{return null}};Cron.prototype.stop=function(){this.options.kill=true;if(this.currentTimeout){clearTimeout(this.currentTimeout)}};Cron.prototype.pause=function(){return(this.options.paused=true)&&!this.options.kill};Cron.prototype.resume=function(){return!(this.options.paused=false)&&!this.options.kill};Cron.prototype.schedule=function(func,partial){if(func&&this.fn){throw new Error("Cron: It is not allowed to schedule two functions using the same Croner instance.")}else if(func){this.fn=func}let waitMs=this.msToNext(partial?partial:this.previousrun);const target=this.next(partial?partial:this.previousrun);if(waitMs===null)return this;if(waitMs>maxDelay){waitMs=maxDelay}this.currentTimeout=setTimeout(()=>{const now=new Date;if(waitMs!==maxDelay&&!this.options.paused&&now.getTime()>=target){this.options.maxRuns--;if(this.options.catch){try{this.fn(this,this.options.context)}catch(_e){}}else{this.fn(this,this.options.context)}this.previousrun=new CronDate(void 0,this.options.timezone);this.schedule()}else{this.schedule(undefined,now)}},waitMs);return this};Cron.prototype._next=function(prev){const hasPreviousRun=prev||this.previousrun?true:false;prev=new CronDate(prev,this.options.timezone);if(this.options.startAt&&prev&&prev.getTime()<this.options.startAt.getTime()){prev=this.options.startAt}const nextRun=this.once||new CronDate(prev,this.options.timezone).increment(this.pattern,this.options,hasPreviousRun);if(this.once&&this.once.getTime()<=prev.getTime()){return null}else if(nextRun===null||this.options.maxRuns<=0||this.options.kill||this.options.stopAt&&nextRun.getTime()>=this.options.stopAt.getTime()){return null}else{return nextRun}};Cron.Cron=Cron;Cron.Cron=Cron;return Cron}); | ||
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.Cron=factory())})(this,function(){"use strict";function minitz(year,month,day,hour,minute,second,timezone,throwOnInvalidTime){return minitz.fromTZ(minitz.tp(year,month,day,hour,minute,second,timezone),throwOnInvalidTime)}minitz.fromTZISO=(localTimeString,timezone,throwOnInvalidTime)=>{return minitz.fromTZ(parseISOLocal(localTimeString,timezone),throwOnInvalidTime)};minitz.fromTZ=function(timePoint,throwOnInvalidTime){const inputDate=new Date(Date.UTC(timePoint.year,timePoint.month-1,timePoint.day,timePoint.hour,timePoint.minute,timePoint.second)),offset=getTimezoneOffset(timePoint.timezone,inputDate),guessedLocalDate=new Date(inputDate.getTime()-offset),guessedInputDateOffset=getTimezoneOffset(timePoint.timezone,guessedLocalDate);if(guessedInputDateOffset-offset===0){return guessedLocalDate}else{const guessedLocalDate2=new Date(inputDate.getTime()-guessedInputDateOffset),guessedInputDateOffset2=getTimezoneOffset(timePoint.timezone,guessedLocalDate2);if(guessedInputDateOffset2-guessedInputDateOffset===0){return guessedLocalDate2}else if(!throwOnInvalidTime){return guessedLocalDate}else{throw new Error("Invalid date passed to fromTZ()")}}};minitz.toTZ=function(date,tzString){const target=new Date(date.toLocaleString("sv-SE",{timeZone:tzString}));return{year:target.getFullYear(),month:target.getMonth()+1,day:target.getDate(),hour:target.getHours(),minute:target.getMinutes(),second:target.getSeconds(),timezone:tzString}};minitz.tp=(y,m,d,h,i,s,t)=>{return{year:y,month:m,day:d,hour:h,minute:i,second:s,timezone:t}};function getTimezoneOffset(timeZone,date=new Date){const tz=date.toLocaleString("en",{timeZone:timeZone,timeStyle:"long"}).split(" ").slice(-1)[0];const dateString=date.toString();return Date.parse(`${dateString} UTC`)-Date.parse(`${dateString} ${tz}`)}function parseISOLocal(dateTimeString,timezone){const parsed=new Date(Date.parse(dateTimeString));if(isNaN(parsed)){throw new Error("minitz: Invalid ISO8601 passed to parser.")}const stringEnd=dateTimeString.substring(9);if(dateTimeString.includes("Z")||stringEnd.includes("-")||stringEnd.includes("+")){return minitz.tp(parsed.getUTCFullYear(),parsed.getUTCMonth()+1,parsed.getUTCDate(),parsed.getUTCHours(),parsed.getUTCMinutes(),parsed.getUTCSeconds(),"Etc/UTC")}else{return minitz.tp(parsed.getFullYear(),parsed.getMonth()+1,parsed.getDate(),parsed.getHours(),parsed.getMinutes(),parsed.getSeconds(),timezone)}}minitz.minitz=minitz;function CronOptions(options){if(options===void 0){options={}}options.legacyMode=options.legacyMode===void 0?true:options.legacyMode;options.paused=options.paused===void 0?false:options.paused;options.maxRuns=options.maxRuns===void 0?Infinity:options.maxRuns;options.catch=options.catch===void 0?false:options.catch;options.interval=options.interval===void 0?0:parseInt(options.interval,10);options.kill=false;if(options.startAt){options.startAt=new CronDate(options.startAt,options.timezone)}if(options.stopAt){options.stopAt=new CronDate(options.stopAt,options.timezone)}if(options.interval!==null){if(isNaN(options.interval)){throw new Error("CronOptions: Supplied value for interval is not a number")}else if(options.interval<0){throw new Error("CronOptions: Supplied value for interval can not be negative")}}return options}function CronDate(date,timezone){this.timezone=timezone;if(date&&date instanceof Date){if(!isNaN(date)){this.fromDate(date)}else{throw new TypeError("CronDate: Invalid date passed as parameter to CronDate constructor")}}else if(date===void 0){this.fromDate(new Date)}else if(date&&typeof date==="string"){this.fromString(date)}else if(date instanceof CronDate){this.fromCronDate(date)}else{throw new TypeError("CronDate: Invalid type ("+typeof date+") passed as parameter to CronDate constructor")}}CronDate.prototype.fromDate=function(inputDate){const date=minitz.toTZ(inputDate,this.timezone);this.milliseconds=inputDate.getMilliseconds();this.seconds=date.second;this.minutes=date.minute;this.hours=date.hour;this.days=date.day;this.months=date.month-1;this.years=date.year};CronDate.prototype.fromCronDate=function(date){this.timezone=date.timezone;this.milliseconds=date.milliseconds;this.seconds=date.seconds;this.minutes=date.minutes;this.hours=date.hours;this.days=date.days;this.months=date.months;this.years=date.years};CronDate.prototype.apply=function(){const newDate=new Date(Date.UTC(this.years,this.months,this.days,this.hours,this.minutes,this.seconds,this.milliseconds));this.milliseconds=newDate.getUTCMilliseconds();this.seconds=newDate.getUTCSeconds();this.minutes=newDate.getUTCMinutes();this.hours=newDate.getUTCHours();this.days=newDate.getUTCDate();this.months=newDate.getUTCMonth();this.years=newDate.getUTCFullYear()};CronDate.prototype.fromString=function(str){return this.fromDate(minitz.fromTZISO(str,this.timezone))};CronDate.prototype.increment=function(pattern,options,hasPreviousRun){if(options.interval>1&&hasPreviousRun){this.seconds+=options.interval}else{this.seconds+=1}this.milliseconds=0;this.apply();const findNext=(target,pattern,offset,override)=>{const startPos=override===void 0?this[target]+offset:0;for(let i=startPos;i<pattern[target].length;i++){let match=pattern[target][i];if(target==="days"){const targetDate=this.getDate(true);targetDate.setDate(i-offset);if(pattern.lastDayOfMonth){const targetDateCopy=new Date(targetDate);targetDateCopy.setDate(i-offset+1);if(targetDateCopy.getMonth()!==this.months){match=true}}const dowMatch=pattern.daysOfWeek[targetDate.getDay()];if(options.legacyMode){if(!pattern.starDayOfWeek&&pattern.starDayOfMonth){match=dowMatch}else if(!pattern.starDayOfWeek&&!pattern.starDayOfMonth){match=match||dowMatch}}else{match=match&&dowMatch}}if(match){this[target]=i-offset;return true}}return false},resetPrevious=offset=>{while(doing+offset>=0){findNext(toDo[doing+offset][0],pattern,toDo[doing+offset][2],0);doing--}};const toDo=[["seconds","minutes",0],["minutes","hours",0],["hours","days",0],["days","months",-1],["months","years",0]];let doing=0;while(doing<5){const currentValue=this[toDo[doing][0]];if(!findNext(toDo[doing][0],pattern,toDo[doing][2])){this[toDo[doing][1]]++;resetPrevious(0);this.apply()}else if(currentValue!==this[toDo[doing][0]]){resetPrevious(-1)}if(this.years>=4e3){return null}doing++}return this};CronDate.prototype.getDate=function(internal){if(internal||!this.timezone){return new Date(this.years,this.months,this.days,this.hours,this.minutes,this.seconds,this.milliseconds)}else{return minitz(this.years,this.months+1,this.days,this.hours,this.minutes,this.seconds,this.timezone)}};CronDate.prototype.getTime=function(){return this.getDate().getTime()};function CronPattern(pattern,timezone){this.pattern=pattern;this.timezone=timezone;this.seconds=Array(60).fill(0);this.minutes=Array(60).fill(0);this.hours=Array(24).fill(0);this.days=Array(31).fill(0);this.months=Array(12).fill(0);this.daysOfWeek=Array(8).fill(0);this.lastDayOfMonth=false;this.starDayOfMonth=false;this.starDayOfWeek=false;this.parse()}CronPattern.prototype.parse=function(){if(!(typeof this.pattern==="string"||this.pattern.constructor===String)){throw new TypeError("CronPattern: Pattern has to be of type string.")}this.pattern=this.handleNicknames(this.pattern);const parts=this.pattern.trim().replace(/\s+/g," ").split(" ");if(parts.length<5||parts.length>6){throw new TypeError("CronPattern: invalid configuration format ('"+this.pattern+"'), exacly five or six space separated parts required.")}if(parts.length===5){parts.unshift("0")}if(parts[3].indexOf("L")>=0){parts[3]=parts[3].replace("L","");this.lastDayOfMonth=true}if(parts[3].toUpperCase()=="*"){this.starDayOfMonth=true}parts[4]=this.replaceAlphaMonths(parts[4]);parts[5]=this.replaceAlphaDays(parts[5]);if(parts[5].toUpperCase()=="*"){this.starDayOfWeek=true}const initDate=new CronDate(new Date,this.timezone).getDate(true);parts[0]=parts[0].replace("?",initDate.getSeconds());parts[1]=parts[1].replace("?",initDate.getMinutes());parts[2]=parts[2].replace("?",initDate.getHours());parts[3]=parts[3].replace("?",initDate.getDate());parts[4]=parts[4].replace("?",initDate.getMonth()+1);parts[5]=parts[5].replace("?",initDate.getDay());this.throwAtIllegalCharacters(parts);this.partToArray("seconds",parts[0],0);this.partToArray("minutes",parts[1],0);this.partToArray("hours",parts[2],0);this.partToArray("days",parts[3],-1);this.partToArray("months",parts[4],-1);this.partToArray("daysOfWeek",parts[5],0);if(this.daysOfWeek[7]){this.daysOfWeek[0]=1}};CronPattern.prototype.partToArray=function(type,conf,valueIndexOffset){const arr=this[type];if(conf==="*"){for(let i=0;i<arr.length;i++){arr[i]=1}return}const split=conf.split(",");if(split.length>1){for(let i=0;i<split.length;i++){this.partToArray(type,split[i],valueIndexOffset)}}else if(conf.indexOf("-")!==-1&&conf.indexOf("/")!==-1){this.handleRangeWithStepping(conf,type,valueIndexOffset)}else if(conf.indexOf("-")!==-1){this.handleRange(conf,type,valueIndexOffset)}else if(conf.indexOf("/")!==-1){this.handleStepping(conf,type,valueIndexOffset)}else if(conf!==""){this.handleNumber(conf,type,valueIndexOffset)}};CronPattern.prototype.throwAtIllegalCharacters=function(parts){const reValidCron=/[^/*0-9,-]+/;for(let i=0;i<parts.length;i++){if(reValidCron.test(parts[i])){throw new TypeError("CronPattern: configuration entry "+i+" ("+parts[i]+") contains illegal characters.")}}};CronPattern.prototype.handleNumber=function(conf,type,valueIndexOffset){const i=parseInt(conf,10)+valueIndexOffset;if(isNaN(i)){throw new TypeError("CronPattern: "+type+" is not a number: '"+conf+"'")}if(i<0||i>=this[type].length){throw new TypeError("CronPattern: "+type+" value out of range: '"+conf+"'")}this[type][i]=1};CronPattern.prototype.handleRangeWithStepping=function(conf,type,valueIndexOffset){const matches=conf.match(/^(\d+)-(\d+)\/(\d+)$/);if(matches===null)throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '"+conf+"'");let[,lower,upper,steps]=matches;lower=parseInt(lower,10)+valueIndexOffset;upper=parseInt(upper,10)+valueIndexOffset;steps=parseInt(steps,10);if(isNaN(lower))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(upper))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(isNaN(steps))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(steps===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(steps>this[type].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[type].length+")");if(lower<0||upper>=this[type].length)throw new TypeError("CronPattern: Value out of range: '"+conf+"'");if(lower>upper)throw new TypeError("CronPattern: From value is larger than to value: '"+conf+"'");for(let i=lower;i<=upper;i+=steps){this[type][i]=1}};CronPattern.prototype.handleRange=function(conf,type,valueIndexOffset){const split=conf.split("-");if(split.length!==2){throw new TypeError("CronPattern: Syntax error, illegal range: '"+conf+"'")}const lower=parseInt(split[0],10)+valueIndexOffset,upper=parseInt(split[1],10)+valueIndexOffset;if(isNaN(lower)){throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)")}else if(isNaN(upper)){throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)")}if(lower<0||upper>=this[type].length){throw new TypeError("CronPattern: Value out of range: '"+conf+"'")}if(lower>upper){throw new TypeError("CronPattern: From value is larger than to value: '"+conf+"'")}for(let i=lower;i<=upper;i++){this[type][i]=1}};CronPattern.prototype.handleStepping=function(conf,type){const split=conf.split("/");if(split.length!==2){throw new TypeError("CronPattern: Syntax error, illegal stepping: '"+conf+"'")}let start=0;if(split[0]!=="*"){start=parseInt(split[0],10)}const steps=parseInt(split[1],10);if(isNaN(steps))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(steps===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(steps>this[type].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[type].length+")");for(let i=start;i<this[type].length;i+=steps){this[type][i]=1}};CronPattern.prototype.replaceAlphaDays=function(conf){return conf.replace(/-sun/gi,"-7").replace(/sun/gi,"0").replace(/mon/gi,"1").replace(/tue/gi,"2").replace(/wed/gi,"3").replace(/thu/gi,"4").replace(/fri/gi,"5").replace(/sat/gi,"6")};CronPattern.prototype.replaceAlphaMonths=function(conf){return conf.replace(/jan/gi,"1").replace(/feb/gi,"2").replace(/mar/gi,"3").replace(/apr/gi,"4").replace(/may/gi,"5").replace(/jun/gi,"6").replace(/jul/gi,"7").replace(/aug/gi,"8").replace(/sep/gi,"9").replace(/oct/gi,"10").replace(/nov/gi,"11").replace(/dec/gi,"12")};CronPattern.prototype.handleNicknames=function(pattern){const cleanPattern=pattern.trim().toLowerCase();if(cleanPattern==="@yearly"||cleanPattern==="@annually"){return"0 0 1 1 *"}else if(cleanPattern==="@monthly"){return"0 0 1 * *"}else if(cleanPattern==="@weekly"){return"0 0 * * 0"}else if(cleanPattern==="@daily"){return"0 0 * * *"}else if(cleanPattern==="@hourly"){return"0 * * * *"}else{return pattern}};const maxDelay=Math.pow(2,32-1)-1;function Cron(pattern,fnOrOptions1,fnOrOptions2){if(!(this instanceof Cron)){return new Cron(pattern,fnOrOptions1,fnOrOptions2)}let options,func;if(typeof fnOrOptions1==="function"){func=fnOrOptions1}else if(typeof fnOrOptions1==="object"){options=fnOrOptions1}else if(fnOrOptions1!==void 0){throw new Error("Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options).")}if(typeof fnOrOptions2==="function"){func=fnOrOptions2}else if(typeof fnOrOptions2==="object"){options=fnOrOptions2}else if(fnOrOptions2!==void 0){throw new Error("Cron: Invalid argument passed for funcIn. Should be one of function, or object (options).")}this.options=CronOptions(options);this.once=void 0;this.pattern=void 0;if(pattern&&(pattern instanceof Date||typeof pattern==="string"&&pattern.indexOf(":")>0)){this.once=new CronDate(pattern,this.options.timezone)}else{this.pattern=new CronPattern(pattern,this.options.timezone)}if(func!==void 0){this.fn=func;this.schedule()}return this}Cron.prototype.next=function(prev){const next=this._next(prev);return next?next.getDate():null};Cron.prototype.enumerate=function(n,previous){if(n>this.options.maxRuns){n=this.options.maxRuns}const enumeration=[];let prev=previous||this.previousrun;while(n--&&(prev=this.next(prev))){enumeration.push(prev)}return enumeration};Cron.prototype.running=function(){const msLeft=this.msToNext(this.previousrun);const running=!this.options.paused&&this.fn!==void 0;return msLeft!==null&&running};Cron.prototype.previous=function(){return this.previousrun?this.previousrun.getDate():null};Cron.prototype.msToNext=function(prev){const next=this._next(prev||this.previousrun);prev=new CronDate(prev,this.options.timezone);if(next){return next.getTime(true)-prev.getTime(true)}else{return null}};Cron.prototype.stop=function(){this.options.kill=true;if(this.currentTimeout){clearTimeout(this.currentTimeout)}};Cron.prototype.pause=function(){return(this.options.paused=true)&&!this.options.kill};Cron.prototype.resume=function(){return!(this.options.paused=false)&&!this.options.kill};Cron.prototype.schedule=function(func,partial){if(func&&this.fn){throw new Error("Cron: It is not allowed to schedule two functions using the same Croner instance.")}else if(func){this.fn=func}let waitMs=this.msToNext(partial?partial:this.previousrun);const target=this.next(partial?partial:this.previousrun);if(waitMs===null)return this;if(waitMs>maxDelay){waitMs=maxDelay}this.currentTimeout=setTimeout(()=>{const now=new Date;if(waitMs!==maxDelay&&!this.options.paused&&now.getTime()>=target){this.options.maxRuns--;if(this.options.catch){try{this.fn(this,this.options.context)}catch(_e){}}else{this.fn(this,this.options.context)}this.previousrun=new CronDate(void 0,this.options.timezone);this.schedule()}else{this.schedule(undefined,now)}},waitMs);return this};Cron.prototype._next=function(prev){const hasPreviousRun=prev||this.previousrun?true:false;prev=new CronDate(prev,this.options.timezone);if(this.options.startAt&&prev&&prev.getTime()<this.options.startAt.getTime()){prev=this.options.startAt}const nextRun=this.once||new CronDate(prev,this.options.timezone).increment(this.pattern,this.options,hasPreviousRun);if(this.once&&this.once.getTime()<=prev.getTime()){return null}else if(nextRun===null||this.options.maxRuns<=0||this.options.kill||this.options.stopAt&&nextRun.getTime()>=this.options.stopAt.getTime()){return null}else{return nextRun}};Cron.Cron=Cron;Cron.Cron=Cron;return Cron}); |
{ | ||
"name": "croner", | ||
"version": "5.0.1", | ||
"version": "5.0.2", | ||
"description": "Trigger functions and/or evaluate cron expressions in JavaScript. No dependencies. Most features. All environmens.", | ||
@@ -84,3 +84,3 @@ "author": "Hexagon <github.com/hexagon>", | ||
"jsdoc-chameleon-template": "^1.0.2", | ||
"minitz": "^2.1.3", | ||
"minitz": "^3.0.0", | ||
"rollup": "^2.75.7", | ||
@@ -87,0 +87,0 @@ "typescript": "^4.7.4", |
@@ -127,2 +127,5 @@ /* ------------------------------------------------------------------------------------ | ||
Cron.prototype.enumerate = function (n, previous) { | ||
if(n > this.options.maxRuns){ | ||
n = this.options.maxRuns; | ||
} | ||
const enumeration = []; | ||
@@ -129,0 +132,0 @@ let prev = previous || this.previousrun; |
/* ------------------------------------------------------------------------------------ | ||
minitz 2.1.3 - MIT License - Hexagon <hexagon@56k.guru> | ||
minitz 3.0.0 - MIT License - Hexagon <hexagon@56k.guru> | ||
@@ -242,41 +242,23 @@ Bundled manually, check https://github.com/Hexagon/minitz for updates | ||
function parseISOLocal(dateTimeString, timezone) { | ||
const dateTimeStringSplit = dateTimeString.split(/\D/); | ||
// Parse date using built in Date.parse | ||
const parsed = new Date(Date.parse(dateTimeString)); | ||
// Check for completeness | ||
if (dateTimeStringSplit.length < 6) { | ||
throw new Error("minitz: Incomplete ISO8601 passed to parser."); | ||
if (isNaN(parsed)) { | ||
throw new Error("minitz: Invalid ISO8601 passed to parser."); | ||
} | ||
const | ||
year = parseInt(dateTimeStringSplit[0], 10), | ||
month = parseInt(dateTimeStringSplit[1], 10), | ||
day = parseInt(dateTimeStringSplit[2], 10), | ||
hour = parseInt(dateTimeStringSplit[3], 10), | ||
minute = parseInt(dateTimeStringSplit[4], 10), | ||
second = parseInt(dateTimeStringSplit[5], 10); | ||
// Check parts for numeric | ||
if( isNaN(year) || isNaN(month) || isNaN(day) || isNaN(hour) || isNaN(minute) || isNaN(second) ) { | ||
throw new Error("minitz: Could not parse ISO8601 string."); | ||
// If | ||
// * date/time is specified in UTC (Z-flag included) | ||
// * or UTC offset is specified (+ or - included after character 9 (20200101 or 2020-01-0)) | ||
// Return time in utc, else return local time and include timezone identifier | ||
const stringEnd = dateTimeString.substring(9); | ||
if (dateTimeString.includes("Z") || stringEnd.includes("-") || stringEnd.includes("+")) { | ||
return minitz.tp(parsed.getUTCFullYear(), parsed.getUTCMonth()+1, parsed.getUTCDate(),parsed.getUTCHours(), parsed.getUTCMinutes(),parsed.getUTCSeconds(), "Etc/UTC"); | ||
} else { | ||
// Check generated date | ||
const generatedDate = new Date(Date.UTC(year, month-1, day, hour, minute, second)); | ||
if (!(year == generatedDate.getUTCFullYear() | ||
&& month == generatedDate.getUTCMonth()+1 | ||
&& day == generatedDate.getUTCDate() | ||
&& hour == generatedDate.getUTCHours() | ||
&& minute == generatedDate.getUTCMinutes() | ||
&& second == generatedDate.getUTCSeconds())) { | ||
throw new Error("minitz: ISO8601 string contains invalid date or time"); | ||
} | ||
// Check for UTC flag | ||
if ((dateTimeString.indexOf("Z") > 0)) { | ||
// Handle date as UTC time, ignoring input timezone | ||
return minitz.tp(year, month, day, hour, minute, second, "Etc/UTC"); | ||
} else { | ||
// Handle date as local time, and convert from specified time zone | ||
// Note: Date already validated by the UTC-parsing | ||
return minitz.tp(year, month, day, hour, minute, second, timezone); | ||
} | ||
return minitz.tp(parsed.getFullYear(), parsed.getMonth()+1, parsed.getDate(),parsed.getHours(), parsed.getMinutes(),parsed.getSeconds(), timezone); | ||
} | ||
// Treat date as local time, in target timezone | ||
} | ||
@@ -283,0 +265,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
198489
2693