Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(Cron Parser): handle aws, next executions and TZ #1026

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,18 @@ declare module '@vue/runtime-core' {
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NDivider: typeof import('naive-ui')['NDivider']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NForm: typeof import('naive-ui')['NForm']
NFormItem: typeof import('naive-ui')['NFormItem']
NH1: typeof import('naive-ui')['NH1']
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NRadio: typeof import('naive-ui')['NRadio']
NRadioGroup: typeof import('naive-ui')['NRadioGroup']
NSpace: typeof import('naive-ui')['NSpace']
NSwitch: typeof import('naive-ui')['NSwitch']
NTable: typeof import('naive-ui')['NTable']
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@
"change-case": "^4.1.2",
"colord": "^2.9.3",
"composerize-ts": "^0.6.2",
"countries-and-timezones": "^3.6.0",
"country-code-lookup": "^0.1.0",
"cron-parser": "^4.9.0",
"cron-validator": "^1.3.1",
"cronstrue": "^2.26.0",
"crypto-js": "^4.1.1",
"date-fns": "^2.29.3",
"dompurify": "^3.0.6",
"email-normalizer": "^1.0.0",
"emojilib": "^3.0.10",
"event-cron-parser": "^1.0.34",
"figlet": "^1.7.0",
"figue": "^1.2.0",
"fuse.js": "^6.6.2",
"get-timezone-offset": "^1.0.5",
"highlight.js": "^11.7.0",
"iarna-toml-esm": "^3.0.5",
"ibantools": "^4.3.3",
Expand Down
50 changes: 47 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions src/tools/crontab-generator/crontab-generator.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, expect, it } from 'vitest';
import { getCronType, getLastExecutionTimes, isCronValid } from './crontab-generator.service';

describe('crontab-generator', () => {
describe('isCronValid', () => {
it('should return true for all valid formats', () => {
// standard format
expect(isCronValid('0 0 * * 1-5')).toBe(true);
expect(isCronValid('23 0-20/2 * * *')).toBe(true);

// AWS formats
expect(isCronValid('0 11-22 ? * MON-FRI *')).toBe(true);
expect(isCronValid('0 0 ? * 1 *')).toBe(true);
});
it('should check standard format', () => {
// standard format
expect(isCronValid('0 0 * * 1-5', 'standard')).toBe(true);
expect(isCronValid('23 0-20/2 * * *', 'standard')).toBe(true);

// AWS format
expect(isCronValid('0 11-22 ? * MON-FRI *', 'standard')).toBe(false);
expect(isCronValid('0 0 ? * 1 *', 'standard')).toBe(false);
});
it('should check aws format', () => {
// standard format
expect(isCronValid('0 0 * * 1-5', 'aws')).toBe(false);
expect(isCronValid('23 0-20/2 * * *', 'aws')).toBe(false);

// AWS format
expect(isCronValid('0 11-22 ? * MON-FRI *', 'aws')).toBe(true);
expect(isCronValid('0 0 ? * 1 *', 'aws')).toBe(true);
});
it('should return false for all invalid formats', () => {
expect(isCronValid('aert')).toBe(false);
expect(isCronValid('40 *')).toBe(false);
});
});

describe('getCronType', () => {
it('should return right type', () => {
expect(getCronType('0 0 * * 1-5')).toBe('standard');
expect(getCronType('23 0-20/2 * * *')).toBe('standard');

// AWS formats
expect(getCronType('0 11-22 ? * MON-FRI *')).toBe('aws');
expect(getCronType('0 0 ? * 1 *')).toBe('aws');

expect(getCronType('aert')).toBe(false);
expect(getCronType('40 *')).toBe(false);
});
});

describe('getLastExecutionTimes', () => {
it('should return next valid datetimes', () => {
expect(getLastExecutionTimes('0 0 * * 1-5')).toHaveLength(5);
expect(getLastExecutionTimes('23 0-20/2 * * *')).toHaveLength(5);

// AWS formats
expect(getLastExecutionTimes('0 11-22 ? * MON-FRI *')).toHaveLength(5);
expect(getLastExecutionTimes('0 0 ? * 1 *')).toHaveLength(5);
});
});
});
47 changes: 47 additions & 0 deletions src/tools/crontab-generator/crontab-generator.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { parseExpression } from 'cron-parser';
import EventCronParser from 'event-cron-parser';

export type CronType = 'standard' | 'aws';

export function getLastExecutionTimes(cronExpression: string, tz: string | undefined = undefined, count: number = 5) {
if (getCronType(cronExpression) === 'standard') {
const interval = parseExpression(cronExpression, { tz });
const times = [];
for (let i = 0; i < count; i++) {
times.push(interval.next().toJSON());
}
return times;
}
if (getCronType(cronExpression) === 'aws') {
const parsed = new EventCronParser(cronExpression);
const times = [];
for (let i = 0; i < count; i++) {
times.push(JSON.stringify(parsed.next()));
}
return times;
}

return [];
}

export function isCronValid(cronExpression: string, cronType: CronType | 'any' = 'any') {
const expressionCronType = getCronType(cronExpression);
return cronType === 'any' ? !!expressionCronType : expressionCronType === cronType;
}

export function getCronType(cronExpression: string) {
try {
parseExpression(cronExpression);
return 'standard';
}
catch (_) {
try {
const parsed = new EventCronParser(cronExpression);
parsed.validate();
return 'aws';
}
catch (_) {
}
}
return false;
}
Loading
Loading