Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

node-cron

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-cron - npm Package Compare versions

Comparing version 1.0.0 to 1.1.1

.hound.yml

2

package.json
{
"name": "node-cron",
"version": "1.0.0",
"version": "1.1.1",
"description": "A simple cron-like task scheduler for Node.js",

@@ -5,0 +5,0 @@ "author": "Lucas Merencia",

@@ -6,5 +6,6 @@ # Node Cron

[![Coverage Status](https://coveralls.io/repos/github/merencia/node-cron/badge.svg?branch=master)](https://coveralls.io/github/merencia/node-cron?branch=master)
[![Code Climate](https://codeclimate.com/github/merencia/node-cron/badges/gpa.svg)](https://codeclimate.com/github/merencia/node-cron)
[![Build Status](https://travis-ci.org/merencia/node-cron.svg?branch=master)](https://travis-ci.org/merencia/node-cron)
[![Dependency Status](https://david-dm.org/merencia/node-cron.svg)](https://david-dm.org/merencia/node-cron)
[![devDependency Status](https://david-dm.org/merencia/node-cron/dev-status.svg)](https://david-dm.org/merencia/node-cron#info=devDependencies)
[![Build Status](https://travis-ci.org/merencia/node-cron.svg?branch=master)](https://travis-ci.org/merencia/node-cron)

@@ -18,3 +19,3 @@ The node-cron module is tiny task scheduler in pure JavaScrip for node.js based on [GNU crontab](https://www.gnu.org/software/mcron/manual/html_node/Crontab-file.html). This module allows you to schedule task in node.js using full crontab syntax.

```sh
```console
$ npm install --save node-cron

@@ -29,3 +30,3 @@ ```

cron.schedule('* * * * *', function(){
console.log('running a task in every minute');
console.log('running a task every minute');
});

@@ -39,2 +40,3 @@ ```

### Allowed fields
```

@@ -61,3 +63,3 @@ # ┌────────────── second (optional)

| month | 1-12 (or names) |
| day of week | 0-7 (or names) |
| day of week | 0-7 (or names, 0 or 7 are sunday) |

@@ -91,3 +93,3 @@

Step values can be used in conjunction with ranges, following a range with '/' and a number. e.g: `1-10/2` that is the same as `2,4,6,8,10`. Steps are also permitted after an asterisk, so if you want to say “every two hours”, just use `*/2`.
Step values can be used in conjunction with ranges, following a range with '/' and a number. e.g: `1-10/2` that is the same as `2,4,6,8,10`. Steps are also permitted after an asterisk, so if you want to say “every two minutes”, just use `*/2`.

@@ -98,3 +100,3 @@ ```javascript

cron.schedule('*/2 * * * *', function(){
console.log('running a task every two hours');
console.log('running a task every two minutes');
});

@@ -125,2 +127,58 @@ ```

## Cron methods
### Schedule
Schedules given task to be executed whenever the cron expression ticks.
Arguments:
- !string expression - Cron expression
- !Function func - Task to be executed
- boolean? immediateStart - Whether to start scheduler immediately after create.
## ScheduledTask methods
### Start
Starts the scheduled task.
```javascript
var cron = require('node-cron');
var task = cron.schedule('* * * * *', function() {
console.log('immediately started');
}, false);
task.start();
```
### Stop
The task won't be executed unless re-started.
```javascript
var cron = require('node-cron');
var task = cron.schedule('* * * * *', function() {
console.log('will execute every minute until stopped');
});
task.stop();
```
### Destroy
The task will be stopped and completely destroyed.
```javascript
var cron = require('node-cron');
var task = cron.schedule('* * * * *', function() {
console.log('will not execute anymore, nor be able to restart');
});
task.destroy();
```
## Issues

@@ -127,0 +185,0 @@

'use strict';
var Task = require('./task');
var Task = require('./task'),
ScheduledTask = require('./scheduled-task');
module.exports = (function(){
function createTask(expression, func){
module.exports = (function() {
/**
* Creates a new task to execute given function when the cron
* expression ticks.
*
* @param {string} expression - cron expression.
* @param {Function} func - task to be executed.
* @param {boolean} immediateStart - whether to start the task immediately.
* @returns {ScheduledTask} update function.
*/
function createTask(expression, func, immediateStart) {
var task = new Task(expression, func);
setInterval(function(){
task.update(new Date());
}, 1000);
return new ScheduledTask(task, immediateStart);
}
return {
schedule: createTask
}
})();
};
}());
'use strict';
var months = ['january','february','march','april','may','june','july',
module.exports = (function() {
var months = ['january','february','march','april','may','june','july',
'august','september','october','november','december'];
var shortMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
var shortMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
'sep', 'oct', 'nov', 'dec'];
var weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
'friday', 'saturday']
var shortWeekDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
var weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
'friday', 'saturday'];
var shortWeekDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
/*
* The node-cron core allows only numbers (including multiple numbers e.g 1,2).
* This module is going to translate the month names, week day names and ranges
* to integers relatives.
*
* Month names example:
* - Pattern 0 1 1 January,Sep *
* - Will be translated to 0 1 1 1,9 *
*
* Week day names example:
* - Pattern 0 1 1 2 Monday,Sat
* - Will be translated to 0 1 1 1,5 *
*
* Ranges example:
* - Pattern 1-5 * * * *
* - Will be translated to 1,2,3,4,5 * * * *
*/
function interpretPattern(pattern){
var convertMonthName = function(pattern, items){
for(var i in items)
pattern = pattern.replace(new RegExp(items[i], 'gi'), parseInt(i) + 1);
return pattern;
function convertMonthName(expression, items){
for(var i = 0; i < items.length; i++){
expression = expression.replace(new RegExp(items[i], 'gi'), parseInt(i, 10) + 1);
}
return expression;
}
var convertWeekDayName = function(pattern, items){
for(var i in items)
pattern = pattern.replace(new RegExp(items[i], 'gi'), parseInt(i));
return pattern;
function convertWeekDayName(expression, items){
for(var i = 0; i < items.length; i++){
expression = expression.replace(new RegExp(items[i], 'gi'), parseInt(i, 10));
}
return expression;
}
var convertRanges = function(pattern){
var rangeRegEx = /(\d+)\-(\d+)/
var match = rangeRegEx.exec(pattern);
function replaceWithRange(expression, text, init, end) {
var numbers = [];
for(var i = init; i <= end; i++) {
numbers.push(i);
}
return expression.replace(new RegExp(text, 'gi'), numbers.join());
}
function convertRanges(expression){
var rangeRegEx = /(\d+)\-(\d+)/;
var match = rangeRegEx.exec(expression);
while(match !== null && match.length > 0){
var rangeText = match[0];
var init = match[1];
var end = match[2];
var numbers = [];
for(var i = init; i <= end; i++)
numbers.push(i);
pattern = pattern.replace(new RegExp(rangeText, 'gi'), numbers.join());
match = rangeRegEx.exec(pattern);
expression = replaceWithRange(expression, match[0], match[1], match[2]);
match = rangeRegEx.exec(expression);
}
return pattern;
return expression;
}
pattern = convertMonthName(pattern, months);
pattern = convertMonthName(pattern, shortMonths);
pattern = convertWeekDayName(pattern, weekDays);
pattern = convertWeekDayName(pattern, shortWeekDays);
pattern = convertRanges(pattern);
return pattern;
}
/*
* The node-cron core allows only numbers (including multiple numbers e.g 1,2).
* This module is going to translate the month names, week day names and ranges
* to integers relatives.
*
* Month names example:
* - expression 0 1 1 January,Sep *
* - Will be translated to 0 1 1 1,9 *
*
* Week day names example:
* - expression 0 1 1 2 Monday,Sat
* - Will be translated to 0 1 1 1,5 *
*
* Ranges example:
* - expression 1-5 * * * *
* - Will be translated to 1,2,3,4,5 * * * *
*/
function interpretExpression(expression){
expression = convertMonthName(expression, months);
expression = convertMonthName(expression, shortMonths);
expression = convertWeekDayName(expression, weekDays);
expression = convertWeekDayName(expression, shortWeekDays);
expression = convertRanges(expression);
return expression;
}
module.exports = interpretPattern;
return interpretExpression;
}());

@@ -5,76 +5,87 @@ 'use strict';

function validate(pattern){
var patterns = pattern.split(' ');
var executablePattern = interprete(pattern);
var executablePatterns = executablePattern.split(' ');
module.exports = ( function(){
if(patterns.length === 5){
patterns = ['0'].concat(patterns);
executablePatterns = ['0'].concat(executablePatterns);
function removeStepValue(expression){
var multiplePattern = /^((\d+(\,\d+){0,})|\*)\/(\d+)$/g;
var match = multiplePattern.exec(expression);
if(match !== null && match.length > 0 ) {
expression = match[1];
}
return expression;
}
if (isInvalidSecond(executablePatterns[0]))
throw patterns[0] + ' is a invalid expression for second';
function isValidExpression(expression, min, max){
expression = removeStepValue(expression);
var options = expression.split(',');
var regexValidation = /^\d+$|^\*$|^\*\/\d+$/;
for(var i = 0; i < options.length; i++){
var option = options[i];
var optionAsInt = parseInt(options[i], 10);
if(optionAsInt < min || optionAsInt > max || !regexValidation.test(option)) {
return false;
}
}
return true;
}
if (isInvalidMinute(executablePatterns[1]))
throw patterns[1] + ' is a invalid expression for minute';
function isInvalidSecond(expression){
return !isValidExpression(expression, 0, 59);
}
if (isInvalidHour(executablePatterns[2]))
throw patterns[2] + ' is a invalid expression for hour';
function isInvalidMinute(expression){
return !isValidExpression(expression, 0, 59);
}
if (isInvalidDayOfMonth(executablePatterns[3]))
throw patterns[3] + ' is a invalid expression for day of month';
function isInvalidHour(expression){
return !isValidExpression(expression, 0, 23);
}
if (isInvalidMonth(executablePatterns[4]))
throw patterns[4] + ' is a invalid expression for month';
function isInvalidDayOfMonth(expression){
return !isValidExpression(expression, 1, 31);
}
if (isInvalidWeekDay(executablePatterns[5]))
throw patterns[5] + ' is a invalid expression for week day';
}
function isInvalidMonth(expression){
return !isValidExpression(expression, 1, 12);
}
function removeStepValue(expression){
var multiplePattern = /^((\d+(\,\d+){0,})|\*)\/(\d+)$/g
var match = multiplePattern.exec(expression);
if(match !== null && match.length > 0 )
expression = match[1];
return expression;
}
function isValidExpression(expression, min, max){
expression = removeStepValue(expression);
var options = expression.split(',');
var regexValidation = /^\d+$|^\*$|^\*\/\d+$/;
for(var i = 0; i < options.length; i++){
var option = options[i];
var optionAsInt = parseInt(options[i]);
if(optionAsInt < min || optionAsInt > max || !regexValidation.test(option))
return false;
function isInvalidWeekDay(expression){
return !isValidExpression(expression, 0, 7);
}
return true;
}
function isInvalidSecond(expression){
return !isValidExpression(expression, 0, 59);
}
function validate(pattern){
var patterns = pattern.split(' ');
var executablePattern = interprete(pattern);
var executablePatterns = executablePattern.split(' ');
function isInvalidMinute(expression){
return !isValidExpression(expression, 0, 59);
}
if(patterns.length === 5){
patterns = ['0'].concat(patterns);
executablePatterns = ['0'].concat(executablePatterns);
}
function isInvalidHour(expression){
return !isValidExpression(expression, 0, 23);
}
if (isInvalidSecond(executablePatterns[0])) {
throw patterns[0] + ' is a invalid expression for second';
}
function isInvalidDayOfMonth(expression){
return !isValidExpression(expression, 1, 31);
}
if (isInvalidMinute(executablePatterns[1])) {
throw patterns[1] + ' is a invalid expression for minute';
}
function isInvalidMonth(expression){
return !isValidExpression(expression, 1, 12);
}
if (isInvalidHour(executablePatterns[2])) {
throw patterns[2] + ' is a invalid expression for hour';
}
function isInvalidWeekDay(expression){
return !isValidExpression(expression, 0, 7);
}
if (isInvalidDayOfMonth(executablePatterns[3])) {
throw patterns[3] + ' is a invalid expression for day of month';
}
module.exports = validate;
if (isInvalidMonth(executablePatterns[4])) {
throw patterns[4] + ' is a invalid expression for month';
}
if (isInvalidWeekDay(executablePatterns[5])) {
throw patterns[5] + ' is a invalid expression for week day';
}
}
return validate;
}());

@@ -8,10 +8,14 @@ 'use strict';

function matchPattern(pattern, value){
var stepValuePattern = /^((\d+(\,\d+){0,})|\*)\/(\d+)$/g
var stepValuePattern = /^((\d+(\,\d+){0,})|\*)\/(\d+)$/g;
var match = stepValuePattern.exec(pattern);
var isStepValue = match !== null && match.length > 0;
if (pattern === '*') return true;
if (isStepValue){
if (pattern === '*') {
return true;
}
if (isStepValue) {
var values = match[1].split(',');
if(values[0] === '*' || values.indexOf(value.toString()) !== -1)
return value % parseInt(match[4]) === 0;
if(values[0] === '*' || values.indexOf(value.toString()) !== -1) {
return value % parseInt(match[4], 10) === 0;
}
}

@@ -21,6 +25,7 @@ else if( pattern.indexOf(',') !== -1 ){

return patterns.indexOf(value.toString()) !== -1;
} else
return pattern === value.toString();
};
}
return pattern === value.toString();
}
function mustRun(task, date){

@@ -33,3 +38,5 @@ var runInSecond = matchPattern(task.expressions[0], date.getSeconds());

var weekDay = date.getDay();
if (weekDay === 0 ) weekDay = 7;
if (weekDay === 0 ) {
weekDay = 7;
}
var runOnDayOfWeek = matchPattern(task.expressions[5], weekDay);

@@ -45,4 +52,5 @@ return runInSecond && runOnMinute && runOnHour && runOnDayOfMonth &&

this.expressions = this.pattern.split(' ');
if (this.expressions.length === 5 )
if (this.expressions.length === 5 ){
this.expressions = [ '0' ].concat(this.expressions);
}
}

@@ -52,3 +60,3 @@

if(mustRun(this, date)){
try{
try {
this.execution();

@@ -59,5 +67,5 @@ } catch(err) {

}
}
};
return Task;
})();
}());
'use strict';
var expect = require('expect.js')
var expect = require('expect.js');
var interpret = require('../src/pattern-interpreter');

@@ -5,0 +5,0 @@

@@ -20,3 +20,3 @@ 'use strict';

executed += 1;
throw 'exception!'
throw 'exception!';
});

@@ -23,0 +23,0 @@ this.clock.tick(3000 * 60);

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc