UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

279 lines (278 loc) • 7.75 kB
// src/internal/cronText.ts /** * Stored ranges. Day of week is stored as an `IsoWeekday` 1–7 (a GMT rule); the written range is * 0–7 (POSIX.1-2024 `crontab`: 0–6 with 0 Sunday; cronie crontab(5): "0 or 7 is Sunday"). */ const SPECS = { minute: { min: 0, max: 59 }, hour: { min: 0, max: 23 }, dayOfMonth: { min: 1, max: 31 }, month: { min: 1, max: 12 }, dayOfWeek: { min: 1, max: 7 } }; const FIELD_NAMES = [ "minute", "hour", "dayOfMonth", "month", "dayOfWeek" ]; /** * A number is any run of ASCII digits, range-checked after reading: POSIX.1-2024 `crontab` says * "a number" with no width, and cronie `entry.c` `get_number` reads every alphanumeric character * and runs `strtol`, so `000` is 0 and `*\/100` is a legal step. */ const NUMBER_TOKEN = /^[0-9]+$/y; const NAME_TOKEN = /^[A-Za-z]{3}$/y; /** cronie crontab(5): "Names can also be used for the 'month' and 'day of week' fields." */ const MONTH_NAMES = { jan: 1, feb: 2, mar: 3, apr: 4, may: 5, jun: 6, jul: 7, aug: 8, sep: 9, oct: 10, nov: 11, dec: 12 }; /** Written numbering: POSIX.1-2024 `crontab` has 0 for Sunday through 6 for Saturday. */ const WEEKDAY_NAMES = { sun: 0, mon: 1, tue: 2, wed: 3, thu: 4, fri: 5, sat: 6 }; function writtenOf(name) { const { min, max } = SPECS[name]; if (name === "month") return { min, max, names: MONTH_NAMES }; if (name === "dayOfWeek") return { min: 0, max: 7, names: WEEKDAY_NAMES }; return { min, max, names: null }; } function range(from, to, step) { const out = []; for (let n = from; n <= to; n += step) out.push(n); return out; } function freezeField(values, star) { return Object.freeze({ values: Object.freeze([...values]), star }); } function isDigits(token) { NUMBER_TOKEN.lastIndex = 0; return NUMBER_TOKEN.test(token); } /** A value is a digit run in the field's range, or a three-letter name where the field has names. */ function readValue(token, written) { if (isDigits(token)) { const value = Number(token); return value >= written.min && value <= written.max ? value : null; } if (written.names === null) return null; NAME_TOKEN.lastIndex = 0; if (!NAME_TOKEN.test(token)) return null; const key = token.toLowerCase(); return Object.hasOwn(written.names, key) ? written.names[key] : null; } /** A step is a digit run of at least 1; cronie `entry.c` rejects 0. */ function readStep(token) { if (!isDigits(token)) return null; const step = Number(token); return step >= 1 ? step : null; } /** Cron numbering to the stored numbering: 0 and 7 are Sunday, stored 7. */ function toStored(name, value) { return name === "dayOfWeek" && value === 0 ? 7 : value; } /** Both ends of `a-b`; a day-of-week range ending in 0 means "...-7" (cronie `entry.c` `get_range`). */ function readEnds(ends, written) { const low = readValue(ends[0], written); let high = readValue(ends[1], written); if (low === null || high === null) return null; if (written.max === 7 && low > high && high === 0) high = 7; return low > high ? null : [low, high]; } /** `a-b` with an optional `/step`. */ function readRangeItem(ends, stepText, written) { const bounds = readEnds(ends, written); const step = stepText === void 0 ? 1 : readStep(stepText); if (bounds === null || step === null) return null; return range(bounds[0], bounds[1], step); } function readItem(item, written) { const [rangeText = "", stepText, ...extraStep] = item.split("/"); const ends = rangeText.split("-"); if (extraStep.length > 0 || ends.length > 2) return null; if (ends.length === 2) return readRangeItem(ends, stepText, written); const only = readValue(rangeText, written); return only === null || stepText !== void 0 ? null : [only]; } function readField(token, name) { const spec = SPECS[name]; const written = writtenOf(name); if (token.startsWith("*")) { if (token === "*") return freezeField(range(spec.min, spec.max, 1), true); const [star, stepText, ...extra] = token.split("/"); const step = stepText === void 0 ? null : readStep(stepText); if (star !== "*" || extra.length > 0 || step === null) return null; return freezeField(normalise(range(written.min, written.max, step), name), true); } const values = []; for (const item of token.split(",")) { const read = readItem(item, written); if (read === null) return null; values.push(...read); } return freezeField(normalise(values, name), false); } function normalise(values, name) { return [...new Set(values.map((value) => toStored(name, value)))].sort((a, b) => a - b); } /** * cronie crontab(5) EXTENSIONS; flags from `entry.c` lines 173-211. Matched exactly and in lower * case, as `entry.c` does with `strcmp`. `@reboot` is absent: it is not a time. cronie has no * month flag (month is always ANDed), so `month` follows the expansion's `*`. */ const NICKNAMES = { "@yearly": { line: "0 0 1 1 *", star: ["dayOfWeek"] }, "@annually": { line: "0 0 1 1 *", star: ["dayOfWeek"] }, "@monthly": { line: "0 0 1 * *", star: ["month", "dayOfWeek"] }, "@weekly": { line: "0 0 * * 0", star: ["dayOfMonth", "month"] }, "@daily": { line: "0 0 * * *", star: [ "dayOfMonth", "month", "dayOfWeek" ] }, "@midnight": { line: "0 0 * * *", star: [ "dayOfMonth", "month", "dayOfWeek" ] }, "@hourly": { line: "0 * * * *", star: [ "hour", "dayOfMonth", "month", "dayOfWeek" ] } }; function readFields(text, starOf) { const tokens = text.split(/[ \t]+/); if (tokens.length !== FIELD_NAMES.length) return null; const fields = {}; for (const [index, name] of FIELD_NAMES.entries()) { const field = readField(tokens[index], name); if (field === null) return null; fields[name] = freezeField(field.values, starOf(name, field.star)); } return Object.freeze(fields); } /** * Read the text of a crontab schedule: one lower-case nickname, or five fields separated by * blanks (minute, hour, day of month, month, day of week). Returns `null` outside the grammar. */ function readCronExpression(text) { if (text.startsWith("@")) { if (!Object.hasOwn(NICKNAMES, text)) return null; const { line, star } = NICKNAMES[text]; return readFields(line, (name) => star.includes(name)); } return readFields(text, (_name, written) => written); } function isInRange(entry, min, max, previous) { return typeof entry === "number" && Number.isInteger(entry) && entry >= min && entry <= max && entry > previous; } function isAscendingIn(copy, name) { const { min, max } = SPECS[name]; let previous = min - 1; for (const entry of copy) { if (!isInRange(entry, min, max, previous)) return false; previous = entry; } return true; } function checkField(value, name) { if (typeof value !== "object" || value === null) return null; const { values, star } = value; if (typeof star !== "boolean" || !Array.isArray(values)) return null; const copy = [...values]; if (copy.length === 0 || !isAscendingIn(copy, name)) return null; return freezeField(copy, star); } /** * Check a caller-built expression against the rules the parser guarantees: every field has a * non-empty, strictly ascending array of integers inside the field's range, and a boolean * `star`. Day of week holds `IsoWeekday` 1–7, so cron's `0` is refused. Returns a fresh frozen * record, or `null`. */ function checkCronExpression(value) { try { if (typeof value !== "object" || value === null || Array.isArray(value)) return null; const source = value; const fields = {}; for (const name of FIELD_NAMES) { const field = checkField(source[name], name); if (field === null) return null; fields[name] = field; } return Object.freeze(fields); } catch { return null; } } export { checkCronExpression, readCronExpression };