Socket
Socket
Sign inDemoInstall

cron

Package Overview
Dependencies
1
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.5 to 1.0.6

199

lib/cron.js

@@ -1,14 +0,6 @@

var CronDate;
var exports;
var timeUnits = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'];
var loadedTimeModule = false;
var exports,
timeUnits = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'],
spawn = require('child_process').spawn,
moment = require('moment-timezone');
try {
CronDate = require("time").Date;
exports = module.exports;
loadedTimeModule = true;
} catch (e) {
CronDate = Date;
}
function CronTime(source, zone) {

@@ -23,7 +15,9 @@ this.source = source;

if ((this.source instanceof Date) || (this.source instanceof CronDate)) {
this.source = new CronDate(this.source);
if (this.source instanceof Date) {
this.source = moment(this.source);
this.realDate = true;
} else {
this._parse();
if (!this._verifyParse())
throw Error("Could not verify a valid date. Please verify your parameters.");
}

@@ -40,2 +34,16 @@ }

];
CronTime.monthConstraints = [
31,
29, //support leap year...not perfect
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
];
CronTime.parseDefaults = ['0', '*', '*', '*', '*', '*'];

@@ -66,2 +74,24 @@ CronTime.aliases = {

CronTime.prototype = {
_verifyParse: function() {
var months = Object.keys(this.month);
for (var i = 0; i < months.length; i++) {
var m = months[i];
var con = CronTime.monthConstraints[parseInt(m, 10)];
var ok = false;
var dsom = Object.keys(this.dayOfMonth);
for (var j = 0; j < dsom.length; j++) {
var dom = dsom[j];
if (dom <= con)
ok = true;
}
if (!ok) {
console.error("Month '" + m + "' is limited to '" + con + "' days.");
return false;
}
}
return true;
},
/**

@@ -71,13 +101,18 @@ * calculates the next send time

sendAt: function() {
var date = (this.source instanceof CronDate) ? this.source : new CronDate();
if (this.zone && date.setTimezone)
date.setTimezone(this.zone);
var date = this.realDate ? this.source : moment();
if (this.zone)
date = date.tz(this.zone);
//add 1 second so next time isn't now (can cause timeout to be 0)
if (!(this.realDate)) date.setSeconds(date.getSeconds() + 1);
if (this.realDate)
return date;
if (this.realDate) {
return date;
}
return this._getNextDateFrom(date);
//add 1 second so next time isn't now (can cause timeout to be 0)
if (date.seconds() == new Date().getSeconds()) {
//console.log('add 1 second?');
date = date.add(1, 's');
}
date = this._getNextDateFrom(date);
return date;
},

@@ -89,3 +124,3 @@

getTimeout: function() {
return Math.max(-1, this.sendAt().getTime() - CronDate.now());
return Math.max(-1, this.sendAt() - moment());
},

@@ -114,6 +149,9 @@

_getNextDateFrom: function(start) {
var date = new CronDate(start);
//console.log("d: " + date);
if (this.zone && date.setTimezone)
date.setTimezone(this.zone);
var date = moment(start);
if (date.toString() == 'Invalid date') {
console.log("ERROR: You specified an invalid date.");
return date;
}
//if (this.zone && date.setTimezone)
//date.setTimezone(this.zone);
if (this.realDate && start < new Date())

@@ -124,27 +162,28 @@ console.log("WARNING: Date in past. Will never be fired.");

//sanity check
//var i = 1000;
while (1) {
var diff = date - start,
origDate = new Date(date);
origDate = new Date(date);
if (!(date.getMonth() in this.month)) {
date.setMonth(date.getMonth() + 1);
date.setDate(1);
date.setHours(0);
date.setMinutes(0);
if (!(date.month() in this.month)) {
date.add(1, 'M');
date.date(1);
date.hours(0);
date.minutes(0);
date.seconds(0);
continue;
}
if (!(date.getDate() in this.dayOfMonth)) {
date.setDate(date.getDate() + 1);
date.setHours(0);
date.setMinutes(0);
if (!(date.date() in this.dayOfMonth)) {
date.add(1, 'd');
date.hours(0);
date.minutes(0);
date.seconds(0);
continue;
}
if (!(date.getDay() in this.dayOfWeek)) {
date.setDate(date.getDate() + 1);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
if (!(date.day() in this.dayOfWeek)) {
date.add(1, 'd');
date.hours(0);
date.minutes(0);
date.seconds(0);
if (date <= origDate) {

@@ -156,7 +195,7 @@ date = this._findDST(origDate);

if (!(date.getHours() in this.hour)) {
origDate = new Date(date);
date.setHours(date.getHours() == 23 && diff > 24 * 60 * 60 * 1000 ? 0 : date.getHours() + 1);
date.setMinutes(0);
date.setSeconds(0);
if (!(date.hours() in this.hour)) {
origDate = moment(date);
date.hours(date.hours() == 23 && diff > 86400000 ? 0 : date.hours() + 1);
date.minutes(0);
date.seconds(0);
if (date <= origDate) {

@@ -168,6 +207,6 @@ date = this._findDST(origDate);

if (!(date.getMinutes() in this.minute)) {
origDate = new Date(date);
date.setMinutes(date.getMinutes() == 59 && diff > 60 * 60 * 1000 ? 0 : date.getMinutes() + 1);
date.setSeconds(0);
if (!(date.minutes() in this.minute)) {
origDate = moment(date);
date.minutes(date.minutes() == 59 && diff > 60 * 60 * 1000 ? 0 : date.minutes() + 1);
date.seconds(0);
if (date <= origDate) {

@@ -179,5 +218,5 @@ date = this._findDST(origDate);

if (!(date.getSeconds() in this.second)) {
origDate = new Date(date);
date.setSeconds(date.getSeconds() == 59 && diff > 60 * 1000 ? 0 : date.getSeconds() + 1);
if (!(date.seconds() in this.second)) {
origDate = moment(date);
date.seconds(date.seconds() == 59 && diff > 60 * 1000 ? 0 : date.seconds() + 1);
if (date <= origDate) {

@@ -199,7 +238,6 @@ date = this._findDST(origDate);

_findDST: function(date) {
var newDate = new Date(date),
var newDate = moment(date),
addSeconds = 1;
while (newDate <= date) {
newDate.setSeconds(date.getSeconds() + addSeconds++);
}
while (newDate <= date)
newDate.add(1, 's');

@@ -267,4 +305,2 @@ return newDate;

_parseField: function(field, type, constraints) {
//var rangePattern = /^(\*)(?:\/(\d+))?$|(\d+)(?:-(\d+))?(?:\/(\d+))?(?:,|$)/g
var rangePattern = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g,

@@ -299,3 +335,2 @@ typeObj = this[type],

} while (pointer <= upper);
});

@@ -311,2 +346,32 @@ } else {

function command2function(cmd)
{
switch(typeof cmd)
{
case 'string':
{
var args = cmd.split(' ');
var command = args.shift();
cmd = spawn.bind(undefined, command, args);
}
break;
case 'object':
{
var command = cmd && cmd.command;
if(command)
{
var args = cmd.args;
var options = cmd.options;
cmd = spawn.bind(undefined, command, args, options);
}
}
break;
}
return cmd
}
function CronJob(cronTime, onTick, onComplete, start, timeZone, context) {

@@ -323,10 +388,10 @@ if (typeof cronTime != "string" && arguments.length == 1) {

if (timeZone && !loadedTimeModule) console.log('You specified a Timezone but have not included the `time` module. Timezone functionality is disabled. Please install the `time` module to use Timezones in your application.');
//if (timeZone && !loadedTimeModule) console.log('You specified a Timezone but have not included the `time` module. Timezone functionality is disabled. Please install the `time` module to use Timezones in your application.');
this.context = (context || this);
this._callbacks = [];
this.onComplete = onComplete;
this.onComplete = command2function(onComplete);
this.cronTime = new CronTime(cronTime, timeZone);
this.addCallback(onTick);
this.addCallback(command2function(onTick));

@@ -373,3 +438,3 @@ if (start) this.start();

},
/**

@@ -376,0 +441,0 @@ * Start the cronjob.

{
"name": "cron",
"description": "Cron jobs for your node",
"version": "1.0.5",
"version": "1.0.6",
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)",

@@ -17,5 +17,7 @@ "bugs" : {

},
"dependencies": {
"moment-timezone": "0.2.4"
},
"devDependencies": {
"nodeunit": ">=0.5.4",
"time" : "0.8.0"
"nodeunit": ">=0.5.4"
},

@@ -38,4 +40,5 @@ "licenses": [

"Vadim Baryshev <vadimbaryshev@gmail.com> (https://github.com/baryshev)",
"Leandro Ferrari <lfthomaz@gmail.com> (https://github.com/lfthomaz)"
"Leandro Ferrari <lfthomaz@gmail.com> (https://github.com/lfthomaz)",
"Gregg Zigler <greggzigler@gmail.com> (https://github.com/greggzigler)"
]
}

@@ -127,7 +127,3 @@ node-cron

If you want to specify timezones, you'll need to install the [time](https://github.com/TooTallNate/node-time) module or place an entry for it in your package.json file.
npm install time
API

@@ -134,0 +130,0 @@ ==========

var CronJob = require('./lib/cron').CronJob;
//var job = new CronJob({
//cronTime: '00 * * * * *',
//onTick: function() {
//console.log("ticking:"+new Date());
//},
//start: false
//});
//job.start();
// WORK
//var job1 = new CronJob('0 * * * * *',
//function executeTick() {
//console.log('job1', new Date());
//},
//function executeComplete() {
//console.log('done...', new Date());
//},
//false
//);
//job1.start();
var pattern = '00 12 32 * *';
function run() {
console.log('run at ' + new Date());
};
var job = new CronJob(pattern, run);
job.start();
var counter =0;
var cronSyncSchedule = '10 49 00 25 */3 *';
var scheduleJob = new CronJob(cronSyncSchedule, function() {
console.log("Current Time: " + new Date().getTime());
counter++;
// check for terminating condition
console.log("Scheduler started run ::" +counter);
}, function() {
console.log('scheduled job is completed successfully');
}, true, null);

@@ -230,21 +230,20 @@ var testCase = require('nodeunit').testCase,

'test a job with a string and a given time zone': function (assert) {
assert.expect(3);
assert.expect(2);
var time = require("time");
var moment = require("moment-timezone");
var zone = "America/Chicago";
// New Orleans time
var t = new time.Date();
t.setTimezone(zone);
var t = moment();
t.tz(zone);
// Current time
d = new Date();
d = moment();
// If current time is New Orleans time, switch to Los Angeles..
if (t.getHours() === d.getHours()) {
if (t.hours() === d.hours()) {
zone = "America/Los_Angeles";
t.setTimezone(zone);
t.tz(zone);
}
assert.notEqual(d.getHours(), t.getHours());
assert.ok(!(Date instanceof time.Date));
assert.notEqual(d.hours(), t.hours());

@@ -254,9 +253,9 @@ // If t = 59s12m then t.setSeconds(60)

// this and no testRun callback.
t.setSeconds(t.getSeconds()+1);
t.add(1, 's');
// Run a job designed to be executed at a given
// time in `zone`, making sure that it is a different
// hour than local time.
var c = new cron.CronJob(t.getSeconds() + ' ' + t.getMinutes() + ' ' + t.getHours() + ' * * *', function(){
var c = new cron.CronJob(t.seconds() + ' ' + t.minutes() + ' ' + t.hours() + ' * * *', function(){
assert.ok(true);
}, undefined, true, zone);
}, null, true, zone);

@@ -269,24 +268,23 @@ setTimeout(function() {

'test a job with a date and a given time zone': function (assert) {
assert.expect(3);
assert.expect(2);
var time = require("time");
var moment = require("moment-timezone");
var zone = "America/Chicago";
// New Orleans time
var t = new time.Date();
t.setTimezone(zone);
var t = moment();
t.tz(zone);
// Current time
d = new Date();
d = moment();
// If current time is New Orleans time, switch to Los Angeles..
if (t.getHours() === d.getHours()) {
if (t.hours() === d.hours()) {
zone = "America/Los_Angeles";
t.setTimezone(zone);
t.tz(zone);
}
assert.notEqual(d.getHours(), t.getHours());
assert.ok(!(Date instanceof time.Date));
assert.notEqual(d.hours(), t.hours());
if ((56 - t.getSeconds()) <= 0) {
setTimeout(testRun, (60000 - (t.getSeconds()*1000)) + 1000);
if ((56 - t.seconds()) <= 0) {
setTimeout(testRun, (60000 - (t.seconds()*1000)) + 1000);
} else {

@@ -297,5 +295,4 @@ testRun();

function testRun() {
var s = d.getSeconds()+2;
d.setSeconds(s);
var c = new cron.CronJob(d, function() {
d.add(2, 's');
var c = new cron.CronJob(d._d, function() {
assert.ok(true);

@@ -355,3 +352,3 @@ }, null, false, zone);

},
'test start, change time, excpetion': function(assert) {
'test start, change time, exception': function(assert) {
assert.expect(2);

@@ -408,3 +405,17 @@ var c = new cron.CronJob('* * * * * *', function() {

}, 1250);
}
},
'test avoid inf loop on invalid time': function(assert) {
// currently, the instantiation never completes
var invalid1 = new cron.CronJob('* 60 * * * *', function() {
assert.ok(true);
}, null, true);
var invalid2 = new cron.CronJob('* * 24 * * *', function() {
assert.ok(true);
}, null, true);
// assert that it gets here, right now it won't
invalid1.stop();
invalid2.stop();
assert.done()
}
});

@@ -40,3 +40,3 @@ var testCase = require('nodeunit').testCase,

},
'test no second digit doesnt throw, i.e. standard cron format (8 8 8 8 5)': function(assert) {
'test no second digit doesnt throw, i.e. standard cron format (* * * * *)': function(assert) {
assert.expect(1);

@@ -49,7 +49,14 @@ assert.doesNotThrow(function() {

'test no second digit defaults to 0, i.e. standard cron format (8 8 8 8 5)': function(assert) {
assert.expect(1);
var now = new Date();
assert.expect(6);
var now = Date.now();
var standard = new cron.CronTime('8 8 8 8 5');
var extended = new cron.CronTime('0 8 8 8 8 5');
assert.ok(standard._getNextDateFrom(now).getTime() === extended._getNextDateFrom(now).getTime());
assert.deepEqual(standard.dayOfWeek, extended.dayOfWeek);
assert.deepEqual(standard.month, extended.month);
assert.deepEqual(standard.dayOfMonth, extended.dayOfMonth);
assert.deepEqual(standard.hour, extended.hour);
assert.deepEqual(standard.minute, extended.minute);
assert.deepEqual(standard.second, extended.second);
assert.done();

@@ -138,3 +145,3 @@ },

var ct = new cron.CronTime(d);
assert.equals(ct.source.getTime(), d.getTime());
assert.ok(ct.source.isSame(d.getTime()));
assert.done();

@@ -169,5 +176,13 @@ },

assert.ok(nextdt > nextDate);
assert.ok(nextdt.getHours() % 4 === 0);
assert.ok(nextdt.hours() % 4 === 0);
assert.done();
},
'test next date from invalid date': function(assert) {
assert.expect(1);
var ct = new cron.CronTime('0 0 * * * *');
var nextDate = new Date('My invalid date string');
var nextdt = ct._getNextDateFrom(nextDate);
assert.equal(nextdt.toString(), 'Invalid date');
assert.done();
},
'test next real date': function(assert) {

@@ -181,5 +196,34 @@ assert.expect(2);

var nextdt = ct._getNextDateFrom(nextDate);
assert.deepEqual(nextdt, nextDate);
assert.ok(nextdt.isSame(nextDate));
assert.done();
},
'test < constraints day of month': function(assert) {
assert.expect(5);
var ltm = [1, 3, 5, 8, 10];
for (var i = 0; i < ltm.length; i++) {
(function(m) {
assert.throws(function() {
var ct = new cron.CronTime('0 0 0 33 ' + m + ' *');
});
})(ltm[i]);
}
assert.done();
},
'test next month selection': function(assert) {
assert.expect(1);
var date = new Date();
var dom = date.getDate() + 1;
var ct = new cron.CronTime('0 0 0 ' + dom + ' * *');
var saDate = ct.sendAt();
if (dom < date.getDate())
date.setMonth(date.getMonth()+1);
assert.equal(date.getMonth(), saDate.month());
assert.done()
}
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc