Socket
Socket
Sign inDemoInstall

cron

Package Overview
Dependencies
0
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.1.3 to 0.2.0

.npmignore

300

lib/cron.js

@@ -1,41 +0,149 @@

/**
* cron.js
* ---
* VERSION 0.1
* ---
* @author James Padolsey
* ---
* Dual licensed under the MIT and GPL licenses.
* - http://www.opensource.org/licenses/mit-license.php
* - http://www.gnu.org/copyleft/gpl.html
*/
function CronTime(time) {
this.source = time;
this.map = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'];
this.constraints = [[0,59],[0,59],[0,23],[1,31],[0,11],[1,7]];
this.aliases = {
jan:0,feb:1,mar:2,apr:3,may:4,jun:5,jul:6,aug:7,sep:8,oct:9,nov:10,dec:11,
sun:1,mon:2,tue:3,wed:4,thu:5,fri:6,sat:7
};
this.second = {};
this.minute = {};
this.hour = {};
this.second = {};
this.minute = {};
this.hour = {};
this.dayOfWeek = {};
this.dayOfMonth = {};
this.month = {};
this.dayOfWeek = {};
this.month = {};
this._parse();
if (!(this.source instanceof Date)) {
this._parse();
} else {
this.realDate = true;
}
};
CronTime.map = ['second', 'minute', 'hour', 'dayOfMonth', 'month', 'dayOfWeek'];
CronTime.constraints = [ [0, 59], [0, 59], [0, 23], [1, 31], [0, 11], [1, 7] ];
CronTime.aliases = {
jan:0, feb:1, mar:2, apr:3, may:4, jun:5, jul:6, aug:7, sep:8, oct:9, nov:10, dec:11,
sun:1, mon:2, tue:3, wed:4, thu:5, fri:6, sat:7
};
CronTime.prototype = {
/**
* calculates the next send time
*/
sendAt: function() {
var date = (this.source instanceof Date) ? this.source : new Date();
//add 1 second so next time isn't now (can cause timeout to be 0)
date.setSeconds(date.getSeconds() + 1);
if (!(this.realDate)) {
var i = 1000;
//sanity check
while(--i) {
if (!(date.getMonth() in this.month)) {
date.setMonth(date.getMonth()+1);
date.setDate(1);
date.setHours(0);
date.setMinutes(0);
continue;
}
if (!(date.getDate() in this.dayOfMonth)) {
date.setDate(date.getDate()+1);
date.setHours(0);
date.setMinutes(0);
continue;
}
if (!(date.getDay()+1 in this.dayOfWeek)) {
date.setDate(date.getDate()+1);
date.setHours(0);
date.setMinutes(0);
continue;
}
if (!(date.getHours() in this.hour)) {
date.setHours(date.getHours()+1);
date.setMinutes(0);
continue;
}
if (!(date.getMinutes() in this.minute)) {
date.setMinutes(date.getMinutes()+1);
date.setSeconds(0);
continue;
}
if(!(date.getSeconds() in this.second)) {
date.setSeconds(date.getSeconds()+1);
continue;
}
break;
}
}
return date;
},
/**
* Get the number of seconds in the future at which to fire our callbacks.
*/
getTimeout: function() {
return Math.max(-1, this.sendAt().getTime() - Date.now());
},
/**
* writes out a cron string
*/
toString: function() {
return this.toJSON().join(' ');
},
/**
* Json representation of the parsed cron syntax.
*/
toJSON: function() {
return [
this._wcOrAll('second'),
this._wcOrAll('minute'),
this._wcOrAll('hour'),
this._wcOrAll('dayOfMonth'),
this._wcOrAll('month'),
this._wcOrAll('dayOfWeek')
];
},
/**
* wildcard, or all params in array (for to string)
*/
_wcOrAll: function(type) {
if(this._hasAll(type)) return '*';
var all = [];
for(var time in this[type]) {
all.push(time);
}
return all.join(',');
},
/**
*/
_hasAll: function(type) {
var constrain = CronTime.constraints[CronTime.map.indexOf(type)];
for(var i = constrain[0], n = constrain[1]; i < n; i++) {
if(!(i in this[type])) return false;
}
return true;
},
/**
* Parse the cron syntax.
*/
_parse: function() {
var aliases = this.aliases,
var aliases = CronTime.aliases,
source = this.source.replace(/[a-z]{1,3}/ig, function(alias){
alias = alias.toLowerCase();

@@ -48,3 +156,2 @@

throw new Error('Unknown alias: ' + alias);
}),

@@ -56,11 +163,13 @@ split = source.replace(/^\s\s*|\s\s*$/g, '').split(/\s+/),

cur = split[len] || '*';
this._parseField(cur, this.map[len], this.constraints[len]);
this._parseField(cur, CronTime.map[len], CronTime.constraints[len]);
}
},
},
/**
* Parse a field from the cron syntax.
*/
_parseField: function(field, type, constraints) {
var rangePattern = /(\d+?)(?:-(\d+?))?(?:\/(\d+?))?(?:,|$)/g,
typeObj = this[type],
diff,
diff, pointer,
low = constraints[0],

@@ -73,5 +182,3 @@ high = constraints[1];

if (field.match(rangePattern)) {
field.replace(rangePattern, function($0, lower, upper, step) {
step = parseInt(step) || 1;

@@ -94,9 +201,5 @@

});
} else {
throw new Error('Field (' + field + ') cannot be parsed');
}
}

@@ -106,71 +209,98 @@ };

function CronJob(cronTime, event, oncomplete) {
if (!(this instanceof CronJob)) {
return new CronJob(cronTime, event);
function CronJob(cronTime, onTick, onComplete, start) {
if (typeof cronTime != "string" && arguments.length == 1) {
//crontime is an object...
onTick = cronTime.onTick;
onComplete = cronTime.onComplete;
start = cronTime.start;
cronTime = cronTime.cronTime;
}
this.events = [event];
this.cronTime = new CronTime(cronTime);
this.now = {};
this.initiated = false;
this.oncomplete = oncomplete;
this._callbacks = [];
this.onComplete = onComplete;
this.cronTime = new CronTime(cronTime);
this.clock();
this.addCallback(onTick);
if (start) this.start();
return this;
}
CronJob.prototype = {
addEvent: function(event) {
this.events.push(event);
/**
* Add a method to fire onTick
*/
addCallback: function(callback) {
//only functions
if(typeof callback == 'function') this._callbacks.push(callback);
},
runEvents: function() {
for (var i = -1, l = this.events.length; ++i < l; ) {
if (typeof this.events[i] === 'function') {
this.events[i](this.oncomplete);
}
/**
* Fire all callbacks registered.
*/
_callback: function() {
for (var i = (this._callbacks.length - 1); i >= 0; i--) {
//send this so the callback can call this.stop();
this._callbacks[i].call(this, this.onComplete);
}
},
clock: function() {
var date = new Date,
now = this.now,
self = this,
cronTime = this.cronTime,
i;
/**
* Start the cronjob.
*/
start: function() {
if(this.running) return;
var timeout = this.cronTime.getTimeout();
if (!this.initiated) {
// Make sure we start the clock precisely ON the 0th millisecond
setTimeout(function(){
self.initiated = true;
self.clock();
}, Math.ceil(+date / 1000) * 1000 - +date);
return;
if (timeout >= 0) {
this.running = true;
this._timeout = setTimeout(function(self) {
self.running = false;
//start before calling back so the callbacks have the ability to stop the cron job
self.start();
self._callback();
}, timeout, this);
} else {
this.running = false;
}
},
this.timer = this.timer || setInterval(function(){self.clock();}, 1000);
/**
* Stop the cronjob.
*/
stop: function()
{
clearTimeout(this._timeout);
this.running = false;
//if (this.onComplete) this.onComplete();
}
};
now.second = date.getSeconds();
now.minute = date.getMinutes();
now.hour = date.getHours();
now.dayOfMonth = date.getDate();
now.month = date.getMonth();
now.dayOfWeek = date.getDay() + 1;
for (i in now) {
if (!(now[i] in cronTime[i])) {
return;
}
}
exports.job = function(cronTime, onTick, onComplete) {
return new CronJob(cronTime, onTick, onComplete);
}
this.runEvents();
exports.time = function(cronTime) {
return new CronTime(cronTime);
}
}
exports.sendAt = function(cronTime) {
return exports.time(cronTime).sendAt();
}
};
exports.timeout = function(cronTime) {
return exports.time(cronTime).getTimeout();
}
exports.CronJob = CronJob;
exports.CronTime = CronTime;
{
"name": "cron",
"description": "CronJob's for your node",
"version": "0.1.3",
"author": "James Padolsey (http://github.com/jamespadolsey)",
"bugs" : {
"web" : "http://github.com/ncb000gt/node-cron/issues"
},
"repository": {
"type": "git",
"url": "http://github.com/ncb000gt/node-cron.git"
},
"engine": [
"node >=0.1.90"
],
"main": "lib/cron",
"scripts": {
"test": "nodeunit tests/*"
},
"dependencies": {
"nodeunit": "=0.5.2"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
},
{
"type": "GPL",
"url": "http://www.gnu.org/copyleft/gpl.html"
}
],
"contributors": [
"Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)",
"Finn Herpich <> (http://github.com/ErrorProne)",
"Clifton Cunningham <clifton.cunningham@gmail.com> (http://github.com/cliftonc)",
"Eric Abouaf <eric.abouaf@gmail.com> (http://github.com/neyric)",
"humanchimp <> (http://github.com/humanchimp)"
]
"name": "cron",
"description": "CronJob's for your node",
"version": "0.2.0",
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (http://github.com/ncb000gt)",
"bugs" : {
"url" : "http://github.com/ncb000gt/node-cron/issues"
},
"repository": {
"type": "git",
"url": "http://github.com/ncb000gt/node-cron.git"
},
"main": "lib/cron",
"scripts": {
"test": "make test"
},
"devDependencies": {
"nodeunit": ">=0.5.4"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
],
"contributors": [
"James Padolsey <> (http://github.com/jamespadolsey)",
"Finn Herpich <fh@three-heads.de> (http://github.com/ErrorProne)",
"Clifton Cunningham <clifton.cunningham@gmail.com> (http://github.com/cliftonc)",
"Eric Abouaf <eric.abouaf@gmail.com> (http://github.com/neyric)",
"humanchimp <morphcham@gmail.com> (http://github.com/humanchimp)",
"Craig Condon <craig@spiceapps.com> (http://github.com/spiceapps)"
]
}

@@ -1,11 +0,21 @@

A NodeJS fork of [jamespadolsey's](http://github.com/jamespadolsey) [cron.js](http://github.com/jamespadolsey/cron.js).
node-cron
=========
[![Build Status](https://secure.travis-ci.org/ncb000gt/node-cron.png)](http://travis-ci.org/#!/ncb000gt/node-cron)
Originally this project was a NodeJS fork of [James Padolsey's][jamespadolsey] [cron.js](http://github.com/padolsey/cron.js).
After [Craig Condon][crcn] made some updates and changes to the code base this has evolved to something that has a bit of both. The cron syntax parsing is mostly James' while using timeout instead of interval is Craig's.
Additionally, this library goes beyond the basic cron syntax and allows you to supply a Date object. This will be used as the trigger for your callback. Cron syntax is still an acceptable CronTime format.
Usage:
==========
var cron = require('cron'), sys = require('sys');
new cron.CronJob('* * * * * *', function(){
sys.puts('You will see this message every second');
});
var cronJob = require('cron').CronJob;
cronJob('* * * * * *', function(){
console.log('You will see this message every second');
}, null, true);
Available Cron patterns:

@@ -23,4 +33,4 @@ ==========

var cron = require('cron'), sys = require('sys');
new CronJob('00 30 11 * * 2-6', function(){
var cronJob = require('cron').CronJob;
var job = cronJob('00 30 11 * * 2-6', function(){
// Runs every weekday (Monday through Friday)

@@ -30,3 +40,20 @@ // at 11:30:00 AM. It does not run on Saturday

});
job.start();
For good measure
==========
var cronJob = require('cron').CronJob;
var job = cronJob({
cronTime: '00 30 11 * * 2-6',
onTick: function() {
// Runs every weekday (Monday through Friday)
// at 11:30:00 AM. It does not run on Saturday
// or Sunday.
},
start: true
});
job.start();
How to check if a cron pattern is valid:

@@ -36,7 +63,7 @@ ==========

try {
new cron.CronTime('invalid cron pattern', function() {
sys.puts('this should not be printed');
cronJob('invalid cron pattern', function() {
console.log('this should not be printed');
})
} catch(ex) {
sys.puts("cron pattern not valid");
console.log("cron pattern not valid");
}

@@ -46,15 +73,34 @@

==========
From source: `sudo npm install`
From npm: `sudo npm install cron`
From source: `npm install`
From npm: `npm install cron`
API
==========
Parameter Based
`CronJob`
* `constructor(cronTime, onTick, onComplete, start)` - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above).
* `cronTime` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
* `onTick` - [REQUIRED] - The function to fire at the specified time.
* `onComplete` - [OPTIONAL] - A function that will fire when the job is complete, when it is stopped.
* `start` - [OPTIONAL] - Specifies whether to start the job after just before exiting the constructor.
* `start` - Runs your job.
* `stop` - Stops your job.
`CronTime`
* `constructor(time)`
* `time` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
Contributors
===========
* Nick Campbell
* Finn Herpich
* James Padolsey
* cliftonc
* Finn
* neyric
* humanchimp
* [James Padolsey][jamespadolsey]
* [Craig Condon][crcn]
* [Finn Herpich][errorprone]
* [cliftonc][cliftonc]
* [neyric][neyric]
* [humanchimp][humanchimp]

@@ -64,8 +110,10 @@ License

This is under a dual license, MIT and GPL. However, the author of cron.js hasn't specified which version of the GPL, once I know I'll update this project and the packaging files.
MIT
Trademarks?
============
Node.js™ is an official trademark of Joyent. This module is not formally related to or endorsed by the official Joyent Node.js open source or commercial project
[jamespadolsey]:http://github.com/padolsey
[crcn]:http://github.com/crcn
[cliftonc]:http://github.com/cliftonc
[neyric]:http://github.com/neyric
[humanchimp]:http://github.com/humanchimp
[errorprone]:http://github.com/ErrorProne

@@ -1,2 +0,2 @@

var cron = require('./src/cron'),
var cron = require('./lib/cron'),
sys = require('sys');

@@ -3,0 +3,0 @@

@@ -5,88 +5,204 @@ var testCase = require('nodeunit').testCase,

module.exports = testCase({
'test second (* * * * * *)': function(assert) {
assert.expect(1);
new cron.CronJob('* * * * * *', function() {
assert.ok(true);
});
setTimeout(function() {
assert.done();
}, 1000);
},
'test second with oncomplete (* * * * * *)': function(assert) {
assert.expect(1);
new cron.CronJob('* * * * * *', function(done) {
done();
}, function () {
assert.ok(true);
});
setTimeout(function() {
assert.done();
}, 1000);
},
'test every second for 5 seconds (* * * * * *)': function(assert) {
assert.expect(5);
new cron.CronJob('* * * * * *', function() {
assert.ok(true);
});
setTimeout(function() {
assert.done();
}, 5000);
},
'test every second for 5 seconds with oncomplete (* * * * * *)': function(assert) {
assert.expect(5);
new cron.CronJob('* * * * * *', function(done) {
done();
}, function() {
assert.ok(true);
});
setTimeout(function() {
assert.done();
}, 5000);
},
'test every 1 second for 5 seconds (*/1 * * * * *)': function(assert) {
assert.expect(5);
new cron.CronJob('*/1 * * * * *', function() {
assert.ok(true);
});
setTimeout(function() {
assert.done();
}, 5000);
},
'test every 1 second for 5 seconds with oncomplete (*/1 * * * * *)': function(assert) {
assert.expect(5);
new cron.CronJob('*/1 * * * * *', function(done) {
done();
}, function() {
assert.ok(true);
});
setTimeout(function() {
assert.done();
}, 5000);
},
'test every second for a range ([start]-[end] * * * * *)': function(assert) {
assert.expect(5);
var d = new Date();
var s = d.getSeconds()+2;
var e = s + 4; //end value is inclusive
new cron.CronJob(s + '-' + e +' * * * * *', function() {
assert.ok(true);
});
setTimeout(function() {
assert.done();
}, 7000);
},
'test every second for a range with oncomplete ([start]-[end] * * * * *)': function(assert) {
assert.expect(5);
var d = new Date();
var s = d.getSeconds()+2;
var e = s + 4; //end value is inclusive
new cron.CronJob(s + '-' + e +' * * * * *', function(done) {
done();
}, function() {
assert.ok(true);
});
setTimeout(function() {
assert.done();
}, 7000);
}
'test second (* * * * * *)': function(assert) {
assert.expect(1);
var c = new cron.CronJob('* * * * * *', function() {
assert.ok(true);
}, null, true);
setTimeout(function() {
c.stop();
assert.done();
}, 1250);
},
'test second with oncomplete (* * * * * *)': function(assert) {
assert.expect(1);
var c = new cron.CronJob('* * * * * *', function(done) {
done();
}, function () {
assert.ok(true);
}, true);
setTimeout(function() {
c.stop();
assert.done();
}, 1250);
},
'test every second for 5 seconds (* * * * * *)': function(assert) {
assert.expect(5);
var c = new cron.CronJob('* * * * * *', function() {
assert.ok(true);
}, null, true);
setTimeout(function() {
c.stop();
assert.done();
}, 5250);
},
'test every second for 5 seconds with oncomplete (* * * * * *)': function(assert) {
assert.expect(5);
var c = new cron.CronJob('* * * * * *', function(done) {
done();
}, function() {
assert.ok(true);
}, true);
setTimeout(function() {
c.stop();
assert.done();
}, 5250);
},
'test every 1 second for 5 seconds (*/1 * * * * *)': function(assert) {
assert.expect(5);
var c = new cron.CronJob('*/1 * * * * *', function() {
assert.ok(true);
}, null, true);
setTimeout(function() {
assert.done();
c.stop();
}, 5250);
},
'test every 1 second for 5 seconds with oncomplete (*/1 * * * * *)': function(assert) {
assert.expect(5);
var c = new cron.CronJob('*/1 * * * * *', function(done) {
done();
}, function() {
assert.ok(true);
}, true);
setTimeout(function() {
c.stop();
assert.done();
}, 5250);
},
'test every second for a range ([start]-[end] * * * * *)': function(assert) {
assert.expect(5);
var prepDate = new Date();
if ((54 - prepDate.getSeconds()) <= 0) {
setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
} else {
testRun();
}
function testRun() {
var d = new Date();
var s = d.getSeconds()+2;
var e = s + 6; //end value is inclusive
var c = new cron.CronJob(s + '-' + e +' * * * * *', function() {
assert.ok(true);
}, null, true);
setTimeout(function() {
c.stop();
assert.done();
}, 6250);
}
},
'test every second for a range with oncomplete ([start]-[end] * * * * *)': function(assert) {
assert.expect(5);
var prepDate = new Date();
if ((54 - prepDate.getSeconds()) <= 0) {
setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
} else {
testRun();
}
function testRun() {
var d = new Date();
var s = d.getSeconds()+2;
var e = s + 6; //end value is inclusive
var c = new cron.CronJob(s + '-' + e +' * * * * *', function() {
assert.ok(true);
}, function() {
assert.ok(true);
}, true);
setTimeout(function() {
c.stop();
assert.done();
}, 6250);
}
},
'test second (* * * * * *) object constructor': function(assert) {
assert.expect(1);
var c = new cron.CronJob({
cronTime: '* * * * * *',
onTick: function() {
assert.ok(true);
},
start: true
});
setTimeout(function() {
c.stop();
assert.done();
}, 1250);
},
'test second with oncomplete (* * * * * *) object constructor': function(assert) {
assert.expect(1);
var c = new cron.CronJob({
cronTime: '* * * * * *',
onTick: function(done) {
done();
},
onComplete: function () {
assert.ok(true);
},
start: true
});
setTimeout(function() {
c.stop();
assert.done();
}, 1250);
},
'test start/stop': function(assert) {
assert.expect(1);
var c = new cron.CronJob('* * * * * *', function() {
assert.ok(true);
this.stop();
});
setTimeout(function() {
c.start();
}, 1000);
setTimeout(function() {
assert.done();
}, 3250);
},
'test specifying a specific date': function(assert) {
assert.expect(1);
var prepDate = new Date();
if ((58 - prepDate.getSeconds()) <= 0) {
setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
} else {
testRun();
}
function testRun() {
var d = new Date();
var s = d.getSeconds()+1;
d.setSeconds(s);
var c = new cron.CronJob(d, function() {
assert.ok(true);
}, null, true);
setTimeout(function() {
c.stop();
assert.done();
}, 2250);
}
},
'test specifying a specific date with oncomplete': function(assert) {
assert.expect(1);
var prepDate = new Date();
if ((58 - prepDate.getSeconds()) <= 0) {
setTimeout(testRun, (60000 - (prepDate.getSeconds()*1000)) + 1000);
} else {
testRun();
}
function testRun() {
var d = new Date();
var s = d.getSeconds()+1;
d.setSeconds(s);
var c = new cron.CronJob(d, function() {
assert.ok(true);
}, function() {
assert.ok(true);
}, true);
setTimeout(function() {
c.stop();
assert.done();
}, 2250);
}
},
});

@@ -116,5 +116,10 @@ var testCase = require('nodeunit').testCase,

assert.done();
},
'test Date': function(assert) {
assert.expect(1);
var d = new Date();
var ct = new cron.CronTime(d);
assert.equals(ct.source.getTime(), d.getTime());
assert.done();
}
});
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc