@cronwatch/sdk 0.10.0 → 0.11.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.
- package/README.md +4 -4
- package/dist/anthropic.d.cts +1 -1
- package/dist/anthropic.d.ts +1 -1
- package/dist/bugsnag.d.cts +1 -1
- package/dist/bugsnag.d.ts +1 -1
- package/dist/{client-DUoSea7X.d.cts → client-CRMfhylq.d.cts} +63 -26
- package/dist/{client-BcIjVBiZ.d.ts → client-D2Xk2600.d.ts} +63 -26
- package/dist/d1.cjs +29 -11
- package/dist/d1.cjs.map +1 -1
- package/dist/d1.d.cts +1 -1
- package/dist/d1.d.ts +1 -1
- package/dist/d1.js +29 -11
- package/dist/d1.js.map +1 -1
- package/dist/datadog.d.cts +1 -1
- package/dist/datadog.d.ts +1 -1
- package/dist/discord.cjs +10 -4
- package/dist/discord.cjs.map +1 -1
- package/dist/discord.d.cts +13 -6
- package/dist/discord.d.ts +13 -6
- package/dist/discord.js +7 -1
- package/dist/discord.js.map +1 -1
- package/dist/{email-VrGsVz43.d.ts → email-BWuUiCkw.d.ts} +1 -1
- package/dist/{email-JZs3vxwq.d.cts → email-Dcqe3k8n.d.cts} +1 -1
- package/dist/honeybadger.d.cts +1 -1
- package/dist/honeybadger.d.ts +1 -1
- package/dist/index.cjs +177 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -9
- package/dist/index.d.ts +15 -9
- package/dist/index.js +176 -40
- package/dist/index.js.map +1 -1
- package/dist/mailgun.d.cts +2 -2
- package/dist/mailgun.d.ts +2 -2
- package/dist/newrelic.d.cts +1 -1
- package/dist/newrelic.d.ts +1 -1
- package/dist/pg-cron.cjs +17 -7
- package/dist/pg-cron.cjs.map +1 -1
- package/dist/pg-cron.d.cts +15 -12
- package/dist/pg-cron.d.ts +15 -12
- package/dist/pg-cron.js +14 -4
- package/dist/pg-cron.js.map +1 -1
- package/dist/postgres.cjs +28 -10
- package/dist/postgres.cjs.map +1 -1
- package/dist/postgres.d.cts +1 -1
- package/dist/postgres.d.ts +1 -1
- package/dist/postgres.js +28 -10
- package/dist/postgres.js.map +1 -1
- package/dist/postmark.d.cts +2 -2
- package/dist/postmark.d.ts +2 -2
- package/dist/resend.d.cts +2 -2
- package/dist/resend.d.ts +2 -2
- package/dist/rollbar.d.cts +1 -1
- package/dist/rollbar.d.ts +1 -1
- package/dist/sendgrid.d.cts +2 -2
- package/dist/sendgrid.d.ts +2 -2
- package/dist/sentry.cjs +4 -1
- package/dist/sentry.cjs.map +1 -1
- package/dist/sentry.d.cts +5 -2
- package/dist/sentry.d.ts +5 -2
- package/dist/sentry.js +4 -1
- package/dist/sentry.js.map +1 -1
- package/dist/ses.d.cts +2 -2
- package/dist/ses.d.ts +2 -2
- package/dist/slack.d.cts +1 -1
- package/dist/slack.d.ts +1 -1
- package/dist/sqlite.cjs +29 -11
- package/dist/sqlite.cjs.map +1 -1
- package/dist/sqlite.d.cts +1 -1
- package/dist/sqlite.d.ts +1 -1
- package/dist/sqlite.js +29 -11
- package/dist/sqlite.js.map +1 -1
- package/dist/twilio.cjs +10 -4
- package/dist/twilio.cjs.map +1 -1
- package/dist/twilio.d.cts +12 -7
- package/dist/twilio.d.ts +12 -7
- package/dist/twilio.js +7 -1
- package/dist/twilio.js.map +1 -1
- package/dist/{types-DyWCxlSi.d.cts → types-DLnlUie8.d.cts} +5 -2
- package/dist/{types-DyWCxlSi.d.ts → types-DLnlUie8.d.ts} +5 -2
- package/dist/webhook.cjs +10 -5
- package/dist/webhook.cjs.map +1 -1
- package/dist/webhook.d.cts +18 -7
- package/dist/webhook.d.ts +18 -7
- package/dist/webhook.js +10 -6
- package/dist/webhook.js.map +1 -1
- package/package.json +1 -1
package/dist/discord.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/alerts/shared.ts","../src/alerts/discord.ts"],"names":[],"mappings":";AAiCO,SAAS,SAAS,GAAA,EAAqB;AAC5C,EAAA,I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,MAAA,GAAS,OAAA,CAAQ,MAAA;AACxC,EAAA,OAAO,OAAA,GAAU,GAAA,CAAI,aAAA,CAAc,KAAA,CAAM,QAAQ,KAAA,CAAM,CAAA,EAAG,IAAI,CAAC,GAAG,eAAA,GAAkB,MAAA,GAAS,MAAA,CAAO,MAAM,IAAI,OAAA,GAAU,MAAA;AAC1H;AAGO,SAAS,cAAc,IAAA,EAAsB;AAClD,EAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,MAAA,EAAQ,iBAAO,CAAA;AACrC;AAGO,SAAS,eAAe,IAAA,EAAsB;AACnD,EAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,mBAAA,EAAqB,MAAM,CAAA;AACjD","file":"discord.js","sourcesContent":["/**\n * Helpers shared by the provider channels. Only fetch and Web Crypto, so the\n * channels run on Node, Cloudflare Workers, Deno and Bun alike. Not an entry\n * point: each channel bundles its own copy.\n */\nimport { isoTime } from \"../duration.js\";\nimport type { Alert } from \"../types.js\";\n\nexport const TIMEOUT_MS = 10_000;\n\n/** Severity for trackers that have levels. Recovered is informational. */\nexport type Severity = \"error\" | \"warning\" | \"info\";\n\nexport function severity(type: Alert[\"type\"]): Severity {\n if (type === \"recovered\") return \"info\";\n if (type === \"slow\" || type === \"over_budget\") return \"warning\";\n return \"error\";\n}\n\n/** The scheme, host and port only. A URL's path or query can hold a credential. */\nexport function origin(url: string): string {\n try {\n return new URL(url).origin;\n } catch {\n return \"(invalid URL)\";\n }\n}\n\n/**\n * The URL, once it is one fetch can post to. Refused without quoting it:\n * fetch's own error for a URL it cannot parse (\"Failed to parse URL from ...\")\n * quotes the whole URL, and a webhook URL's path is its credential.\n */\nexport function postable(url: string): string {\n let protocol: string;\n try {\n protocol = new URL(url).protocol;\n } catch {\n throw new Error(\"only http and https URLs can be posted to, not this URL\");\n }\n if (protocol !== \"http:\" && protocol !== \"https:\") throw new Error(`only http and https URLs can be posted to, not ${protocol}`);\n return url;\n}\n\n/** How much of a provider's error body goes into the error message. */\nexport const ERROR_BODY_MAX = 200;\n\n/**\n * POSTs and throws on a non-2xx answer. The error names the provider and the\n * URL's origin, plus the start of the response body with every secret the\n * channel holds cut out, in case a provider echoes one back. Secrets are cut\n * from a prefix long enough to hold one that starts inside the first\n * ERROR_BODY_MAX characters, and only then is it cut to that length, so no\n * part of a secret survives at the edge. A redirect is refused rather than\n * followed: the credential headers would go with it to wherever it points.\n */\nexport async function post(provider: string, url: string, init: { headers: Record<string, string>; body: string }, secrets: (string | undefined)[] = []): Promise<Response> {\n const response = await fetch(postable(url), { method: \"POST\", headers: init.headers, body: init.body, redirect: \"error\", signal: AbortSignal.timeout(TIMEOUT_MS) });\n if (!response.ok) {\n let text = \"\";\n try {\n text = await response.text();\n } catch {\n // The status is enough.\n }\n throw new Error(`${provider} ${origin(url)} answered ${response.status}${text ? `: ${errorBody(text, secrets)}` : \"\"}`);\n }\n return response;\n}\n\n/** The start of an error body, secrets cut out first, then cut to ERROR_BODY_MAX on a code point. */\nexport function errorBody(text: string, secrets: (string | undefined)[] = []): string {\n const kept = secrets.filter((s): s is string => typeof s === \"string\" && s.length >= 4);\n const longest = kept.reduce((n, s) => Math.max(n, s.length), 0);\n let head = cut(text, ERROR_BODY_MAX + longest);\n for (const secret of kept) head = head.split(secret).join(\"[redacted]\");\n return cut(head, ERROR_BODY_MAX);\n}\n\n/** A credential with the spaces and newlines a paste leaves around it taken off. Anything not a string is empty. */\nexport function trimmed(value: unknown): string {\n return typeof value === \"string\" ? value.trim() : \"\";\n}\n\n/** Base64 of UTF-8, without Buffer. */\nexport function base64(text: string): string {\n let binary = \"\";\n for (const byte of new TextEncoder().encode(text)) binary += String.fromCharCode(byte);\n return btoa(binary);\n}\n\nexport function basicAuth(user: string, password: string): string {\n return `Basic ${base64(`${user}:${password}`)}`;\n}\n\nexport function hex(bytes: ArrayBuffer | Uint8Array): string {\n return Array.from(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes), (b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport async function sha256Hex(text: string): Promise<string> {\n return hex(await crypto.subtle.digest(\"SHA-256\", new TextEncoder().encode(text)));\n}\n\n/**\n * A stable 32 hex character id for one alert: the same job, type and time\n * always give the same id, so a provider that deduplicates on it drops a\n * resend of an alert it already took.\n */\nexport async function alertId(alert: Alert): Promise<string> {\n return (await sha256Hex(`${alert.job}\\n${alert.type}\\n${alert.at}`)).slice(0, 32);\n}\n\n/** The same id laid out as a UUID, for APIs that ask for one. */\nexport function asUuid(id: string): string {\n return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20, 32)}`;\n}\n\n/** Cuts to at most `max` UTF-16 units without splitting a surrogate pair. */\nexport function cut(text: string, max: number): string {\n if (text.length <= max) return text;\n let end = max;\n const code = text.charCodeAt(end - 1);\n if (code >= 0xd800 && code <= 0xdbff) end -= 1;\n return text.slice(0, end);\n}\n\n/** The run fields worth attaching to a tracker event. A start before the year 1 or after 9999 is null. */\nexport function runSummary(alert: Alert): Record<string, unknown> | null {\n const run = alert.run;\n if (!run) return null;\n return { id: run.id, status: run.status, startedAt: isoTime(run.startedAt), durationMs: run.durationMs, trigger: run.trigger };\n}\n\n/** Title, message, triage and link as one plain text block, the way every channel reads. */\nexport function plainText(alert: Alert, link: string | undefined): string {\n return [alert.title, \"\", alert.message, ...(alert.triage ? [\"\", `Triage: ${alert.triage}`] : []), ...(link ? [\"\", `Open: ${link}`] : [])].join(\"\\n\");\n}\n","import type { Alert, AlertChannel } from \"../types.js\";\nimport { cut, postable } from \"./shared.js\";\n\nexport interface DiscordOptions {\n /** A channel webhook URL from Server Settings, Integrations, Webhooks. */\n webhookUrl: string;\n link?: (alert: Alert) => string;\n}\n\nconst COLOR: Record<Alert[\"type\"], number> = {\n missed: 0xb7791f,\n failed: 0xc62828,\n stuck: 0xc62828,\n slow: 0xb7791f,\n over_budget: 0xb7791f,\n recovered: 0x1f8a4c,\n};\n\nconst TIMEOUT_MS = 10_000;\n\n/** The longest embed description Discord takes. The title (under 256) and it stay well inside the embed's 6000. */\nexport const DESCRIPTION_MAX = 4096;\n\n/** Sends alerts to a Discord channel through a webhook. */\nexport function discord(options: DiscordOptions): AlertChannel {\n if (!options.webhookUrl) throw new Error(\"discord() needs a webhookUrl\");\n return {\n name: \"discord\",\n async send(alert) {\n const url = options.link?.(alert);\n const response = await fetch(postable(options.webhookUrl), {\n method: \"POST\",\n headers: { \"content-type\": \"application/json\" },\n // Refused, not followed: a webhook URL is its own credential.\n redirect: \"error\",\n signal: AbortSignal.timeout(TIMEOUT_MS),\n body: JSON.stringify({\n content: alert.title,\n // Job output can hold anything, \"@everyone\" included; ping no one.\n allowed_mentions: { parse: [] },\n embeds: [\n {\n title: alert.title,\n ...(url ? { url } : {}),\n description: embedDescription(alert),\n color: COLOR[alert.type],\n timestamp: new Date(alert.at).toISOString(),\n },\n ],\n }),\n });\n if (!response.ok) throw new Error(`Discord webhook answered ${response.status}: ${(await response.text()).slice(0, 200)}`);\n },\n };\n}\n\n/**\n * The message in a code block, then the triage. Each part has its own cap,\n * and escaping can grow both, so the whole is held to DESCRIPTION_MAX (in\n * UTF-16 units) by cutting the message's block, never the triage: Discord\n * refuses a longer one on every retry.\n */\nexport function embedDescription(alert: Alert): string {\n const triage = alert.triage ? `\\n**Triage:** ${escapeMarkdown(alert.triage.slice(0, 1000))}` : \"\";\n const fences = \"```\\n\".length + \"\\n```\".length;\n return \"```\\n\" + cut(codeBlockSafe(alert.message.slice(0, 3800)), DESCRIPTION_MAX - fences - triage.length) + \"\\n```\" + triage;\n}\n\n/** Breaks up ``` so text inside a code block cannot close it. */\nexport function codeBlockSafe(text: string): string {\n return text.replace(/```/g, \"```\");\n}\n\n/** Escapes the characters Discord reads as markdown, links included. */\nexport function escapeMarkdown(text: string): string {\n return text.replace(/[\\\\`*_~|[\\]()<>]/g, \"\\\\$&\");\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/alerts/shared.ts","../src/alerts/discord.ts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,CAAe,MAAM,MAAA,CAAO,KAAA,CAAM,GAAG,GAAI,CAAC,CAAC,CAAA,CAAA,GAAK,EAAA;AAC/F,EAAA,MAAM,MAAA,GAAS,OAAA,CAAQ,MAAA,GAAS,OAAA,CAAQ,MAAA;AACxC,EAAA,OAAO,OAAA,GAAU,GAAA,CAAI,aAAA,CAAc,KAAA,CAAM,QAAQ,KAAA,CAAM,CAAA,EAAG,IAAI,CAAC,GAAG,eAAA,GAAkB,MAAA,GAAS,MAAA,CAAO,MAAM,IAAI,OAAA,GAAU,MAAA;AAC1H;AAGO,SAAS,cAAc,IAAA,EAAsB;AAClD,EAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,MAAA,EAAQ,iBAAO,CAAA;AACrC;AAGO,SAAS,eAAe,IAAA,EAAsB;AACnD,EAAA,OAAO,IAAA,CAAK,OAAA,CAAQ,mBAAA,EAAqB,MAAM,CAAA;AACjD;;;ACnEO,IAAMA,gBAAAA,GAA2B;AAEjC,IAAMC,iBAAAA,GAA4B;AAElC,IAAMC,cAAAA,GAAyB;AAE/B,IAAMC,eAAAA,GAA0B","file":"discord.js","sourcesContent":["/**\n * Helpers shared by the provider channels. Only fetch and Web Crypto, so the\n * channels run on Node, Cloudflare Workers, Deno and Bun alike. Not an entry\n * point: each channel bundles its own copy.\n */\nimport { isoTime } from \"../duration.js\";\nimport type { Alert } from \"../types.js\";\n\nexport const TIMEOUT_MS = 10_000;\n\n/** Severity for trackers that have levels. Recovered is informational. */\nexport type Severity = \"error\" | \"warning\" | \"info\";\n\nexport function severity(type: Alert[\"type\"]): Severity {\n if (type === \"recovered\") return \"info\";\n if (type === \"slow\" || type === \"over_budget\") return \"warning\";\n return \"error\";\n}\n\n/** The scheme, host and port only. A URL's path or query can hold a credential. */\nexport function origin(url: string): string {\n try {\n return new URL(url).origin;\n } catch {\n return \"(invalid URL)\";\n }\n}\n\n/**\n * The URL, once it is one fetch can post to. Refused without quoting it:\n * fetch's own error for a URL it cannot parse (\"Failed to parse URL from ...\")\n * quotes the whole URL, and a webhook URL's path is its credential.\n */\nexport function postable(url: string): string {\n let protocol: string;\n try {\n protocol = new URL(url).protocol;\n } catch {\n throw new Error(\"only http and https URLs can be posted to, not this URL\");\n }\n if (protocol !== \"http:\" && protocol !== \"https:\") throw new Error(`only http and https URLs can be posted to, not ${protocol}`);\n return url;\n}\n\n/** How much of a provider's error body goes into the error message. */\nexport const ERROR_BODY_MAX = 200;\n\n/**\n * POSTs and throws on a non-2xx answer. The error names the provider and the\n * URL's origin, plus the start of the response body with every secret the\n * channel holds cut out, in case a provider echoes one back. Secrets are cut\n * from a prefix long enough to hold one that starts inside the first\n * ERROR_BODY_MAX characters, and only then is it cut to that length, so no\n * part of a secret survives at the edge. A redirect is refused rather than\n * followed: the credential headers would go with it to wherever it points.\n */\nexport async function post(provider: string, url: string, init: { headers: Record<string, string>; body: string }, secrets: (string | undefined)[] = []): Promise<Response> {\n const response = await fetch(postable(url), { method: \"POST\", headers: init.headers, body: init.body, redirect: \"error\", signal: AbortSignal.timeout(TIMEOUT_MS) });\n if (!response.ok) {\n let text = \"\";\n try {\n text = await response.text();\n } catch {\n // The status is enough.\n }\n throw new Error(`${provider} ${origin(url)} answered ${response.status}${text ? `: ${errorBody(text, secrets)}` : \"\"}`);\n }\n return response;\n}\n\n/** The start of an error body, secrets cut out first, then cut to ERROR_BODY_MAX on a code point. */\nexport function errorBody(text: string, secrets: (string | undefined)[] = []): string {\n const kept = secrets.filter((s): s is string => typeof s === \"string\" && s.length >= 4);\n const longest = kept.reduce((n, s) => Math.max(n, s.length), 0);\n let head = cut(text, ERROR_BODY_MAX + longest);\n for (const secret of kept) head = head.split(secret).join(\"[redacted]\");\n return cut(head, ERROR_BODY_MAX);\n}\n\n/** A credential with the spaces and newlines a paste leaves around it taken off. Anything not a string is empty. */\nexport function trimmed(value: unknown): string {\n return typeof value === \"string\" ? value.trim() : \"\";\n}\n\n/** Base64 of UTF-8, without Buffer. */\nexport function base64(text: string): string {\n let binary = \"\";\n for (const byte of new TextEncoder().encode(text)) binary += String.fromCharCode(byte);\n return btoa(binary);\n}\n\nexport function basicAuth(user: string, password: string): string {\n return `Basic ${base64(`${user}:${password}`)}`;\n}\n\nexport function hex(bytes: ArrayBuffer | Uint8Array): string {\n return Array.from(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes), (b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport async function sha256Hex(text: string): Promise<string> {\n return hex(await crypto.subtle.digest(\"SHA-256\", new TextEncoder().encode(text)));\n}\n\n/**\n * A stable 32 hex character id for one alert: the same job, type and time\n * always give the same id, so a provider that deduplicates on it drops a\n * resend of an alert it already took.\n */\nexport async function alertId(alert: Alert): Promise<string> {\n return (await sha256Hex(`${alert.job}\\n${alert.type}\\n${alert.at}`)).slice(0, 32);\n}\n\n/** The same id laid out as a UUID, for APIs that ask for one. */\nexport function asUuid(id: string): string {\n return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20, 32)}`;\n}\n\n/** Cuts to at most `max` UTF-16 units without splitting a surrogate pair. */\nexport function cut(text: string, max: number): string {\n if (text.length <= max) return text;\n let end = max;\n const code = text.charCodeAt(end - 1);\n if (code >= 0xd800 && code <= 0xdbff) end -= 1;\n return text.slice(0, end);\n}\n\n/** The run fields worth attaching to a tracker event. A start before the year 1 or after 9999 is null. */\nexport function runSummary(alert: Alert): Record<string, unknown> | null {\n const run = alert.run;\n if (!run) return null;\n return { id: run.id, status: run.status, startedAt: isoTime(run.startedAt), durationMs: run.durationMs, trigger: run.trigger };\n}\n\n/** Title, message, triage and link as one plain text block, the way every channel reads. */\nexport function plainText(alert: Alert, link: string | undefined): string {\n return [alert.title, \"\", alert.message, ...(alert.triage ? [\"\", `Triage: ${alert.triage}`] : []), ...(link ? [\"\", `Open: ${link}`] : [])].join(\"\\n\");\n}\n","import type { Alert, AlertChannel } from \"../types.js\";\nimport { cut, postable } from \"./shared.js\";\n\nexport interface DiscordOptions {\n /** A channel webhook URL from Server Settings, Integrations, Webhooks. */\n webhookUrl: string;\n link?: (alert: Alert) => string;\n}\n\nconst COLOR: Record<Alert[\"type\"], number> = {\n missed: 0xb7791f,\n failed: 0xc62828,\n stuck: 0xc62828,\n slow: 0xb7791f,\n over_budget: 0xb7791f,\n recovered: 0x1f8a4c,\n};\n\nconst TIMEOUT_MS = 10_000;\n\n/** The longest embed description Discord takes. The title (under 256) and it stay well inside the embed's 6000. */\nexport const DESCRIPTION_MAX = 4096;\n\n/** Sends alerts to a Discord channel through a webhook. */\nexport function discord(options: DiscordOptions): AlertChannel {\n if (!options.webhookUrl) throw new Error(\"discord() needs a webhookUrl\");\n return {\n name: \"discord\",\n async send(alert) {\n const url = options.link?.(alert);\n const response = await fetch(postable(options.webhookUrl), {\n method: \"POST\",\n headers: { \"content-type\": \"application/json\" },\n // Refused, not followed: a webhook URL is its own credential.\n redirect: \"error\",\n signal: AbortSignal.timeout(TIMEOUT_MS),\n body: JSON.stringify({\n content: alert.title,\n // Job output can hold anything, \"@everyone\" included; ping no one.\n allowed_mentions: { parse: [] },\n embeds: [\n {\n title: alert.title,\n ...(url ? { url } : {}),\n description: embedDescription(alert),\n color: COLOR[alert.type],\n timestamp: new Date(alert.at).toISOString(),\n },\n ],\n }),\n });\n if (!response.ok) throw new Error(`Discord webhook answered ${response.status}: ${(await response.text()).slice(0, 200)}`);\n },\n };\n}\n\n/**\n * The message in a code block, then the triage. Each part has its own cap,\n * and escaping can grow both, so the whole is held to DESCRIPTION_MAX (in\n * UTF-16 units) by cutting the message's block, never the triage: Discord\n * refuses a longer one on every retry.\n */\nexport function embedDescription(alert: Alert): string {\n const triage = alert.triage ? `\\n**Triage:** ${escapeMarkdown(alert.triage.slice(0, 1000))}` : \"\";\n const fences = \"```\\n\".length + \"\\n```\".length;\n return \"```\\n\" + cut(codeBlockSafe(alert.message.slice(0, 3800)), DESCRIPTION_MAX - fences - triage.length) + \"\\n```\" + triage;\n}\n\n/** Breaks up ``` so text inside a code block cannot close it. */\nexport function codeBlockSafe(text: string): string {\n return text.replace(/```/g, \"```\");\n}\n\n/** Escapes the characters Discord reads as markdown, links included. */\nexport function escapeMarkdown(text: string): string {\n return text.replace(/[\\\\`*_~|[\\]()<>]/g, \"\\\\$&\");\n}\n","// @cronwatch/sdk/discord: what the entry point exports. The helpers the\n// channel is built from are internal; the names below are kept, deprecated,\n// for anyone who imported them, and are no longer exported from 1.0.\nimport * as internal from \"../alerts/discord.js\";\n\nexport { discord } from \"../alerts/discord.js\";\nexport type { DiscordOptions } from \"../alerts/discord.js\";\n\n/** @deprecated Internal to the Discord channel; no longer exported from 1.0. */\nexport const DESCRIPTION_MAX = internal.DESCRIPTION_MAX;\n/** @deprecated Internal to the Discord channel; no longer exported from 1.0. */\nexport const embedDescription = internal.embedDescription;\n/** @deprecated Internal to the Discord channel; no longer exported from 1.0. */\nexport const codeBlockSafe = internal.codeBlockSafe;\n/** @deprecated Internal to the Discord channel; no longer exported from 1.0. */\nexport const escapeMarkdown = internal.escapeMarkdown;\n"]}
|
package/dist/honeybadger.d.cts
CHANGED
package/dist/honeybadger.d.ts
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -80,13 +80,46 @@ function beyondDates(at) {
|
|
|
80
80
|
function readEnv(name) {
|
|
81
81
|
return typeof process === "undefined" || !process.env ? void 0 : process.env[name];
|
|
82
82
|
}
|
|
83
|
+
function readSecretEnv(name) {
|
|
84
|
+
const value = readEnv(name);
|
|
85
|
+
return value === void 0 || value.trim() === "" ? void 0 : value;
|
|
86
|
+
}
|
|
87
|
+
function secretOption(value, what) {
|
|
88
|
+
if (value === void 0 || value === null) return value;
|
|
89
|
+
if (typeof value !== "string") {
|
|
90
|
+
throw new TypeError(`${what} must be a string, or null to opt out, not ${Array.isArray(value) ? "an array" : typeof value}`);
|
|
91
|
+
}
|
|
92
|
+
return value.trim() === "" ? void 0 : value;
|
|
93
|
+
}
|
|
94
|
+
function environment() {
|
|
95
|
+
if (typeof process === "undefined" || !process.env) return "";
|
|
96
|
+
for (const value of [process.env.CRONWATCH_ENV, process.env.APP_ENV, process.env.NODE_ENV]) {
|
|
97
|
+
const name = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
98
|
+
if (name === "") continue;
|
|
99
|
+
if (name === "prod") return "production";
|
|
100
|
+
if (name === "dev" || name === "local" || name === "test" || name === "testing") return "development";
|
|
101
|
+
return name;
|
|
102
|
+
}
|
|
103
|
+
return "";
|
|
104
|
+
}
|
|
83
105
|
function isDevelopment() {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
106
|
+
return environment() === "development";
|
|
107
|
+
}
|
|
108
|
+
function isProduction() {
|
|
109
|
+
return environment() === "production";
|
|
87
110
|
}
|
|
88
111
|
var cache = /* @__PURE__ */ new Map();
|
|
89
112
|
var crons = /* @__PURE__ */ new WeakMap();
|
|
113
|
+
var never = /* @__PURE__ */ new WeakSet();
|
|
114
|
+
var PROBE_FROM_MS = Date.UTC(2e3, 0, 1);
|
|
115
|
+
function neverFires(cron) {
|
|
116
|
+
try {
|
|
117
|
+
return cron.nextRuns(1, new Date(PROBE_FROM_MS)).length === 0;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
return error instanceof RangeError && /call stack/i.test(error.message);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
var ONE_TIME_DATE = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}/;
|
|
90
123
|
var EARLY_SLACK_MS = 6e4;
|
|
91
124
|
function parseSchedule(schedule, timezone) {
|
|
92
125
|
const key = `${timezone ?? ""}|${schedule}`;
|
|
@@ -100,6 +133,10 @@ function parseSchedule(schedule, timezone) {
|
|
|
100
133
|
if (everyMs < 1e3) throw new Error(`schedule "${schedule}" is shorter than one second`);
|
|
101
134
|
parsed = { kind: "interval", source: text, everyMs };
|
|
102
135
|
} else {
|
|
136
|
+
if (text.slice(1).includes(":")) {
|
|
137
|
+
const reason = ONE_TIME_DATE.test(text) ? "CronPattern: a one-time date is not supported" : "Invalid ISO8601 passed to timezone parser.";
|
|
138
|
+
throw new Error(`schedule "${schedule}" is not a cron expression or "every <duration>": ${reason}`);
|
|
139
|
+
}
|
|
103
140
|
let cron;
|
|
104
141
|
try {
|
|
105
142
|
cron = new croner.Cron(text, timezone ? { timezone } : {});
|
|
@@ -108,6 +145,7 @@ function parseSchedule(schedule, timezone) {
|
|
|
108
145
|
}
|
|
109
146
|
parsed = { kind: "cron", source: text, ...timezone ? { timezone } : {} };
|
|
110
147
|
crons.set(parsed, cron);
|
|
148
|
+
if (neverFires(cron)) never.add(parsed);
|
|
111
149
|
}
|
|
112
150
|
cache.set(key, parsed);
|
|
113
151
|
return parsed;
|
|
@@ -134,6 +172,7 @@ function countFrom(from) {
|
|
|
134
172
|
function fireAfter(parsed, from) {
|
|
135
173
|
const cron = crons.get(parsed);
|
|
136
174
|
if (!cron) throw new Error(`schedule "${parsed.source}" was not made by parseSchedule`);
|
|
175
|
+
if (never.has(parsed)) return null;
|
|
137
176
|
const start = countFrom(from);
|
|
138
177
|
if (start === null) return null;
|
|
139
178
|
let probe = start;
|
|
@@ -149,6 +188,7 @@ function fireAfter(parsed, from) {
|
|
|
149
188
|
function firesBetween(parsed, from, to, limit) {
|
|
150
189
|
const cron = crons.get(parsed);
|
|
151
190
|
if (!cron) throw new Error(`schedule "${parsed.source}" was not made by parseSchedule`);
|
|
191
|
+
if (never.has(parsed)) return [];
|
|
152
192
|
const out = [];
|
|
153
193
|
const start = countFrom(from);
|
|
154
194
|
if (start === null) return out;
|
|
@@ -283,16 +323,24 @@ function failureCount(state) {
|
|
|
283
323
|
function emptyState(job) {
|
|
284
324
|
return { job, open: {}, consecutiveFailures: 0, silencedUntil: null, lastAlertAt: null, pendingRecovery: [], undelivered: [] };
|
|
285
325
|
}
|
|
326
|
+
function isObject(value) {
|
|
327
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
328
|
+
}
|
|
286
329
|
function normalizeState(state, job) {
|
|
287
|
-
if (!state) return emptyState(job);
|
|
330
|
+
if (!isObject(state)) return emptyState(job);
|
|
288
331
|
const { sending, ...rest } = state;
|
|
332
|
+
const open = isObject(state.open) ? Object.fromEntries(Object.entries(state.open).filter(([, at]) => typeof at === "number")) : {};
|
|
333
|
+
const number = (value) => typeof value === "number" ? value : null;
|
|
334
|
+
const list = (value) => Array.isArray(value) ? value : [];
|
|
289
335
|
return {
|
|
290
336
|
...emptyState(job),
|
|
291
337
|
...rest,
|
|
292
|
-
open
|
|
338
|
+
open,
|
|
293
339
|
consecutiveFailures: failureCount(state),
|
|
294
|
-
|
|
295
|
-
|
|
340
|
+
silencedUntil: number(state.silencedUntil),
|
|
341
|
+
lastAlertAt: number(state.lastAlertAt),
|
|
342
|
+
pendingRecovery: list(state.pendingRecovery).filter((c) => typeof c === "string"),
|
|
343
|
+
undelivered: list(state.undelivered).filter((alert) => isObject(alert)),
|
|
296
344
|
...Array.isArray(sending) && sending.length > 0 ? { sending: [...sending] } : {}
|
|
297
345
|
};
|
|
298
346
|
}
|
|
@@ -496,8 +544,12 @@ function applySilence(previous, evaluation, now) {
|
|
|
496
544
|
return { state: muteOpens(previous, evaluation.state), alerts: [] };
|
|
497
545
|
}
|
|
498
546
|
function staleAlert(alert, state) {
|
|
499
|
-
if (alert.type === "recovered")
|
|
500
|
-
|
|
547
|
+
if (alert.type === "recovered") {
|
|
548
|
+
const after2 = isObject(alert.details) ? alert.details.after : void 0;
|
|
549
|
+
if (!Array.isArray(after2)) return true;
|
|
550
|
+
return after2.some((condition) => typeof condition !== "string" || state.open[condition] !== void 0);
|
|
551
|
+
}
|
|
552
|
+
return typeof alert.at !== "number" || state.open[alert.type] !== alert.at;
|
|
501
553
|
}
|
|
502
554
|
function jobHealth(def, lastRun, state, now) {
|
|
503
555
|
const open = openConditions(state);
|
|
@@ -810,6 +862,15 @@ function checkExpectation(expect, output) {
|
|
|
810
862
|
}
|
|
811
863
|
return ok ? null : "Output did not pass the expect() check";
|
|
812
864
|
}
|
|
865
|
+
function readStoredJob(stored) {
|
|
866
|
+
const definition = stored.definition;
|
|
867
|
+
if (typeof definition !== "object" || definition === null || Array.isArray(definition)) {
|
|
868
|
+
return { job: { ...stored, definition: { name: stored.name } }, readable: false };
|
|
869
|
+
}
|
|
870
|
+
const read = { ...definition };
|
|
871
|
+
if ("tags" in read && !(Array.isArray(read.tags) && read.tags.every((tag) => typeof tag === "string"))) delete read.tags;
|
|
872
|
+
return { job: { ...stored, definition: read }, readable: true };
|
|
873
|
+
}
|
|
813
874
|
|
|
814
875
|
// src/routes/escape.ts
|
|
815
876
|
function escapeHtml(value) {
|
|
@@ -1581,12 +1642,18 @@ function jobPage(job, runs, now, base, complete = true) {
|
|
|
1581
1642
|
return layout(`${job.name}: CronWatch`, body, base, { refresh: 60 });
|
|
1582
1643
|
}
|
|
1583
1644
|
function messagePage(title, message, base, signIn = false) {
|
|
1584
|
-
const form = signIn ? `<form class="signin" method="
|
|
1645
|
+
const form = signIn ? `<form class="signin" method="post" action="${h(base)}/signin"><label for="token">Token</label><input id="token" name="token" type="password" autocomplete="current-password" autocapitalize="off" spellcheck="false" required><button class="primary" type="submit">Sign in</button></form>` : "";
|
|
1585
1646
|
return layout(title, `<header class="top">${brand(base)}</header><main class="message"><h1>${h(title)}</h1><p>${h(message)}</p>${form}</main>`, base);
|
|
1586
1647
|
}
|
|
1587
1648
|
|
|
1649
|
+
// src/version.ts
|
|
1650
|
+
var VERSION = "0.11.0";
|
|
1651
|
+
var API_VERSION = 1;
|
|
1652
|
+
|
|
1588
1653
|
// src/routes/index.ts
|
|
1589
1654
|
var COOKIE = "cronwatch_token";
|
|
1655
|
+
var LIBRARY = "@cronwatch/sdk";
|
|
1656
|
+
var LANGUAGE = "typescript";
|
|
1590
1657
|
async function cookieValue(token) {
|
|
1591
1658
|
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(`cronwatch-cookie:${token}`));
|
|
1592
1659
|
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
@@ -1670,6 +1737,20 @@ function forwardedOrigin(request, url) {
|
|
|
1670
1737
|
return url.origin;
|
|
1671
1738
|
}
|
|
1672
1739
|
}
|
|
1740
|
+
function bearerToken(header) {
|
|
1741
|
+
const match = header === null ? null : /^Bearer\s+([\s\S]*)$/i.exec(header);
|
|
1742
|
+
return match ? match[1] : null;
|
|
1743
|
+
}
|
|
1744
|
+
function signInReturn(referer, publicOrigin, base) {
|
|
1745
|
+
if (referer === null || !referer.startsWith(publicOrigin + "/")) return `${base}/`;
|
|
1746
|
+
let url;
|
|
1747
|
+
try {
|
|
1748
|
+
url = new URL(referer);
|
|
1749
|
+
} catch {
|
|
1750
|
+
return `${base}/`;
|
|
1751
|
+
}
|
|
1752
|
+
return url.searchParams.has("token") ? `${base}/` : referer;
|
|
1753
|
+
}
|
|
1673
1754
|
function stripBase(pathname, base) {
|
|
1674
1755
|
let path = pathname.startsWith(base) ? pathname.slice(base.length) : pathname;
|
|
1675
1756
|
if (path === "") path = "/";
|
|
@@ -1712,8 +1793,12 @@ function runsLimit(value) {
|
|
|
1712
1793
|
return Number.isFinite(n) ? Math.min(MAX_RUNS, Math.max(1, n)) : DEFAULT_RUNS;
|
|
1713
1794
|
}
|
|
1714
1795
|
function createRoutes(cw, options = {}) {
|
|
1796
|
+
return cw.routes(options);
|
|
1797
|
+
}
|
|
1798
|
+
function buildRoutes(cw, options = {}) {
|
|
1715
1799
|
const optedOut = options.token === null;
|
|
1716
|
-
const
|
|
1800
|
+
const given = secretOption(options.token, "routes: token");
|
|
1801
|
+
const configured = optedOut ? null : given || readSecretEnv("CRONWATCH_TOKEN") || null;
|
|
1717
1802
|
const base = (options.basePath ?? "/cronwatch").replace(/\/+$/, "");
|
|
1718
1803
|
const developing = isDevelopment();
|
|
1719
1804
|
const fixedOrigin = configuredOrigin(options.origin);
|
|
@@ -1745,7 +1830,17 @@ function createRoutes(cw, options = {}) {
|
|
|
1745
1830
|
if (method !== "GET" && method !== "HEAD" && crossSite(request, publicOrigin)) {
|
|
1746
1831
|
return wantsHtml ? html(messagePage("Cross-site request refused", "Changes can only be made from the dashboard itself.", base), 403) : api({ ok: false, error: "Cross-site request refused" }, 403);
|
|
1747
1832
|
}
|
|
1748
|
-
const bearer = request.headers.get("authorization")
|
|
1833
|
+
const bearer = bearerToken(request.headers.get("authorization"));
|
|
1834
|
+
const signInCookie = async () => {
|
|
1835
|
+
const secure = publicOrigin.startsWith("https:") ? "; Secure" : "";
|
|
1836
|
+
return `${COOKIE}=${await expectedCookie(token)}; Path=${base || "/"}; HttpOnly; SameSite=Lax; Max-Age=${60 * 60 * 24 * 30}${secure}`;
|
|
1837
|
+
};
|
|
1838
|
+
const signInPage = () => generated ? html(messagePage("Sign in", "CRONWATCH_TOKEN is not set, so this development server made a token. The sign-in link is in the server log: open it once, or enter the token from it below, and this browser stays signed in.", base, true), 401) : html(messagePage("Sign in", "Enter your CRONWATCH_TOKEN and this browser stays signed in.", base, true), 401);
|
|
1839
|
+
if (token && method === "POST" && path === "/signin") {
|
|
1840
|
+
const sent = (await readBody(request)).token;
|
|
1841
|
+
if (sent === void 0 || !constantTimeEqual(sent, token)) return signInPage();
|
|
1842
|
+
return redirect(signInReturn(request.headers.get("referer"), publicOrigin, base), { "set-cookie": await signInCookie() });
|
|
1843
|
+
}
|
|
1749
1844
|
if (token) {
|
|
1750
1845
|
const query = wantsHtml && method === "GET" ? url.searchParams.get("token") : null;
|
|
1751
1846
|
const sent = readCookie(request, COOKIE);
|
|
@@ -1753,17 +1848,12 @@ function createRoutes(cw, options = {}) {
|
|
|
1753
1848
|
const cronSecretOk = isCheck && bearer !== null && cw.cronSecret !== null && constantTimeEqual(bearer, cw.cronSecret);
|
|
1754
1849
|
const tokenOk = bearer !== null ? constantTimeEqual(bearer, token) : query !== null ? constantTimeEqual(query, token) : sent !== null && constantTimeEqual(sent, await expectedCookie(token));
|
|
1755
1850
|
if (!cronSecretOk && !tokenOk) {
|
|
1756
|
-
if (
|
|
1757
|
-
|
|
1758
|
-
}
|
|
1759
|
-
return wantsHtml ? html(messagePage("Sign in", `Open this page with ?token=<your CRONWATCH_TOKEN> once and it will stay signed in.`, base, true), 401) : api({ ok: false, error: "Unauthorized" }, 401);
|
|
1851
|
+
if (wantsHtml) return signInPage();
|
|
1852
|
+
return generated ? api({ ok: false, error: "Unauthorized: CRONWATCH_TOKEN is not set, so this development server made a token; it is in the server log" }, 401) : api({ ok: false, error: "Unauthorized" }, 401);
|
|
1760
1853
|
}
|
|
1761
1854
|
if (query !== null) {
|
|
1762
1855
|
url.searchParams.delete("token");
|
|
1763
|
-
|
|
1764
|
-
return redirect(url.pathname + (url.search || ""), {
|
|
1765
|
-
"set-cookie": `${COOKIE}=${await expectedCookie(token)}; Path=${base || "/"}; HttpOnly; SameSite=Lax; Max-Age=${60 * 60 * 24 * 30}${secure}`
|
|
1766
|
-
});
|
|
1856
|
+
return redirect(url.pathname + (url.search || ""), { "set-cookie": await signInCookie() });
|
|
1767
1857
|
}
|
|
1768
1858
|
}
|
|
1769
1859
|
const redirectBack = () => {
|
|
@@ -1814,6 +1904,9 @@ function createRoutes(cw, options = {}) {
|
|
|
1814
1904
|
}
|
|
1815
1905
|
if (parts[0] === "api") {
|
|
1816
1906
|
const rest = parts.slice(1);
|
|
1907
|
+
if (method === "GET" && rest.length === 0) {
|
|
1908
|
+
return api({ ok: true, library: LIBRARY, language: LANGUAGE, version: VERSION, api: API_VERSION });
|
|
1909
|
+
}
|
|
1817
1910
|
if (method === "GET" && rest[0] === "jobs" && rest.length === 1) {
|
|
1818
1911
|
return api({ ok: true, jobs: await cw.jobs() });
|
|
1819
1912
|
}
|
|
@@ -1841,9 +1934,13 @@ function createRoutes(cw, options = {}) {
|
|
|
1841
1934
|
} catch (e) {
|
|
1842
1935
|
return api({ ok: false, error: e.message }, 400);
|
|
1843
1936
|
}
|
|
1844
|
-
|
|
1937
|
+
await cw.silence(name, duration);
|
|
1938
|
+
return api({ ok: true, job: await cw.jobSummary(name) });
|
|
1939
|
+
}
|
|
1940
|
+
if (rest[2] === "unsilence") {
|
|
1941
|
+
await cw.unsilence(name);
|
|
1942
|
+
return api({ ok: true, job: await cw.jobSummary(name) });
|
|
1845
1943
|
}
|
|
1846
|
-
if (rest[2] === "unsilence") return api({ ok: true, state: await cw.unsilence(name) });
|
|
1847
1944
|
}
|
|
1848
1945
|
if (rest[0] === "check" && rest.length === 1) {
|
|
1849
1946
|
if (method === "GET" && bearer === null) {
|
|
@@ -2051,7 +2148,7 @@ function validateDefinition(def) {
|
|
|
2051
2148
|
function sameState(a, b) {
|
|
2052
2149
|
return JSON.stringify(a) === JSON.stringify(b);
|
|
2053
2150
|
}
|
|
2054
|
-
var
|
|
2151
|
+
var Cronwatch = class {
|
|
2055
2152
|
store;
|
|
2056
2153
|
alerts;
|
|
2057
2154
|
triage;
|
|
@@ -2064,7 +2161,7 @@ var CronWatch = class {
|
|
|
2064
2161
|
/** cronSecret was passed as null: handlers may run without a secret. */
|
|
2065
2162
|
secretOptOut;
|
|
2066
2163
|
redact;
|
|
2067
|
-
/** "check": queue alerts for another process's check instead of sending them. See
|
|
2164
|
+
/** "check": queue alerts for another process's check instead of sending them. See CronwatchOptions.deliver. */
|
|
2068
2165
|
deferDelivery;
|
|
2069
2166
|
defaults;
|
|
2070
2167
|
definitions = /* @__PURE__ */ new Map();
|
|
@@ -2083,12 +2180,15 @@ var CronWatch = class {
|
|
|
2083
2180
|
usingDefaultStore = false;
|
|
2084
2181
|
warnedNoSecret = false;
|
|
2085
2182
|
warnedDeferredStart = false;
|
|
2183
|
+
/** Stored jobs read with a definition that was not a JSON object (see readJob). */
|
|
2184
|
+
unreadable = /* @__PURE__ */ new WeakSet();
|
|
2086
2185
|
constructor(options = {}) {
|
|
2087
2186
|
this.store = options.store ?? (this.usingDefaultStore = true, memory());
|
|
2088
2187
|
this.alerts = options.alerts ?? [consoleChannel()];
|
|
2089
2188
|
this.triage = options.triage;
|
|
2090
2189
|
this.sources = options.sources ?? [];
|
|
2091
|
-
const
|
|
2190
|
+
const given = secretOption(options.cronSecret, "cronSecret");
|
|
2191
|
+
const secret = options.cronSecret === void 0 ? readSecretEnv("CRON_SECRET") : given;
|
|
2092
2192
|
this.cronSecret = secret ? secret : null;
|
|
2093
2193
|
this.secretOptOut = options.cronSecret === null;
|
|
2094
2194
|
this.retentionMs = parseDuration(options.retention ?? "30d", "retention");
|
|
@@ -2149,7 +2249,7 @@ var CronWatch = class {
|
|
|
2149
2249
|
return outcome.result;
|
|
2150
2250
|
},
|
|
2151
2251
|
handler(fn, options) {
|
|
2152
|
-
const own = options?.secret;
|
|
2252
|
+
const own = secretOption(options?.secret, "handler: secret");
|
|
2153
2253
|
const secret = own === null ? null : own ? own : self.cronSecret;
|
|
2154
2254
|
const optedOut = own === null || !own && self.secretOptOut;
|
|
2155
2255
|
return async (request) => {
|
|
@@ -2201,7 +2301,7 @@ var CronWatch = class {
|
|
|
2201
2301
|
if (!this.ready) {
|
|
2202
2302
|
this.ready = (async () => {
|
|
2203
2303
|
if (this.store.init) await this.store.init();
|
|
2204
|
-
if (this.usingDefaultStore &&
|
|
2304
|
+
if (this.usingDefaultStore && isProduction()) {
|
|
2205
2305
|
console.warn("[cronwatch] using the in-memory store: runs and state are lost on restart. Pass a store from @cronwatch/sdk/sqlite or @cronwatch/sdk/postgres.");
|
|
2206
2306
|
}
|
|
2207
2307
|
})().catch((error) => {
|
|
@@ -2251,13 +2351,23 @@ var CronWatch = class {
|
|
|
2251
2351
|
const jobs = await this.store.listJobs();
|
|
2252
2352
|
const listed = new Set(jobs.map((job) => job.name));
|
|
2253
2353
|
const missing = [...this.definitions.values()].filter((definition) => !listed.has(definition.name));
|
|
2254
|
-
if (missing.length === 0) return jobs;
|
|
2354
|
+
if (missing.length === 0) return jobs.map((job) => this.readJob(job));
|
|
2255
2355
|
for (const definition of missing) {
|
|
2256
2356
|
if (this.definitions.get(definition.name) !== definition) continue;
|
|
2257
2357
|
this.synced.delete(definition.name);
|
|
2258
2358
|
await this.sync(definition);
|
|
2259
2359
|
}
|
|
2260
|
-
return this.store.listJobs();
|
|
2360
|
+
return (await this.store.listJobs()).map((job) => this.readJob(job));
|
|
2361
|
+
}
|
|
2362
|
+
/** A stored job read leniently (readStoredJob); one whose definition is unreadable is remembered, so that evaluating it throws. */
|
|
2363
|
+
readJob(stored) {
|
|
2364
|
+
const { job, readable } = readStoredJob(stored);
|
|
2365
|
+
if (!readable) this.unreadable.add(job);
|
|
2366
|
+
return job;
|
|
2367
|
+
}
|
|
2368
|
+
/** Throws for a job whose stored definition was not a JSON object: reported, and shown as failing, while the others carry on. */
|
|
2369
|
+
evaluable(stored) {
|
|
2370
|
+
if (this.unreadable.has(stored)) throw new Error(`job "${stored.name}": its stored definition is not a JSON object`);
|
|
2261
2371
|
}
|
|
2262
2372
|
/**
|
|
2263
2373
|
* Runs `fn` after every earlier state update for the same job has settled,
|
|
@@ -2676,6 +2786,10 @@ ${id}`;
|
|
|
2676
2786
|
async recordRun(input, options = {}) {
|
|
2677
2787
|
const declared = this.definitions.get(input.job);
|
|
2678
2788
|
if (!declared) throw new Error(`recordRun: job "${input.job}" is not declared; call job() first`);
|
|
2789
|
+
if (typeof input.id !== "string" || input.id.length === 0 || input.id.length > MAX_RUN_ID) {
|
|
2790
|
+
const got = typeof input.id === "string" ? `${input.id.length} characters` : typeof input.id;
|
|
2791
|
+
throw new Error(`recordRun: run ids must be 1 to ${MAX_RUN_ID} characters (got ${got}; job "${input.job}")`);
|
|
2792
|
+
}
|
|
2679
2793
|
if (input.id.includes("\0")) throw new Error(`recordRun: run ids cannot contain a NUL character (job "${input.job}")`);
|
|
2680
2794
|
for (const [metric, value] of Object.entries(input.metrics ?? {})) {
|
|
2681
2795
|
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
@@ -2804,7 +2918,10 @@ ${id}`;
|
|
|
2804
2918
|
for (const listed of await this.store.runningRuns()) {
|
|
2805
2919
|
try {
|
|
2806
2920
|
const declared = this.definitions.get(listed.job);
|
|
2807
|
-
const
|
|
2921
|
+
const found = declared ? null : await this.store.getJob(listed.job);
|
|
2922
|
+
const stored = found ? this.readJob(found) : null;
|
|
2923
|
+
if (stored) this.evaluable(stored);
|
|
2924
|
+
const definition = declared ? toStored(declared) : stored?.definition;
|
|
2808
2925
|
if (!definition || !isStuck(definition, listed, now)) continue;
|
|
2809
2926
|
const run = await this.store.getRun(listed.id);
|
|
2810
2927
|
if (!run || run.status !== "running" || run.job !== listed.job) continue;
|
|
@@ -2823,6 +2940,7 @@ ${id}`;
|
|
|
2823
2940
|
const retries = { spentMs: 0 };
|
|
2824
2941
|
for (const stored of await this.storedJobs()) {
|
|
2825
2942
|
try {
|
|
2943
|
+
this.evaluable(stored);
|
|
2826
2944
|
const recent = await this.store.listRuns(stored.name, BASELINE_WINDOW);
|
|
2827
2945
|
let nextExpectedAt = null;
|
|
2828
2946
|
const { state, result: held } = await this.updateState(stored.name, (previous) => {
|
|
@@ -2858,6 +2976,7 @@ ${id}`;
|
|
|
2858
2976
|
let recent = [];
|
|
2859
2977
|
try {
|
|
2860
2978
|
recent = await this.store.listRuns(stored.name, Math.max(runs, BASELINE_WINDOW));
|
|
2979
|
+
this.evaluable(stored);
|
|
2861
2980
|
const state = await this.readState(stored.name);
|
|
2862
2981
|
const { nextExpectedAt } = onCheck(stored.definition, stored, recent[0] ?? null, state, now);
|
|
2863
2982
|
return { job: summarize(stored, recent, state, nextExpectedAt, now), runs: recent.slice(0, runs) };
|
|
@@ -2892,7 +3011,7 @@ ${id}`;
|
|
|
2892
3011
|
if (definition) await this.sync(definition, true);
|
|
2893
3012
|
const stored = await this.store.getJob(name);
|
|
2894
3013
|
if (!stored) return null;
|
|
2895
|
-
return (await this.snapshot(stored, this.now(), 0)).job;
|
|
3014
|
+
return (await this.snapshot(this.readJob(stored), this.now(), 0)).job;
|
|
2896
3015
|
}
|
|
2897
3016
|
/** A job's runs, newest first. `limit` is a whole number from 1 to 500. */
|
|
2898
3017
|
async runs(name, limit = 50) {
|
|
@@ -2939,21 +3058,27 @@ ${id}`;
|
|
|
2939
3058
|
this.synced.delete(name);
|
|
2940
3059
|
await this.store.deleteJob(name);
|
|
2941
3060
|
}
|
|
2942
|
-
/**
|
|
3061
|
+
/**
|
|
3062
|
+
* The dashboard and JSON API as fetch-style handlers: the one way to mount
|
|
3063
|
+
* them. A Next.js app exports them from app/cronwatch/[[...path]]/route.ts:
|
|
3064
|
+
*
|
|
3065
|
+
* export const { GET, POST, DELETE } = cw.routes();
|
|
3066
|
+
*/
|
|
2943
3067
|
routes(options) {
|
|
2944
|
-
return
|
|
3068
|
+
return buildRoutes(this, options);
|
|
2945
3069
|
}
|
|
2946
3070
|
/**
|
|
2947
3071
|
* Check on an interval, for long-running servers. Default every minute.
|
|
2948
3072
|
* Not for serverless functions or Cloudflare Workers, where nothing runs
|
|
2949
|
-
* between requests: call check() from a cron there instead.
|
|
3073
|
+
* between requests: call check() from a cron there instead. (A job's
|
|
3074
|
+
* start() opens a run; this starts the checks.)
|
|
2950
3075
|
*/
|
|
2951
|
-
|
|
3076
|
+
startChecking(every = "1m") {
|
|
2952
3077
|
if (this.timer) return;
|
|
2953
3078
|
const ms = Math.min(TIMER_MAX_MS, Math.max(5e3, parseDuration(every, "check interval")));
|
|
2954
3079
|
if (this.deferDelivery && !this.warnedDeferredStart) {
|
|
2955
3080
|
this.warnedDeferredStart = true;
|
|
2956
|
-
console.warn('[cronwatch]
|
|
3081
|
+
console.warn('[cronwatch] startChecking() was called with deliver: "check", so these checks send no alerts. Another process must run checks with deliver: "now" (the default) to send them.');
|
|
2957
3082
|
}
|
|
2958
3083
|
const tick = () => this.check().catch((e) => this.onError(e, "check"));
|
|
2959
3084
|
this.timer = setInterval(tick, ms);
|
|
@@ -2964,6 +3089,15 @@ ${id}`;
|
|
|
2964
3089
|
}, 1e3);
|
|
2965
3090
|
if (typeof this.firstTick === "object" && "unref" in this.firstTick) this.firstTick.unref();
|
|
2966
3091
|
}
|
|
3092
|
+
/**
|
|
3093
|
+
* startChecking(), under its former name.
|
|
3094
|
+
* @deprecated Renamed `startChecking`, since a job's `start()` opens a run.
|
|
3095
|
+
* This name still works through 1.x and goes in 2.0.
|
|
3096
|
+
*/
|
|
3097
|
+
start(every = "1m") {
|
|
3098
|
+
this.startChecking(every);
|
|
3099
|
+
}
|
|
3100
|
+
/** Stop the checks startChecking() began. */
|
|
2967
3101
|
stop() {
|
|
2968
3102
|
if (this.timer) clearInterval(this.timer);
|
|
2969
3103
|
if (this.firstTick) clearTimeout(this.firstTick);
|
|
@@ -3076,9 +3210,10 @@ ${id}`;
|
|
|
3076
3210
|
}
|
|
3077
3211
|
};
|
|
3078
3212
|
var RESERVED_RUN_ID_PREFIX = "pgcron:";
|
|
3213
|
+
var MAX_RUN_ID = 200;
|
|
3079
3214
|
function checkRunId(job, id, method) {
|
|
3080
|
-
if (typeof id !== "string" || id.length === 0 || id.length >
|
|
3081
|
-
throw new Error(`job "${job}": ${method}() needs a run id of 1 to
|
|
3215
|
+
if (typeof id !== "string" || id.length === 0 || id.length > MAX_RUN_ID) {
|
|
3216
|
+
throw new Error(`job "${job}": ${method}() needs a run id of 1 to ${MAX_RUN_ID} characters (got ${typeof id === "string" ? `${id.length} characters` : typeof id})`);
|
|
3082
3217
|
}
|
|
3083
3218
|
if (id.includes("\0")) throw new Error(`job "${job}": ${method}() cannot take a run id containing a NUL character`);
|
|
3084
3219
|
if (id.startsWith(RESERVED_RUN_ID_PREFIX)) {
|
|
@@ -3139,13 +3274,16 @@ function custom(name, send) {
|
|
|
3139
3274
|
await send(alert);
|
|
3140
3275
|
} };
|
|
3141
3276
|
}
|
|
3277
|
+
var CronWatch = Cronwatch;
|
|
3142
3278
|
|
|
3143
3279
|
// src/index.ts
|
|
3144
3280
|
function cronwatch(options = {}) {
|
|
3145
|
-
return new
|
|
3281
|
+
return new Cronwatch(options);
|
|
3146
3282
|
}
|
|
3147
3283
|
|
|
3148
3284
|
exports.CronWatch = CronWatch;
|
|
3285
|
+
exports.Cronwatch = Cronwatch;
|
|
3286
|
+
exports.VERSION = VERSION;
|
|
3149
3287
|
exports.composeAlert = composeAlert;
|
|
3150
3288
|
exports.consoleChannel = consoleChannel;
|
|
3151
3289
|
exports.createRoutes = createRoutes;
|