@n8n/scheduler 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export declare class InvalidScheduleError extends Error {
2
+ constructor(message: string);
3
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidScheduleError = void 0;
4
+ class InvalidScheduleError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = 'InvalidScheduleError';
8
+ }
9
+ }
10
+ exports.InvalidScheduleError = InvalidScheduleError;
11
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAQA,MAAa,oBAAqB,SAAQ,KAAK;IAC9C,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACpC,CAAC;CACD;AALD,oDAKC"}
@@ -0,0 +1,6 @@
1
+ export { ScheduleKindList, TaskStatusList } from './types';
2
+ export type { ScheduleKind, CronSchedule, IntervalSchedule, OneOffSchedule, Schedule, TaskStatus, ScheduledJob, ScheduledTask, } from './types';
3
+ export { InvalidScheduleError } from './errors';
4
+ export { computeNextRunAt } from './recurrence/next-run';
5
+ export { validateSchedule } from './recurrence/validate';
6
+ export type { SchedulerStore } from './storage/storage';
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateSchedule = exports.computeNextRunAt = exports.InvalidScheduleError = exports.TaskStatusList = exports.ScheduleKindList = void 0;
4
+ var types_1 = require("./types");
5
+ Object.defineProperty(exports, "ScheduleKindList", { enumerable: true, get: function () { return types_1.ScheduleKindList; } });
6
+ Object.defineProperty(exports, "TaskStatusList", { enumerable: true, get: function () { return types_1.TaskStatusList; } });
7
+ var errors_1 = require("./errors");
8
+ Object.defineProperty(exports, "InvalidScheduleError", { enumerable: true, get: function () { return errors_1.InvalidScheduleError; } });
9
+ var next_run_1 = require("./recurrence/next-run");
10
+ Object.defineProperty(exports, "computeNextRunAt", { enumerable: true, get: function () { return next_run_1.computeNextRunAt; } });
11
+ var validate_1 = require("./recurrence/validate");
12
+ Object.defineProperty(exports, "validateSchedule", { enumerable: true, get: function () { return validate_1.validateSchedule; } });
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAcA,iCAA2D;AAAlD,yGAAA,gBAAgB,OAAA;AAAE,uGAAA,cAAc,OAAA;AAazC,mCAAgD;AAAvC,8GAAA,oBAAoB,OAAA;AAE7B,kDAAyD;AAAhD,4GAAA,gBAAgB,OAAA;AACzB,kDAAyD;AAAhD,4GAAA,gBAAgB,OAAA"}
@@ -0,0 +1,2 @@
1
+ import type { Schedule } from '../types';
2
+ export declare function computeNextRunAt(schedule: Schedule, after: Date): Date | null;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.computeNextRunAt = computeNextRunAt;
4
+ const cron_parser_1 = require("cron-parser");
5
+ const errors_1 = require("../errors");
6
+ const validate_1 = require("./validate");
7
+ const MS_PER_SECOND = 1000;
8
+ function cronNextRun(schedule, after, timezone) {
9
+ try {
10
+ const it = cron_parser_1.CronExpressionParser.parse(schedule.cronExpression, {
11
+ currentDate: after,
12
+ tz: timezone,
13
+ });
14
+ return it.next().toDate();
15
+ }
16
+ catch (error) {
17
+ throw new errors_1.InvalidScheduleError(`Failed to evaluate cron expression ${JSON.stringify(schedule.cronExpression)} in timezone ${JSON.stringify(timezone)}: ${error.message}`);
18
+ }
19
+ }
20
+ function intervalNextRun(schedule, after) {
21
+ return new Date(after.getTime() + schedule.intervalSeconds * MS_PER_SECOND);
22
+ }
23
+ function oneOffNextRun(schedule, after) {
24
+ return after.getTime() < schedule.fireAt.getTime() ? schedule.fireAt : null;
25
+ }
26
+ function computeNextRunAt(schedule, after) {
27
+ (0, validate_1.validateSchedule)(schedule);
28
+ switch (schedule.kind) {
29
+ case 'cron':
30
+ if (schedule.timezone === null) {
31
+ throw new errors_1.InvalidScheduleError('Cron timezone must be resolved to a concrete zone before computing the next run, got null');
32
+ }
33
+ return cronNextRun(schedule, after, schedule.timezone);
34
+ case 'interval':
35
+ return intervalNextRun(schedule, after);
36
+ case 'one_off':
37
+ return oneOffNextRun(schedule, after);
38
+ default: {
39
+ const exhaustive = schedule;
40
+ throw new errors_1.InvalidScheduleError(`Unknown schedule kind: ${JSON.stringify(exhaustive.kind)}`);
41
+ }
42
+ }
43
+ }
44
+ //# sourceMappingURL=next-run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next-run.js","sourceRoot":"","sources":["../../src/recurrence/next-run.ts"],"names":[],"mappings":";;AAwDA,4CAsBC;AA9ED,6CAAmD;AAEnD,sCAAiD;AAEjD,yCAA8C;AAE9C,MAAM,aAAa,GAAG,IAAI,CAAC;AAU3B,SAAS,WAAW,CAAC,QAAsB,EAAE,KAAW,EAAE,QAAgB;IACzE,IAAI,CAAC;QACJ,MAAM,EAAE,GAAG,kCAAoB,CAAC,KAAK,CAAC,QAAQ,CAAC,cAAc,EAAE;YAC9D,WAAW,EAAE,KAAK;YAClB,EAAE,EAAE,QAAQ;SACZ,CAAC,CAAC;QACH,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,MAAM,EAAE,CAAC;IAC3B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,MAAM,IAAI,6BAAoB,CAC7B,sCAAsC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,cAAc,CAAC,gBAAgB,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAM,KAAe,CAAC,OAAO,EAAE,CACpJ,CAAC;IACH,CAAC;AACF,CAAC;AAOD,SAAS,eAAe,CAAC,QAA0B,EAAE,KAAW;IAC/D,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,GAAG,QAAQ,CAAC,eAAe,GAAG,aAAa,CAAC,CAAC;AAC7E,CAAC;AAGD,SAAS,aAAa,CAAC,QAAwB,EAAE,KAAW;IAC3D,OAAO,KAAK,CAAC,OAAO,EAAE,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC;AAC7E,CAAC;AAcD,SAAgB,gBAAgB,CAAC,QAAkB,EAAE,KAAW;IAC/D,IAAA,2BAAgB,EAAC,QAAQ,CAAC,CAAC;IAE3B,QAAQ,QAAQ,CAAC,IAAI,EAAE,CAAC;QACvB,KAAK,MAAM;YACV,IAAI,QAAQ,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;gBAChC,MAAM,IAAI,6BAAoB,CAC7B,2FAA2F,CAC3F,CAAC;YACH,CAAC;YACD,OAAO,WAAW,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACxD,KAAK,UAAU;YACd,OAAO,eAAe,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QACzC,KAAK,SAAS;YACb,OAAO,aAAa,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;QACvC,OAAO,CAAC,CAAC,CAAC;YACT,MAAM,UAAU,GAAU,QAAQ,CAAC;YACnC,MAAM,IAAI,6BAAoB,CAC7B,0BAA0B,IAAI,CAAC,SAAS,CAAE,UAAuB,CAAC,IAAI,CAAC,EAAE,CACzE,CAAC;QACH,CAAC;IACF,CAAC;AACF,CAAC"}
@@ -0,0 +1,2 @@
1
+ import type { Schedule } from '../types';
2
+ export declare function validateSchedule(schedule: Schedule): void;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateSchedule = validateSchedule;
4
+ const cron_parser_1 = require("cron-parser");
5
+ const luxon_1 = require("luxon");
6
+ const errors_1 = require("../errors");
7
+ const CRON_FIELD_COUNT = 6;
8
+ function validateCron(schedule) {
9
+ const expression = schedule.cronExpression;
10
+ if (typeof expression !== 'string') {
11
+ throw new errors_1.InvalidScheduleError(`cron.cronExpression must be a string, got ${JSON.stringify(expression)}`);
12
+ }
13
+ const fieldCount = expression.trim().split(/\s+/).length;
14
+ if (fieldCount !== CRON_FIELD_COUNT) {
15
+ throw new errors_1.InvalidScheduleError(`Cron expression must have ${CRON_FIELD_COUNT} fields (seconds included), got ${fieldCount}: ${JSON.stringify(expression)}`);
16
+ }
17
+ if (schedule.timezone !== null && !luxon_1.IANAZone.isValidZone(schedule.timezone)) {
18
+ throw new errors_1.InvalidScheduleError(`Unknown IANA timezone: ${JSON.stringify(schedule.timezone)}`);
19
+ }
20
+ try {
21
+ cron_parser_1.CronExpressionParser.parse(expression, {
22
+ tz: schedule.timezone ?? 'UTC',
23
+ });
24
+ }
25
+ catch (error) {
26
+ throw new errors_1.InvalidScheduleError(`Invalid cron expression ${JSON.stringify(expression)}: ${error.message}`);
27
+ }
28
+ }
29
+ function validateInterval(schedule) {
30
+ if (!Number.isInteger(schedule.intervalSeconds) || schedule.intervalSeconds <= 0) {
31
+ throw new errors_1.InvalidScheduleError(`interval.intervalSeconds must be a positive integer, got ${JSON.stringify(schedule.intervalSeconds)}`);
32
+ }
33
+ }
34
+ function validateOneOff(schedule) {
35
+ const fireAt = schedule.fireAt;
36
+ if (!(fireAt instanceof Date) || Number.isNaN(fireAt.getTime())) {
37
+ throw new errors_1.InvalidScheduleError('one_off.fireAt must be a valid Date');
38
+ }
39
+ }
40
+ function validateSchedule(schedule) {
41
+ switch (schedule.kind) {
42
+ case 'cron':
43
+ return validateCron(schedule);
44
+ case 'interval':
45
+ return validateInterval(schedule);
46
+ case 'one_off':
47
+ return validateOneOff(schedule);
48
+ default: {
49
+ const exhaustive = schedule;
50
+ throw new errors_1.InvalidScheduleError(`Unknown schedule kind: ${JSON.stringify(exhaustive.kind)}`);
51
+ }
52
+ }
53
+ }
54
+ //# sourceMappingURL=validate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate.js","sourceRoot":"","sources":["../../src/recurrence/validate.ts"],"names":[],"mappings":";;AA8DA,4CAeC;AA7ED,6CAAmD;AACnD,iCAAiC;AAEjC,sCAAiD;AAIjD,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAE3B,SAAS,YAAY,CAAC,QAAsB;IAG3C,MAAM,UAAU,GAAY,QAAQ,CAAC,cAAc,CAAC;IACpD,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;QACpC,MAAM,IAAI,6BAAoB,CAC7B,6CAA6C,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CACzE,CAAC;IACH,CAAC;IAED,MAAM,UAAU,GAAG,UAAU,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC;IACzD,IAAI,UAAU,KAAK,gBAAgB,EAAE,CAAC;QACrC,MAAM,IAAI,6BAAoB,CAC7B,6BAA6B,gBAAgB,mCAAmC,UAAU,KAAK,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAC3H,CAAC;IACH,CAAC;IAGD,IAAI,QAAQ,CAAC,QAAQ,KAAK,IAAI,IAAI,CAAC,gBAAQ,CAAC,WAAW,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC5E,MAAM,IAAI,6BAAoB,CAAC,0BAA0B,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC/F,CAAC;IAED,IAAI,CAAC;QACJ,kCAAoB,CAAC,KAAK,CAAC,UAAU,EAAE;YACtC,EAAE,EAAE,QAAQ,CAAC,QAAQ,IAAI,KAAK;SAC9B,CAAC,CAAC;IACJ,CAAC;IAAC,OAAO,KA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Task } from '../types';
2
+ export interface SchedulerStore {
3
+ getDueJobs(now: Date, limit: number): Promise<ScheduledJob[]>;
4
+ saveJob(job: ScheduledJob): Promise<void>;
5
+ createTask(task: ScheduledTask): Promise<void>;
6
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/storage/storage.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export * from './schedule';
2
+ export * from './task';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./schedule"), exports);
18
+ __exportStar(require("./task"), exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,yCAAuB"}
@@ -0,0 +1,24 @@
1
+ import type { CronExpression } from 'n8n-workflow';
2
+ export declare const ScheduleKindList: readonly ["cron", "interval", "one_off"];
3
+ export type ScheduleKind = (typeof ScheduleKindList)[number];
4
+ export interface CronSchedule {
5
+ kind: 'cron';
6
+ cronExpression: CronExpression;
7
+ timezone: string | null;
8
+ }
9
+ export interface IntervalSchedule {
10
+ kind: 'interval';
11
+ intervalSeconds: number;
12
+ }
13
+ export interface OneOffSchedule {
14
+ kind: 'one_off';
15
+ fireAt: Date;
16
+ }
17
+ export type Schedule = CronSchedule | IntervalSchedule | OneOffSchedule;
18
+ export interface ScheduledJob {
19
+ id: string;
20
+ schedule: Schedule;
21
+ enabled: boolean;
22
+ nextRunAt: Date | null;
23
+ lastFiredAt: Date | null;
24
+ }
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScheduleKindList = void 0;
4
+ exports.ScheduleKindList = ['cron', 'interval', 'one_off'];
5
+ //# sourceMappingURL=schedule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schedule.js","sourceRoot":"","sources":["../../src/types/schedule.ts"],"names":[],"mappings":";;;AAiBa,QAAA,gBAAgB,GAAG,CAAC,MAAM,EAAE,UAAU,EAAE,SAAS,CAAU,CAAC"}
@@ -0,0 +1,13 @@
1
+ export declare const TaskStatusList: readonly ["pending", "running", "succeeded", "failed", "missed", "cancelled"];
2
+ export type TaskStatus = (typeof TaskStatusList)[number];
3
+ export interface ScheduledTask {
4
+ id: string;
5
+ jobId: string;
6
+ taskType: string;
7
+ payload: unknown;
8
+ scheduledFor: Date;
9
+ runAt: Date;
10
+ status: TaskStatus;
11
+ attempts: number;
12
+ maxAttempts: number;
13
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TaskStatusList = void 0;
4
+ exports.TaskStatusList = [
5
+ 'pending',
6
+ 'running',
7
+ 'succeeded',
8
+ 'failed',
9
+ 'missed',
10
+ 'cancelled',
11
+ ];
12
+ //# sourceMappingURL=task.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"task.js","sourceRoot":"","sources":["../../src/types/task.ts"],"names":[],"mappings":";;;AAaa,QAAA,cAAc,GAAG;IAC7B,SAAS;IACT,SAAS;IACT,WAAW;IACX,QAAQ;IACR,QAAQ;IACR,WAAW;CACF,CAAC"}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@n8n/scheduler",
3
+ "version": "0.1.0",
4
+ "main": "dist/index.js",
5
+ "module": "src/index.ts",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/**/*"
9
+ ],
10
+ "dependencies": {
11
+ "cron-parser": "5.6.1",
12
+ "luxon": "3.7.2",
13
+ "n8n-workflow": "2.29.0"
14
+ },
15
+ "devDependencies": {
16
+ "@types/luxon": "3.2.0",
17
+ "@vitest/coverage-v8": "4.1.9",
18
+ "vitest": "^4.1.9",
19
+ "vitest-mock-extended": "^3.1.0",
20
+ "@n8n/typescript-config": "1.7.0",
21
+ "@n8n/vitest-config": "1.16.0"
22
+ },
23
+ "license": "LicenseRef-n8n-sustainable-use",
24
+ "scripts": {
25
+ "clean": "rimraf dist .turbo",
26
+ "dev": "pnpm watch",
27
+ "typecheck": "tsc --noEmit",
28
+ "build": "tsc -p tsconfig.build.json",
29
+ "build:unchecked": "tsc -p tsconfig.build.json --noCheck",
30
+ "format": "biome format --write .",
31
+ "format:check": "biome ci .",
32
+ "lint": "eslint . --quiet",
33
+ "lint:fix": "eslint . --fix",
34
+ "watch": "tsc -p tsconfig.build.json --watch",
35
+ "test": "vitest run --passWithNoTests",
36
+ "test:unit": "vitest run --passWithNoTests",
37
+ "test:dev": "vitest --silent=false"
38
+ }
39
+ }