@connextar/house 0.22.2 → 0.23.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 (119) hide show
  1. package/README.md +574 -62
  2. package/dist/admin/index.d.ts +2 -2
  3. package/dist/admin/index.d.ts.map +1 -1
  4. package/dist/admin/index.js +2 -2
  5. package/dist/admin/index.js.map +1 -1
  6. package/dist/admin/proof.d.ts +49 -9
  7. package/dist/admin/proof.d.ts.map +1 -1
  8. package/dist/admin/proof.js +83 -4
  9. package/dist/admin/proof.js.map +1 -1
  10. package/dist/admin/second-factor.d.ts +3 -1
  11. package/dist/admin/second-factor.d.ts.map +1 -1
  12. package/dist/admin/second-factor.js +3 -2
  13. package/dist/admin/second-factor.js.map +1 -1
  14. package/dist/admin/view-as.d.ts +37 -5
  15. package/dist/admin/view-as.d.ts.map +1 -1
  16. package/dist/admin/view-as.js +67 -6
  17. package/dist/admin/view-as.js.map +1 -1
  18. package/dist/analytics/browser.d.ts +108 -0
  19. package/dist/analytics/browser.d.ts.map +1 -0
  20. package/dist/analytics/browser.js +319 -0
  21. package/dist/analytics/browser.js.map +1 -0
  22. package/dist/analytics/client-index.d.ts +6 -0
  23. package/dist/analytics/client-index.d.ts.map +1 -0
  24. package/dist/analytics/client-index.js +6 -0
  25. package/dist/analytics/client-index.js.map +1 -0
  26. package/dist/analytics/config.d.ts +50 -0
  27. package/dist/analytics/config.d.ts.map +1 -0
  28. package/dist/analytics/config.js +39 -0
  29. package/dist/analytics/config.js.map +1 -0
  30. package/dist/analytics/index.d.ts +10 -0
  31. package/dist/analytics/index.d.ts.map +1 -0
  32. package/dist/analytics/index.js +10 -0
  33. package/dist/analytics/index.js.map +1 -0
  34. package/dist/analytics/ingest.d.ts +30 -0
  35. package/dist/analytics/ingest.d.ts.map +1 -0
  36. package/dist/analytics/ingest.js +64 -0
  37. package/dist/analytics/ingest.js.map +1 -0
  38. package/dist/analytics/react-index.d.ts +6 -0
  39. package/dist/analytics/react-index.d.ts.map +1 -0
  40. package/dist/analytics/react-index.js +6 -0
  41. package/dist/analytics/react-index.js.map +1 -0
  42. package/dist/analytics/react.d.ts +75 -0
  43. package/dist/analytics/react.d.ts.map +1 -0
  44. package/dist/analytics/react.js +159 -0
  45. package/dist/analytics/react.js.map +1 -0
  46. package/dist/analytics/redact.d.ts +35 -0
  47. package/dist/analytics/redact.d.ts.map +1 -0
  48. package/dist/analytics/redact.js +109 -0
  49. package/dist/analytics/redact.js.map +1 -0
  50. package/dist/analytics/server-index.d.ts +6 -0
  51. package/dist/analytics/server-index.d.ts.map +1 -0
  52. package/dist/analytics/server-index.js +6 -0
  53. package/dist/analytics/server-index.js.map +1 -0
  54. package/dist/analytics/server.d.ts +89 -0
  55. package/dist/analytics/server.d.ts.map +1 -0
  56. package/dist/analytics/server.js +211 -0
  57. package/dist/analytics/server.js.map +1 -0
  58. package/dist/jobs/idle.d.ts +49 -0
  59. package/dist/jobs/idle.d.ts.map +1 -0
  60. package/dist/jobs/idle.js +61 -0
  61. package/dist/jobs/idle.js.map +1 -0
  62. package/dist/jobs/index.d.ts +2 -1
  63. package/dist/jobs/index.d.ts.map +1 -1
  64. package/dist/jobs/index.js +2 -1
  65. package/dist/jobs/index.js.map +1 -1
  66. package/dist/jobs/next-index.d.ts +5 -0
  67. package/dist/jobs/next-index.d.ts.map +1 -0
  68. package/dist/jobs/next-index.js +5 -0
  69. package/dist/jobs/next-index.js.map +1 -0
  70. package/dist/jobs/next.d.ts +25 -0
  71. package/dist/jobs/next.d.ts.map +1 -0
  72. package/dist/jobs/next.js +51 -0
  73. package/dist/jobs/next.js.map +1 -0
  74. package/dist/jobs/prisma-index.d.ts +6 -0
  75. package/dist/jobs/prisma-index.d.ts.map +1 -0
  76. package/dist/jobs/prisma-index.js +6 -0
  77. package/dist/jobs/prisma-index.js.map +1 -0
  78. package/dist/jobs/prisma.d.ts +77 -0
  79. package/dist/jobs/prisma.d.ts.map +1 -0
  80. package/dist/jobs/prisma.js +92 -0
  81. package/dist/jobs/prisma.js.map +1 -0
  82. package/dist/jobs/scheduler.d.ts +7 -0
  83. package/dist/jobs/scheduler.d.ts.map +1 -1
  84. package/dist/jobs/scheduler.js +10 -0
  85. package/dist/jobs/scheduler.js.map +1 -1
  86. package/dist/maintenance/react.d.ts +9 -2
  87. package/dist/maintenance/react.d.ts.map +1 -1
  88. package/dist/maintenance/react.js +15 -4
  89. package/dist/maintenance/react.js.map +1 -1
  90. package/dist/rate-limit/better-auth.d.ts +50 -0
  91. package/dist/rate-limit/better-auth.d.ts.map +1 -0
  92. package/dist/rate-limit/better-auth.js +64 -0
  93. package/dist/rate-limit/better-auth.js.map +1 -0
  94. package/dist/rate-limit/index.d.ts +13 -0
  95. package/dist/rate-limit/index.d.ts.map +1 -0
  96. package/dist/rate-limit/index.js +13 -0
  97. package/dist/rate-limit/index.js.map +1 -0
  98. package/dist/rate-limit/keys.d.ts +45 -0
  99. package/dist/rate-limit/keys.d.ts.map +1 -0
  100. package/dist/rate-limit/keys.js +95 -0
  101. package/dist/rate-limit/keys.js.map +1 -0
  102. package/dist/rate-limit/limiter.d.ts +114 -0
  103. package/dist/rate-limit/limiter.d.ts.map +1 -0
  104. package/dist/rate-limit/limiter.js +166 -0
  105. package/dist/rate-limit/limiter.js.map +1 -0
  106. package/dist/rate-limit/prisma-index.d.ts +6 -0
  107. package/dist/rate-limit/prisma-index.d.ts.map +1 -0
  108. package/dist/rate-limit/prisma-index.js +6 -0
  109. package/dist/rate-limit/prisma-index.js.map +1 -0
  110. package/dist/rate-limit/prisma.d.ts +116 -0
  111. package/dist/rate-limit/prisma.d.ts.map +1 -0
  112. package/dist/rate-limit/prisma.js +79 -0
  113. package/dist/rate-limit/prisma.js.map +1 -0
  114. package/dist/rate-limit/store.d.ts +27 -0
  115. package/dist/rate-limit/store.d.ts.map +1 -0
  116. package/dist/rate-limit/store.js +28 -0
  117. package/dist/rate-limit/store.js.map +1 -0
  118. package/docs/Maintenance.md +7 -2
  119. package/package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;AAED,wFAAwF;AACxF,MAAM,WAAW,aAAa;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IAC5C,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IACzC,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,0BAA0B;IACzC,aAAa,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,0FAA0F;IAC1F,eAAe,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,MAAM,CAAC,EAAE;QAAE,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAA;KAAE,CAAC;CACjE;AAED,wGAAwG;AACxG,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,0BAA0B;0BAIvC,aAAa,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;EAgC/E"}
@@ -0,0 +1,211 @@
1
+ const MESSAGE_LIMIT = 1000;
2
+ const FRAME = /^\s+at /;
3
+ export function createServerAnalytics(options) {
4
+ const { analytics } = options;
5
+ const logger = options.logger ?? console;
6
+ function safeMessage(source) {
7
+ if (source.name.startsWith("PrismaClient") || source.constructor?.name?.startsWith("PrismaClient")) {
8
+ const code = source.code;
9
+ return typeof code === "string" && /^[A-Z]\d{3,4}$/.test(code)
10
+ ? `Prisma ${code}`
11
+ : "Prisma error (message withheld)";
12
+ }
13
+ return analytics.redact(source.message).slice(0, MESSAGE_LIMIT);
14
+ }
15
+ /** A copy of the error that is safe to send: its name, a redacted message, and redacted frames only. */
16
+ function redactedError(error) {
17
+ const source = error instanceof Error ? error : new Error(String(error));
18
+ const copy = new Error(safeMessage(source));
19
+ copy.name = source.name;
20
+ if (source.stack) {
21
+ const head = source.message ? `${source.name}: ${source.message}` : source.name;
22
+ const body = source.stack.startsWith(head) ? source.stack.slice(head.length) : source.stack;
23
+ const frames = body.split("\n").filter((line) => FRAME.test(line));
24
+ copy.stack = [`${copy.name}: ${copy.message}`, ...frames.map((line) => analytics.redact(line))].join("\n");
25
+ }
26
+ else {
27
+ copy.stack = `${copy.name}: ${copy.message}`;
28
+ }
29
+ return copy;
30
+ }
31
+ async function reportServerError(error, properties = {}) {
32
+ const posthog = options.posthog();
33
+ if (!posthog)
34
+ return;
35
+ try {
36
+ await posthog.captureExceptionImmediate(redactedError(error), undefined, {
37
+ ...analytics.redactDeep(properties),
38
+ $process_person_profile: false,
39
+ });
40
+ }
41
+ catch (failure) {
42
+ logger.error("[analytics] an error report failed", failure);
43
+ }
44
+ }
45
+ function later(task) {
46
+ if (options.after) {
47
+ try {
48
+ options.after(task);
49
+ return;
50
+ }
51
+ catch {
52
+ // Outside a request scope `after` throws: send now instead.
53
+ }
54
+ }
55
+ void task();
56
+ }
57
+ /** For `handleError`'s 500s: sent once the response has gone. */
58
+ function reportServerErrorAfterResponse(error, properties = {}) {
59
+ if (!options.posthog())
60
+ return;
61
+ later(() => reportServerError(error, properties));
62
+ }
63
+ /** `export const onRequestError = server.onRequestError` in `instrumentation.ts`: the route pattern, never the URL. */
64
+ async function onRequestError(error, request, context) {
65
+ await reportServerError(error, {
66
+ source: "server",
67
+ route: context.routePath,
68
+ route_type: context.routeType,
69
+ method: request.method,
70
+ });
71
+ }
72
+ async function refused() {
73
+ try {
74
+ if (options.isViewingAs && (await options.isViewingAs()))
75
+ return true;
76
+ }
77
+ catch {
78
+ return true;
79
+ }
80
+ try {
81
+ const headers = await options.requestHeaders?.();
82
+ if (headers?.get("sec-gpc") === "1")
83
+ return true;
84
+ }
85
+ catch {
86
+ // Outside a request: no header to honour.
87
+ }
88
+ return false;
89
+ }
90
+ /** A product event for a person, its properties redacted; never during a view or against `Sec-GPC: 1`. */
91
+ async function captureServerEvent(distinctId, event, properties) {
92
+ const posthog = options.posthog();
93
+ if (!posthog || (await refused()))
94
+ return;
95
+ try {
96
+ posthog.capture({ distinctId, event, properties: properties && analytics.redactDeep(properties) });
97
+ await posthog.flush();
98
+ }
99
+ catch (error) {
100
+ logger.error("[analytics] a server event failed", error);
101
+ }
102
+ }
103
+ function captureServerEventAfterResponse(distinctId, event, properties) {
104
+ if (!options.posthog())
105
+ return;
106
+ later(() => captureServerEvent(distinctId, event, properties));
107
+ }
108
+ return {
109
+ redactedError,
110
+ reportServerError,
111
+ reportServerErrorAfterResponse,
112
+ onRequestError,
113
+ captureServerEvent,
114
+ captureServerEventAfterResponse,
115
+ /** For `register()`: puts the after-response reporter where the app's `handleError` finds it. */
116
+ registerErrorReporter(target = globalThis) {
117
+ Object.assign(target, { [analytics.errorReporterSymbol]: reportServerErrorAfterResponse });
118
+ },
119
+ };
120
+ }
121
+ /** The reporter `registerErrorReporter` put on `globalThis`, for `handleError` — a no-op until it is registered. */
122
+ export function registeredErrorReporter(analytics, target = globalThis) {
123
+ const found = target[analytics.errorReporterSymbol];
124
+ return typeof found === "function"
125
+ ? found
126
+ : () => { };
127
+ }
128
+ // ── GA4 Measurement Protocol ─────────────────────────────────────────────────
129
+ const ENDPOINT = "https://www.google-analytics.com/mp/collect";
130
+ function cookieValue(cookieHeader, name) {
131
+ for (const part of cookieHeader.split(";")) {
132
+ const index = part.indexOf("=");
133
+ if (index <= 0 || part.slice(0, index).trim() !== name)
134
+ continue;
135
+ const raw = part.slice(index + 1).trim();
136
+ try {
137
+ return decodeURIComponent(raw);
138
+ }
139
+ catch {
140
+ return raw;
141
+ }
142
+ }
143
+ return undefined;
144
+ }
145
+ /** `_ga`'s client id and the `_ga_<id>` session — both, or null. */
146
+ export function readGaCookies(cookieHeader, measurementId) {
147
+ const ga = cookieValue(cookieHeader, "_ga")?.match(/^GA\d\.\d+\.(\d+\.\d+)$/);
148
+ if (!ga)
149
+ return null;
150
+ const session = cookieValue(cookieHeader, `_ga_${measurementId.replace(/^G-/, "")}`);
151
+ const sessionId = session?.match(/^GS1\.\d+\.(\d+)\./)?.[1] ?? session?.match(/^GS2\.\d+\.s(\d+)/)?.[1];
152
+ if (!sessionId)
153
+ return null;
154
+ return { clientId: ga[1], sessionId };
155
+ }
156
+ /** How the account was made, from the Better Auth route that made it. */
157
+ export function signUpMethod(path, providerId) {
158
+ if (path === "/sign-up/email")
159
+ return "password";
160
+ if (path?.startsWith("/magic-link"))
161
+ return "magic_link";
162
+ if (path?.startsWith("/callback") || path?.startsWith("/oauth2/callback"))
163
+ return providerId ?? "social";
164
+ if (path?.startsWith("/passkey"))
165
+ return "passkey";
166
+ return "other";
167
+ }
168
+ /** `databaseHooks.user.create.after: async (_user, context) => recordSignUp(context)`. Never throws. */
169
+ export function measurementProtocol(options) {
170
+ const hosts = new Set(options.productionHosts);
171
+ const logger = options.logger ?? console;
172
+ return {
173
+ async recordSignUp(context) {
174
+ const { measurementId, apiSecret } = options;
175
+ if (!measurementId || !apiSecret || !context)
176
+ return;
177
+ try {
178
+ const headers = context.headers ?? context.request?.headers;
179
+ if (!headers || !hosts.has(headers.get("host") ?? ""))
180
+ return;
181
+ if (headers.get("sec-gpc") === "1")
182
+ return;
183
+ const cookies = readGaCookies(headers.get("cookie") ?? "", measurementId);
184
+ if (!cookies)
185
+ return;
186
+ const url = `${ENDPOINT}?measurement_id=${encodeURIComponent(measurementId)}&api_secret=${encodeURIComponent(apiSecret)}`;
187
+ await (options.fetch ?? fetch)(url, {
188
+ method: "POST",
189
+ body: JSON.stringify({
190
+ client_id: cookies.clientId,
191
+ events: [
192
+ {
193
+ name: "sign_up",
194
+ params: {
195
+ method: signUpMethod(context.path, context.params?.id),
196
+ engagement_time_msec: 1,
197
+ session_id: cookies.sessionId,
198
+ },
199
+ },
200
+ ],
201
+ }),
202
+ signal: AbortSignal.timeout(2000),
203
+ });
204
+ }
205
+ catch (error) {
206
+ logger.error("[analytics] could not record a sign-up", error);
207
+ }
208
+ },
209
+ };
210
+ }
211
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/analytics/server.ts"],"names":[],"mappings":"AA2CA,MAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,MAAM,KAAK,GAAG,SAAS,CAAC;AAIxB,MAAM,UAAU,qBAAqB,CAAC,OAA+B;IACnE,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;IAC9B,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,OAAO,CAAC;IAEzC,SAAS,WAAW,CAAC,MAAa;QAChC,IAAI,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,IAAI,MAAM,CAAC,WAAW,EAAE,IAAI,EAAE,UAAU,CAAC,cAAc,CAAC,EAAE,CAAC;YACnG,MAAM,IAAI,GAAI,MAAqC,CAAC,IAAI,CAAC;YACzD,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC;gBAC5D,CAAC,CAAC,UAAU,IAAI,EAAE;gBAClB,CAAC,CAAC,iCAAiC,CAAC;QACxC,CAAC;QACD,OAAO,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC;IAClE,CAAC;IAED,wGAAwG;IACxG,SAAS,aAAa,CAAC,KAAc;QACnC,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QACzE,MAAM,IAAI,GAAG,IAAI,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;QAC5C,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACxB,IAAI,MAAM,CAAC,KA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G,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;oBAAE,OAAO;gBAC9D,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,GAAG;oBAAE,OAAO;gBAC3C,MAAM,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,aAAa,CAAC,CAAC;gBAC1E,IAAI,CAAC,OAAO;oBAAE,OAAO;gBACrB,MAAM,GAAG,GAAG,GAAG,QAAQ,mBAAmB,kBAAkB,CAAC,aAAa,CAAC,eAAe,kBAAkB,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC1H,MAAM,CAAC,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC,GAAG,EAAE;oBAClC,MAAM,EAAE,MAAM;oBACd,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;wBACnB,SAAS,EAAE,OAAO,CAAC,QAAQ;wBAC3B,MAAM,EAAE;4BACN;gCACE,IAAI,EAAE,SAAS;gCACf,MAAM,EAAE;oCACN,MAAM,EAAE,YAAY,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;oCACtD,oBAAoB,EAAE,CAAC;oCACvB,UAAU,EAAE,OAAO,CAAC,SAAS;iCAC9B;6BACF;yBACF;qBACF,CAAC;oBACF,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC;iBAClC,CAAC,CAAC;YACL,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,CAAC,KAAK,CAAC,wCAAwC,EAAE,KAAK,CAAC,CAAC;YAChE,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,49 @@
1
+ import { type Scheduler, type TickResult } from "./scheduler.js";
2
+ /**
3
+ * The idle tick: a tick route that leaves a serverless database asleep between windows.
4
+ *
5
+ * Vercel Cron calls the tick every few minutes, and most calls have nothing to do: every job's window is one it has
6
+ * already claimed. Asking the database to find that out keeps a serverless database (Neon) awake around the clock;
7
+ * AltEd measured about twenty compute hours a day spent that way. The scheduler already knows every job's window, so
8
+ * it can say, without the database, whether anything could have changed since the last call — the signature, every
9
+ * tick job's window key now — and the tick runs once per signature. The rest answer from a cache.
10
+ *
11
+ * Quickie wrote this first, with a module-level `ranJustNow` flag to tell a cached answer from a fresh run. One
12
+ * instance serves concurrent calls (Vercel's Fluid compute), so the flag was shared between them and a response could
13
+ * say "idle" for a run or "ran" for a cached answer. Here each call has its own answer, `{ status: "ran" | "idle" }`.
14
+ */
15
+ /**
16
+ * Runs `run` once per signature and answers later calls with the same signature from a cache. `fresh` says whether
17
+ * this call ran it. If `run` throws, nothing is kept and the error comes back to this call.
18
+ * `@connextar/house/jobs/next` has one over Next's data cache; `memoryTickMemo` is one for tests.
19
+ */
20
+ export type TickMemo = (signature: string, run: () => Promise<TickResult[]>) => Promise<{
21
+ results: TickResult[];
22
+ fresh: boolean;
23
+ }>;
24
+ export type IdleTickOutcome = {
25
+ status: "ran";
26
+ signature: string;
27
+ results: TickResult[];
28
+ summary: string;
29
+ } | {
30
+ status: "idle";
31
+ signature: string;
32
+ summary: string;
33
+ };
34
+ /** A job that failed before claiming its window — its switches unreadable, its store down — must be tried again. */
35
+ export declare const tickUnfinished: (results: readonly TickResult[]) => boolean;
36
+ /**
37
+ * The tick route's body, once per signature: `{ status: "ran", results }` when this call ran the scheduler,
38
+ * `{ status: "idle" }` when an earlier call already ran it for the same windows. A tick in which a job failed before
39
+ * claiming its window is not remembered, so the next call runs it again.
40
+ *
41
+ * Expire the memo whenever something the signature cannot see changes — a job switched back on inside its window —
42
+ * or that job waits for its next window (`expireIdleTick` in `/jobs/next`).
43
+ */
44
+ export declare function idleTick(scheduler: Scheduler, memo: TickMemo, now?: Date): Promise<IdleTickOutcome>;
45
+ /** A `TickMemo` in memory — for tests, and for a long-lived server with one process. */
46
+ export declare function memoryTickMemo(): TickMemo & {
47
+ clear(): void;
48
+ };
49
+ //# sourceMappingURL=idle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idle.d.ts","sourceRoot":"","sources":["../../src/jobs/idle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,SAAS,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEhF;;;;;;;;;;;;GAYG;AAEH;;;;GAIG;AACH,MAAM,MAAM,QAAQ,GAAG,CACrB,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,MAAM,OAAO,CAAC,UAAU,EAAE,CAAC,KAC7B,OAAO,CAAC;IAAE,OAAO,EAAE,UAAU,EAAE,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC;AAExD,MAAM,MAAM,eAAe,GACvB;IAAE,MAAM,EAAE,KAAK,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,UAAU,EAAE,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAC5E;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAU3D,oHAAoH;AACpH,eAAO,MAAM,cAAc,GAAI,SAAS,SAAS,UAAU,EAAE,YACc,CAAC;AAE5E;;;;;;;GAOG;AACH,wBAAsB,QAAQ,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,GAAE,IAAiB,GAAG,OAAO,CAAC,eAAe,CAAC,CAgBrH;AAED,wFAAwF;AACxF,wBAAgB,cAAc,IAAI,QAAQ,GAAG;IAAE,KAAK,IAAI,IAAI,CAAA;CAAE,CAgB7D"}
@@ -0,0 +1,61 @@
1
+ import { summariseTick } from "./scheduler.js";
2
+ /** A tick that could not finish its work: thrown out of the memo so nothing is kept, and the next call runs again. */
3
+ class UnfinishedTick extends Error {
4
+ results;
5
+ constructor(results) {
6
+ super("The tick did not finish: a job failed before it could claim its window.");
7
+ this.results = results;
8
+ this.name = "UnfinishedTick";
9
+ }
10
+ }
11
+ /** A job that failed before claiming its window — its switches unreadable, its store down — must be tried again. */
12
+ export const tickUnfinished = (results) => results.some((result) => result.status === "failed" && !result.windowKey);
13
+ /**
14
+ * The tick route's body, once per signature: `{ status: "ran", results }` when this call ran the scheduler,
15
+ * `{ status: "idle" }` when an earlier call already ran it for the same windows. A tick in which a job failed before
16
+ * claiming its window is not remembered, so the next call runs it again.
17
+ *
18
+ * Expire the memo whenever something the signature cannot see changes — a job switched back on inside its window —
19
+ * or that job waits for its next window (`expireIdleTick` in `/jobs/next`).
20
+ */
21
+ export async function idleTick(scheduler, memo, now = new Date()) {
22
+ const signature = scheduler.signature(now);
23
+ try {
24
+ const { results, fresh } = await memo(signature, async () => {
25
+ const results = await scheduler.tick(now);
26
+ if (tickUnfinished(results))
27
+ throw new UnfinishedTick(results);
28
+ return results;
29
+ });
30
+ if (!fresh)
31
+ return { status: "idle", signature, summary: "Nothing new since the last run in these windows" };
32
+ return { status: "ran", signature, results, summary: summariseTick(results) };
33
+ }
34
+ catch (error) {
35
+ if (error instanceof UnfinishedTick) {
36
+ return { status: "ran", signature, results: error.results, summary: summariseTick(error.results) };
37
+ }
38
+ throw error;
39
+ }
40
+ }
41
+ /** A `TickMemo` in memory — for tests, and for a long-lived server with one process. */
42
+ export function memoryTickMemo() {
43
+ const kept = new Map();
44
+ const memo = (async (signature, run) => {
45
+ const known = kept.get(signature);
46
+ if (known)
47
+ return { results: await known, fresh: false };
48
+ const running = run();
49
+ kept.set(signature, running);
50
+ try {
51
+ return { results: await running, fresh: true };
52
+ }
53
+ catch (error) {
54
+ kept.delete(signature);
55
+ throw error;
56
+ }
57
+ });
58
+ memo.clear = () => kept.clear();
59
+ return memo;
60
+ }
61
+ //# sourceMappingURL=idle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idle.js","sourceRoot":"","sources":["../../src/jobs/idle.ts"],"names":[],"mappings":"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AC;QACf,IAAI,KAAK,YAAY,cAAc,EAAE,CAAC;YACpC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;QACrG,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED,wFAAwF;AACxF,MAAM,UAAU,cAAc;IAC5B,MAAM,IAAI,GAAG,IAAI,GAAG,EAAiC,CAAC;IACtD,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,EAAE;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAClC,IAAI,KAAK;YAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;QACzD,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAC7B,IAAI,CAAC;YACH,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;QACjD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YACvB,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC,CAAiC,CAAC;IACnC,IAAI,CAAC,KAAK,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IAChC,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -21,7 +21,8 @@ export { countLines, countsSummary, declaredCounts, describeSpan, formatCount, f
21
21
  export { buildJobView, deriveHealth, describeHealth, describeRun, describeSwitch, formatDay, formatWhen, runNowFor, runNowScopesFor, type HealthInput, type HealthWords, type JobAudience, type JobFacts, type JobHealth, type JobHealthState, type JobRunView, type JobView, type RunFacts, type WhenOptions, } from "./health.js";
22
22
  export { DEFAULT_COOLDOWN_MS, DEFAULT_TIMEOUT_MS, MAX_JOB_ID, MAX_WINDOW_KEY, defineJob, defineJobs, jobDefinitionProblems, runNowScopes, type AnyJob, type JobActor, type JobContext, type JobDefinition, type RunNowRule, type RunNowScope, } from "./registry.js";
23
23
  export { DEFAULT_JOB_RUN_PRUNE_BATCH, DEFAULT_JOB_RUN_RETENTION_DAYS, JOB_RUN_PRUNE_COUNTS, pruneJobRuns, type JobRunPruneCounts, type PruneJobRunsOptions, } from "./prune.js";
24
- export { createScheduler, refusalStatus, summariseTick, type RunNowCaller, type RunNowOutcome, type RunNowRefusal, type Scheduler, type SchedulerOptions, type SwitchCaller, type SwitchOutcome, type SwitchRefusal, type TickResult, type TickStatus, } from "./scheduler.js";
24
+ export { idleTick, memoryTickMemo, tickUnfinished, type IdleTickOutcome, type TickMemo } from "./idle.js";
25
+ export { createScheduler, refusalStatus, summariseTick, tickSignature, type RunNowCaller, type RunNowOutcome, type RunNowRefusal, type Scheduler, type SchedulerOptions, type SwitchCaller, type SwitchOutcome, type SwitchRefusal, type TickResult, type TickStatus, } from "./scheduler.js";
25
26
  export { createMemoryJobStore, pausedFromStored, type JobRunClaim, type JobRunOutcome, type JobRunPrune, type JobRunQuery, type JobRunRecord, type JobStore, type JobSwitchChange, type JobSwitchState, } from "./store.js";
26
27
  export { MANUAL_KEY_PREFIX, daily, everyHours, hourly, isManualKey, monthly, weekly, type CalendarWindowOptions, type JobWindow, } from "./windows.js";
27
28
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAAE,gBAAgB,EAAE,KAAK,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC7F,OAAO,EACL,UAAU,EACV,aAAa,EACb,cA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ChB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,eAAe,EACpB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,iBAAiB,EACjB,KAAK,EACL,UAAU,EACV,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,EACN,KAAK,qBAAqB,EAC1B,KAAK,SAAS,GACf,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAAE,gBAAgB,EAAE,KAAK,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC7F,OAAO,EACL,UAAU,EACV,aAAa,EACb,cAAc,EACd,YAAY,EACZ,WAAW,EACX,cAAc,EACd,WAAW,EACX,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACvB,KAAK,SAAS,EACd,KAAK,UAAU,EACf,KAAK,SAAS,EACd,KAAK,WAAW,GACjB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,WAAW,EACX,cAAc,EACd,SAAS,EACT,UAAU,EACV,SAAS,EACT,eAAe,EACf,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,KAAK,WAAW,GACjB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,UAAU,EACV,cAAc,EACd,SAAS,EACT,UAAU,EACV,qBAAqB,EACrB,YAAY,EACZ,KAAK,MAAM,EACX,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,WAAW,GACjB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,2BAA2B,EAC3B,8BAA8B,EAC9B,oBAAoB,EACpB,YAAY,EACZ,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,GACzB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,cAAc,EAAE,KAAK,eAAe,EAAE,KAAK,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC1G,OAAO,EACL,eAAe,EACf,aAAa,EACb,aAAa,EACb,aAAa,EACb,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,gBAAgB,EACrB,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,KAAK,UAAU,GAChB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,oBAAoB,EACpB,gBAAgB,EAChB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,QAAQ,EACb,KAAK,eAAe,EACpB,KAAK,cAAc,GACpB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,iBAAiB,EACjB,KAAK,EACL,UAAU,EACV,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,EACN,KAAK,qBAAqB,EAC1B,KAAK,SAAS,GACf,MAAM,cAAc,CAAC"}
@@ -21,7 +21,8 @@ export { countLines, countsSummary, declaredCounts, describeSpan, formatCount, f
21
21
  export { buildJobView, deriveHealth, describeHealth, describeRun, describeSwitch, formatDay, formatWhen, runNowFor, runNowScopesFor, } from "./health.js";
22
22
  export { DEFAULT_COOLDOWN_MS, DEFAULT_TIMEOUT_MS, MAX_JOB_ID, MAX_WINDOW_KEY, defineJob, defineJobs, jobDefinitionProblems, runNowScopes, } from "./registry.js";
23
23
  export { DEFAULT_JOB_RUN_PRUNE_BATCH, DEFAULT_JOB_RUN_RETENTION_DAYS, JOB_RUN_PRUNE_COUNTS, pruneJobRuns, } from "./prune.js";
24
- export { createScheduler, refusalStatus, summariseTick, } from "./scheduler.js";
24
+ export { idleTick, memoryTickMemo, tickUnfinished } from "./idle.js";
25
+ export { createScheduler, refusalStatus, summariseTick, tickSignature, } from "./scheduler.js";
25
26
  export { createMemoryJobStore, pausedFromStored, } from "./store.js";
26
27
  export { MANUAL_KEY_PREFIX, daily, everyHours, hourly, isManualKey, monthly, weekly, } from "./windows.js";
27
28
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAAE,gBAAgB,EAA6C,MAAM,gBAAgB,CAAC;AAC7F,OAAO,EACL,UAAU,EACV,aAAa,EACb,cAAc,EACd,YAAY,EACZ,WAAW,EACX,cAAc,EACd,WAAW,GAQZ,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,WAAW,EACX,cAAc,EACd,SAAS,EACT,UAAU,EACV,SAAS,EACT,eAAe,GAWhB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,UAAU,EACV,cAAc,EACd,SAAS,EACT,UAAU,EACV,qBAAqB,EACrB,YAAY,GAOb,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,2BAA2B,EAC3B,8BAA8B,EAC9B,oBAAoB,EACpB,YAAY,GAGb,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,eAAe,EACf,aAAa,EACb,aAAa,GAWd,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,oBAAoB,EACpB,gBAAgB,GASjB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,iBAAiB,EACjB,KAAK,EACL,UAAU,EACV,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,GAGP,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,EAAE,gBAAgB,EAA6C,MAAM,gBAAgB,CAAC;AAC7F,OAAO,EACL,UAAU,EACV,aAAa,EACb,cAAc,EACd,YAAY,EACZ,WAAW,EACX,cAAc,EACd,WAAW,GAQZ,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,WAAW,EACX,cAAc,EACd,SAAS,EACT,UAAU,EACV,SAAS,EACT,eAAe,GAWhB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,UAAU,EACV,cAAc,EACd,SAAS,EACT,UAAU,EACV,qBAAqB,EACrB,YAAY,GAOb,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,2BAA2B,EAC3B,8BAA8B,EAC9B,oBAAoB,EACpB,YAAY,GAGb,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,cAAc,EAAuC,MAAM,WAAW,CAAC;AAC1G,OAAO,EACL,eAAe,EACf,aAAa,EACb,aAAa,EACb,aAAa,GAWd,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,oBAAoB,EACpB,gBAAgB,GASjB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,iBAAiB,EACjB,KAAK,EACL,UAAU,EACV,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,GAGP,MAAM,cAAc,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * `@connextar/house/jobs/next` — the idle tick's memo over Next's data cache. The only jobs entry that imports Next.
3
+ */
4
+ export { DEFAULT_TICK_TAG, expireIdleTick, nextTickMemo, type NextTickMemoOptions } from "./next.js";
5
+ //# sourceMappingURL=next-index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next-index.d.ts","sourceRoot":"","sources":["../../src/jobs/next-index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,YAAY,EAAE,KAAK,mBAAmB,EAAE,MAAM,WAAW,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * `@connextar/house/jobs/next` — the idle tick's memo over Next's data cache. The only jobs entry that imports Next.
3
+ */
4
+ export { DEFAULT_TICK_TAG, expireIdleTick, nextTickMemo } from "./next.js";
5
+ //# sourceMappingURL=next-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next-index.js","sourceRoot":"","sources":["../../src/jobs/next-index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,YAAY,EAA4B,MAAM,WAAW,CAAC"}
@@ -0,0 +1,25 @@
1
+ import type { TickMemo } from "./idle.js";
2
+ /**
3
+ * `TickMemo` over Next's shared data cache, for `idleTick`: the first call with a signature runs the tick, and every
4
+ * other call in those windows — on any instance — answers from the cache without touching the database.
5
+ *
6
+ * Each call builds its own cached function, closing over its own `fresh` flag. Next keys `unstable_cache` on the
7
+ * function's source, the key parts and the arguments, never on the closure, so every call shares one entry while each
8
+ * knows for itself whether it ran. Quickie's single module-level flag was shared by concurrent calls on one instance.
9
+ *
10
+ * Outside Next's runtime (a test calling the route handler) there is no data cache, and the tick simply runs.
11
+ */
12
+ export declare const DEFAULT_TICK_TAG = "cron-tick";
13
+ export interface NextTickMemoOptions {
14
+ /** The cache tag, to expire with `expireIdleTick`. Default `cron-tick`. */
15
+ tag?: string;
16
+ /** Read afresh at least this often, in seconds, whatever the signature. Default an hour. */
17
+ revalidateSeconds?: number;
18
+ }
19
+ export declare function nextTickMemo(options?: NextTickMemoOptions): TickMemo;
20
+ /**
21
+ * Expire the remembered tick, so the next call runs the scheduler: call it where something the signature cannot see
22
+ * changes — a job switched back on inside its window. Outside a request there is nothing cached to expire.
23
+ */
24
+ export declare function expireIdleTick(tag?: string): void;
25
+ //# sourceMappingURL=next.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next.d.ts","sourceRoot":"","sources":["../../src/jobs/next.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAG1C;;;;;;;;;GASG;AAEH,eAAO,MAAM,gBAAgB,cAAc,CAAC;AAE5C,MAAM,WAAW,mBAAmB;IAClC,2EAA2E;IAC3E,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,4FAA4F;IAC5F,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAID,wBAAgB,YAAY,CAAC,OAAO,GAAE,mBAAwB,GAAG,QAAQ,CAyBxE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,GAAG,GAAE,MAAyB,GAAG,IAAI,CAMnE"}
@@ -0,0 +1,51 @@
1
+ import { revalidateTag, unstable_cache } from "next/cache.js";
2
+ /**
3
+ * `TickMemo` over Next's shared data cache, for `idleTick`: the first call with a signature runs the tick, and every
4
+ * other call in those windows — on any instance — answers from the cache without touching the database.
5
+ *
6
+ * Each call builds its own cached function, closing over its own `fresh` flag. Next keys `unstable_cache` on the
7
+ * function's source, the key parts and the arguments, never on the closure, so every call shares one entry while each
8
+ * knows for itself whether it ran. Quickie's single module-level flag was shared by concurrent calls on one instance.
9
+ *
10
+ * Outside Next's runtime (a test calling the route handler) there is no data cache, and the tick simply runs.
11
+ */
12
+ export const DEFAULT_TICK_TAG = "cron-tick";
13
+ const missingCache = (error) => error instanceof Error && /incrementalCache missing/i.test(error.message);
14
+ export function nextTickMemo(options = {}) {
15
+ const tag = options.tag ?? DEFAULT_TICK_TAG;
16
+ const revalidate = options.revalidateSeconds ?? 60 * 60;
17
+ if (!Number.isFinite(revalidate) || revalidate <= 0) {
18
+ throw new Error("[jobs] nextTickMemo's revalidateSeconds must be more than 0.");
19
+ }
20
+ return async (signature, run) => {
21
+ let fresh = false;
22
+ const cached = unstable_cache(
23
+ // The signature is the cache key: an argument so Next keys on it, unused in the body.
24
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
25
+ async (_signature) => {
26
+ fresh = true;
27
+ return run();
28
+ }, ["house-jobs-tick", tag], { tags: [tag], revalidate });
29
+ try {
30
+ return { results: await cached(signature), fresh };
31
+ }
32
+ catch (error) {
33
+ if (!fresh && missingCache(error))
34
+ return { results: await run(), fresh: true };
35
+ throw error;
36
+ }
37
+ };
38
+ }
39
+ /**
40
+ * Expire the remembered tick, so the next call runs the scheduler: call it where something the signature cannot see
41
+ * changes — a job switched back on inside its window. Outside a request there is nothing cached to expire.
42
+ */
43
+ export function expireIdleTick(tag = DEFAULT_TICK_TAG) {
44
+ try {
45
+ revalidateTag(tag, { expire: 0 });
46
+ }
47
+ catch {
48
+ // No request scope: nothing cached here to expire.
49
+ }
50
+ }
51
+ //# sourceMappingURL=next.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next.js","sourceRoot":"","sources":["../../src/jobs/next.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAK9D;;;;;;;;;GASG;AAEH,MAAM,CAAC,MAAM,gBAAgB,GAAG,WAAW,CAAC;AAS5C,MAAM,YAAY,GAAG,CAAC,KAAc,EAAE,EAAE,CAAC,KAAK,YAAY,KAAK,IAAI,2BAA2B,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AAEnH,MAAM,UAAU,YAAY,CAAC,UAA+B,EAAE;IAC5D,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,gBAAgB,CAAC;IAC5C,MAAM,UAAU,GAAG,OAAO,CAAC,iBAAiB,IAAI,EAAE,GAAG,EAAE,CAAC;IACxD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,UAAU,IAAI,CAAC,EAAE,CAAC;QACpD,MAAM,IAAI,KAAK,CAAC,8DAA8D,CAAC,CAAC;IAClF,CAAC;IACD,OAAO,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,EAAE;QAC9B,IAAI,KAAK,GAAG,KAAK,CAAC;QAClB,MAAM,MAAM,GAAG,cAAc;QAC3B,sFAAsF;QACtF,6DAA6D;QAC7D,KAAK,EAAE,UAAkB,EAAyB,EAAE;YAClD,KAAK,GAAG,IAAI,CAAC;YACb,OAAO,GAAG,EAAE,CAAC;QACf,CAAC,EACD,CAAC,iBAAiB,EAAE,GAAG,CAAC,EACxB,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,UAAU,EAAE,CAC5B,CAAC;QACF,IAAI,CAAC;YACH,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,CAAC;QACrD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,KAAK,IAAI,YAAY,CAAC,KAAK,CAAC;gBAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;YAChF,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,MAAc,gBAAgB;IAC3D,IAAI,CAAC;QACH,aAAa,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;IACpC,CAAC;IAAC,MAAM,CAAC;QACP,mDAAmD;IACrD,CAAC;AACH,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `@connextar/house/jobs/prisma` — the reference `JobStore` over the reference `CronRun` model, claiming a window
3
+ * with `INSERT … ON CONFLICT DO NOTHING`. Imports nothing: the Prisma client is passed in.
4
+ */
5
+ export { prismaJobStore, pruneRunsSql, type CronRunDelegate, type CronRunRow, type PrismaJobStoreOptions, type RawSql, } from "./prisma.js";
6
+ //# sourceMappingURL=prisma-index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prisma-index.d.ts","sourceRoot":"","sources":["../../src/jobs/prisma-index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EACL,cAAc,EACd,YAAY,EACZ,KAAK,eAAe,EACpB,KAAK,UAAU,EACf,KAAK,qBAAqB,EAC1B,KAAK,MAAM,GACZ,MAAM,aAAa,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * `@connextar/house/jobs/prisma` — the reference `JobStore` over the reference `CronRun` model, claiming a window
3
+ * with `INSERT … ON CONFLICT DO NOTHING`. Imports nothing: the Prisma client is passed in.
4
+ */
5
+ export { prismaJobStore, pruneRunsSql, } from "./prisma.js";
6
+ //# sourceMappingURL=prisma-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prisma-index.js","sourceRoot":"","sources":["../../src/jobs/prisma-index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EACL,cAAc,EACd,YAAY,GAKb,MAAM,aAAa,CAAC"}
@@ -0,0 +1,77 @@
1
+ import type { JobStore } from "./store.js";
2
+ /**
3
+ * The reference `JobStore` over the reference `CronRun` model (the README's), for any app on Prisma and Postgres.
4
+ *
5
+ * AltEd, Quickie and senco each wrote this adapter from the README, and two of the three still claim a window the way
6
+ * the README used to say: a plain insert, with the unique index's refusal caught as P2002. Postgres raises and logs
7
+ * that refusal as an error and rolls the statement back, every time — AltEd's server counted 127 of them a night
8
+ * before v0.173.4 moved to `INSERT … ON CONFLICT DO NOTHING`, which Prisma writes as `createManyAndReturn` with
9
+ * `skipDuplicates`: a second claim comes back empty, "already ran", and nothing is logged. This is that adapter.
10
+ *
11
+ * Typed structurally, so the package imports no Prisma: pass `prisma.cronRun` as `runs` and `prisma` as `sql`. An app
12
+ * whose Prisma types object passes them through `as never`; the shape is checked by the tests here, not by Prisma.
13
+ *
14
+ * The switches stay the app's (`readSwitches`, `writeSwitch`): AltEd keeps them in a settings table it shares with a
15
+ * screen of its own, Quickie in `cron_switch`, senco as a log in `cron_switch_change`, and the audit entry has to be
16
+ * written in the app's own transaction.
17
+ */
18
+ /** A row of the reference `CronRun` model, as Prisma answers it. */
19
+ export interface CronRunRow {
20
+ id: string;
21
+ job: string;
22
+ windowKey: string;
23
+ tenantId: string | null;
24
+ triggeredBy: string | null;
25
+ startedAt: Date;
26
+ finishedAt: Date | null;
27
+ ok: boolean;
28
+ counts: unknown;
29
+ error: string | null;
30
+ }
31
+ /** The part of `prisma.cronRun` the store calls. `createManyAndReturn` needs Prisma 5.14 or later, on Postgres. */
32
+ export interface CronRunDelegate {
33
+ createManyAndReturn(args: {
34
+ data: {
35
+ job: string;
36
+ windowKey: string;
37
+ tenantId: string | null;
38
+ triggeredBy: string | null;
39
+ startedAt: Date;
40
+ }[];
41
+ skipDuplicates: true;
42
+ select: {
43
+ id: true;
44
+ };
45
+ }): Promise<{
46
+ id: string;
47
+ }[]>;
48
+ update(args: {
49
+ where: {
50
+ id: string;
51
+ };
52
+ data: Record<string, unknown>;
53
+ }): Promise<unknown>;
54
+ findFirst(args: {
55
+ where: Record<string, unknown>;
56
+ orderBy: Record<string, "asc" | "desc">[];
57
+ }): Promise<CronRunRow | null>;
58
+ }
59
+ /** `prisma` itself, for the one statement Prisma's query API cannot say: pruning by window function. */
60
+ export interface RawSql {
61
+ $executeRawUnsafe(query: string, ...values: unknown[]): Promise<number>;
62
+ }
63
+ export interface PrismaJobStoreOptions {
64
+ /** `prisma.cronRun`. */
65
+ runs: CronRunDelegate;
66
+ /** `prisma` — needed only for `pruneRuns`; without it, `pruneJobRuns` says so by name. */
67
+ sql?: RawSql;
68
+ /** The table and columns as they are in the database, for `pruneRuns`. Default: the reference model's. */
69
+ table?: string;
70
+ columns?: Partial<Record<"id" | "job" | "tenantId" | "ok" | "startedAt" | "finishedAt", string>>;
71
+ readSwitches: JobStore["readSwitches"];
72
+ writeSwitch: JobStore["writeSwitch"];
73
+ }
74
+ /** One statement, at most `limit` rows, oldest first, never a run under way, a job's latest run or its latest success. */
75
+ export declare function pruneRunsSql(table?: string, columns?: PrismaJobStoreOptions["columns"]): string;
76
+ export declare function prismaJobStore(options: PrismaJobStoreOptions): Required<JobStore>;
77
+ //# sourceMappingURL=prisma.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prisma.d.ts","sourceRoot":"","sources":["../../src/jobs/prisma.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAgB,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGzD;;;;;;;;;;;;;;;GAeG;AAEH,oEAAoE;AACpE,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,SAAS,EAAE,IAAI,CAAC;IAChB,UAAU,EAAE,IAAI,GAAG,IAAI,CAAC;IACxB,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,mHAAmH;AACnH,MAAM,WAAW,eAAe;IAC9B,mBAAmB,CAAC,IAAI,EAAE;QACxB,IAAI,EAAE;YACJ,GAAG,EAAE,MAAM,CAAC;YACZ,SAAS,EAAE,MAAM,CAAC;YAClB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;YACxB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;YAC3B,SAAS,EAAE,IAAI,CAAC;SACjB,EAAE,CAAC;QACJ,cAAc,EAAE,IAAI,CAAC;QACrB,MAAM,EAAE;YAAE,EAAE,EAAE,IAAI,CAAA;SAAE,CAAC;KACtB,GAAG,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC,CAAC;IAC9B,MAAM,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE;YAAE,EAAE,EAAE,MAAM,CAAA;SAAE,CAAC;QA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