@cronwatch/sdk 0.9.0 → 0.10.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.
Files changed (67) hide show
  1. package/dist/anthropic.d.cts +1 -1
  2. package/dist/anthropic.d.ts +1 -1
  3. package/dist/bugsnag.d.cts +1 -1
  4. package/dist/bugsnag.d.ts +1 -1
  5. package/dist/{client-CQm-8-mR.d.ts → client-BcIjVBiZ.d.ts} +68 -13
  6. package/dist/{client-D35cfnNV.d.cts → client-DUoSea7X.d.cts} +68 -13
  7. package/dist/d1.cjs.map +1 -1
  8. package/dist/d1.d.cts +1 -1
  9. package/dist/d1.d.ts +1 -1
  10. package/dist/d1.js.map +1 -1
  11. package/dist/datadog.d.cts +1 -1
  12. package/dist/datadog.d.ts +1 -1
  13. package/dist/discord.cjs +17 -2
  14. package/dist/discord.cjs.map +1 -1
  15. package/dist/discord.d.cts +11 -2
  16. package/dist/discord.d.ts +11 -2
  17. package/dist/discord.js +16 -3
  18. package/dist/discord.js.map +1 -1
  19. package/dist/{email-D1d5niMy.d.cts → email-JZs3vxwq.d.cts} +1 -1
  20. package/dist/{email-BxCp9RQV.d.ts → email-VrGsVz43.d.ts} +1 -1
  21. package/dist/honeybadger.d.cts +1 -1
  22. package/dist/honeybadger.d.ts +1 -1
  23. package/dist/index.cjs +207 -88
  24. package/dist/index.cjs.map +1 -1
  25. package/dist/index.d.cts +4 -4
  26. package/dist/index.d.ts +4 -4
  27. package/dist/index.js +207 -88
  28. package/dist/index.js.map +1 -1
  29. package/dist/mailgun.d.cts +2 -2
  30. package/dist/mailgun.d.ts +2 -2
  31. package/dist/newrelic.d.cts +1 -1
  32. package/dist/newrelic.d.ts +1 -1
  33. package/dist/pg-cron.cjs +2 -1
  34. package/dist/pg-cron.cjs.map +1 -1
  35. package/dist/pg-cron.d.cts +2 -2
  36. package/dist/pg-cron.d.ts +2 -2
  37. package/dist/pg-cron.js +2 -1
  38. package/dist/pg-cron.js.map +1 -1
  39. package/dist/postgres.cjs.map +1 -1
  40. package/dist/postgres.d.cts +1 -1
  41. package/dist/postgres.d.ts +1 -1
  42. package/dist/postgres.js.map +1 -1
  43. package/dist/postmark.d.cts +2 -2
  44. package/dist/postmark.d.ts +2 -2
  45. package/dist/resend.d.cts +2 -2
  46. package/dist/resend.d.ts +2 -2
  47. package/dist/rollbar.d.cts +1 -1
  48. package/dist/rollbar.d.ts +1 -1
  49. package/dist/sendgrid.d.cts +2 -2
  50. package/dist/sendgrid.d.ts +2 -2
  51. package/dist/sentry.d.cts +1 -1
  52. package/dist/sentry.d.ts +1 -1
  53. package/dist/ses.d.cts +2 -2
  54. package/dist/ses.d.ts +2 -2
  55. package/dist/slack.d.cts +1 -1
  56. package/dist/slack.d.ts +1 -1
  57. package/dist/sqlite.cjs.map +1 -1
  58. package/dist/sqlite.d.cts +1 -1
  59. package/dist/sqlite.d.ts +1 -1
  60. package/dist/sqlite.js.map +1 -1
  61. package/dist/twilio.d.cts +1 -1
  62. package/dist/twilio.d.ts +1 -1
  63. package/dist/{types-Cz47Boks.d.cts → types-DyWCxlSi.d.cts} +14 -1
  64. package/dist/{types-Cz47Boks.d.ts → types-DyWCxlSi.d.ts} +14 -1
  65. package/dist/webhook.d.cts +1 -1
  66. package/dist/webhook.d.ts +1 -1
  67. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
1
  import Anthropic from '@anthropic-ai/sdk';
2
- import { n as TriageFn } from './types-Cz47Boks.cjs';
2
+ import { o as TriageFn } from './types-DyWCxlSi.cjs';
3
3
 
4
4
  interface AnthropicTriageOptions {
5
5
  /** Defaults to what the Anthropic SDK resolves: ANTHROPIC_API_KEY, or an `ant auth login` profile. */
@@ -1,5 +1,5 @@
1
1
  import Anthropic from '@anthropic-ai/sdk';
2
- import { n as TriageFn } from './types-Cz47Boks.js';
2
+ import { o as TriageFn } from './types-DyWCxlSi.js';
3
3
 
4
4
  interface AnthropicTriageOptions {
5
5
  /** Defaults to what the Anthropic SDK resolves: ANTHROPIC_API_KEY, or an `ant auth login` profile. */
@@ -1,4 +1,4 @@
1
- import { A as Alert, a as AlertChannel } from './types-Cz47Boks.cjs';
1
+ import { A as Alert, a as AlertChannel } from './types-DyWCxlSi.cjs';
2
2
 
3
3
  /**
4
4
  * Bugsnag, Error Reporting API, payload version 5.
package/dist/bugsnag.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as Alert, a as AlertChannel } from './types-Cz47Boks.js';
1
+ import { A as Alert, a as AlertChannel } from './types-DyWCxlSi.js';
2
2
 
3
3
  /**
4
4
  * Bugsnag, Error Reporting API, payload version 5.
@@ -1,4 +1,4 @@
1
- import { S as Store, a as AlertChannel, n as TriageFn, i as JobOptions, R as Run, A as Alert, D as Duration, J as JobDefinition, f as CheckResult, k as JobSummary, j as JobState } from './types-Cz47Boks.js';
1
+ import { S as Store, a as AlertChannel, o as TriageFn, i as JobOptions, R as Run, A as Alert, D as Duration, J as JobDefinition, f as CheckResult, k as JobSummary, j as JobState } from './types-DyWCxlSi.js';
2
2
 
3
3
  /** What a job function receives. */
4
4
  interface JobContext {
@@ -167,6 +167,15 @@ interface RunHandle {
167
167
  interface SourceHost {
168
168
  /** Declare a job, as CronWatch.job(). Throws for an invalid name or option. */
169
169
  job(name: string, options?: JobOptions): unknown;
170
+ /**
171
+ * The definitions declared now, as CronWatch.definedJobs(): a source reads
172
+ * it to declare again a job forgotten since (the dashboard's forget), whose
173
+ * definition has not changed. A host without it is taken to keep every
174
+ * job the source declared.
175
+ */
176
+ definedJobs?(): {
177
+ name: string;
178
+ }[];
170
179
  /** See CronWatch.recordRun(). */
171
180
  recordRun(run: Run, options?: RecordRunOptions): Promise<Alert[]>;
172
181
  readonly store: Store;
@@ -287,9 +296,23 @@ declare class CronWatch {
287
296
  * that replaced it, never its own over it, and one forgotten since writes
288
297
  * its own. The writes of one name take turns, in the order they were asked
289
298
  * for, so one still under way cannot land after a later one; and a name
290
- * declared again while its write was under way is still to be written.
299
+ * declared again while its write was under way is still to be written. A
300
+ * name is marked as written only while that same declaration stands, so a
301
+ * forget that lands during the write (deleting the row after it) leaves
302
+ * the name to be written again, as does one forgotten before it.
303
+ *
304
+ * With `confirm`, as a run starts, a name already written is read back:
305
+ * another process may have forgotten the job since, and a job still
306
+ * declared here comes back on its next run.
291
307
  */
292
308
  private sync;
309
+ /**
310
+ * Every stored job, once each declaration has been written. A job declared
311
+ * here that the store no longer has was forgotten by another process after
312
+ * this one wrote it: it is written again, as its next run would, so it is
313
+ * checked and shown while any process still declares it.
314
+ */
315
+ private storedJobs;
293
316
  /**
294
317
  * Runs `fn` after every earlier state update for the same job has settled,
295
318
  * so two runs (or a run and a check) in this process never read and write
@@ -321,7 +344,8 @@ declare class CronWatch {
321
344
  private execute;
322
345
  /**
323
346
  * Sets a finished run's status and error from how it ended, then redacts
324
- * its output and error. Shared by execute() and RunHandle.finish().
347
+ * its output and error and caps them, in that order. Shared by execute()
348
+ * and RunHandle.finish().
325
349
  */
326
350
  private conclude;
327
351
  /**
@@ -374,17 +398,28 @@ declare class CronWatch {
374
398
  * alone and reported. A finished run is judged as if it had been wrapped
375
399
  * here (expect, failures, duration, budgets) and its output and error are
376
400
  * redacted the same way; one finishing after a check marked it timeout is
377
- * judged only when it succeeded, as RunHandle.finish() does. Returns the
378
- * alerts it sent.
401
+ * judged only when it succeeded, as RunHandle.finish() does. A metric that
402
+ * is not a finite number throws before anything is written, as
403
+ * job.metric() does. Returns the alerts it sent.
379
404
  */
380
405
  recordRun(input: Run, options?: RecordRunOptions): Promise<Alert[]>;
381
406
  /** recordRun() for a run already stored. */
382
407
  private recordOver;
383
408
  /**
384
409
  * Evaluate a finished run (ok, failed, or timed out by a check), already
385
- * written, against the job's state and send what that produces. Never throws.
410
+ * written, against the job's state and send what that produces. The
411
+ * alerts are written with that state (see outbox()). Never throws.
386
412
  */
387
413
  private finishRun;
414
+ /**
415
+ * An evaluation as it is written: its drafts composed into alerts and held
416
+ * in the same state (holdAlerts), so the write that opens a condition also
417
+ * keeps its alerts, and a process that stops before sending them does not
418
+ * lose them. Called inside updateState(), so it only computes.
419
+ */
420
+ private outbox;
421
+ /** Reports alerts let go because a job's queue was full. */
422
+ private reportDropped;
388
423
  /**
389
424
  * The runs before `run`, newest first, with up to BASELINE_WINDOW
390
425
  * successful ones when the store has them. One small read normally; a
@@ -410,16 +445,24 @@ declare class CronWatch {
410
445
  job: JobSummary;
411
446
  runs: Run[];
412
447
  }[]>;
448
+ /** A job's summary, or null for one the store does not have. One declared here and forgotten elsewhere is written again, as storedJobs() does. */
413
449
  jobSummary(name: string): Promise<JobSummary | null>;
414
450
  /** A job's runs, newest first. `limit` is a whole number from 1 to 500. */
415
451
  runs(name: string, limit?: number): Promise<Run[]>;
416
452
  getRun(id: string): Promise<Run | null>;
417
- /** Stop alerts for a job for a while. State keeps updating underneath. */
453
+ /**
454
+ * Stop alerts for a job for a while. State keeps updating underneath. The
455
+ * end is a whole millisecond, held at 2^53 - 1 (see silenceEnd).
456
+ */
418
457
  silence(name: string, duration: Duration): Promise<JobState>;
419
458
  unsilence(name: string): Promise<JobState>;
420
459
  /** Read, change and write one job's state, in turn with every other update to it. */
421
460
  private patchState;
422
- /** Remove a job and its runs from the store. A job still declared in code comes back on its next run. */
461
+ /**
462
+ * Remove a job and its runs from the store. A job still declared in code
463
+ * comes back: here on its next run, and in any other process that declares
464
+ * it on its next run there, or at that process's next check or dashboard read.
465
+ */
423
466
  forget(name: string): Promise<void>;
424
467
  /** The dashboard and JSON API as fetch-style handlers. See createRoutes(). */
425
468
  routes(options?: RoutesOptions): Routes;
@@ -430,11 +473,21 @@ declare class CronWatch {
430
473
  */
431
474
  start(every?: Duration): void;
432
475
  stop(): void;
476
+ /**
477
+ * Stop the interval, wait for a check already under way (bounded by its
478
+ * own channel, triage and retry timeouts; what it throws was reported to
479
+ * whoever started it), then close the store, so that check neither writes
480
+ * after the store is closed nor loses the alerts it would queue.
481
+ */
433
482
  close(): Promise<void>;
434
483
  /**
435
- * Compose, triage and send each draft. The state was saved before this
436
- * (updateState), so a slow channel holds up nothing else; afterwards only
437
- * the delivery fields are written back, onto a fresh read of the state.
484
+ * Triage and send each alert the outbox holds (see outbox()). The state,
485
+ * with the alerts in it, was saved before this, so a slow channel holds up
486
+ * nothing else; afterwards only the delivery fields are written back, onto
487
+ * a fresh read of the state, and the alerts leave `sending`. Triage is made
488
+ * here, never stored with the held alert: the write that opens a condition
489
+ * cannot wait for it, and a retry triages an alert that has none. With
490
+ * deliver: "check" the alerts were queued for a check elsewhere instead.
438
491
  */
439
492
  private dispatch;
440
493
  /**
@@ -447,8 +500,10 @@ declare class CronWatch {
447
500
  private retryUndelivered;
448
501
  /**
449
502
  * Mark delivered alerts done, drop stale ones, and keep failed ones for the
450
- * next check. A failed alert replaces its stored copy, so a triage made on
451
- * this attempt is kept. lastAlertAt moves only on a delivery.
503
+ * next check, taking them all out of `sending` (recordSent). A failed alert
504
+ * replaces its stored copy, so a triage made on this attempt is kept.
505
+ * lastAlertAt moves only on a delivery. When this write fails, alerts
506
+ * still in `sending` are retried once their lease runs out.
452
507
  */
453
508
  private recordDelivery;
454
509
  /** Send to every channel at once. True when at least one accepted it, or there are none. */
@@ -1,4 +1,4 @@
1
- import { S as Store, a as AlertChannel, n as TriageFn, i as JobOptions, R as Run, A as Alert, D as Duration, J as JobDefinition, f as CheckResult, k as JobSummary, j as JobState } from './types-Cz47Boks.cjs';
1
+ import { S as Store, a as AlertChannel, o as TriageFn, i as JobOptions, R as Run, A as Alert, D as Duration, J as JobDefinition, f as CheckResult, k as JobSummary, j as JobState } from './types-DyWCxlSi.cjs';
2
2
 
3
3
  /** What a job function receives. */
4
4
  interface JobContext {
@@ -167,6 +167,15 @@ interface RunHandle {
167
167
  interface SourceHost {
168
168
  /** Declare a job, as CronWatch.job(). Throws for an invalid name or option. */
169
169
  job(name: string, options?: JobOptions): unknown;
170
+ /**
171
+ * The definitions declared now, as CronWatch.definedJobs(): a source reads
172
+ * it to declare again a job forgotten since (the dashboard's forget), whose
173
+ * definition has not changed. A host without it is taken to keep every
174
+ * job the source declared.
175
+ */
176
+ definedJobs?(): {
177
+ name: string;
178
+ }[];
170
179
  /** See CronWatch.recordRun(). */
171
180
  recordRun(run: Run, options?: RecordRunOptions): Promise<Alert[]>;
172
181
  readonly store: Store;
@@ -287,9 +296,23 @@ declare class CronWatch {
287
296
  * that replaced it, never its own over it, and one forgotten since writes
288
297
  * its own. The writes of one name take turns, in the order they were asked
289
298
  * for, so one still under way cannot land after a later one; and a name
290
- * declared again while its write was under way is still to be written.
299
+ * declared again while its write was under way is still to be written. A
300
+ * name is marked as written only while that same declaration stands, so a
301
+ * forget that lands during the write (deleting the row after it) leaves
302
+ * the name to be written again, as does one forgotten before it.
303
+ *
304
+ * With `confirm`, as a run starts, a name already written is read back:
305
+ * another process may have forgotten the job since, and a job still
306
+ * declared here comes back on its next run.
291
307
  */
292
308
  private sync;
309
+ /**
310
+ * Every stored job, once each declaration has been written. A job declared
311
+ * here that the store no longer has was forgotten by another process after
312
+ * this one wrote it: it is written again, as its next run would, so it is
313
+ * checked and shown while any process still declares it.
314
+ */
315
+ private storedJobs;
293
316
  /**
294
317
  * Runs `fn` after every earlier state update for the same job has settled,
295
318
  * so two runs (or a run and a check) in this process never read and write
@@ -321,7 +344,8 @@ declare class CronWatch {
321
344
  private execute;
322
345
  /**
323
346
  * Sets a finished run's status and error from how it ended, then redacts
324
- * its output and error. Shared by execute() and RunHandle.finish().
347
+ * its output and error and caps them, in that order. Shared by execute()
348
+ * and RunHandle.finish().
325
349
  */
326
350
  private conclude;
327
351
  /**
@@ -374,17 +398,28 @@ declare class CronWatch {
374
398
  * alone and reported. A finished run is judged as if it had been wrapped
375
399
  * here (expect, failures, duration, budgets) and its output and error are
376
400
  * redacted the same way; one finishing after a check marked it timeout is
377
- * judged only when it succeeded, as RunHandle.finish() does. Returns the
378
- * alerts it sent.
401
+ * judged only when it succeeded, as RunHandle.finish() does. A metric that
402
+ * is not a finite number throws before anything is written, as
403
+ * job.metric() does. Returns the alerts it sent.
379
404
  */
380
405
  recordRun(input: Run, options?: RecordRunOptions): Promise<Alert[]>;
381
406
  /** recordRun() for a run already stored. */
382
407
  private recordOver;
383
408
  /**
384
409
  * Evaluate a finished run (ok, failed, or timed out by a check), already
385
- * written, against the job's state and send what that produces. Never throws.
410
+ * written, against the job's state and send what that produces. The
411
+ * alerts are written with that state (see outbox()). Never throws.
386
412
  */
387
413
  private finishRun;
414
+ /**
415
+ * An evaluation as it is written: its drafts composed into alerts and held
416
+ * in the same state (holdAlerts), so the write that opens a condition also
417
+ * keeps its alerts, and a process that stops before sending them does not
418
+ * lose them. Called inside updateState(), so it only computes.
419
+ */
420
+ private outbox;
421
+ /** Reports alerts let go because a job's queue was full. */
422
+ private reportDropped;
388
423
  /**
389
424
  * The runs before `run`, newest first, with up to BASELINE_WINDOW
390
425
  * successful ones when the store has them. One small read normally; a
@@ -410,16 +445,24 @@ declare class CronWatch {
410
445
  job: JobSummary;
411
446
  runs: Run[];
412
447
  }[]>;
448
+ /** A job's summary, or null for one the store does not have. One declared here and forgotten elsewhere is written again, as storedJobs() does. */
413
449
  jobSummary(name: string): Promise<JobSummary | null>;
414
450
  /** A job's runs, newest first. `limit` is a whole number from 1 to 500. */
415
451
  runs(name: string, limit?: number): Promise<Run[]>;
416
452
  getRun(id: string): Promise<Run | null>;
417
- /** Stop alerts for a job for a while. State keeps updating underneath. */
453
+ /**
454
+ * Stop alerts for a job for a while. State keeps updating underneath. The
455
+ * end is a whole millisecond, held at 2^53 - 1 (see silenceEnd).
456
+ */
418
457
  silence(name: string, duration: Duration): Promise<JobState>;
419
458
  unsilence(name: string): Promise<JobState>;
420
459
  /** Read, change and write one job's state, in turn with every other update to it. */
421
460
  private patchState;
422
- /** Remove a job and its runs from the store. A job still declared in code comes back on its next run. */
461
+ /**
462
+ * Remove a job and its runs from the store. A job still declared in code
463
+ * comes back: here on its next run, and in any other process that declares
464
+ * it on its next run there, or at that process's next check or dashboard read.
465
+ */
423
466
  forget(name: string): Promise<void>;
424
467
  /** The dashboard and JSON API as fetch-style handlers. See createRoutes(). */
425
468
  routes(options?: RoutesOptions): Routes;
@@ -430,11 +473,21 @@ declare class CronWatch {
430
473
  */
431
474
  start(every?: Duration): void;
432
475
  stop(): void;
476
+ /**
477
+ * Stop the interval, wait for a check already under way (bounded by its
478
+ * own channel, triage and retry timeouts; what it throws was reported to
479
+ * whoever started it), then close the store, so that check neither writes
480
+ * after the store is closed nor loses the alerts it would queue.
481
+ */
433
482
  close(): Promise<void>;
434
483
  /**
435
- * Compose, triage and send each draft. The state was saved before this
436
- * (updateState), so a slow channel holds up nothing else; afterwards only
437
- * the delivery fields are written back, onto a fresh read of the state.
484
+ * Triage and send each alert the outbox holds (see outbox()). The state,
485
+ * with the alerts in it, was saved before this, so a slow channel holds up
486
+ * nothing else; afterwards only the delivery fields are written back, onto
487
+ * a fresh read of the state, and the alerts leave `sending`. Triage is made
488
+ * here, never stored with the held alert: the write that opens a condition
489
+ * cannot wait for it, and a retry triages an alert that has none. With
490
+ * deliver: "check" the alerts were queued for a check elsewhere instead.
438
491
  */
439
492
  private dispatch;
440
493
  /**
@@ -447,8 +500,10 @@ declare class CronWatch {
447
500
  private retryUndelivered;
448
501
  /**
449
502
  * Mark delivered alerts done, drop stale ones, and keep failed ones for the
450
- * next check. A failed alert replaces its stored copy, so a triage made on
451
- * this attempt is kept. lastAlertAt moves only on a delivery.
503
+ * next check, taking them all out of `sending` (recordSent). A failed alert
504
+ * replaces its stored copy, so a triage made on this attempt is kept.
505
+ * lastAlertAt moves only on a delivery. When this write fails, alerts
506
+ * still in `sending` are retried once their lease runs out.
452
507
  */
453
508
  private recordDelivery;
454
509
  /** Send to every channel at once. True when at least one accepted it, or there are none. */
package/dist/d1.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/output.ts","../src/stores/sql.ts","../src/stores/d1.ts"],"names":["text","json"],"mappings":";;;AAeO,SAAS,SAASA,KAAAA,EAAsB;AAC7C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A,EAAQ;AAClB,MAAA,OAAO,OAAA,CAAQ,GAAA,CAAI,KAAA,EAAO,CAAC,MAAM,CAAC,CAAA;AAAA,IACpC;AAAA,GACF;AACF","file":"d1.cjs","sourcesContent":["/** Output is capped so a chatty job cannot fill the store. The tail is kept. */\nexport const OUTPUT_CAP = 16 * 1024;\n\n/**\n * NUL characters are removed first, since Postgres refuses them in TEXT and\n * JSONB and the whole run row would be lost. The cap then applies to what is\n * left.\n */\nexport function capOutput(text: string): string {\n const clean = stripNul(text);\n if (clean.length <= OUTPUT_CAP) return clean;\n return \"[earlier output trimmed]\\n\" + clean.slice(clean.length - OUTPUT_CAP);\n}\n\n/** Removes every U+0000. */\nexport function stripNul(text: string): string {\n return text.includes(\"\\u0000\") ? text.replace(/\\u0000/g, \"\") : text;\n}\n\n/**\n * Removes every U+0000 from JSON text, keys and strings alike, by dropping\n * each \\u0000 escape (a NUL can appear in JSON no other way). Escapes are\n * read left to right in pairs, so an escaped backslash followed by \"u0000\"\n * is left as it is.\n */\nexport function stripJsonNul(json: string): string {\n return json.includes(\"\\\\u0000\") ? json.replace(/\\\\(u0000|[\\s\\S])/g, (escape, next: string) => (next === \"u0000\" ? \"\" : escape)) : json;\n}\n\n/** \"Name: message\" and the first five stack frames, capped like output. */\nexport function errorMessage(error: unknown): string {\n return capOutput(describeError(error));\n}\n\nfunction describeError(error: unknown): string {\n if (error instanceof Error) {\n // The stack repeats the header (over several lines when the message has\n // newlines), so take only its frames.\n const frames = (error.stack ?? \"\").split(\"\\n\").filter((line) => /^\\s+at /.test(line)).slice(0, 5);\n return `${error.name}: ${error.message}${frames.length ? `\\n${frames.join(\"\\n\")}` : \"\"}`;\n }\n if (typeof error === \"string\") return error;\n try {\n return JSON.stringify(error) ?? String(error);\n } catch {\n return String(error);\n }\n}\n\nconst REDACTED = \"[redacted]\";\n\n// Bounded quantifiers throughout, so a long line cannot make these backtrack.\n// They apply in this order, each to the text the ones before it left.\nconst SECRET_PATTERNS: [RegExp, string | ((match: string, ...groups: string[]) => string)][] = [\n // A PEM private key, header to footer. Without a footer (the output was\n // trimmed) it runs to the end of the base64 body. A \"-\" that starts five\n // dashes ends the body, so the footer is never swallowed into it.\n [\n /-----BEGIN (?:[A-Z0-9]{1,20} ){0,3}PRIVATE KEY-----(?:[A-Za-z0-9+/=\\s,:]|-(?!----)){0,16384}(?:-----END (?:[A-Z0-9]{1,20} ){0,3}PRIVATE KEY-----)?/g,\n REDACTED,\n ],\n // password=..., API_KEY: ..., \"client_secret\": \"...\", TOKEN='...', token=...,\n // :password=>\"...\" (but not max_tokens: 800). A quoted value is blanked to\n // its closing quote, spaces and all, and keeps its quotes.\n [\n /\\b([A-Za-z0-9_-]{0,40}(?:secret|token|passw(?:or)?d|pwd|api[_-]?key|access[_-]?key|private[_-]?key|credential)[A-Za-z0-9_-]{0,40}(?<![Tt][Oo][Kk][Ee][Nn][Ss])\"?\\s{0,3}(?:=>|[=:])\\s{0,3})(?:(\")[^\"\\n]{1,4096}\"|(')[^'\\n]{1,4096}'|[\"']?[^\\s\"',;&]{1,4096})/gi,\n (_match, name: string, double?: string, single?: string) => {\n const quote = double ?? single ?? \"\";\n return `${name}${quote}${REDACTED}${quote}`;\n },\n ],\n // Authorization: Basic <base64> and Authorization: Token <token>, also as a JSON or hash entry.\n [/\\b((?:proxy-)?authorization[\"']?\\s{0,3}(?:=>|[=:])\\s{0,3}[\"']?\\s{0,3}(?:basic|token)\\s{1,3})[A-Za-z0-9._~+/=:-]{1,4096}/gi, `$1${REDACTED}`],\n // Credentials inside a URL: postgres://user:password@host. The password\n // runs to the last \"@\" before a \"/\" or a space, so one that contains \"@\"\n // is blanked whole.\n [/(\\b[a-z][a-z0-9+.-]{0,30}:\\/\\/[^\\s/:@]{0,256}:)[^\\s/]{1,256}@/gi, `$1${REDACTED}@`],\n // Authorization: Bearer <token>\n [/\\b(Bearer\\s{1,3})[A-Za-z0-9._~+/=-]{8,4096}/g, `$1${REDACTED}`],\n // A bare JWT: three base64url segments, the first starting eyJ.\n [/\\beyJ[A-Za-z0-9_-]{4,4096}\\.[A-Za-z0-9_-]{4,4096}\\.[A-Za-z0-9_-]{0,4096}/g, REDACTED],\n // Incoming webhook URLs carry their secret in the path.\n [/(\\bhooks\\.slack\\.com\\/(?:services|workflows|triggers)\\/)[A-Za-z0-9/_-]{1,255}/gi, `$1${REDACTED}`],\n [/(\\bdiscord(?:app)?\\.com\\/api\\/(?:v\\d{1,2}\\/)?webhooks\\/)[A-Za-z0-9/_-]{1,255}/gi, `$1${REDACTED}`],\n // Well-known token shapes: AWS, GitHub, Slack, Stripe, Anthropic, OpenAI and Google style keys.\n [/\\b(?:AKIA|ASIA)[0-9A-Z]{16}\\b/g, REDACTED],\n [/\\b(?:gh[pousr]_[A-Za-z0-9]{30,255}|github_pat_[A-Za-z0-9_]{20,255})\\b/g, REDACTED],\n [/\\bxox[abposr]-[A-Za-z0-9-]{10,255}/g, REDACTED],\n [/\\b[rsp]k_(?:live|test)_[A-Za-z0-9]{10,255}\\b/g, REDACTED],\n [/\\bwhsec_[A-Za-z0-9+/=]{16,255}/g, REDACTED],\n [/\\bsk-[A-Za-z0-9_-]{20,255}/g, REDACTED],\n [/\\bAIza[0-9A-Za-z_-]{35}(?![0-9A-Za-z_-])/g, REDACTED],\n];\n\n/**\n * The default `redact`: blanks values that look like secrets (key=value pairs\n * with secret-ish names, Authorization headers, URL credentials, bearer\n * tokens, JWTs, PEM private keys, webhook URLs and well-known token formats)\n * before output or an error is stored, shown or sent anywhere.\n */\nexport function redactSecrets(text: string): string {\n let out = text;\n for (const [pattern, replacement] of SECRET_PATTERNS) out = out.replace(pattern, replacement as string);\n return out;\n}\n","import { stripJsonNul, stripNul } from \"../output.js\";\nimport type { JobState, Run, StoredJob, StoredJobDefinition } from \"../types.js\";\n\n/**\n * The schema, statements and row mapping shared by the SQLite and Postgres\n * stores. Imports no driver, so either entry point can pull it in alone.\n * Statements are written with `?` placeholders; Postgres numbers them.\n */\n\nexport type Dialect = \"sqlite\" | \"postgres\";\n\nexport const DEFAULT_PREFIX = \"cronwatch_\";\n\n// Postgres truncates identifiers past 63 bytes; the longest name we build is the prefix plus \"runs_job_started\".\nconst MAX_PREFIX = 63 - \"runs_job_started\".length;\n\n/**\n * Table names are built from the prefix, so it must be a plain lowercase\n * identifier. Uppercase is refused rather than folded: Postgres lowercases\n * unquoted names, so \"Monitoring_\" would quietly become \"monitoring_\".\n */\nexport function tablePrefix(prefix: string = DEFAULT_PREFIX): string {\n if (!/^[a-z_][a-z0-9_]*$/.test(prefix) || prefix.length > MAX_PREFIX) {\n throw new Error(\n `cronwatch: invalid table prefix ${JSON.stringify(prefix)}. Use lowercase letters, digits and underscores, ` +\n `not starting with a digit, at most ${MAX_PREFIX} characters.`,\n );\n }\n return prefix;\n}\n\nexport function schema(dialect: Dialect, p: string): string {\n const pg = dialect === \"postgres\";\n const int = pg ? \"BIGINT\" : \"INTEGER\";\n const json = pg ? \"JSONB\" : \"TEXT\";\n return `\n CREATE TABLE IF NOT EXISTS ${p}jobs (\n name TEXT PRIMARY KEY,\n definition ${json} NOT NULL,\n created_at ${int} NOT NULL,\n updated_at ${int} NOT NULL\n );\n CREATE TABLE IF NOT EXISTS ${p}runs (${pg ? \"\\n seq BIGSERIAL,\" : \"\"}\n id TEXT PRIMARY KEY,\n job TEXT NOT NULL,\n status TEXT NOT NULL,\n started_at ${int} NOT NULL,\n finished_at ${int},\n duration_ms ${int},\n error TEXT,\n output TEXT,\n metrics ${json} NOT NULL DEFAULT '{}',\n trigger TEXT NOT NULL DEFAULT 'run'\n );\n CREATE INDEX IF NOT EXISTS ${p}runs_job_started ON ${p}runs (job, started_at DESC);\n CREATE INDEX IF NOT EXISTS ${p}runs_running ON ${p}runs (status) WHERE status = 'running';\n CREATE TABLE IF NOT EXISTS ${p}state (\n job TEXT PRIMARY KEY,\n state ${json} NOT NULL\n );\n `;\n}\n\nexport function statements(dialect: Dialect, p: string) {\n const pg = dialect === \"postgres\";\n // Insertion order, to break ties between runs that started in the same millisecond.\n const seq = pg ? \"seq\" : \"rowid\";\n // Byte order on both, so names sort the same whatever the database's collation.\n const byName = pg ? `name COLLATE \"C\"` : \"name\";\n // The version inside a state's JSON, as stateVersion() reads it: a whole\n // number from 0 to 2^53 - 1, else 0 (none, or a foreign row's 1.5 or \"x\",\n // which must neither fail the statement nor refuse every write for good).\n // Each CASE tests the JSON type before any cast.\n const version = (column: string) => {\n if (pg) {\n const v = `(${column}->>'version')::numeric`;\n return `CASE WHEN jsonb_typeof(${column}->'version') <> 'number' THEN 0 WHEN ${v} % 1 = 0 AND ${v} BETWEEN 0 AND 9007199254740991 THEN ${v}::bigint ELSE 0 END`;\n }\n const v = `json_extract(${column}, '$.version')`;\n return `CASE WHEN json_type(${column}, '$.version') NOT IN ('integer', 'real') THEN 0 WHEN ${v} = CAST(${v} AS INTEGER) AND ${v} BETWEEN 0 AND 9007199254740991 THEN CAST(${v} AS INTEGER) ELSE 0 END`;\n };\n const sql = {\n upsertJob: `INSERT INTO ${p}jobs (name, definition, created_at, updated_at) VALUES (?, ?, ?, ?)\n ON CONFLICT (name) DO UPDATE SET definition = excluded.definition, updated_at = excluded.updated_at`,\n getJob: `SELECT * FROM ${p}jobs WHERE name = ?`,\n listJobs: `SELECT * FROM ${p}jobs ORDER BY ${byName}`,\n deleteRuns: `DELETE FROM ${p}runs WHERE job = ?`,\n deleteState: `DELETE FROM ${p}state WHERE job = ?`,\n deleteJob: `DELETE FROM ${p}jobs WHERE name = ?`,\n insertRun: `INSERT INTO ${p}runs (id, job, status, started_at, finished_at, duration_ms, error, output, metrics, trigger)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n updateRun: `UPDATE ${p}runs SET status = ?, finished_at = ?, duration_ms = ?, error = ?, output = ?, metrics = ? WHERE id = ?`,\n getRun: `SELECT * FROM ${p}runs WHERE id = ?`,\n listRuns: `SELECT * FROM ${p}runs WHERE job = ? ORDER BY started_at DESC, ${seq} DESC LIMIT ?`,\n runningRuns: `SELECT * FROM ${p}runs WHERE status = 'running' ORDER BY started_at, ${seq}`,\n getState: `SELECT state FROM ${p}state WHERE job = ?`,\n setState: `INSERT INTO ${p}state (job, state) VALUES (?, ?) ON CONFLICT (job) DO UPDATE SET state = excluded.state`,\n // compareAndSetState. Expecting version 0 also matches a missing row, so\n // that case inserts; any other version must find its row.\n casInsert: `INSERT INTO ${p}state (job, state) VALUES (?, ?)\n ON CONFLICT (job) DO UPDATE SET state = excluded.state WHERE ${version(`${p}state.state`)} = 0`,\n casUpdate: `UPDATE ${p}state SET state = ? WHERE job = ? AND ${version(\"state\")} = ?`,\n // Each job's newest run is kept whatever its age: without it, a job that\n // runs less often than the retention looks like it never ran.\n prune: `DELETE FROM ${p}runs WHERE status <> 'running' AND started_at < ?\n AND started_at < (SELECT MAX(r.started_at) FROM ${p}runs r WHERE r.job = ${p}runs.job)`,\n };\n if (pg) {\n for (const key of Object.keys(sql) as (keyof typeof sql)[]) {\n let n = 0;\n sql[key] = sql[key].replace(/\\?/g, () => `$${++n}`);\n }\n }\n return sql;\n}\n\n/**\n * updateRunIf: the update above, only while the stored status is one of\n * `count` statuses. Built per count, since the list is bound value by value.\n */\nexport function updateRunIfSql(dialect: Dialect, p: string, count: number): string {\n const text = `UPDATE ${p}runs SET status = ?, finished_at = ?, duration_ms = ?, error = ?, output = ?, metrics = ? WHERE id = ? AND status IN (${Array.from({ length: count }, () => \"?\").join(\", \")})`;\n if (dialect !== \"postgres\") return text;\n let n = 0;\n return text.replace(/\\?/g, () => `$${++n}`);\n}\n\n/**\n * Postgres refuses U+0000 in TEXT and JSONB, and a refused write loses the\n * whole row, so every dialect writes text without it: a run's trigger,\n * output, error and metric names, and every key and string of a definition\n * and a state. Identifiers (a job's name, a run's id) are written as given;\n * the client refuses one with a NUL before it gets here.\n */\nconst text = (value: string | null) => (value === null ? null : stripNul(value));\nconst jsonText = (value: unknown) => stripJsonNul(JSON.stringify(value));\n\n// Parameters in statement order, so both drivers bind the same values.\nexport const params = {\n upsertJob: (definition: StoredJobDefinition, now: number) => [definition.name, jsonText(definition), now, now],\n insertRun: (run: Run) => [\n run.id, run.job, run.status, run.startedAt, run.finishedAt, run.durationMs, text(run.error), text(run.output), jsonText(run.metrics), stripNul(run.trigger),\n ],\n updateRun: (run: Run) => [run.status, run.finishedAt, run.durationMs, text(run.error), text(run.output), jsonText(run.metrics), run.id],\n updateRunIf: (run: Run, fromStatuses: Run[\"status\"][]) => [\n run.status, run.finishedAt, run.durationMs, text(run.error), text(run.output), jsonText(run.metrics), run.id, ...fromStatuses,\n ],\n setState: (state: JobState) => [state.job, jsonText(state)],\n casInsert: (state: JobState) => [state.job, jsonText(state)],\n casUpdate: (state: JobState, expectedVersion: number) => [jsonText(state), state.job, expectedVersion],\n};\n\n// SQLite hands back JSON as TEXT and Postgres as parsed JSONB; Postgres returns BIGINT as a string.\ntype Json<T> = T | string;\ntype Int = number | string;\n\nexport interface JobRow { name: string; definition: Json<StoredJobDefinition>; created_at: Int; updated_at: Int }\nexport interface RunRow {\n id: string; job: string; status: Run[\"status\"]; started_at: Int; finished_at: Int | null;\n duration_ms: Int | null; error: string | null; output: string | null; metrics: Json<Record<string, number>> | null; trigger: string;\n}\nexport interface StateRow { state: Json<JobState> }\n\nconst json = <T>(v: Json<T>): T => (typeof v === \"string\" ? (JSON.parse(v) as T) : v);\nconst num = (v: Int | null): number | null => (v === null ? null : Number(v));\n\nexport function rowToJob(r: JobRow): StoredJob {\n return { name: r.name, definition: json(r.definition), createdAt: Number(r.created_at), updatedAt: Number(r.updated_at) };\n}\n\nexport function rowToRun(r: RunRow): Run {\n return {\n id: r.id, job: r.job, status: r.status, startedAt: Number(r.started_at), finishedAt: num(r.finished_at),\n durationMs: num(r.duration_ms), error: r.error, output: r.output, metrics: r.metrics === null ? {} : json(r.metrics), trigger: r.trigger,\n };\n}\n\nexport function rowToState(r: StateRow): JobState {\n return json(r.state);\n}\n","import type { Store, StoredJob } from \"../types.js\";\nimport { params, rowToJob, rowToRun, rowToState, schema, statements, tablePrefix, updateRunIfSql, type JobRow, type RunRow, type StateRow } from \"./sql.js\";\n\n/**\n * The parts of a Cloudflare D1 binding this store uses. Written out here so\n * the SDK needs nothing from @cloudflare/workers-types: a Worker's\n * `env.DB: D1Database` fits it as it is.\n */\nexport interface D1PreparedStatementLike {\n bind(...values: unknown[]): D1PreparedStatementLike;\n first(): Promise<unknown>;\n all(): Promise<{ results?: unknown[] }>;\n run(): Promise<{ meta?: { changes?: number } }>;\n}\n\nexport interface D1DatabaseLike {\n prepare(query: string): D1PreparedStatementLike;\n batch(statements: D1PreparedStatementLike[]): Promise<unknown[]>;\n}\n\nexport interface D1Options {\n /** Table name prefix: lowercase letters, digits and underscores. Default \"cronwatch_\". */\n prefix?: string;\n /**\n * Create the tables on first use (CREATE TABLE IF NOT EXISTS), once per\n * isolate. Default true. Pass false when a D1 migration creates them\n * instead (see the Cloudflare docs page for the file).\n */\n createTables?: boolean;\n}\n\n/** Databases (and prefixes) whose tables this isolate has already made sure of. */\nconst created = new WeakMap<object, Set<string>>();\n\n/**\n * Keeps everything in Cloudflare D1, for Workers. D1 is SQLite, so this is\n * the SQLite store's schema, statements and row mapping over D1's\n * prepare, bind and batch. Create it where the binding is, inside the\n * handler: `d1(env.DB)`.\n */\nexport function d1(database: D1DatabaseLike, options: D1Options = {}): Store {\n if (!database || typeof database.prepare !== \"function\" || typeof database.batch !== \"function\") {\n throw new Error(\"cronwatch: d1() needs a D1 database binding, such as env.DB\");\n }\n const p = tablePrefix(options.prefix);\n // D1 keeps its own tables under _cf_ and refuses writes to them.\n if (p.startsWith(\"_cf_\")) throw new Error(`cronwatch: invalid table prefix ${JSON.stringify(p)}. D1 reserves names starting with _cf_.`);\n const sql = statements(\"sqlite\", p);\n const createTables = options.createTables ?? true;\n\n const stmt = (text: string, values: unknown[] = []) => {\n const s = database.prepare(text);\n return values.length > 0 ? s.bind(...values) : s;\n };\n const first = async <T>(text: string, values: unknown[]): Promise<T | null> => ((await stmt(text, values).first()) as T | null) ?? null;\n const all = async <T>(text: string, values: unknown[] = []): Promise<T[]> => ((await stmt(text, values).all()).results ?? []) as T[];\n const changes = async (text: string, values: unknown[]): Promise<number> => (await stmt(text, values).run()).meta?.changes ?? 0;\n\n return {\n async init() {\n if (!createTables || created.get(database)?.has(p)) return;\n // exec() splits on newlines, so each statement goes in on its own, in one batch.\n const ddl = schema(\"sqlite\", p).split(\";\").map((s) => s.trim()).filter(Boolean);\n await database.batch(ddl.map((s) => database.prepare(s)));\n // Remembered only once it worked, and as a flag rather than a promise,\n // so no invocation ever waits on another's unfinished I/O.\n let prefixes = created.get(database);\n if (!prefixes) created.set(database, (prefixes = new Set()));\n prefixes.add(p);\n },\n async upsertJob(definition, now) {\n await stmt(sql.upsertJob, params.upsertJob(definition, now)).run();\n },\n async getJob(name) {\n const r = await first<JobRow>(sql.getJob, [name]);\n return r ? rowToJob(r) : null;\n },\n async listJobs(): Promise<StoredJob[]> {\n return (await all<JobRow>(sql.listJobs)).map(rowToJob);\n },\n async deleteJob(name) {\n // A batch is one transaction.\n await database.batch([stmt(sql.deleteRuns, [name]), stmt(sql.deleteState, [name]), stmt(sql.deleteJob, [name])]);\n },\n async insertRun(run) {\n await stmt(sql.insertRun, params.insertRun(run)).run();\n },\n async updateRun(run) {\n await stmt(sql.updateRun, params.updateRun(run)).run();\n },\n async updateRunIf(run, fromStatuses) {\n if (fromStatuses.length === 0) return false;\n return (await changes(updateRunIfSql(\"sqlite\", p, fromStatuses.length), params.updateRunIf(run, fromStatuses))) > 0;\n },\n async getRun(id) {\n const r = await first<RunRow>(sql.getRun, [id]);\n return r ? rowToRun(r) : null;\n },\n async listRuns(job, limit) {\n return (await all<RunRow>(sql.listRuns, [job, limit])).map(rowToRun);\n },\n async lastRun(job) {\n const r = await first<RunRow>(sql.listRuns, [job, 1]);\n return r ? rowToRun(r) : null;\n },\n async runningRuns() {\n return (await all<RunRow>(sql.runningRuns)).map(rowToRun);\n },\n async getState(job) {\n const r = await first<StateRow>(sql.getState, [job]);\n return r ? rowToState(r) : null;\n },\n async setState(state) {\n await stmt(sql.setState, params.setState(state)).run();\n },\n async compareAndSetState(state, expectedVersion) {\n const written = expectedVersion === 0\n ? await changes(sql.casInsert, params.casInsert(state))\n : await changes(sql.casUpdate, params.casUpdate(state, expectedVersion));\n return written > 0;\n },\n async prune(before) {\n return changes(sql.prune, [before]);\n },\n };\n}\n"]}
1
+ {"version":3,"sources":["../src/output.ts","../src/stores/sql.ts","../src/stores/d1.ts"],"names":["text","json"],"mappings":";;;AAyCO,SAAS,SAASA,KAAAA,EAAsB;AAC7C,EAAA,OAAOA,KAAAA,CAAK,SAAS,IAAQ,CAAA,GAAIA,MAAK,OAAA,CAAQ,SAAA,EAAW,EAAE,CAAA,GAAIA,KAAAA;AACjE;AAQO,SAAS,aAAaC,KAAAA,EAAsB;AACjD,EAAA,OAAOA,KAAAA,CAAK,QAAA,CAAS,SAAS,CAAA,GAAIA,MAAK,OAAA,CAAQ,mBAAA,EAAqB,CAAC,MAAA,EAAQ,IAAA,KAAkB,IAAA,KAAS,OAAA,GAAU,EAAA,GAAK,MAAO,CAAA,GAAIA,KAAAA;AACpI;;;AC1CO,IAAM,cAAA,GAAiB,YAAA;AAG9B,IAAM,UAAA,GAAa,KAAK,kBAAA,CAAmB,MAAA;AAOpC,SAAS,WAAA,CAAY,SAAiB,cAAA,EAAwB;AACnE,EAAA,IAAI,CAAC,oBAAA,CAAqB,IAAA,CAAK,MAAM,CAAA,IAAK,MAAA,CAAO,SAAS,UAAA,EAAY;AACpE,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,mCAAmC,IAAA,CAAK,SAAA,CAAU,MAAM,CAAC,uFACjB,UAAU,CAAA,YAAA;AAAA,KACpD;AAAA,EACF;AACA,EAAA,OAAO,MAAA;AACT;AAEO,SAAS,MAAA,CAAO,SAAkB,CAAA,EAAmB;AAE1D,EAAA,MAAM,GAAA,GAAsB,SAAA;AAC5B,EAAA,MAAMA,KAAAA,GAAsB,MA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AAA,IACvD,CAAA;AAAA,IACA,MAAM,kBAAA,CAAmB,KAAA,EAAO,eAAA,EAAiB;AAC/C,MAAA,MAAM,OAAA,GAAU,oBAAoB,CAAA,GAChC,MAAM,QAAQ,GAAA,CAAI,SAAA,EAAW,OAAO,SAAA,CAAU,KAAK,CAAC,CAAA,GACpD,MAAM,QAAQ,GAAA,CAAI,SAAA,EAAW,OAAO,SAAA,CAAU,KAAA,EAAO,eAAe,CAAC,CAAA;AACzE,MAAA,OAAO,OAAA,GAAU,CAAA;AAAA,IACnB,CAAA;AAAA,IACA,MAAM,MAAM,MAAA,EAAQ;AAClB,MAAA,OAAO,OAAA,CAAQ,GAAA,CAAI,KAAA,EAAO,CAAC,MAAM,CAAC,CAAA;AAAA,IACpC;AAAA,GACF;AACF","file":"d1.cjs","sourcesContent":["/** Output is capped so a chatty job cannot fill the store. The tail is kept. */\nexport const OUTPUT_CAP = 16 * 1024;\n\n/**\n * NUL characters are removed first, since Postgres refuses them in TEXT and\n * JSONB and the whole run row would be lost. The cap then applies to what is\n * left.\n */\nexport function capOutput(text: string): string {\n const clean = stripNul(text);\n if (clean.length <= OUTPUT_CAP) return clean;\n return TRIMMED + clean.slice(clean.length - OUTPUT_CAP);\n}\n\nconst TRIMMED = \"[earlier output trimmed]\\n\";\n\n/**\n * How much text before the kept tail redaction reads, and never keeps: three\n * times the longest secret a default pattern can match (a PEM key's 16 KB body\n * with its header and footer, under OUTPUT_CAP + 1024), since a replacement\n * grows what it replaces at most threefold.\n */\nexport const REDACT_EDGE = 3 * (OUTPUT_CAP + 1024);\n\n/**\n * Output or an error as it is stored: redacted, then capped like capOutput,\n * so the cut cannot fall inside a secret and keep what follows its label.\n * Text of at most OUTPUT_CAP + REDACT_EDGE is redacted whole. Longer text is\n * cut to that many units from its end first, and after redacting, the first\n * REDACT_EDGE units are never kept: a secret whose label fell before that cut\n * is left out with them. NULs go before and after `redact`.\n */\nexport function redactAndCap(text: string, redact: (text: string) => string): string {\n const clean = stripNul(text);\n const from = clean.length - (OUTPUT_CAP + REDACT_EDGE);\n if (from <= 0) return capOutput(redact(clean));\n const redacted = stripNul(redact(clean.slice(from)));\n return TRIMMED + redacted.slice(Math.max(redacted.length - OUTPUT_CAP, REDACT_EDGE));\n}\n\n/** Removes every U+0000. */\nexport function stripNul(text: string): string {\n return text.includes(\"\\u0000\") ? text.replace(/\\u0000/g, \"\") : text;\n}\n\n/**\n * Removes every U+0000 from JSON text, keys and strings alike, by dropping\n * each \\u0000 escape (a NUL can appear in JSON no other way). Escapes are\n * read left to right in pairs, so an escaped backslash followed by \"u0000\"\n * is left as it is.\n */\nexport function stripJsonNul(json: string): string {\n return json.includes(\"\\\\u0000\") ? json.replace(/\\\\(u0000|[\\s\\S])/g, (escape, next: string) => (next === \"u0000\" ? \"\" : escape)) : json;\n}\n\n/** \"Name: message\" and the first five stack frames, capped like output. */\nexport function errorMessage(error: unknown): string {\n return capOutput(describeError(error));\n}\n\n/** \"Name: message\" and the first five stack frames, not capped: see redactAndCap. */\nexport function describeError(error: unknown): string {\n if (error instanceof Error) {\n // The stack repeats the header (over several lines when the message has\n // newlines), so take only its frames.\n const frames = (error.stack ?? \"\").split(\"\\n\").filter((line) => /^\\s+at /.test(line)).slice(0, 5);\n return `${error.name}: ${error.message}${frames.length ? `\\n${frames.join(\"\\n\")}` : \"\"}`;\n }\n if (typeof error === \"string\") return error;\n try {\n return JSON.stringify(error) ?? String(error);\n } catch {\n return String(error);\n }\n}\n\nconst REDACTED = \"[redacted]\";\n\n// Bounded quantifiers throughout, so a long line cannot make these backtrack.\n// They apply in this order, each to the text the ones before it left.\nconst SECRET_PATTERNS: [RegExp, string | ((match: string, ...groups: string[]) => string)][] = [\n // A PEM private key, header to footer. Without a footer (the output was\n // trimmed) it runs to the end of the base64 body. A \"-\" that starts five\n // dashes ends the body, so the footer is never swallowed into it.\n [\n /-----BEGIN (?:[A-Z0-9]{1,20} ){0,3}PRIVATE KEY-----(?:[A-Za-z0-9+/=\\s,:]|-(?!----)){0,16384}(?:-----END (?:[A-Z0-9]{1,20} ){0,3}PRIVATE KEY-----)?/g,\n REDACTED,\n ],\n // password=..., API_KEY: ..., \"client_secret\": \"...\", TOKEN='...', token=...,\n // :password=>\"...\" (but not max_tokens: 800). A quoted value is blanked to\n // its closing quote, spaces and all, and keeps its quotes.\n [\n /\\b([A-Za-z0-9_-]{0,40}(?:secret|token|passw(?:or)?d|pwd|api[_-]?key|access[_-]?key|private[_-]?key|credential)[A-Za-z0-9_-]{0,40}(?<![Tt][Oo][Kk][Ee][Nn][Ss])\"?\\s{0,3}(?:=>|[=:])\\s{0,3})(?:(\")[^\"\\n]{1,4096}\"|(')[^'\\n]{1,4096}'|[\"']?[^\\s\"',;&]{1,4096})/gi,\n (_match, name: string, double?: string, single?: string) => {\n const quote = double ?? single ?? \"\";\n return `${name}${quote}${REDACTED}${quote}`;\n },\n ],\n // Authorization: Basic <base64> and Authorization: Token <token>, also as a JSON or hash entry.\n [/\\b((?:proxy-)?authorization[\"']?\\s{0,3}(?:=>|[=:])\\s{0,3}[\"']?\\s{0,3}(?:basic|token)\\s{1,3})[A-Za-z0-9._~+/=:-]{1,4096}/gi, `$1${REDACTED}`],\n // Credentials inside a URL: postgres://user:password@host. The password\n // runs to the last \"@\" before a \"/\" or a space, so one that contains \"@\"\n // is blanked whole.\n [/(\\b[a-z][a-z0-9+.-]{0,30}:\\/\\/[^\\s/:@]{0,256}:)[^\\s/]{1,256}@/gi, `$1${REDACTED}@`],\n // Authorization: Bearer <token>\n [/\\b(Bearer\\s{1,3})[A-Za-z0-9._~+/=-]{8,4096}/g, `$1${REDACTED}`],\n // A bare JWT: three base64url segments, the first starting eyJ.\n [/\\beyJ[A-Za-z0-9_-]{4,4096}\\.[A-Za-z0-9_-]{4,4096}\\.[A-Za-z0-9_-]{0,4096}/g, REDACTED],\n // Incoming webhook URLs carry their secret in the path.\n [/(\\bhooks\\.slack\\.com\\/(?:services|workflows|triggers)\\/)[A-Za-z0-9/_-]{1,255}/gi, `$1${REDACTED}`],\n [/(\\bdiscord(?:app)?\\.com\\/api\\/(?:v\\d{1,2}\\/)?webhooks\\/)[A-Za-z0-9/_-]{1,255}/gi, `$1${REDACTED}`],\n // Well-known token shapes: AWS, GitHub, Slack, Stripe, Anthropic, OpenAI and Google style keys.\n [/\\b(?:AKIA|ASIA)[0-9A-Z]{16}\\b/g, REDACTED],\n [/\\b(?:gh[pousr]_[A-Za-z0-9]{30,255}|github_pat_[A-Za-z0-9_]{20,255})\\b/g, REDACTED],\n [/\\bxox[abposr]-[A-Za-z0-9-]{10,255}/g, REDACTED],\n [/\\b[rsp]k_(?:live|test)_[A-Za-z0-9]{10,255}\\b/g, REDACTED],\n [/\\bwhsec_[A-Za-z0-9+/=]{16,255}/g, REDACTED],\n [/\\bsk-[A-Za-z0-9_-]{20,255}/g, REDACTED],\n [/\\bAIza[0-9A-Za-z_-]{35}(?![0-9A-Za-z_-])/g, REDACTED],\n];\n\n/**\n * The default `redact`: blanks values that look like secrets (key=value pairs\n * with secret-ish names, Authorization headers, URL credentials, bearer\n * tokens, JWTs, PEM private keys, webhook URLs and well-known token formats)\n * before output or an error is stored, shown or sent anywhere.\n */\nexport function redactSecrets(text: string): string {\n let out = text;\n for (const [pattern, replacement] of SECRET_PATTERNS) out = out.replace(pattern, replacement as string);\n return out;\n}\n","import { stripJsonNul, stripNul } from \"../output.js\";\nimport type { JobState, Run, StoredJob, StoredJobDefinition } from \"../types.js\";\n\n/**\n * The schema, statements and row mapping shared by the SQLite and Postgres\n * stores. Imports no driver, so either entry point can pull it in alone.\n * Statements are written with `?` placeholders; Postgres numbers them.\n */\n\nexport type Dialect = \"sqlite\" | \"postgres\";\n\nexport const DEFAULT_PREFIX = \"cronwatch_\";\n\n// Postgres truncates identifiers past 63 bytes; the longest name we build is the prefix plus \"runs_job_started\".\nconst MAX_PREFIX = 63 - \"runs_job_started\".length;\n\n/**\n * Table names are built from the prefix, so it must be a plain lowercase\n * identifier. Uppercase is refused rather than folded: Postgres lowercases\n * unquoted names, so \"Monitoring_\" would quietly become \"monitoring_\".\n */\nexport function tablePrefix(prefix: string = DEFAULT_PREFIX): string {\n if (!/^[a-z_][a-z0-9_]*$/.test(prefix) || prefix.length > MAX_PREFIX) {\n throw new Error(\n `cronwatch: invalid table prefix ${JSON.stringify(prefix)}. Use lowercase letters, digits and underscores, ` +\n `not starting with a digit, at most ${MAX_PREFIX} characters.`,\n );\n }\n return prefix;\n}\n\nexport function schema(dialect: Dialect, p: string): string {\n const pg = dialect === \"postgres\";\n const int = pg ? \"BIGINT\" : \"INTEGER\";\n const json = pg ? \"JSONB\" : \"TEXT\";\n return `\n CREATE TABLE IF NOT EXISTS ${p}jobs (\n name TEXT PRIMARY KEY,\n definition ${json} NOT NULL,\n created_at ${int} NOT NULL,\n updated_at ${int} NOT NULL\n );\n CREATE TABLE IF NOT EXISTS ${p}runs (${pg ? \"\\n seq BIGSERIAL,\" : \"\"}\n id TEXT PRIMARY KEY,\n job TEXT NOT NULL,\n status TEXT NOT NULL,\n started_at ${int} NOT NULL,\n finished_at ${int},\n duration_ms ${int},\n error TEXT,\n output TEXT,\n metrics ${json} NOT NULL DEFAULT '{}',\n trigger TEXT NOT NULL DEFAULT 'run'\n );\n CREATE INDEX IF NOT EXISTS ${p}runs_job_started ON ${p}runs (job, started_at DESC);\n CREATE INDEX IF NOT EXISTS ${p}runs_running ON ${p}runs (status) WHERE status = 'running';\n CREATE TABLE IF NOT EXISTS ${p}state (\n job TEXT PRIMARY KEY,\n state ${json} NOT NULL\n );\n `;\n}\n\nexport function statements(dialect: Dialect, p: string) {\n const pg = dialect === \"postgres\";\n // Insertion order, to break ties between runs that started in the same millisecond.\n const seq = pg ? \"seq\" : \"rowid\";\n // Byte order on both, so names sort the same whatever the database's collation.\n const byName = pg ? `name COLLATE \"C\"` : \"name\";\n // The version inside a state's JSON, as stateVersion() reads it: a whole\n // number from 0 to 2^53 - 1, else 0 (none, or a foreign row's 1.5 or \"x\",\n // which must neither fail the statement nor refuse every write for good).\n // Each CASE tests the JSON type before any cast.\n const version = (column: string) => {\n if (pg) {\n const v = `(${column}->>'version')::numeric`;\n return `CASE WHEN jsonb_typeof(${column}->'version') <> 'number' THEN 0 WHEN ${v} % 1 = 0 AND ${v} BETWEEN 0 AND 9007199254740991 THEN ${v}::bigint ELSE 0 END`;\n }\n const v = `json_extract(${column}, '$.version')`;\n return `CASE WHEN json_type(${column}, '$.version') NOT IN ('integer', 'real') THEN 0 WHEN ${v} = CAST(${v} AS INTEGER) AND ${v} BETWEEN 0 AND 9007199254740991 THEN CAST(${v} AS INTEGER) ELSE 0 END`;\n };\n const sql = {\n upsertJob: `INSERT INTO ${p}jobs (name, definition, created_at, updated_at) VALUES (?, ?, ?, ?)\n ON CONFLICT (name) DO UPDATE SET definition = excluded.definition, updated_at = excluded.updated_at`,\n getJob: `SELECT * FROM ${p}jobs WHERE name = ?`,\n listJobs: `SELECT * FROM ${p}jobs ORDER BY ${byName}`,\n deleteRuns: `DELETE FROM ${p}runs WHERE job = ?`,\n deleteState: `DELETE FROM ${p}state WHERE job = ?`,\n deleteJob: `DELETE FROM ${p}jobs WHERE name = ?`,\n insertRun: `INSERT INTO ${p}runs (id, job, status, started_at, finished_at, duration_ms, error, output, metrics, trigger)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n updateRun: `UPDATE ${p}runs SET status = ?, finished_at = ?, duration_ms = ?, error = ?, output = ?, metrics = ? WHERE id = ?`,\n getRun: `SELECT * FROM ${p}runs WHERE id = ?`,\n listRuns: `SELECT * FROM ${p}runs WHERE job = ? ORDER BY started_at DESC, ${seq} DESC LIMIT ?`,\n runningRuns: `SELECT * FROM ${p}runs WHERE status = 'running' ORDER BY started_at, ${seq}`,\n getState: `SELECT state FROM ${p}state WHERE job = ?`,\n setState: `INSERT INTO ${p}state (job, state) VALUES (?, ?) ON CONFLICT (job) DO UPDATE SET state = excluded.state`,\n // compareAndSetState. Expecting version 0 also matches a missing row, so\n // that case inserts; any other version must find its row.\n casInsert: `INSERT INTO ${p}state (job, state) VALUES (?, ?)\n ON CONFLICT (job) DO UPDATE SET state = excluded.state WHERE ${version(`${p}state.state`)} = 0`,\n casUpdate: `UPDATE ${p}state SET state = ? WHERE job = ? AND ${version(\"state\")} = ?`,\n // Each job's newest run is kept whatever its age: without it, a job that\n // runs less often than the retention looks like it never ran.\n prune: `DELETE FROM ${p}runs WHERE status <> 'running' AND started_at < ?\n AND started_at < (SELECT MAX(r.started_at) FROM ${p}runs r WHERE r.job = ${p}runs.job)`,\n };\n if (pg) {\n for (const key of Object.keys(sql) as (keyof typeof sql)[]) {\n let n = 0;\n sql[key] = sql[key].replace(/\\?/g, () => `$${++n}`);\n }\n }\n return sql;\n}\n\n/**\n * updateRunIf: the update above, only while the stored status is one of\n * `count` statuses. Built per count, since the list is bound value by value.\n */\nexport function updateRunIfSql(dialect: Dialect, p: string, count: number): string {\n const text = `UPDATE ${p}runs SET status = ?, finished_at = ?, duration_ms = ?, error = ?, output = ?, metrics = ? WHERE id = ? AND status IN (${Array.from({ length: count }, () => \"?\").join(\", \")})`;\n if (dialect !== \"postgres\") return text;\n let n = 0;\n return text.replace(/\\?/g, () => `$${++n}`);\n}\n\n/**\n * Postgres refuses U+0000 in TEXT and JSONB, and a refused write loses the\n * whole row, so every dialect writes text without it: a run's trigger,\n * output, error and metric names, and every key and string of a definition\n * and a state. Identifiers (a job's name, a run's id) are written as given;\n * the client refuses one with a NUL before it gets here.\n */\nconst text = (value: string | null) => (value === null ? null : stripNul(value));\nconst jsonText = (value: unknown) => stripJsonNul(JSON.stringify(value));\n\n// Parameters in statement order, so both drivers bind the same values.\nexport const params = {\n upsertJob: (definition: StoredJobDefinition, now: number) => [definition.name, jsonText(definition), now, now],\n insertRun: (run: Run) => [\n run.id, run.job, run.status, run.startedAt, run.finishedAt, run.durationMs, text(run.error), text(run.output), jsonText(run.metrics), stripNul(run.trigger),\n ],\n updateRun: (run: Run) => [run.status, run.finishedAt, run.durationMs, text(run.error), text(run.output), jsonText(run.metrics), run.id],\n updateRunIf: (run: Run, fromStatuses: Run[\"status\"][]) => [\n run.status, run.finishedAt, run.durationMs, text(run.error), text(run.output), jsonText(run.metrics), run.id, ...fromStatuses,\n ],\n setState: (state: JobState) => [state.job, jsonText(state)],\n casInsert: (state: JobState) => [state.job, jsonText(state)],\n casUpdate: (state: JobState, expectedVersion: number) => [jsonText(state), state.job, expectedVersion],\n};\n\n// SQLite hands back JSON as TEXT and Postgres as parsed JSONB; Postgres returns BIGINT as a string.\ntype Json<T> = T | string;\ntype Int = number | string;\n\nexport interface JobRow { name: string; definition: Json<StoredJobDefinition>; created_at: Int; updated_at: Int }\nexport interface RunRow {\n id: string; job: string; status: Run[\"status\"]; started_at: Int; finished_at: Int | null;\n duration_ms: Int | null; error: string | null; output: string | null; metrics: Json<Record<string, number>> | null; trigger: string;\n}\nexport interface StateRow { state: Json<JobState> }\n\nconst json = <T>(v: Json<T>): T => (typeof v === \"string\" ? (JSON.parse(v) as T) : v);\nconst num = (v: Int | null): number | null => (v === null ? null : Number(v));\n\nexport function rowToJob(r: JobRow): StoredJob {\n return { name: r.name, definition: json(r.definition), createdAt: Number(r.created_at), updatedAt: Number(r.updated_at) };\n}\n\nexport function rowToRun(r: RunRow): Run {\n return {\n id: r.id, job: r.job, status: r.status, startedAt: Number(r.started_at), finishedAt: num(r.finished_at),\n durationMs: num(r.duration_ms), error: r.error, output: r.output, metrics: r.metrics === null ? {} : json(r.metrics), trigger: r.trigger,\n };\n}\n\nexport function rowToState(r: StateRow): JobState {\n return json(r.state);\n}\n","import type { Store, StoredJob } from \"../types.js\";\nimport { params, rowToJob, rowToRun, rowToState, schema, statements, tablePrefix, updateRunIfSql, type JobRow, type RunRow, type StateRow } from \"./sql.js\";\n\n/**\n * The parts of a Cloudflare D1 binding this store uses. Written out here so\n * the SDK needs nothing from @cloudflare/workers-types: a Worker's\n * `env.DB: D1Database` fits it as it is.\n */\nexport interface D1PreparedStatementLike {\n bind(...values: unknown[]): D1PreparedStatementLike;\n first(): Promise<unknown>;\n all(): Promise<{ results?: unknown[] }>;\n run(): Promise<{ meta?: { changes?: number } }>;\n}\n\nexport interface D1DatabaseLike {\n prepare(query: string): D1PreparedStatementLike;\n batch(statements: D1PreparedStatementLike[]): Promise<unknown[]>;\n}\n\nexport interface D1Options {\n /** Table name prefix: lowercase letters, digits and underscores. Default \"cronwatch_\". */\n prefix?: string;\n /**\n * Create the tables on first use (CREATE TABLE IF NOT EXISTS), once per\n * isolate. Default true. Pass false when a D1 migration creates them\n * instead (see the Cloudflare docs page for the file).\n */\n createTables?: boolean;\n}\n\n/** Databases (and prefixes) whose tables this isolate has already made sure of. */\nconst created = new WeakMap<object, Set<string>>();\n\n/**\n * Keeps everything in Cloudflare D1, for Workers. D1 is SQLite, so this is\n * the SQLite store's schema, statements and row mapping over D1's\n * prepare, bind and batch. Create it where the binding is, inside the\n * handler: `d1(env.DB)`.\n */\nexport function d1(database: D1DatabaseLike, options: D1Options = {}): Store {\n if (!database || typeof database.prepare !== \"function\" || typeof database.batch !== \"function\") {\n throw new Error(\"cronwatch: d1() needs a D1 database binding, such as env.DB\");\n }\n const p = tablePrefix(options.prefix);\n // D1 keeps its own tables under _cf_ and refuses writes to them.\n if (p.startsWith(\"_cf_\")) throw new Error(`cronwatch: invalid table prefix ${JSON.stringify(p)}. D1 reserves names starting with _cf_.`);\n const sql = statements(\"sqlite\", p);\n const createTables = options.createTables ?? true;\n\n const stmt = (text: string, values: unknown[] = []) => {\n const s = database.prepare(text);\n return values.length > 0 ? s.bind(...values) : s;\n };\n const first = async <T>(text: string, values: unknown[]): Promise<T | null> => ((await stmt(text, values).first()) as T | null) ?? null;\n const all = async <T>(text: string, values: unknown[] = []): Promise<T[]> => ((await stmt(text, values).all()).results ?? []) as T[];\n const changes = async (text: string, values: unknown[]): Promise<number> => (await stmt(text, values).run()).meta?.changes ?? 0;\n\n return {\n async init() {\n if (!createTables || created.get(database)?.has(p)) return;\n // exec() splits on newlines, so each statement goes in on its own, in one batch.\n const ddl = schema(\"sqlite\", p).split(\";\").map((s) => s.trim()).filter(Boolean);\n await database.batch(ddl.map((s) => database.prepare(s)));\n // Remembered only once it worked, and as a flag rather than a promise,\n // so no invocation ever waits on another's unfinished I/O.\n let prefixes = created.get(database);\n if (!prefixes) created.set(database, (prefixes = new Set()));\n prefixes.add(p);\n },\n async upsertJob(definition, now) {\n await stmt(sql.upsertJob, params.upsertJob(definition, now)).run();\n },\n async getJob(name) {\n const r = await first<JobRow>(sql.getJob, [name]);\n return r ? rowToJob(r) : null;\n },\n async listJobs(): Promise<StoredJob[]> {\n return (await all<JobRow>(sql.listJobs)).map(rowToJob);\n },\n async deleteJob(name) {\n // A batch is one transaction.\n await database.batch([stmt(sql.deleteRuns, [name]), stmt(sql.deleteState, [name]), stmt(sql.deleteJob, [name])]);\n },\n async insertRun(run) {\n await stmt(sql.insertRun, params.insertRun(run)).run();\n },\n async updateRun(run) {\n await stmt(sql.updateRun, params.updateRun(run)).run();\n },\n async updateRunIf(run, fromStatuses) {\n if (fromStatuses.length === 0) return false;\n return (await changes(updateRunIfSql(\"sqlite\", p, fromStatuses.length), params.updateRunIf(run, fromStatuses))) > 0;\n },\n async getRun(id) {\n const r = await first<RunRow>(sql.getRun, [id]);\n return r ? rowToRun(r) : null;\n },\n async listRuns(job, limit) {\n return (await all<RunRow>(sql.listRuns, [job, limit])).map(rowToRun);\n },\n async lastRun(job) {\n const r = await first<RunRow>(sql.listRuns, [job, 1]);\n return r ? rowToRun(r) : null;\n },\n async runningRuns() {\n return (await all<RunRow>(sql.runningRuns)).map(rowToRun);\n },\n async getState(job) {\n const r = await first<StateRow>(sql.getState, [job]);\n return r ? rowToState(r) : null;\n },\n async setState(state) {\n await stmt(sql.setState, params.setState(state)).run();\n },\n async compareAndSetState(state, expectedVersion) {\n const written = expectedVersion === 0\n ? await changes(sql.casInsert, params.casInsert(state))\n : await changes(sql.casUpdate, params.casUpdate(state, expectedVersion));\n return written > 0;\n },\n async prune(before) {\n return changes(sql.prune, [before]);\n },\n };\n}\n"]}
package/dist/d1.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { S as Store } from './types-Cz47Boks.cjs';
1
+ import { S as Store } from './types-DyWCxlSi.cjs';
2
2
 
3
3
  /**
4
4
  * The parts of a Cloudflare D1 binding this store uses. Written out here so
package/dist/d1.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { S as Store } from './types-Cz47Boks.js';
1
+ import { S as Store } from './types-DyWCxlSi.js';
2
2
 
3
3
  /**
4
4
  * The parts of a Cloudflare D1 binding this store uses. Written out here so