@alzulejos/laranja-core 0.2.4
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.
- package/dist/api.d.ts +274 -0
- package/dist/api.js +45 -0
- package/dist/api.js.map +1 -0
- package/dist/auth.d.ts +37 -0
- package/dist/auth.js +78 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +104 -0
- package/dist/client.js +240 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +151 -0
- package/dist/config.js +89 -0
- package/dist/config.js.map +1 -0
- package/dist/env.d.ts +55 -0
- package/dist/env.js +66 -0
- package/dist/env.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/ir.d.ts +204 -0
- package/dist/ir.js +21 -0
- package/dist/ir.js.map +1 -0
- package/dist/nest-schedule.d.ts +127 -0
- package/dist/nest-schedule.js +222 -0
- package/dist/nest-schedule.js.map +1 -0
- package/dist/schedule.d.ts +62 -0
- package/dist/schedule.js +140 -0
- package/dist/schedule.js.map +1 -0
- package/package.json +23 -0
package/dist/schedule.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schedule representation. laranja stores schedules in a PROVIDER-NEUTRAL
|
|
3
|
+
* structured form so the back half can lower them to whatever the target cloud
|
|
4
|
+
* expects (AWS EventBridge `rate(...)`/`cron(...)`, GCP/Cloudflare Unix cron,
|
|
5
|
+
* Azure NCRONTAB, …). The front half never bakes in a provider string.
|
|
6
|
+
*
|
|
7
|
+
* `rate` ("every N units") is portable across every provider. `cron` carries a
|
|
8
|
+
* raw expression tagged with its dialect — an explicit escape hatch (only "aws"
|
|
9
|
+
* exists today; new dialects land with their providers).
|
|
10
|
+
*
|
|
11
|
+
* The builders are pure, which lets the static scanner constant-fold calls like
|
|
12
|
+
* `@Cron(rate(5, "minutes"))` at scan time without executing user code.
|
|
13
|
+
*/
|
|
14
|
+
import cronstrue from "cronstrue";
|
|
15
|
+
function toSingular(unit) {
|
|
16
|
+
return unit.replace(/s$/, "");
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Builds a neutral "every N units" schedule.
|
|
20
|
+
*
|
|
21
|
+
* @example rate(5, "minutes") // { kind: "rate", value: 5, unit: "minute" }
|
|
22
|
+
* @example rate(1, "hour") // { kind: "rate", value: 1, unit: "hour" }
|
|
23
|
+
*/
|
|
24
|
+
export function rate(value, unit) {
|
|
25
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
26
|
+
throw new Error(`rate(): value must be a positive integer, got ${value}`);
|
|
27
|
+
}
|
|
28
|
+
return { kind: "rate", value, unit: toSingular(unit) };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Shorthand for `rate(1, unit)`.
|
|
32
|
+
* @example every("day") // { kind: "rate", value: 1, unit: "day" }
|
|
33
|
+
*/
|
|
34
|
+
export function every(unit) {
|
|
35
|
+
return rate(1, unit);
|
|
36
|
+
}
|
|
37
|
+
const RATE_STRING_RE = /^rate\((\d+)\s+(minute|minutes|hour|hours|day|days)\)$/;
|
|
38
|
+
const CRON_STRING_RE = /^cron\((.+)\)$/;
|
|
39
|
+
/**
|
|
40
|
+
* Parse a raw AWS schedule STRING (`"rate(5 minutes)"` / `"cron(0 12 * * ? *)"`)
|
|
41
|
+
* into the neutral structured form. Returns undefined for anything that isn't a
|
|
42
|
+
* valid AWS expression (e.g. a 5-field Unix cron string), so callers can raise a
|
|
43
|
+
* clear error. (Other dialects' string parsing arrives with their providers.)
|
|
44
|
+
*/
|
|
45
|
+
export function parseScheduleString(s) {
|
|
46
|
+
const trimmed = s.trim();
|
|
47
|
+
const r = RATE_STRING_RE.exec(trimmed);
|
|
48
|
+
if (r) {
|
|
49
|
+
const value = Number(r[1]);
|
|
50
|
+
if (value < 1)
|
|
51
|
+
return undefined;
|
|
52
|
+
return { kind: "rate", value, unit: toSingular(r[2]) };
|
|
53
|
+
}
|
|
54
|
+
const c = CRON_STRING_RE.exec(trimmed);
|
|
55
|
+
if (c)
|
|
56
|
+
return { kind: "cron", expression: c[1], dialect: "aws" };
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
/** Throws a clear, located error if `s` isn't a valid schedule. */
|
|
60
|
+
export function assertSchedule(s, where) {
|
|
61
|
+
if (s.kind === "rate") {
|
|
62
|
+
if (!Number.isInteger(s.value) || s.value < 1) {
|
|
63
|
+
throw new Error(`Invalid schedule at ${where}: rate value must be a positive integer, got ${s.value}.`);
|
|
64
|
+
}
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
// Only the "aws" cron dialect exists today; others land with their providers.
|
|
68
|
+
if (s.dialect !== "aws") {
|
|
69
|
+
throw new Error(`Invalid schedule at ${where}: unknown cron dialect "${s.dialect}".`);
|
|
70
|
+
}
|
|
71
|
+
if (!s.expression.trim()) {
|
|
72
|
+
throw new Error(`Invalid schedule at ${where}: empty cron expression.`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Remap one AWS day-of-week field back to standard Unix numbering. AWS uses 1-7
|
|
77
|
+
* with 1=Sunday; cronstrue (like Unix cron) uses 0-6 with 0=Sunday — so numeric
|
|
78
|
+
* tokens shift down by one. Only the value/range segment is remapped; a trailing
|
|
79
|
+
* `/step` is a count, not a day, and passes through. Names (MON..SUN) pass through.
|
|
80
|
+
* Mirrors the inverse of `translateDow` in nest-schedule.ts.
|
|
81
|
+
*/
|
|
82
|
+
function awsDowToStandard(field) {
|
|
83
|
+
if (field === "*" || field === "?")
|
|
84
|
+
return "*";
|
|
85
|
+
return field
|
|
86
|
+
.split(",")
|
|
87
|
+
.map((part) => part
|
|
88
|
+
.split("/")
|
|
89
|
+
.map((seg, i) => i === 0
|
|
90
|
+
? seg
|
|
91
|
+
.split("-")
|
|
92
|
+
.map((t) => {
|
|
93
|
+
const n = Number(t);
|
|
94
|
+
return /^\d+$/.test(t) && n >= 1 && n <= 7 ? String(n - 1) : t;
|
|
95
|
+
})
|
|
96
|
+
.join("-")
|
|
97
|
+
: seg)
|
|
98
|
+
.join("/"))
|
|
99
|
+
.join(",");
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Convert laranja's stored AWS EventBridge cron (`min hour dom month dow [year]`,
|
|
103
|
+
* with `?` placeholders and AWS day-of-week numbering) into a standard 5-field
|
|
104
|
+
* Unix expression (`*` placeholders, 0=Sunday) that cronstrue understands. The
|
|
105
|
+
* inverse of the lowering `nestCronToSchedule` performs. Returns the input
|
|
106
|
+
* untouched for anything that isn't 5- or 6-field, so the caller can fall back.
|
|
107
|
+
*/
|
|
108
|
+
function awsCronToStandard(expr) {
|
|
109
|
+
const parts = expr.trim().split(/\s+/);
|
|
110
|
+
if (parts.length !== 5 && parts.length !== 6)
|
|
111
|
+
return expr;
|
|
112
|
+
const [minute, hour, dom, month, dow] = parts; // parts[5] (year) is dropped
|
|
113
|
+
return [minute, hour, dom === "?" ? "*" : dom, month, awsDowToStandard(dow)].join(" ");
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Human-readable description of a Schedule — the single source of truth for how a
|
|
117
|
+
* schedule is shown to a user (the CLI `plan` table and the dashboard both call
|
|
118
|
+
* this, so the wording never drifts). `rate` renders structurally; `cron` is
|
|
119
|
+
* normalized from our AWS dialect to standard Unix cron and handed to cronstrue.
|
|
120
|
+
* Anything cronstrue can't parse falls back to the raw expression, so we never
|
|
121
|
+
* show a misleading label.
|
|
122
|
+
*
|
|
123
|
+
* @example describeSchedule({ kind: "rate", value: 5, unit: "minute" }) // "Every 5 minutes"
|
|
124
|
+
* @example describeSchedule({ kind: "cron", expression: "* * * * ? *", dialect: "aws" }) // "Every minute"
|
|
125
|
+
*/
|
|
126
|
+
export function describeSchedule(schedule) {
|
|
127
|
+
if (schedule.kind === "rate") {
|
|
128
|
+
return schedule.value === 1
|
|
129
|
+
? `Every ${schedule.unit}`
|
|
130
|
+
: `Every ${schedule.value} ${schedule.unit}s`;
|
|
131
|
+
}
|
|
132
|
+
const standard = awsCronToStandard(schedule.expression);
|
|
133
|
+
try {
|
|
134
|
+
return cronstrue.toString(standard, { throwExceptionOnParseError: true, verbose: false });
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return schedule.expression;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=schedule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schedule.js","sourceRoot":"","sources":["../src/schedule.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,SAAS,MAAM,WAAW,CAAC;AAgBlC,SAAS,UAAU,CAAC,IAAc;IAChC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAiB,CAAC;AAChD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,IAAI,CAAC,KAAa,EAAE,IAAc;IAChD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CAAC,iDAAiD,KAAK,EAAE,CAAC,CAAC;IAC5E,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;AACzD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAC,IAAkB;IACtC,OAAO,IAAI,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;AACvB,CAAC;AAED,MAAM,cAAc,GAAG,wDAAwD,CAAC;AAChF,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAExC;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,CAAS;IAC3C,MAAM,OAAO,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IACzB,MAAM,CAAC,GAAG,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC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AG,EAAE,KAAK,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACzF,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAkB;IACjD,IAAI,QAAQ,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;QAC7B,OAAO,QAAQ,CAAC,KAAK,KAAK,CAAC;YACzB,CAAC,CAAC,SAAS,QAAQ,CAAC,IAAI,EAAE;YAC1B,CAAC,CAAC,SAAS,QAAQ,CAAC,KAAK,IAAI,QAAQ,CAAC,IAAI,GAAG,CAAC;IAClD,CAAC;IACD,MAAM,QAAQ,GAAG,iBAAiB,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;IACxD,IAAI,CAAC;QACH,OAAO,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,0BAA0B,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;IAC5F,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,QAAQ,CAAC,UAAU,CAAC;IAC7B,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alzulejos/laranja-core",
|
|
3
|
+
"version": "0.2.4",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"cronstrue": "^3.24.0"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
}
|
|
23
|
+
}
|