Initial import from local backup (Documents-Playground/pakerpale)
This commit is contained in:
21
node_modules/node-cron/src/convert-expression/asterisk-to-range-conversion.js
generated
vendored
Normal file
21
node_modules/node-cron/src/convert-expression/asterisk-to-range-conversion.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
module.exports = (() => {
|
||||
function convertAsterisk(expression, replecement){
|
||||
if(expression.indexOf('*') !== -1){
|
||||
return expression.replace('*', replecement);
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
function convertAsterisksToRanges(expressions){
|
||||
expressions[0] = convertAsterisk(expressions[0], '0-59');
|
||||
expressions[1] = convertAsterisk(expressions[1], '0-59');
|
||||
expressions[2] = convertAsterisk(expressions[2], '0-23');
|
||||
expressions[3] = convertAsterisk(expressions[3], '1-31');
|
||||
expressions[4] = convertAsterisk(expressions[4], '1-12');
|
||||
expressions[5] = convertAsterisk(expressions[5], '0-6');
|
||||
return expressions;
|
||||
}
|
||||
|
||||
return convertAsterisksToRanges;
|
||||
})();
|
||||
66
node_modules/node-cron/src/convert-expression/index.js
generated
vendored
Normal file
66
node_modules/node-cron/src/convert-expression/index.js
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
'use strict';
|
||||
|
||||
var monthNamesConversion = require('./month-names-conversion');
|
||||
var weekDayNamesConversion = require('./week-day-names-conversion');
|
||||
var convertAsterisksToRanges = require('./asterisk-to-range-conversion');
|
||||
var convertRanges = require('./range-conversion');
|
||||
var convertSteps = require('./step-values-conversion');
|
||||
|
||||
module.exports = (() => {
|
||||
|
||||
function appendSeccondExpression(expressions){
|
||||
if(expressions.length === 5){
|
||||
return ['0'].concat(expressions);
|
||||
}
|
||||
return expressions;
|
||||
}
|
||||
|
||||
function removeSpaces(str) {
|
||||
return str.replace(/\s{2,}/g, ' ').trim();
|
||||
}
|
||||
|
||||
// Function that takes care of normalization.
|
||||
function normalizeIntegers(expressions) {
|
||||
for (var i=0; i < expressions.length; i++){
|
||||
var numbers = expressions[i].split(',');
|
||||
for (var j=0; j<numbers.length; j++){
|
||||
numbers[j] = parseInt(numbers[j]);
|
||||
}
|
||||
expressions[i] = numbers;
|
||||
}
|
||||
return expressions;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 interprete(expression){
|
||||
var expressions = removeSpaces(expression).split(' ');
|
||||
expressions = appendSeccondExpression(expressions);
|
||||
expressions[4] = monthNamesConversion(expressions[4]);
|
||||
expressions[5] = weekDayNamesConversion(expressions[5]);
|
||||
expressions = convertAsterisksToRanges(expressions);
|
||||
expressions = convertRanges(expressions);
|
||||
expressions = convertSteps(expressions);
|
||||
|
||||
expressions = normalizeIntegers(expressions);
|
||||
|
||||
return expressions.join(' ');
|
||||
}
|
||||
|
||||
return interprete;
|
||||
})();
|
||||
22
node_modules/node-cron/src/convert-expression/month-names-conversion.js
generated
vendored
Normal file
22
node_modules/node-cron/src/convert-expression/month-names-conversion.js
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
module.exports = (() => {
|
||||
var months = ['january','february','march','april','may','june','july',
|
||||
'august','september','october','november','december'];
|
||||
var shortMonths = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug',
|
||||
'sep', 'oct', 'nov', 'dec'];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function interprete(monthExpression){
|
||||
monthExpression = convertMonthName(monthExpression, months);
|
||||
monthExpression = convertMonthName(monthExpression, shortMonths);
|
||||
return monthExpression;
|
||||
}
|
||||
|
||||
return interprete;
|
||||
})();
|
||||
42
node_modules/node-cron/src/convert-expression/range-conversion.js
generated
vendored
Normal file
42
node_modules/node-cron/src/convert-expression/range-conversion.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
'use strict';
|
||||
module.exports = ( () => {
|
||||
function replaceWithRange(expression, text, init, end) {
|
||||
|
||||
var numbers = [];
|
||||
var last = parseInt(end);
|
||||
var first = parseInt(init);
|
||||
|
||||
if(first > last){
|
||||
last = parseInt(init);
|
||||
first = parseInt(end);
|
||||
}
|
||||
|
||||
for(var i = first; i <= last; i++) {
|
||||
numbers.push(i);
|
||||
}
|
||||
|
||||
return expression.replace(new RegExp(text, 'gi'), numbers.join());
|
||||
}
|
||||
|
||||
function convertRange(expression){
|
||||
var rangeRegEx = /(\d+)\-(\d+)/;
|
||||
var match = rangeRegEx.exec(expression);
|
||||
while(match !== null && match.length > 0){
|
||||
expression = replaceWithRange(expression, match[0], match[1], match[2]);
|
||||
match = rangeRegEx.exec(expression);
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
function convertAllRanges(expressions){
|
||||
for(var i = 0; i < expressions.length; i++){
|
||||
expressions[i] = convertRange(expressions[i]);
|
||||
}
|
||||
return expressions;
|
||||
}
|
||||
|
||||
return convertAllRanges;
|
||||
})();
|
||||
|
||||
|
||||
|
||||
27
node_modules/node-cron/src/convert-expression/step-values-conversion.js
generated
vendored
Normal file
27
node_modules/node-cron/src/convert-expression/step-values-conversion.js
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = (() => {
|
||||
function convertSteps(expressions){
|
||||
var stepValuePattern = /^(.+)\/(\d+)$/;
|
||||
for(var i = 0; i < expressions.length; i++){
|
||||
var match = stepValuePattern.exec(expressions[i]);
|
||||
var isStepValue = match !== null && match.length > 0;
|
||||
if(isStepValue){
|
||||
var values = match[1].split(',');
|
||||
var setpValues = [];
|
||||
var divider = parseInt(match[2], 10);
|
||||
for(var j = 0; j <= values.length; j++){
|
||||
var value = parseInt(values[j], 10);
|
||||
if(value % divider === 0){
|
||||
setpValues.push(value);
|
||||
}
|
||||
}
|
||||
expressions[i] = setpValues.join(',');
|
||||
}
|
||||
}
|
||||
return expressions;
|
||||
}
|
||||
|
||||
return convertSteps;
|
||||
})();
|
||||
|
||||
21
node_modules/node-cron/src/convert-expression/week-day-names-conversion.js
generated
vendored
Normal file
21
node_modules/node-cron/src/convert-expression/week-day-names-conversion.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
module.exports = (() => {
|
||||
var weekDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday',
|
||||
'friday', 'saturday'];
|
||||
var shortWeekDays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function convertWeekDays(expression){
|
||||
expression = expression.replace('7', '0');
|
||||
expression = convertWeekDayName(expression, weekDays);
|
||||
return convertWeekDayName(expression, shortWeekDays);
|
||||
}
|
||||
|
||||
return convertWeekDays;
|
||||
})();
|
||||
61
node_modules/node-cron/src/node-cron.js
generated
vendored
Normal file
61
node_modules/node-cron/src/node-cron.js
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
'use strict';
|
||||
|
||||
var Task = require('./task'),
|
||||
ScheduledTask = require('./scheduled-task'),
|
||||
validation = require('./pattern-validation');
|
||||
|
||||
module.exports = (() => {
|
||||
|
||||
/**
|
||||
* 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 {Object} options - a set of options for the scheduled task:
|
||||
* - scheduled <boolean>: if a schaduled task is ready and running to be
|
||||
* performed when the time mach with the cron excpression.
|
||||
* - timezone <string>: the tiemzone to execute the tasks.
|
||||
*
|
||||
* Example:
|
||||
* {
|
||||
* "scheduled": true,
|
||||
* "timezone": "America/Sao_Paulo"
|
||||
* }
|
||||
*
|
||||
* @returns {ScheduledTask} update function.
|
||||
*/
|
||||
function createTask(expression, func, options) {
|
||||
// Added for immediateStart depreciation
|
||||
if(typeof options === 'boolean'){
|
||||
console.warn('DEPRECIATION: imediateStart is deprecated and will be removed soon in favor of the options param.');
|
||||
options = {
|
||||
scheduled: options
|
||||
};
|
||||
}
|
||||
|
||||
if(!options){
|
||||
options = {
|
||||
scheduled: true
|
||||
};
|
||||
}
|
||||
|
||||
var task = new Task(expression, func);
|
||||
return new ScheduledTask(task, options);
|
||||
}
|
||||
|
||||
function validate(expression) {
|
||||
try {
|
||||
validation(expression);
|
||||
} catch(e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return {
|
||||
schedule: createTask,
|
||||
validate: validate
|
||||
};
|
||||
})();
|
||||
87
node_modules/node-cron/src/pattern-validation.js
generated
vendored
Normal file
87
node_modules/node-cron/src/pattern-validation.js
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
'use strict';
|
||||
|
||||
var convertExpression = require('./convert-expression');
|
||||
|
||||
|
||||
module.exports = ( () => {
|
||||
function isValidExpression(expression, min, max){
|
||||
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;
|
||||
}
|
||||
|
||||
function isInvalidSecond(expression){
|
||||
return !isValidExpression(expression, 0, 59);
|
||||
}
|
||||
|
||||
function isInvalidMinute(expression){
|
||||
return !isValidExpression(expression, 0, 59);
|
||||
}
|
||||
|
||||
function isInvalidHour(expression){
|
||||
return !isValidExpression(expression, 0, 23);
|
||||
}
|
||||
|
||||
function isInvalidDayOfMonth(expression){
|
||||
return !isValidExpression(expression, 1, 31);
|
||||
}
|
||||
|
||||
function isInvalidMonth(expression){
|
||||
return !isValidExpression(expression, 1, 12);
|
||||
}
|
||||
|
||||
function isInvalidWeekDay(expression){
|
||||
return !isValidExpression(expression, 0, 7);
|
||||
}
|
||||
|
||||
function validateFields(patterns, executablePatterns){
|
||||
if (isInvalidSecond(executablePatterns[0])) {
|
||||
throw patterns[0] + ' is a invalid expression for second';
|
||||
}
|
||||
|
||||
if (isInvalidMinute(executablePatterns[1])) {
|
||||
throw patterns[1] + ' is a invalid expression for minute';
|
||||
}
|
||||
|
||||
if (isInvalidHour(executablePatterns[2])) {
|
||||
throw patterns[2] + ' is a invalid expression for hour';
|
||||
}
|
||||
|
||||
if (isInvalidDayOfMonth(executablePatterns[3])) {
|
||||
throw patterns[3] + ' is a invalid expression for day of month';
|
||||
}
|
||||
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
||||
function validate(pattern){
|
||||
if (typeof pattern !== 'string'){
|
||||
throw 'pattern must be a string!';
|
||||
}
|
||||
|
||||
var patterns = pattern.split(' ');
|
||||
var executablePattern = convertExpression(pattern);
|
||||
var executablePatterns = executablePattern.split(' ');
|
||||
|
||||
if(patterns.length === 5){
|
||||
patterns = ['0'].concat(patterns);
|
||||
}
|
||||
|
||||
validateFields(patterns, executablePatterns);
|
||||
}
|
||||
|
||||
return validate;
|
||||
})();
|
||||
96
node_modules/node-cron/src/scheduled-task.js
generated
vendored
Normal file
96
node_modules/node-cron/src/scheduled-task.js
generated
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
'use strict';
|
||||
|
||||
var tzOffset = require('tz-offset');
|
||||
|
||||
/**
|
||||
* Creates a new scheduled task.
|
||||
*
|
||||
* @param {Task} task - task to schedule.
|
||||
* @param {*} options - task options.
|
||||
*/
|
||||
function ScheduledTask(task, options) {
|
||||
var timezone = options.timezone;
|
||||
|
||||
/**
|
||||
* Starts updating the task.
|
||||
*
|
||||
* @returns {ScheduledTask} instance of this task.
|
||||
*/
|
||||
this.start = () => {
|
||||
this.status = 'scheduled';
|
||||
if (this.task && !this.tick) {
|
||||
this.tick = setTimeout(this.task, 1000 - new Date().getMilliseconds() + 1);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Stops updating the task.
|
||||
*
|
||||
* @returns {ScheduledTask} instance of this task.
|
||||
*/
|
||||
this.stop = () => {
|
||||
this.status = 'stoped';
|
||||
if (this.tick) {
|
||||
clearTimeout(this.tick);
|
||||
this.tick = null;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns the current task status.
|
||||
*
|
||||
* @returns {string} current task status.
|
||||
* The return may be:
|
||||
* - scheduled: when a task is scheduled and waiting to be executed.
|
||||
* - running: the task status while the task is executing.
|
||||
* - stoped: when the task is stoped.
|
||||
* - destroyed: whe the task is destroyed, in that status the task cannot be re-started.
|
||||
* - failed: a task is maker as failed when the previous execution fails.
|
||||
*/
|
||||
this.getStatus = () => {
|
||||
return this.status;
|
||||
};
|
||||
|
||||
/**
|
||||
* Destroys the scheduled task.
|
||||
*/
|
||||
this.destroy = () => {
|
||||
this.stop();
|
||||
this.status = 'destroyed';
|
||||
|
||||
this.task = null;
|
||||
};
|
||||
|
||||
task.on('started', () => {
|
||||
this.status = 'running';
|
||||
});
|
||||
|
||||
task.on('done', () => {
|
||||
this.status = 'scheduled';
|
||||
});
|
||||
|
||||
task.on('failed', () => {
|
||||
this.status = 'failed';
|
||||
});
|
||||
|
||||
this.task = () => {
|
||||
var date = new Date();
|
||||
if(timezone){
|
||||
date = tzOffset.timeAt(date, timezone);
|
||||
}
|
||||
this.tick = setTimeout(this.task, 1000 - date.getMilliseconds() + 1);
|
||||
task.update(date);
|
||||
};
|
||||
|
||||
this.tick = null;
|
||||
|
||||
if (options.scheduled !== false) {
|
||||
this.start();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ScheduledTask;
|
||||
70
node_modules/node-cron/src/task.js
generated
vendored
Normal file
70
node_modules/node-cron/src/task.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
'use strict';
|
||||
|
||||
var convertExpression = require('./convert-expression');
|
||||
var validatePattern = require('./pattern-validation');
|
||||
|
||||
var events = require('events');
|
||||
|
||||
|
||||
function matchPattern(pattern, value){
|
||||
if( pattern.indexOf(',') !== -1 ){
|
||||
var patterns = pattern.split(',');
|
||||
return patterns.indexOf(value.toString()) !== -1;
|
||||
}
|
||||
return pattern === value.toString();
|
||||
}
|
||||
|
||||
function mustRun(task, date){
|
||||
var runInSecond = matchPattern(task.expressions[0], date.getSeconds());
|
||||
var runOnMinute = matchPattern(task.expressions[1], date.getMinutes());
|
||||
var runOnHour = matchPattern(task.expressions[2], date.getHours());
|
||||
var runOnDayOfMonth = matchPattern(task.expressions[3], date.getDate());
|
||||
var runOnMonth = matchPattern(task.expressions[4], date.getMonth() + 1);
|
||||
var runOnDayOfWeek = matchPattern(task.expressions[5], date.getDay());
|
||||
|
||||
var runOnDay = false;
|
||||
var delta = task.initialPattern.length === 6 ? 0 : -1;
|
||||
|
||||
if (task.initialPattern[3 + delta] === '*') {
|
||||
runOnDay = runOnDayOfWeek;
|
||||
} else if (task.initialPattern[5 + delta] === '*') {
|
||||
runOnDay = runOnDayOfMonth;
|
||||
} else {
|
||||
runOnDay = runOnDayOfMonth || runOnDayOfWeek;
|
||||
}
|
||||
|
||||
return runInSecond && runOnMinute && runOnHour && runOnDay && runOnMonth;
|
||||
}
|
||||
|
||||
function Task(pattern, execution){
|
||||
validatePattern(pattern);
|
||||
this.initialPattern = pattern.split(' ');
|
||||
this.pattern = convertExpression(pattern);
|
||||
this.execution = execution;
|
||||
this.expressions = this.pattern.split(' ');
|
||||
|
||||
events.EventEmitter.call(this);
|
||||
|
||||
this.update = (date) => {
|
||||
if(mustRun(this, date)){
|
||||
new Promise((resolve, reject) => {
|
||||
this.emit('started', this);
|
||||
var ex = this.execution();
|
||||
if(ex instanceof Promise){
|
||||
ex.then(resolve).catch(reject);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
}).then(() => {
|
||||
this.emit('done', this);
|
||||
}).catch((error) => {
|
||||
console.error(error);
|
||||
this.emit('failed', error);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Task.prototype = events.EventEmitter.prototype;
|
||||
module.exports = Task;
|
||||
|
||||
Reference in New Issue
Block a user