@rebasepro/server 0.16.0 → 0.16.1-canary.g041c925

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 (210) hide show
  1. package/dist/{GCSStorageController-BEmDYFKc.js → GCSStorageController-Bl7nmhdv.js} +7 -6
  2. package/dist/{GCSStorageController-BEmDYFKc.js.map → GCSStorageController-Bl7nmhdv.js.map} +1 -1
  3. package/dist/{S3StorageController-B2EDXXMH.js → S3StorageController-CWvvrRpF.js} +7 -6
  4. package/dist/{S3StorageController-B2EDXXMH.js.map → S3StorageController-CWvvrRpF.js.map} +1 -1
  5. package/dist/{admin_block-DLILvzle.js → admin_block-BeypnEfb.js} +6 -6
  6. package/dist/admin_block-BeypnEfb.js.map +1 -0
  7. package/dist/api/ast-schema-editor.d.ts +23 -0
  8. package/dist/api/contract-routes.d.ts +1 -1
  9. package/dist/api/errors.d.ts +1 -1
  10. package/dist/api/index.d.ts +3 -3
  11. package/dist/api/live-schema-routes.d.ts +122 -0
  12. package/dist/api/logs-routes.d.ts +1 -1
  13. package/dist/api/mount.d.ts +41 -0
  14. package/dist/api/rest/api-generator.d.ts +2 -2
  15. package/dist/api/rest/index.d.ts +1 -1
  16. package/dist/api/rest/query-parser.d.ts +1 -1
  17. package/dist/api/schema-editor-routes.d.ts +1 -1
  18. package/dist/api/types.d.ts +12 -4
  19. package/dist/{schema-editor-routes-CV9k0w3G.js → ast-schema-editor-CDGgrIn7.js} +37 -45
  20. package/dist/ast-schema-editor-CDGgrIn7.js.map +1 -0
  21. package/dist/auth/adapter-middleware.d.ts +2 -2
  22. package/dist/auth/admin-roles-route.d.ts +2 -2
  23. package/dist/auth/admin-user-ops.d.ts +3 -3
  24. package/dist/auth/admin-users-route.d.ts +4 -6
  25. package/dist/auth/api-keys/api-key-middleware.d.ts +2 -2
  26. package/dist/auth/api-keys/api-key-permission-guard.d.ts +1 -1
  27. package/dist/auth/api-keys/api-key-routes.d.ts +2 -2
  28. package/dist/auth/api-keys/api-key-store.d.ts +1 -1
  29. package/dist/auth/api-keys/index.d.ts +9 -9
  30. package/dist/auth/apple-oauth.d.ts +2 -2
  31. package/dist/auth/auth-hooks.d.ts +4 -4
  32. package/dist/auth/bitbucket-oauth.d.ts +2 -2
  33. package/dist/auth/builtin-auth-adapter.d.ts +8 -4
  34. package/dist/auth/capabilities.d.ts +51 -0
  35. package/dist/auth/captcha.d.ts +86 -0
  36. package/dist/auth/cookie-utils.d.ts +2 -2
  37. package/dist/auth/discord-oauth.d.ts +2 -2
  38. package/dist/auth/facebook-oauth.d.ts +2 -2
  39. package/dist/auth/github-oauth.d.ts +2 -2
  40. package/dist/auth/gitlab-oauth.d.ts +2 -2
  41. package/dist/auth/google-oauth.d.ts +1 -1
  42. package/dist/auth/index.d.ts +58 -54
  43. package/dist/auth/jwks-routes.d.ts +1 -1
  44. package/dist/auth/jwt.d.ts +1 -1
  45. package/dist/auth/linkedin-oauth.d.ts +2 -2
  46. package/dist/auth/magic-link-routes.d.ts +10 -3
  47. package/dist/auth/mfa-gate.d.ts +1 -1
  48. package/dist/auth/mfa-routes.d.ts +3 -3
  49. package/dist/auth/microsoft-oauth.d.ts +2 -2
  50. package/dist/auth/middleware.d.ts +6 -7
  51. package/dist/auth/otp-routes.d.ts +89 -0
  52. package/dist/auth/rate-limiter.d.ts +2 -2
  53. package/dist/auth/require-auth.d.ts +1 -1
  54. package/dist/auth/reset-password-admin.d.ts +4 -4
  55. package/dist/auth/routes.d.ts +18 -10
  56. package/dist/auth/session-routes.d.ts +3 -3
  57. package/dist/auth/slack-oauth.d.ts +2 -2
  58. package/dist/auth/spotify-oauth.d.ts +2 -2
  59. package/dist/auth/token-revocation.d.ts +2 -2
  60. package/dist/auth/twitter-oauth.d.ts +2 -2
  61. package/dist/{auth-5Et5mnUA.js → auth-DzarZhLS.js} +603 -4376
  62. package/dist/auth-DzarZhLS.js.map +1 -0
  63. package/dist/backup/backup-common.d.ts +1 -1
  64. package/dist/backup/backup-routes.d.ts +3 -3
  65. package/dist/backup/index.d.ts +3 -3
  66. package/dist/{backup-C6ljYVTp.js → backup-CRdZkA6c.js} +7 -6
  67. package/dist/{backup-C6ljYVTp.js.map → backup-CRdZkA6c.js.map} +1 -1
  68. package/dist/boot/bindings.d.ts +81 -0
  69. package/dist/boot/boot.d.ts +5 -5
  70. package/dist/boot/bundle.d.ts +10 -1
  71. package/dist/boot/driver.d.ts +1 -1
  72. package/dist/boot/env.d.ts +28 -1
  73. package/dist/boot/fetch-bundle.d.ts +144 -1
  74. package/dist/boot/local-provisioner.d.ts +30 -0
  75. package/dist/boot/options.d.ts +30 -6
  76. package/dist/boot/resource-adapters.d.ts +31 -0
  77. package/dist/boot/resource-loading.d.ts +10 -0
  78. package/dist/boot/role.d.ts +3 -2
  79. package/dist/boot/sources.d.ts +1 -1
  80. package/dist/boot/version-skew.d.ts +1 -1
  81. package/dist/collections/BackendCollectionRegistry.d.ts +1 -1
  82. package/dist/collections/loader.d.ts +1 -1
  83. package/dist/{contract-routes-DZ-LBpSL.js → contract-routes-DDSRyGSl.js} +8 -7
  84. package/dist/contract-routes-DDSRyGSl.js.map +1 -0
  85. package/dist/cron/cron-routes.d.ts +2 -2
  86. package/dist/cron/cron-scheduler.d.ts +3 -3
  87. package/dist/cron/index.d.ts +8 -8
  88. package/dist/{cron-loader-YhhQeVBM.js → cron-loader-d9WMFENB.js} +8 -7
  89. package/dist/{cron-loader-YhhQeVBM.js.map → cron-loader-d9WMFENB.js.map} +1 -1
  90. package/dist/{cron-routes-maM_RlUu.js → cron-routes-B_GKclr5.js} +9 -8
  91. package/dist/cron-routes-B_GKclr5.js.map +1 -0
  92. package/dist/{cron-scheduler-DIpYBmZP.js → cron-scheduler-BB82dWuU.js} +9 -9
  93. package/dist/cron-scheduler-BB82dWuU.js.map +1 -0
  94. package/dist/{cron-store-DfH_4Cd9.js → cron-store-BTZqwkM1.js} +10 -9
  95. package/dist/{cron-store-DfH_4Cd9.js.map → cron-store-BTZqwkM1.js.map} +1 -1
  96. package/dist/{ddl-bootstrap-Cywoj8Ta.js → ddl-bootstrap-euwN8FOy.js} +21 -6
  97. package/dist/ddl-bootstrap-euwN8FOy.js.map +1 -0
  98. package/dist/deploy/pod-contract.d.ts +139 -0
  99. package/dist/dev-secrets.d.ts +52 -0
  100. package/dist/{dynamic-import-Dvh-K5fl.js → dynamic-import-X40pTZUQ.js} +5 -4
  101. package/dist/{dynamic-import-Dvh-K5fl.js.map → dynamic-import-X40pTZUQ.js.map} +1 -1
  102. package/dist/email/dev-sink.d.ts +88 -0
  103. package/dist/email/index.d.ts +9 -7
  104. package/dist/email/link-base.d.ts +1 -1
  105. package/dist/email/smtp-email-service.d.ts +2 -2
  106. package/dist/email/templates.d.ts +12 -0
  107. package/dist/email/types.d.ts +18 -6
  108. package/dist/env.d.ts +1 -1
  109. package/dist/{errors-EBYiaJ2E.js → errors-DBwpj9N8.js} +8 -7
  110. package/dist/errors-DBwpj9N8.js.map +1 -0
  111. package/dist/{function-loader-DDS1v7YX.js → function-loader-LLdmBFoL.js} +9 -8
  112. package/dist/{function-loader-DDS1v7YX.js.map → function-loader-LLdmBFoL.js.map} +1 -1
  113. package/dist/{function-routes-Btcez1T-.js → function-routes-ClT6UQpD.js} +8 -7
  114. package/dist/function-routes-ClT6UQpD.js.map +1 -0
  115. package/dist/functions/context.d.ts +141 -0
  116. package/dist/functions/define-function.d.ts +1 -1
  117. package/dist/functions/function-routes.d.ts +9 -3
  118. package/dist/functions/guards.d.ts +76 -0
  119. package/dist/functions/index.d.ts +96 -5
  120. package/dist/functions/index.js +919 -0
  121. package/dist/functions/index.js.map +1 -0
  122. package/dist/functions/internal.d.ts +26 -0
  123. package/dist/functions/proxy.d.ts +1 -1
  124. package/dist/functions/request-timeout.d.ts +9 -1
  125. package/dist/functions/runtime-env.d.ts +92 -0
  126. package/dist/functions/wait-until.d.ts +74 -0
  127. package/dist/history/history-routes.d.ts +2 -2
  128. package/dist/history/index.d.ts +1 -1
  129. package/dist/index.d.ts +65 -54
  130. package/dist/index.es.js +3498 -305
  131. package/dist/index.es.js.map +1 -1
  132. package/dist/init/docs.d.ts +1 -1
  133. package/dist/init/middlewares.d.ts +1 -1
  134. package/dist/init/shutdown.d.ts +4 -0
  135. package/dist/init/storage.d.ts +1 -1
  136. package/dist/init/surfaces.d.ts +10 -0
  137. package/dist/init.d.ts +171 -22
  138. package/dist/jobs/index.d.ts +5 -5
  139. package/dist/{jobs-CyOKXXlu.js → jobs-CeZzdeR8.js} +10 -9
  140. package/dist/{jobs-CyOKXXlu.js.map → jobs-CeZzdeR8.js.map} +1 -1
  141. package/dist/{jwt-DxH9fLPt.js → jwt-CpMqxVct.js} +9 -8
  142. package/dist/{jwt-DxH9fLPt.js.map → jwt-CpMqxVct.js.map} +1 -1
  143. package/dist/{logger-DfvF_8r-.js → logger-DS03e908.js} +104 -10
  144. package/dist/logger-DS03e908.js.map +1 -0
  145. package/dist/{logs-routes-CWBLQj2l.js → logs-routes-DB72iQSr.js} +36 -9
  146. package/dist/logs-routes-DB72iQSr.js.map +1 -0
  147. package/dist/metrics/index.d.ts +1 -1
  148. package/dist/{openapi-generator-BCKJRUS4.js → openapi-generator-wFJo22gg.js} +7 -11
  149. package/dist/{openapi-generator-BCKJRUS4.js.map → openapi-generator-wFJo22gg.js.map} +1 -1
  150. package/dist/{proxy-Bj5DVllb.js → proxy-CMymhnwG.js} +9 -6
  151. package/dist/{proxy-Bj5DVllb.js.map → proxy-CMymhnwG.js.map} +1 -1
  152. package/dist/query-parser-C3hHu17g.js +355 -0
  153. package/dist/query-parser-C3hHu17g.js.map +1 -0
  154. package/dist/{request-timeout-BuFoEKwT.js → request-timeout-C8gkc-j7.js} +21 -6
  155. package/dist/request-timeout-C8gkc-j7.js.map +1 -0
  156. package/dist/rls-audit/index.d.ts +111 -0
  157. package/dist/{rolldown-runtime-DSJWtz9O.js → rolldown-runtime-dW7B1o5h.js} +4 -3
  158. package/dist/schema-edit/apply-schema-change.d.ts +141 -0
  159. package/dist/schema-edit/github-repository.d.ts +67 -0
  160. package/dist/schema-edit/local-git-repository.d.ts +28 -0
  161. package/dist/schema-edit/project-root.d.ts +43 -0
  162. package/dist/schema-edit/remote-source.d.ts +25 -0
  163. package/dist/schema-edit/schema-edit-permissions.d.ts +129 -0
  164. package/dist/schema-editor-routes-CRKMZnXu.js +87 -0
  165. package/dist/schema-editor-routes-CRKMZnXu.js.map +1 -0
  166. package/dist/schemas-DBxgjM9A.js +4153 -0
  167. package/dist/schemas-DBxgjM9A.js.map +1 -0
  168. package/dist/{selection-_z6TM1DB.js → selection-CRpqKUbt.js} +6 -5
  169. package/dist/{selection-_z6TM1DB.js.map → selection-CRpqKUbt.js.map} +1 -1
  170. package/dist/services/webhook-service.d.ts +1 -1
  171. package/dist/singleton.d.ts +7 -0
  172. package/dist/{src-CrCxd8km.js → src-6VqGNeDB.js} +122 -5
  173. package/dist/src-6VqGNeDB.js.map +1 -0
  174. package/dist/{src-BPfYOeN4.js → src-C5vFEj-t.js} +8 -6
  175. package/dist/src-C5vFEj-t.js.map +1 -0
  176. package/dist/storage/GCSStorageController.d.ts +1 -1
  177. package/dist/storage/LocalStorageController.d.ts +1 -1
  178. package/dist/storage/S3StorageController.d.ts +1 -1
  179. package/dist/storage/cache-headers.d.ts +87 -0
  180. package/dist/storage/index.d.ts +11 -11
  181. package/dist/storage/path-pattern.d.ts +58 -0
  182. package/dist/storage/policies.d.ts +88 -0
  183. package/dist/storage/range.d.ts +63 -0
  184. package/dist/storage/rendition-cache.d.ts +45 -0
  185. package/dist/storage/routes.d.ts +17 -5
  186. package/dist/storage/storage-registry.d.ts +1 -1
  187. package/dist/storage/triggers.d.ts +66 -0
  188. package/dist/storage/tus-handler.d.ts +34 -3
  189. package/dist/topics/runtime.d.ts +68 -0
  190. package/dist/{types-DSnOC4mF.js → types-BfKcm9do.js} +5 -4
  191. package/dist/{types-DSnOC4mF.js.map → types-BfKcm9do.js.map} +1 -1
  192. package/dist/utils/host.d.ts +58 -0
  193. package/dist/utils/logger.d.ts +0 -15
  194. package/dist/utils/request-id.d.ts +1 -1
  195. package/functions/package.json +24 -0
  196. package/package.json +13 -7
  197. package/dist/admin_block-DLILvzle.js.map +0 -1
  198. package/dist/auth-5Et5mnUA.js.map +0 -1
  199. package/dist/contract-routes-DZ-LBpSL.js.map +0 -1
  200. package/dist/cron-routes-maM_RlUu.js.map +0 -1
  201. package/dist/cron-scheduler-DIpYBmZP.js.map +0 -1
  202. package/dist/ddl-bootstrap-Cywoj8Ta.js.map +0 -1
  203. package/dist/errors-EBYiaJ2E.js.map +0 -1
  204. package/dist/function-routes-Btcez1T-.js.map +0 -1
  205. package/dist/logger-DfvF_8r-.js.map +0 -1
  206. package/dist/logs-routes-CWBLQj2l.js.map +0 -1
  207. package/dist/request-timeout-BuFoEKwT.js.map +0 -1
  208. package/dist/schema-editor-routes-CV9k0w3G.js.map +0 -1
  209. package/dist/src-BPfYOeN4.js.map +0 -1
  210. package/dist/src-CrCxd8km.js.map +0 -1
@@ -1,6 +1,6 @@
1
1
  import { Hono } from "hono";
2
2
  import { CollectionConfig } from "@rebasepro/types";
3
- import { HonoEnv } from "../api/types";
3
+ import { HonoEnv } from "../api/types.js";
4
4
  /**
5
5
  * Serve the OpenAPI document, and Swagger UI outside production.
6
6
  *
@@ -1,5 +1,5 @@
1
1
  import { Hono } from "hono";
2
- import { HonoEnv } from "../api/types";
2
+ import { HonoEnv } from "../api/types.js";
3
3
  interface MiddlewareConfig {
4
4
  maxBodySize?: number;
5
5
  compression?: boolean;
@@ -9,6 +9,10 @@ interface ShutdownConfig {
9
9
  jobQueue?: {
10
10
  stop(): Promise<void>;
11
11
  };
12
+ /** Structural, same reason. */
13
+ rlsAudit?: {
14
+ stop(): void;
15
+ };
12
16
  realtimeServices: Record<string, RealtimeProvider>;
13
17
  }
14
18
  /**
@@ -1,4 +1,4 @@
1
- import { BackendStorageConfig, StorageController, StorageRegistry } from "../storage";
1
+ import { BackendStorageConfig, StorageController, StorageRegistry } from "../storage/index.js";
2
2
  export declare function initializeStorage(storageConfig: BackendStorageConfig | StorageController | Record<string, BackendStorageConfig | StorageController> | undefined, isProduction: boolean): Promise<{
3
3
  storageRegistry?: StorageRegistry;
4
4
  storageController?: StorageController;
@@ -59,6 +59,16 @@ export interface RuntimeOwnership {
59
59
  cronScheduler: boolean;
60
60
  /** Start the durable job queue's workers. Only relevant when `jobs.enabled`. */
61
61
  jobWorkers: boolean;
62
+ /**
63
+ * Run the scheduled row-level-security audit. Only relevant when
64
+ * `rlsAudit.enabled`.
65
+ *
66
+ * Unlike the two above, this one is not claim-protected — every process that
67
+ * owns it introspects the catalog on its own timer. That is read-only and
68
+ * harmless, just wasteful, so a split deployment should give it to one
69
+ * process the same way it gives it cron.
70
+ */
71
+ rlsAudit: boolean;
62
72
  }
63
73
  /** A fully-resolved answer for every owned singleton. */
64
74
  export type ResolvedOwnership = RuntimeOwnership;
package/dist/init.d.ts CHANGED
@@ -1,17 +1,42 @@
1
- import { AuthAdapter, BackendBootstrapper, BootstrappedAuth, DatabaseAdapter, DataDriver, DataSourceDefinition, CollectionCallbacks, AnyCollectionConfig, HealthCheckResult, HistoryConfig, InitializedDriver, RealtimeProvider } from "@rebasepro/types";
2
- import { BackendCollectionRegistry } from "./collections/BackendCollectionRegistry";
3
- import { DriverRegistry } from "./services/driver-registry";
1
+ import { AuthAdapter, BackendBootstrapper, BootstrappedAuth, DatabaseAdapter, DataDriver, CollectionCallbacks, AnyCollectionConfig, HealthCheckResult, HistoryConfig, InitializedDriver, RealtimeProvider } from "@rebasepro/types";
2
+ import { BackendCollectionRegistry } from "./collections/BackendCollectionRegistry.js";
3
+ import { DriverRegistry } from "./services/driver-registry.js";
4
4
  import { Server } from "http";
5
5
  import { Hono } from "hono";
6
- import { HonoEnv } from "./api/types";
7
- import { type RuntimeOwnershipOptions, type RuntimeSurfaceOptions } from "./init/surfaces";
8
- import type { JwtSigningKeyConfig } from "./auth/jwt-keys";
9
- import type { JobQueueOptions } from "./jobs/types";
10
- import { BackendStorageConfig, StorageController, StorageRegistry } from "./storage";
11
- import { type DataRateLimitConfig } from "./auth/rate-limiter";
12
- import { EmailConfig } from "./email";
13
- import type { OAuthProvider } from "./auth/interfaces";
14
- import type { AuthHooks } from "./auth/auth-hooks";
6
+ import { HonoEnv } from "./api/types.js";
7
+ import type { SchemaEditPolicy, RemoteRepositoryConfig } from "./schema-edit/schema-edit-permissions.js";
8
+ /**
9
+ * Who may change this project's schema through a running backend.
10
+ *
11
+ * An alias rather than its own shape: the policy is defined next to the code
12
+ * that enforces it, and a second declaration here would be a second place for
13
+ * it to drift.
14
+ *
15
+ * @see `packages/server/src/schema-edit/schema-edit-permissions.ts`
16
+ */
17
+ export interface LiveSchemaConfig extends SchemaEditPolicy {
18
+ /**
19
+ * A repository to commit to when the source is not on this machine.
20
+ *
21
+ * A bundle deployment ships compiled output, so there is nothing for the
22
+ * collection editor to rewrite locally. Configure this and the change is
23
+ * read from the repository, rewritten, and committed back through the Git
24
+ * Data API — no clone, no working tree.
25
+ *
26
+ * Absent, live schema editing needs the project mounted and says so.
27
+ */
28
+ repository?: RemoteRepositoryConfig;
29
+ }
30
+ import { type RlsAuditConfig } from "./rls-audit/index.js";
31
+ import type { CaptchaConfig } from "./auth/captcha.js";
32
+ import { type RuntimeOwnershipOptions, type RuntimeSurfaceOptions } from "./init/surfaces.js";
33
+ import type { JwtSigningKeyConfig } from "./auth/jwt-keys.js";
34
+ import type { JobQueueOptions } from "./jobs/types.js";
35
+ import { BackendStorageConfig, StorageController, StorageRegistry } from "./storage/index.js";
36
+ import { type DataRateLimitConfig } from "./auth/rate-limiter.js";
37
+ import { EmailConfig } from "./email/index.js";
38
+ import type { OAuthProvider } from "./auth/interfaces.js";
39
+ import type { AuthHooks } from "./auth/auth-hooks.js";
15
40
  export interface RebaseAuthConfig {
16
41
  /**
17
42
  * The collection that represents auth users.
@@ -87,6 +112,22 @@ export interface RebaseAuthConfig {
87
112
  * by any signed-in user. Available on the client as `auth.findUserByEmail`.
88
113
  */
89
114
  allowUserLookup?: boolean;
115
+ /**
116
+ * Bot protection on the auth routes that cost something to hit.
117
+ *
118
+ * Off unless `enabled`. Rate limiting bounds one caller; a botnet of a
119
+ * thousand addresses sending one request each never touches a per-IP window,
120
+ * and `/register` and `/forgot-password` both send mail — so the bill for an
121
+ * unprotected form is paid in reputation on a sending domain.
122
+ *
123
+ * ```ts
124
+ * auth: { captcha: { enabled: true, provider: "turnstile", secret: process.env.TURNSTILE_SECRET } }
125
+ * ```
126
+ *
127
+ * Verification fails closed: if the provider cannot be reached, the request
128
+ * is refused. See `auth/captcha.ts` for why that is the safe direction.
129
+ */
130
+ captcha?: CaptchaConfig;
90
131
  /**
91
132
  * A static secret key for server-to-server / script authentication.
92
133
  *
@@ -218,6 +259,18 @@ export interface RebaseAuthConfig {
218
259
  * Requires email to be configured.
219
260
  */
220
261
  magicLink?: boolean;
262
+ /**
263
+ * Enable email one-time codes — six digits, typed in, instead of a link.
264
+ *
265
+ * A magic link opens the session on whichever device holds the mailbox,
266
+ * which is the wrong device whenever those are two different machines: a
267
+ * television, a terminal, a second browser. A code crosses that gap because
268
+ * a person carries it.
269
+ *
270
+ * `POST /auth/otp` sends one; `POST /auth/otp/verify` trades it for a
271
+ * session. Requires email to be configured, as magic link does.
272
+ */
273
+ emailOtp?: boolean;
221
274
  /**
222
275
  * Opt-in httpOnly cookie mode for refresh tokens.
223
276
  *
@@ -231,7 +284,7 @@ export interface RebaseAuthConfig {
231
284
  * **Requires** `credentials: "include"` on client-side fetch calls to
232
285
  * auth endpoints, and CORS must allow credentials (no `origin: "*"`).
233
286
  */
234
- cookieAuth?: import("./auth").CookieAuthConfig;
287
+ cookieAuth?: import("./auth/index.js").CookieAuthConfig;
235
288
  }
236
289
  /** @see RebaseBackendConfig.baas */
237
290
  export interface BaasOptions {
@@ -278,6 +331,15 @@ export interface RebaseBackendConfig {
278
331
  * `collectionsDir` to write to.
279
332
  */
280
333
  schemaEditor?: boolean;
334
+ /**
335
+ * Live schema editing — changing the database and committing the change.
336
+ *
337
+ * Distinct from `schemaEditor`, which only rewrites collection source.
338
+ * There is nothing to switch on here: the surface mounts whenever there is
339
+ * a `collectionsDir`, and refuses in its own words when it cannot act. What
340
+ * this configures is *who* may act.
341
+ */
342
+ liveSchema?: LiveSchemaConfig;
281
343
  /** Options that only apply when collections are derived from the database. */
282
344
  baas?: BaasOptions;
283
345
  /**
@@ -303,7 +365,6 @@ export interface RebaseBackendConfig {
303
365
  * still owns their schema/registry but does **not** generate server data
304
366
  * routes for them. Server-mediated sources (the default) need no entry.
305
367
  */
306
- dataSources?: DataSourceDefinition[];
307
368
  /**
308
369
  * Database bootstrappers.
309
370
  */
@@ -350,7 +411,6 @@ export interface RebaseBackendConfig {
350
411
  * only need explicit entries for "direct" transport sources (e.g.
351
412
  * external storage) that the backend does not proxy.
352
413
  */
353
- storageSources?: import("@rebasepro/types").StorageSourceDefinition[];
354
414
  /**
355
415
  * Per-object access control for storage — the analogue of a collection's
356
416
  * security rules, and the thing `requireAuth` / `publicRead` cannot
@@ -375,7 +435,31 @@ export interface RebaseBackendConfig {
375
435
  * {@link storagePublicRead}, or {@link storageInsecureAllowAnyAuthenticated}
376
436
  * is set — see `assertStorageAccessControlConfigured`.
377
437
  */
378
- storageAuthorize?: import("./storage/types").StorageAuthorize;
438
+ storageAuthorize?: import("./storage/types.js").StorageAuthorize;
439
+ /**
440
+ * Per-object access control, declared rather than coded.
441
+ *
442
+ * Storage is not under row-level security, so without this or
443
+ * {@link storageAuthorize} any authenticated caller may read, overwrite,
444
+ * delete or list any key they can name — the keys share one flat namespace.
445
+ *
446
+ * ```ts
447
+ * storagePolicies: [
448
+ * { path: "public/**", operations: ["read"], allow: "public" },
449
+ * { path: "users/:uid/**", allow: ({ params, user }) => user?.uid === params.uid }
450
+ * ]
451
+ * ```
452
+ *
453
+ * **A key matched by no policy is refused.** Every widening is an explicit
454
+ * line, and a mistake here denies rather than grants.
455
+ *
456
+ * `storageAuthorize` still runs when no policy matched, so ownership that
457
+ * lives in a row — which no path pattern can express — stays expressible.
458
+ * Neither can narrow the other: each only says yes.
459
+ *
460
+ * Satisfies the production boot guard on its own.
461
+ */
462
+ storagePolicies?: import("./storage/policies.js").StoragePolicy[];
379
463
  /**
380
464
  * Allow unauthenticated read access to stored files (default: false).
381
465
  *
@@ -385,6 +469,59 @@ export interface RebaseBackendConfig {
385
469
  * production storage boot guard (see {@link storageAuthorize}).
386
470
  */
387
471
  storagePublicRead?: boolean;
472
+ /**
473
+ * Keep derived image renditions — the results of `?width=`, `?format=` and
474
+ * friends — in the storage source rather than only in this process's
475
+ * memory.
476
+ *
477
+ * Without it a transform is recomputed by every instance, and again by all
478
+ * of them after every deploy: the cache is per-process and does not survive
479
+ * a restart. With it, the first instance to compute a variant writes it
480
+ * under a reserved prefix in the same bucket as the source, and every
481
+ * instance reads it from there.
482
+ *
483
+ * ```ts
484
+ * storageRenditionCache: { enabled: true }
485
+ * ```
486
+ *
487
+ * Off by default because turning it on makes a `GET` write to your bucket:
488
+ * it costs per object, and on read-only credentials it cannot work. When a
489
+ * write does fail the request still succeeds — the transform is served from
490
+ * memory and the failure is logged once.
491
+ *
492
+ * Renditions are keyed by the source object's version, so replacing an
493
+ * image serves the new one immediately; the superseded rendition is left
494
+ * behind. Set a lifecycle rule on `_rebase/renditions/` to collect them.
495
+ */
496
+ storageRenditionCache?: import("./storage/rendition-cache.js").RenditionCacheConfig;
497
+ /**
498
+ * Run something when an object lands, or when one goes.
499
+ *
500
+ * Every other write in the product can be reacted to — a row has
501
+ * `beforeSave` and `afterSave`, a schedule has a cron job — and an upload
502
+ * had nothing, so the work an upload implies had to be done by the client
503
+ * after the upload returned, which means it is not done at all when the
504
+ * client goes away between the two calls.
505
+ *
506
+ * ```ts
507
+ * storageTriggers: [
508
+ * {
509
+ * path: "uploads/:uid/**",
510
+ * events: ["finalize"],
511
+ * handler: async ({ key, params }) => { … }
512
+ * }
513
+ * ]
514
+ * ```
515
+ *
516
+ * Same pattern language as {@link storagePolicies}. Fires for the
517
+ * multipart and resumable paths alike, after the object is durably
518
+ * written, and never for internal writes such as the image-rendition cache.
519
+ * Handlers are awaited and a throwing one is logged rather than failing the
520
+ * request — the object is already stored, so telling the client otherwise
521
+ * would invite it to repeat a write that succeeded. Enqueue a job for
522
+ * anything that must happen or is slow.
523
+ */
524
+ storageTriggers?: import("./storage/triggers.js").StorageTrigger[];
388
525
  /**
389
526
  * Opt out of the storage access-control boot guard, keeping the legacy
390
527
  * behaviour where **any** authenticated user can read, overwrite, delete or
@@ -462,7 +599,7 @@ export interface RebaseBackendConfig {
462
599
  *
463
600
  * A name that is not in the bundle fails the boot — see `selectFunctions`.
464
601
  */
465
- functionsSelection?: import("./functions/selection").FunctionSelection;
602
+ functionsSelection?: import("./functions/selection.js").FunctionSelection;
466
603
  /**
467
604
  * Forward `/api/functions/*` to another process instead of serving it here.
468
605
  *
@@ -538,7 +675,7 @@ export interface RebaseBackendConfig {
538
675
  * Global lifecycle callbacks applied to every collection.
539
676
  *
540
677
  * Same type as per-collection `callbacks` — fires on **every** data path
541
- * (REST API, WebSocket / realtime, server-side `rebase.data`).
678
+ * (REST API, WebSocket / realtime, server-side `rebase.dataAsAdmin`).
542
679
  *
543
680
  * Execution order: global callbacks → collection callbacks → property callbacks.
544
681
  *
@@ -572,8 +709,20 @@ export interface RebaseBackendConfig {
572
709
  schemaVersion?: string;
573
710
  /** Runtime version reported by the contract endpoint. Informational. */
574
711
  runtimeVersion?: string;
712
+ /**
713
+ * Periodic row-level-security audit of the database this server uses.
714
+ *
715
+ * Off unless `enabled` is set. `@rebasepro/rls-check` is thorough and
716
+ * read-only, and the reason nobody runs it is that it is a command — so this
717
+ * runs it on a timer and serves the result at `GET /api/admin/rls-audit`.
718
+ *
719
+ * Needs the optional peer `@rebasepro/rls-check`, which is not a hard
720
+ * dependency because it carries a Postgres driver and this package is
721
+ * engine-agnostic.
722
+ */
723
+ rlsAudit?: RlsAuditConfig;
575
724
  }
576
- export { isAuthAdapter } from "./auth/require-auth";
725
+ export { isAuthAdapter } from "./auth/require-auth.js";
577
726
  /**
578
727
  * Type guard to detect whether `database` is a `DatabaseAdapter`.
579
728
  */
@@ -585,12 +734,12 @@ export interface RebaseBackendInstance {
585
734
  realtimeService: RealtimeProvider;
586
735
  auth?: BootstrappedAuth;
587
736
  history?: {
588
- historyService: import("./history/history-routes").HistoryService;
737
+ historyService: import("./history/history-routes.js").HistoryService;
589
738
  };
590
739
  storageRegistry?: StorageRegistry;
591
740
  storageController?: StorageController;
592
741
  collectionRegistry: BackendCollectionRegistry;
593
- cronScheduler?: import("./cron").CronScheduler;
742
+ cronScheduler?: import("./cron/index.js").CronScheduler;
594
743
  /**
595
744
  * The durable job queue, when `jobs.enabled` is set and the driver can run
596
745
  * SQL. Present on the instance because the *producers* live in application
@@ -603,7 +752,7 @@ export interface RebaseBackendInstance {
603
752
  * jobQueue?.register(WEBHOOK_DELIVERY_TASK, ctx => dispatcher.deliverQueuedJob(ctx.payload));
604
753
  * ```
605
754
  */
606
- jobQueue?: import("./jobs").JobQueue;
755
+ jobQueue?: import("./jobs/index.js").JobQueue;
607
756
  /**
608
757
  * Attach collection callbacks AFTER initialization.
609
758
  *
@@ -1,5 +1,5 @@
1
- export { createJobStore } from "./job-store";
2
- export type { JobStore } from "./job-store";
3
- export { createJobQueue, defaultBackoff } from "./job-queue";
4
- export type { JobQueue } from "./job-queue";
5
- export type { EnqueueOptions, JobContext, JobHandler, JobQueueClient, JobQueueOptions, JobRecord, JobStatus } from "./types";
1
+ export { createJobStore } from "./job-store.js";
2
+ export type { JobStore } from "./job-store.js";
3
+ export { createJobQueue, defaultBackoff } from "./job-queue.js";
4
+ export type { JobQueue } from "./job-queue.js";
5
+ export type { EnqueueOptions, JobContext, JobHandler, JobQueueClient, JobQueueOptions, JobRecord, JobStatus } from "./types.js";
@@ -1,11 +1,12 @@
1
- import { createRequire as __createRequire } from "module";
2
- import process from "process";
3
- __createRequire(import.meta.url);
4
- import { n as __exportAll } from "./rolldown-runtime-DSJWtz9O.js";
5
- import "./src-BPfYOeN4.js";
6
- import "./src-CrCxd8km.js";
7
- import { n as createDdlBootstrapper, o as revokeInternalTableSql, r as hasInCauseChain, s as isSQLAdmin } from "./ddl-bootstrap-Cywoj8Ta.js";
8
- import { t as logger } from "./logger-DfvF_8r-.js";
1
+ import { createRequire as __rebaseCreateRequire } from "module";
2
+ import __rebaseProcess from "process";
3
+ globalThis.process ??= __rebaseProcess;
4
+ __rebaseCreateRequire(import.meta.url);
5
+ import { n as __exportAll } from "./rolldown-runtime-dW7B1o5h.js";
6
+ import "./src-C5vFEj-t.js";
7
+ import "./src-6VqGNeDB.js";
8
+ import { n as createDdlBootstrapper, o as revokeInternalTableSql, r as hasInCauseChain, s as isSQLAdmin } from "./ddl-bootstrap-euwN8FOy.js";
9
+ import { t as logger } from "./logger-DS03e908.js";
9
10
  import { randomUUID } from "crypto";
10
11
  //#region src/jobs/job-store.ts
11
12
  /**
@@ -323,4 +324,4 @@ var jobs_exports = /* @__PURE__ */ __exportAll({
323
324
  //#endregion
324
325
  export { createJobStore as i, createJobQueue as n, defaultBackoff as r, jobs_exports as t };
325
326
 
326
- //# sourceMappingURL=jobs-CyOKXXlu.js.map
327
+ //# sourceMappingURL=jobs-CeZzdeR8.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"jobs-CyOKXXlu.js","names":[],"sources":["../src/jobs/job-store.ts","../src/jobs/job-queue.ts","../src/jobs/index.ts"],"sourcesContent":["import type { DataDriver } from \"@rebasepro/types\";\nimport { isSQLAdmin } from \"@rebasepro/types\";\nimport { revokeInternalTableSql } from \"@rebasepro/common\";\nimport { logger } from \"../utils/logger.js\";\nimport { createDdlBootstrapper, hasInCauseChain, type SqlExec } from \"../boot/ddl-bootstrap.js\";\nimport type { JobRecord } from \"./types.js\";\n\n/**\n * The queue's storage, as SQL against `rebase.jobs`.\n *\n * Split from the worker for the same reason `cron-store` is split from\n * `cron-scheduler`: the interesting parts here are three statements that have\n * to be exactly right under concurrency, and they are much easier to reason\n * about — and to test — away from a polling loop.\n */\n\nconst TABLE = \"rebase.jobs\";\n\n/** How long finished jobs are kept before the boot sweep removes them. */\nconst SUCCEEDED_RETENTION_DAYS = 3;\n/**\n * Failures outlive successes by a lot. A dead-lettered job is evidence, and the\n * person who needs it is usually looking on Monday for something that happened\n * on Friday night.\n */\nconst FAILED_RETENTION_DAYS = 30;\n\n/** A row as Postgres returns it. */\ninterface JobRow {\n id: string;\n task: string;\n payload: unknown;\n status: string;\n run_at: string;\n attempts: number;\n max_attempts: number;\n last_error: string | null;\n created_at: string;\n updated_at: string;\n}\n\nfunction toRecord(row: JobRow): JobRecord {\n return {\n id: row.id,\n task: row.task,\n payload: row.payload,\n status: row.status as JobRecord[\"status\"],\n runAt: new Date(row.run_at).toISOString(),\n attempts: Number(row.attempts),\n maxAttempts: Number(row.max_attempts),\n lastError: row.last_error,\n createdAt: new Date(row.created_at).toISOString(),\n updatedAt: new Date(row.updated_at).toISOString()\n };\n}\n\n/** Same rule as `cron-store`: match the SQLSTATE, never the message. */\nfunction isUniqueViolation(err: unknown): boolean {\n return hasInCauseChain(err, (e) => e.code === \"23505\");\n}\n\nexport interface JobStore {\n ensureTable(): Promise<void>;\n /** Returns the new job's id, or `null` if an idempotency key matched unfinished work. */\n insert(job: {\n task: string;\n payload: unknown;\n runAt: Date;\n maxAttempts: number;\n idempotencyKey?: string;\n }): Promise<string | null>;\n /** Atomically take up to `limit` runnable jobs for this worker. */\n claim(limit: number, workerId: string): Promise<JobRecord[]>;\n complete(id: string): Promise<void>;\n /** Back to `pending` with a later `runAt`, or `failed` when out of attempts. */\n fail(id: string, error: string, retryAt: Date | null): Promise<void>;\n /** Return jobs stranded by a worker that died holding them. Resolves with how many. */\n reapExpired(visibilityTimeoutMs: number): Promise<number>;\n fetch(id: string): Promise<JobRecord | null>;\n}\n\nexport function createJobStore(driver: DataDriver): JobStore | undefined {\n const admin = driver.admin;\n if (!isSQLAdmin(admin)) {\n logger.warn(\n \"⚠️ [jobs] DataDriver does not support SQL admin — the durable job queue is unavailable. \" +\n \"Work that would have been queued runs inline instead.\"\n );\n return undefined;\n }\n\n // Two shapes of the same call: the bootstrapper's `SqlExec` takes an\n // options object, while everything below reads better with a positional\n // parameter array.\n const execRaw: SqlExec = (sqlText, options) =>\n admin.executeSql(sqlText, options?.params ? { params: options.params } : undefined);\n const exec = (sqlText: string, params?: unknown[]) =>\n execRaw(sqlText, params ? { params } : undefined);\n\n const ddl = createDdlBootstrapper(execRaw, \"jobs\");\n\n return {\n async ensureTable(): Promise<void> {\n await ddl.ensureObject(\"Creating schema rebase\", \"CREATE SCHEMA IF NOT EXISTS rebase\");\n\n await ddl.ensureObject(`Creating ${TABLE}`, `\n CREATE TABLE IF NOT EXISTS ${TABLE} (\n id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,\n task TEXT NOT NULL,\n payload JSONB,\n status TEXT NOT NULL DEFAULT 'pending',\n run_at TIMESTAMPTZ NOT NULL DEFAULT now(),\n attempts INTEGER NOT NULL DEFAULT 0,\n max_attempts INTEGER NOT NULL DEFAULT 3,\n locked_at TIMESTAMPTZ,\n locked_by TEXT,\n idempotency_key TEXT,\n last_error TEXT,\n created_at TIMESTAMPTZ NOT NULL DEFAULT now(),\n updated_at TIMESTAMPTZ NOT NULL DEFAULT now()\n )\n `);\n\n // The claim query's index. Partial, because the rows it has to find\n // fast are a shrinking minority of a table that also holds every\n // success and every dead letter.\n await ddl.ensureObject(\"Creating idx_jobs_runnable\", `\n CREATE INDEX IF NOT EXISTS idx_jobs_runnable\n ON ${TABLE}(run_at, created_at)\n WHERE status = 'pending'\n `);\n\n // Finds jobs stranded by a dead worker.\n await ddl.ensureObject(\"Creating idx_jobs_running\", `\n CREATE INDEX IF NOT EXISTS idx_jobs_running\n ON ${TABLE}(locked_at)\n WHERE status = 'running'\n `);\n\n // What makes `idempotencyKey` a guarantee rather than a\n // check-then-act race: two instances reacting to one event both\n // reach the INSERT, and the index decides. Partial on unfinished\n // work — see `EnqueueOptions.idempotencyKey` for why a key must be\n // reusable once its job is done.\n await ddl.ensureObject(\"Creating idx_jobs_idempotency\", `\n CREATE UNIQUE INDEX IF NOT EXISTS idx_jobs_idempotency\n ON ${TABLE}(idempotency_key)\n WHERE idempotency_key IS NOT NULL AND status IN ('pending', 'running')\n `);\n\n const ready = await ddl.isReadable(TABLE);\n\n if (ready) {\n await ddl.step(\"Job retention sweep\", async () => {\n await exec(\n `DELETE FROM ${TABLE}\n WHERE (status = 'succeeded' AND updated_at < now() - make_interval(days => $1))\n OR (status = 'failed' AND updated_at < now() - make_interval(days => $2))`,\n [SUCCEEDED_RETENTION_DAYS, FAILED_RETENTION_DAYS]\n );\n });\n\n // Payloads are arbitrary application data — a webhook body, a\n // user id, whatever was passed — and a writable queue lets any\n // signed-in user schedule work of their choosing under the\n // server's own authority. Same reasoning, and the same\n // unconditional re-application, as `cron_claims`.\n await ddl.step(\"Revoking end-user access to jobs\", () =>\n exec(revokeInternalTableSql(\"rebase\", \"jobs\")));\n\n logger.info(\"✅ Job queue table ready\");\n } else {\n logger.error(\n `❌ [jobs] ${TABLE} is unavailable — nothing can be queued and nothing queued earlier ` +\n \"will run. Callers fall back to running the work inline.\"\n );\n }\n },\n\n async insert(job): Promise<string | null> {\n try {\n const rows = await exec(\n `INSERT INTO ${TABLE} (task, payload, run_at, max_attempts, idempotency_key)\n VALUES ($1, $2::jsonb, $3, $4, $5)\n RETURNING id`,\n [\n job.task,\n JSON.stringify(job.payload ?? null),\n job.runAt.toISOString(),\n job.maxAttempts,\n job.idempotencyKey ?? null\n ]\n );\n return (rows?.[0]?.id as string) ?? null;\n } catch (error) {\n // The losing side of an idempotency race. Not an error: the\n // work the caller wanted is already queued, which is the\n // outcome they asked for.\n if (isUniqueViolation(error)) return null;\n throw error;\n }\n },\n\n async claim(limit: number, workerId: string): Promise<JobRecord[]> {\n // `FOR UPDATE SKIP LOCKED` is the whole design. The inner select\n // takes row locks on the jobs it picks and *skips* any a concurrent\n // worker already holds, so N workers polling the same table divide\n // the work instead of contending for the head of it — and no job is\n // ever handed to two of them.\n //\n // `attempts` is incremented here, on claim, rather than on failure.\n // A worker that is killed mid-job never reports anything, so\n // counting on failure would let a job that crashes the process be\n // retried forever, once per restart, taking the process down each\n // time.\n const rows = await exec(\n `UPDATE ${TABLE} SET\n status = 'running',\n attempts = attempts + 1,\n locked_at = now(),\n locked_by = $2,\n updated_at = now()\n WHERE id IN (\n SELECT id FROM ${TABLE}\n WHERE status = 'pending' AND run_at <= now()\n ORDER BY run_at, created_at\n LIMIT $1\n FOR UPDATE SKIP LOCKED\n )\n RETURNING *`,\n [limit, workerId]\n );\n return (rows as unknown as JobRow[]).map(toRecord);\n },\n\n async complete(id: string): Promise<void> {\n await exec(\n `UPDATE ${TABLE} SET status = 'succeeded', locked_at = NULL, locked_by = NULL,\n last_error = NULL, updated_at = now()\n WHERE id = $1`,\n [id]\n );\n },\n\n async fail(id: string, error: string, retryAt: Date | null): Promise<void> {\n if (retryAt) {\n await exec(\n `UPDATE ${TABLE} SET status = 'pending', run_at = $2, locked_at = NULL,\n locked_by = NULL, last_error = $3, updated_at = now()\n WHERE id = $1`,\n [id, retryAt.toISOString(), error]\n );\n return;\n }\n await exec(\n `UPDATE ${TABLE} SET status = 'failed', locked_at = NULL, locked_by = NULL,\n last_error = $2, updated_at = now()\n WHERE id = $1`,\n [id, error]\n );\n },\n\n async reapExpired(visibilityTimeoutMs: number): Promise<number> {\n // A worker killed while holding a job cannot release it, so nothing\n // but a timeout will ever free the row. Jobs that still have\n // attempts left go back to `pending`; the rest are dead-lettered\n // with an error that says what happened, because \"attempts: 3,\n // lastError: null\" is otherwise a genuinely baffling row to find.\n const seconds = Math.max(1, Math.round(visibilityTimeoutMs / 1000));\n\n const revived = await exec(\n `UPDATE ${TABLE} SET status = 'pending', locked_at = NULL, locked_by = NULL,\n last_error = 'Worker stopped responding; the job was reclaimed', updated_at = now()\n WHERE status = 'running'\n AND locked_at < now() - make_interval(secs => $1)\n AND attempts < max_attempts\n RETURNING id`,\n [seconds]\n );\n\n const buried = await exec(\n `UPDATE ${TABLE} SET status = 'failed', locked_at = NULL, locked_by = NULL,\n last_error = 'Worker stopped responding on the final attempt', updated_at = now()\n WHERE status = 'running'\n AND locked_at < now() - make_interval(secs => $1)\n AND attempts >= max_attempts\n RETURNING id`,\n [seconds]\n );\n\n const count = (revived?.length ?? 0) + (buried?.length ?? 0);\n if (count > 0) {\n logger.warn(\n `[jobs] Reclaimed ${count} job(s) from a worker that stopped responding ` +\n `(${revived?.length ?? 0} retryable, ${buried?.length ?? 0} dead-lettered)`\n );\n }\n return count;\n },\n\n async fetch(id: string): Promise<JobRecord | null> {\n const rows = await exec(`SELECT * FROM ${TABLE} WHERE id = $1`, [id]);\n const row = (rows as unknown as JobRow[])[0];\n return row ? toRecord(row) : null;\n }\n };\n}\n","import { randomUUID } from \"crypto\";\nimport { logger } from \"../utils/logger.js\";\nimport type { JobStore } from \"./job-store.js\";\nimport type { EnqueueOptions, JobHandler, JobQueueClient, JobQueueOptions, JobRecord } from \"./types.js\";\n\n/**\n * The worker: claim, run, record, repeat.\n *\n * Everything difficult about running jobs concurrently is in `job-store.ts`,\n * where one `UPDATE … FOR UPDATE SKIP LOCKED` does the arbitration. What is\n * left here is a loop and the decisions around a handler that throws.\n */\n\nconst DEFAULT_CONCURRENCY = 5;\nconst DEFAULT_POLL_INTERVAL_MS = 2_000;\nconst DEFAULT_VISIBILITY_TIMEOUT_MS = 5 * 60_000;\nconst DEFAULT_MAX_ATTEMPTS = 3;\n\n/** 1s, 5s, 25s, 125s … capped at an hour. */\nexport function defaultBackoff(attempt: number): number {\n return Math.min(1_000 * Math.pow(5, Math.max(0, attempt - 1)), 60 * 60_000);\n}\n\n/**\n * How often the reaper runs, relative to the visibility timeout.\n *\n * A quarter of it, so a stranded job waits at most 1.25× the timeout rather\n * than 2× — and so the sweep is not itself a per-poll query against a table\n * whose interesting rows are, almost always, none.\n */\nconst REAP_INTERVAL_FACTOR = 0.25;\n\nexport interface JobQueue extends JobQueueClient {\n start(): void;\n stop(): Promise<void>;\n /** Run one poll's worth of work and return how many jobs ran. For tests and for `/jobs/drain`. */\n runOnce(): Promise<number>;\n /** Registered after construction — how `tasks` from config and internal producers meet. */\n register<P = unknown>(task: string, handler: JobHandler<P>): void;\n isRunning(): boolean;\n}\n\nexport function createJobQueue(store: JobStore, options: JobQueueOptions = {}): JobQueue {\n const concurrency = options.concurrency ?? DEFAULT_CONCURRENCY;\n const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n const visibilityTimeoutMs = options.visibilityTimeoutMs ?? DEFAULT_VISIBILITY_TIMEOUT_MS;\n const defaultMaxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;\n const backoff = options.backoff ?? defaultBackoff;\n\n const handlers = new Map<string, JobHandler<never>>();\n for (const [task, handler] of Object.entries(options.tasks ?? {})) {\n handlers.set(task, handler);\n }\n\n // Identifies this process in `locked_by`. Purely diagnostic — the claim is\n // enforced by the row lock, not by this — but \"which pod had it when it\n // stopped\" is the first question anyone asks of a stuck job.\n const workerId = `${process.pid}-${randomUUID().slice(0, 8)}`;\n\n let timer: NodeJS.Timeout | null = null;\n let running = false;\n let draining = false;\n /** Resolves when the in-flight poll finishes, so `stop()` can wait for it. */\n let inFlight: Promise<unknown> = Promise.resolve();\n let lastReapAt = 0;\n\n async function runJob(job: JobRecord): Promise<void> {\n const handler = handlers.get(job.task);\n\n if (!handler) {\n // Not a failure. A rolling deploy runs old and new code at once, and\n // an instance that has not been updated yet must not burn the\n // attempts of a job belonging to one that has. Give the row back\n // and let a peer — or this process after its next deploy — take it.\n //\n // The attempt increment from the claim is deliberately not undone:\n // a task nobody in the fleet implements would otherwise cycle\n // forever, and this way it dead-letters after `maxAttempts` with an\n // error naming the task.\n logger.warn(`[jobs] No handler registered for task \"${job.task}\" — returning the job to the queue`);\n await store.fail(\n job.id,\n `No handler registered for task \"${job.task}\"`,\n job.attempts < job.maxAttempts ? new Date(Date.now() + backoff(job.attempts)) : null\n );\n return;\n }\n\n try {\n await handler({\n id: job.id,\n task: job.task,\n payload: job.payload as never,\n attempt: job.attempts,\n maxAttempts: job.maxAttempts\n } as never);\n await store.complete(job.id);\n } catch (error) {\n const message = error instanceof Error ? (error.stack ?? error.message) : String(error);\n const willRetry = job.attempts < job.maxAttempts;\n\n // Truncated, because `last_error` holds a stack and a queue that\n // accumulates megabytes of them is its own outage.\n await store.fail(job.id, message.slice(0, 4_000), willRetry ? new Date(Date.now() + backoff(job.attempts)) : null);\n\n if (willRetry) {\n logger.warn(`[jobs] \"${job.task}\" failed on attempt ${job.attempts}/${job.maxAttempts}; retrying`, { jobId: job.id });\n } else {\n // The last attempt is an error, not a warning: nothing else will\n // touch this job, and if nobody looks at the table it is simply\n // lost work.\n logger.error(`[jobs] \"${job.task}\" failed permanently after ${job.attempts} attempts`, { jobId: job.id, error: message });\n }\n }\n }\n\n async function poll(): Promise<number> {\n // The reaper, on its own cadence.\n const now = Date.now();\n if (now - lastReapAt > visibilityTimeoutMs * REAP_INTERVAL_FACTOR) {\n lastReapAt = now;\n try {\n await store.reapExpired(visibilityTimeoutMs);\n } catch (error) {\n logger.error(\"[jobs] Failed to reclaim expired jobs\", { error });\n }\n }\n\n const jobs = await store.claim(concurrency, workerId);\n if (jobs.length === 0) return 0;\n\n // Settled, not `all`: `runJob` handles its own errors, but a store\n // write failing inside it must not abandon this batch's siblings.\n await Promise.allSettled(jobs.map(runJob));\n return jobs.length;\n }\n\n function schedule(delayMs: number): void {\n if (!running) return;\n timer = setTimeout(() => {\n void tick();\n }, delayMs);\n // Never hold the process open. A queue with nothing to do should not be\n // the reason `rebase dev` will not exit.\n timer.unref?.();\n }\n\n async function tick(): Promise<void> {\n if (!running) return;\n const work = (async () => {\n try {\n return await poll();\n } catch (error) {\n logger.error(\"[jobs] Poll failed\", { error });\n return 0;\n }\n })();\n inFlight = work;\n const count = await work;\n\n // A full batch means there is probably more waiting, so go straight\n // back rather than sleeping through a backlog.\n schedule(count >= concurrency ? 0 : pollIntervalMs);\n }\n\n return {\n start(): void {\n if (running) return;\n running = true;\n logger.info(`[jobs] Worker started (concurrency ${concurrency}, poll ${pollIntervalMs}ms)`);\n schedule(0);\n },\n\n async stop(): Promise<void> {\n running = false;\n if (timer) {\n clearTimeout(timer);\n timer = null;\n }\n if (draining) return;\n draining = true;\n // Jobs in flight keep their claim until they finish or the\n // visibility timeout expires, so waiting here is what turns a\n // graceful shutdown into \"no job runs twice\".\n await inFlight.catch(() => undefined);\n draining = false;\n },\n\n runOnce(): Promise<number> {\n return poll();\n },\n\n register<P = unknown>(task: string, handler: JobHandler<P>): void {\n if (handlers.has(task)) {\n logger.warn(`[jobs] Task \"${task}\" was already registered; the later handler wins`);\n }\n handlers.set(task, handler as JobHandler<never>);\n },\n\n isRunning(): boolean {\n return running;\n },\n\n async enqueue<P = unknown>(task: string, payload?: P, enqueueOptions: EnqueueOptions = {}): Promise<string | null> {\n return store.insert({\n task,\n payload: payload ?? null,\n runAt: new Date(Date.now() + (enqueueOptions.delayMs ?? 0)),\n maxAttempts: enqueueOptions.maxAttempts ?? defaultMaxAttempts,\n idempotencyKey: enqueueOptions.idempotencyKey\n });\n }\n };\n}\n","export { createJobStore } from \"./job-store\";\nexport type { JobStore } from \"./job-store\";\nexport { createJobQueue, defaultBackoff } from \"./job-queue\";\nexport type { JobQueue } from \"./job-queue\";\nexport type {\n EnqueueOptions,\n JobContext,\n JobHandler,\n JobQueueClient,\n JobQueueOptions,\n JobRecord,\n JobStatus\n} from \"./types\";\n"],"mappings":";;;;;;;;;;;;;;;;;;AAgBA,IAAM,QAAQ;;AAGd,IAAM,2BAA2B;;;;;;AAMjC,IAAM,wBAAwB;AAgB9B,SAAS,SAAS,KAAwB;CACtC,OAAO;EACH,IAAI,IAAI;EACR,MAAM,IAAI;EACV,SAAS,IAAI;EACb,QAAQ,IAAI;EACZ,OAAO,IAAI,KAAK,IAAI,M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CAClB;CAEA,eAAe,OAAsB;EACjC,IAAI,CAAC,SAAS;EACd,MAAM,QAAQ,YAAY;GACtB,IAAI;IACA,OAAO,MAAM,KAAK;GACtB,SAAS,OAAO;IACZ,OAAO,MAAM,sBAAsB,EAAE,MAAM,CAAC;IAC5C,OAAO;GACX;EACJ,EAAA,CAAG;EACH,WAAW;EAKX,SAAS,MAJW,QAIF,cAAc,IAAI,cAAc;CACtD;CAEA,OAAO;EACH,QAAc;GACV,IAAI,SAAS;GACb,UAAU;GACV,OAAO,KAAK,sCAAsC,YAAY,SAAS,eAAe,IAAI;GAC1F,SAAS,CAAC;EACd;EAEA,MAAM,OAAsB;GACxB,UAAU;GACV,IAAI,OAAO;IACP,aAAa,KAAK;IAClB,QAAQ;GACZ;GACA,IAAI,UAAU;GACd,WAAW;GAIX,MAAM,SAAS,YAAY,KAAA,CAAS;GACpC,WAAW;EACf;EAEA,UAA2B;GACvB,OAAO,KAAK;EAChB;EAEA,SAAsB,MAAc,SAA8B;GAC9D,IAAI,SAAS,IAAI,IAAI,GACjB,OAAO,KAAK,gBAAgB,KAAK,iDAAiD;GAEtF,SAAS,IAAI,MAAM,OAA4B;EACnD;EAEA,YAAqB;GACjB,OAAO;EACX;EAEA,MAAM,QAAqB,MAAc,SAAa,iBAAiC,CAAC,GAA2B;GAC/G,OAAO,MAAM,OAAO;IAChB;IACA,SAAS,WAAW;IACpB,OAAO,IAAI,KAAK,KAAK,IAAI,KAAK,eAAe,WAAW,EAAE;IAC1D,aAAa,eAAe,eAAe;IAC3C,gBAAgB,eAAe;GACnC,CAAC;EACL;CACJ;AACJ"}
1
+ {"version":3,"file":"jobs-CeZzdeR8.js","names":[],"sources":["../src/jobs/job-store.ts","../src/jobs/job-queue.ts","../src/jobs/index.ts"],"sourcesContent":["import type { DataDriver } from \"@rebasepro/types\";\nimport { isSQLAdmin } from \"@rebasepro/types\";\nimport { revokeInternalTableSql } from \"@rebasepro/common\";\nimport { logger } from \"../utils/logger.js\";\nimport { createDdlBootstrapper, hasInCauseChain, type SqlExec } from \"../boot/ddl-bootstrap.js\";\nimport type { JobRecord } from \"./types.js\";\n\n/**\n * The queue's storage, as SQL against `rebase.jobs`.\n *\n * Split from the worker for the same reason `cron-store` is split from\n * `cron-scheduler`: the interesting parts here are three statements that have\n * to be exactly right under concurrency, and they are much easier to reason\n * about — and to test — away from a polling loop.\n */\n\nconst TABLE = \"rebase.jobs\";\n\n/** How long finished jobs are kept before the boot sweep removes them. */\nconst SUCCEEDED_RETENTION_DAYS = 3;\n/**\n * Failures outlive successes by a lot. A dead-lettered job is evidence, and the\n * person who needs it is usually looking on Monday for something that happened\n * on Friday night.\n */\nconst FAILED_RETENTION_DAYS = 30;\n\n/** A row as Postgres returns it. */\ninterface JobRow {\n id: string;\n task: string;\n payload: unknown;\n status: string;\n run_at: string;\n attempts: number;\n max_attempts: number;\n last_error: string | null;\n created_at: string;\n updated_at: string;\n}\n\nfunction toRecord(row: JobRow): JobRecord {\n return {\n id: row.id,\n task: row.task,\n payload: row.payload,\n status: row.status as JobRecord[\"status\"],\n runAt: new Date(row.run_at).toISOString(),\n attempts: Number(row.attempts),\n maxAttempts: Number(row.max_attempts),\n lastError: row.last_error,\n createdAt: new Date(row.created_at).toISOString(),\n updatedAt: new Date(row.updated_at).toISOString()\n };\n}\n\n/** Same rule as `cron-store`: match the SQLSTATE, never the message. */\nfunction isUniqueViolation(err: unknown): boolean {\n return hasInCauseChain(err, (e) => e.code === \"23505\");\n}\n\nexport interface JobStore {\n ensureTable(): Promise<void>;\n /** Returns the new job's id, or `null` if an idempotency key matched unfinished work. */\n insert(job: {\n task: string;\n payload: unknown;\n runAt: Date;\n maxAttempts: number;\n idempotencyKey?: string;\n }): Promise<string | null>;\n /** Atomically take up to `limit` runnable jobs for this worker. */\n claim(limit: number, workerId: string): Promise<JobRecord[]>;\n complete(id: string): Promise<void>;\n /** Back to `pending` with a later `runAt`, or `failed` when out of attempts. */\n fail(id: string, error: string, retryAt: Date | null): Promise<void>;\n /** Return jobs stranded by a worker that died holding them. Resolves with how many. */\n reapExpired(visibilityTimeoutMs: number): Promise<number>;\n fetch(id: string): Promise<JobRecord | null>;\n}\n\nexport function createJobStore(driver: DataDriver): JobStore | undefined {\n const admin = driver.admin;\n if (!isSQLAdmin(admin)) {\n logger.warn(\n \"⚠️ [jobs] DataDriver does not support SQL admin — the durable job queue is unavailable. \" +\n \"Work that would have been queued runs inline instead.\"\n );\n return undefined;\n }\n\n // Two shapes of the same call: the bootstrapper's `SqlExec` takes an\n // options object, while everything below reads better with a positional\n // parameter array.\n const execRaw: SqlExec = (sqlText, options) =>\n admin.executeSql(sqlText, options?.params ? { params: options.params } : undefined);\n const exec = (sqlText: string, params?: unknown[]) =>\n execRaw(sqlText, params ? { params } : undefined);\n\n const ddl = createDdlBootstrapper(execRaw, \"jobs\");\n\n return {\n async ensureTable(): Promise<void> {\n await ddl.ensureObject(\"Creating schema rebase\", \"CREATE SCHEMA IF NOT EXISTS rebase\");\n\n await ddl.ensureObject(`Creating ${TABLE}`, `\n CREATE TABLE IF NOT EXISTS ${TABLE} (\n id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text,\n task TEXT NOT NULL,\n payload JSONB,\n status TEXT NOT NULL DEFAULT 'pending',\n run_at TIMESTAMPTZ NOT NULL DEFAULT now(),\n attempts INTEGER NOT NULL DEFAULT 0,\n max_attempts INTEGER NOT NULL DEFAULT 3,\n locked_at TIMESTAMPTZ,\n locked_by TEXT,\n idempotency_key TEXT,\n last_error TEXT,\n created_at TIMESTAMPTZ NOT NULL DEFAULT now(),\n updated_at TIMESTAMPTZ NOT NULL DEFAULT now()\n )\n `);\n\n // The claim query's index. Partial, because the rows it has to find\n // fast are a shrinking minority of a table that also holds every\n // success and every dead letter.\n await ddl.ensureObject(\"Creating idx_jobs_runnable\", `\n CREATE INDEX IF NOT EXISTS idx_jobs_runnable\n ON ${TABLE}(run_at, created_at)\n WHERE status = 'pending'\n `);\n\n // Finds jobs stranded by a dead worker.\n await ddl.ensureObject(\"Creating idx_jobs_running\", `\n CREATE INDEX IF NOT EXISTS idx_jobs_running\n ON ${TABLE}(locked_at)\n WHERE status = 'running'\n `);\n\n // What makes `idempotencyKey` a guarantee rather than a\n // check-then-act race: two instances reacting to one event both\n // reach the INSERT, and the index decides. Partial on unfinished\n // work — see `EnqueueOptions.idempotencyKey` for why a key must be\n // reusable once its job is done.\n await ddl.ensureObject(\"Creating idx_jobs_idempotency\", `\n CREATE UNIQUE INDEX IF NOT EXISTS idx_jobs_idempotency\n ON ${TABLE}(idempotency_key)\n WHERE idempotency_key IS NOT NULL AND status IN ('pending', 'running')\n `);\n\n const ready = await ddl.isReadable(TABLE);\n\n if (ready) {\n await ddl.step(\"Job retention sweep\", async () => {\n await exec(\n `DELETE FROM ${TABLE}\n WHERE (status = 'succeeded' AND updated_at < now() - make_interval(days => $1))\n OR (status = 'failed' AND updated_at < now() - make_interval(days => $2))`,\n [SUCCEEDED_RETENTION_DAYS, FAILED_RETENTION_DAYS]\n );\n });\n\n // Payloads are arbitrary application data — a webhook body, a\n // user id, whatever was passed — and a writable queue lets any\n // signed-in user schedule work of their choosing under the\n // server's own authority. Same reasoning, and the same\n // unconditional re-application, as `cron_claims`.\n await ddl.step(\"Revoking end-user access to jobs\", () =>\n exec(revokeInternalTableSql(\"rebase\", \"jobs\")));\n\n logger.info(\"✅ Job queue table ready\");\n } else {\n logger.error(\n `❌ [jobs] ${TABLE} is unavailable — nothing can be queued and nothing queued earlier ` +\n \"will run. Callers fall back to running the work inline.\"\n );\n }\n },\n\n async insert(job): Promise<string | null> {\n try {\n const rows = await exec(\n `INSERT INTO ${TABLE} (task, payload, run_at, max_attempts, idempotency_key)\n VALUES ($1, $2::jsonb, $3, $4, $5)\n RETURNING id`,\n [\n job.task,\n JSON.stringify(job.payload ?? null),\n job.runAt.toISOString(),\n job.maxAttempts,\n job.idempotencyKey ?? null\n ]\n );\n return (rows?.[0]?.id as string) ?? null;\n } catch (error) {\n // The losing side of an idempotency race. Not an error: the\n // work the caller wanted is already queued, which is the\n // outcome they asked for.\n if (isUniqueViolation(error)) return null;\n throw error;\n }\n },\n\n async claim(limit: number, workerId: string): Promise<JobRecord[]> {\n // `FOR UPDATE SKIP LOCKED` is the whole design. The inner select\n // takes row locks on the jobs it picks and *skips* any a concurrent\n // worker already holds, so N workers polling the same table divide\n // the work instead of contending for the head of it — and no job is\n // ever handed to two of them.\n //\n // `attempts` is incremented here, on claim, rather than on failure.\n // A worker that is killed mid-job never reports anything, so\n // counting on failure would let a job that crashes the process be\n // retried forever, once per restart, taking the process down each\n // time.\n const rows = await exec(\n `UPDATE ${TABLE} SET\n status = 'running',\n attempts = attempts + 1,\n locked_at = now(),\n locked_by = $2,\n updated_at = now()\n WHERE id IN (\n SELECT id FROM ${TABLE}\n WHERE status = 'pending' AND run_at <= now()\n ORDER BY run_at, created_at\n LIMIT $1\n FOR UPDATE SKIP LOCKED\n )\n RETURNING *`,\n [limit, workerId]\n );\n return (rows as unknown as JobRow[]).map(toRecord);\n },\n\n async complete(id: string): Promise<void> {\n await exec(\n `UPDATE ${TABLE} SET status = 'succeeded', locked_at = NULL, locked_by = NULL,\n last_error = NULL, updated_at = now()\n WHERE id = $1`,\n [id]\n );\n },\n\n async fail(id: string, error: string, retryAt: Date | null): Promise<void> {\n if (retryAt) {\n await exec(\n `UPDATE ${TABLE} SET status = 'pending', run_at = $2, locked_at = NULL,\n locked_by = NULL, last_error = $3, updated_at = now()\n WHERE id = $1`,\n [id, retryAt.toISOString(), error]\n );\n return;\n }\n await exec(\n `UPDATE ${TABLE} SET status = 'failed', locked_at = NULL, locked_by = NULL,\n last_error = $2, updated_at = now()\n WHERE id = $1`,\n [id, error]\n );\n },\n\n async reapExpired(visibilityTimeoutMs: number): Promise<number> {\n // A worker killed while holding a job cannot release it, so nothing\n // but a timeout will ever free the row. Jobs that still have\n // attempts left go back to `pending`; the rest are dead-lettered\n // with an error that says what happened, because \"attempts: 3,\n // lastError: null\" is otherwise a genuinely baffling row to find.\n const seconds = Math.max(1, Math.round(visibilityTimeoutMs / 1000));\n\n const revived = await exec(\n `UPDATE ${TABLE} SET status = 'pending', locked_at = NULL, locked_by = NULL,\n last_error = 'Worker stopped responding; the job was reclaimed', updated_at = now()\n WHERE status = 'running'\n AND locked_at < now() - make_interval(secs => $1)\n AND attempts < max_attempts\n RETURNING id`,\n [seconds]\n );\n\n const buried = await exec(\n `UPDATE ${TABLE} SET status = 'failed', locked_at = NULL, locked_by = NULL,\n last_error = 'Worker stopped responding on the final attempt', updated_at = now()\n WHERE status = 'running'\n AND locked_at < now() - make_interval(secs => $1)\n AND attempts >= max_attempts\n RETURNING id`,\n [seconds]\n );\n\n const count = (revived?.length ?? 0) + (buried?.length ?? 0);\n if (count > 0) {\n logger.warn(\n `[jobs] Reclaimed ${count} job(s) from a worker that stopped responding ` +\n `(${revived?.length ?? 0} retryable, ${buried?.length ?? 0} dead-lettered)`\n );\n }\n return count;\n },\n\n async fetch(id: string): Promise<JobRecord | null> {\n const rows = await exec(`SELECT * FROM ${TABLE} WHERE id = $1`, [id]);\n const row = (rows as unknown as JobRow[])[0];\n return row ? toRecord(row) : null;\n }\n };\n}\n","import { randomUUID } from \"crypto\";\nimport { logger } from \"../utils/logger.js\";\nimport type { JobStore } from \"./job-store.js\";\nimport type { EnqueueOptions, JobHandler, JobQueueClient, JobQueueOptions, JobRecord } from \"./types.js\";\n\n/**\n * The worker: claim, run, record, repeat.\n *\n * Everything difficult about running jobs concurrently is in `job-store.ts`,\n * where one `UPDATE … FOR UPDATE SKIP LOCKED` does the arbitration. What is\n * left here is a loop and the decisions around a handler that throws.\n */\n\nconst DEFAULT_CONCURRENCY = 5;\nconst DEFAULT_POLL_INTERVAL_MS = 2_000;\nconst DEFAULT_VISIBILITY_TIMEOUT_MS = 5 * 60_000;\nconst DEFAULT_MAX_ATTEMPTS = 3;\n\n/** 1s, 5s, 25s, 125s … capped at an hour. */\nexport function defaultBackoff(attempt: number): number {\n return Math.min(1_000 * Math.pow(5, Math.max(0, attempt - 1)), 60 * 60_000);\n}\n\n/**\n * How often the reaper runs, relative to the visibility timeout.\n *\n * A quarter of it, so a stranded job waits at most 1.25× the timeout rather\n * than 2× — and so the sweep is not itself a per-poll query against a table\n * whose interesting rows are, almost always, none.\n */\nconst REAP_INTERVAL_FACTOR = 0.25;\n\nexport interface JobQueue extends JobQueueClient {\n start(): void;\n stop(): Promise<void>;\n /** Run one poll's worth of work and return how many jobs ran. For tests and for `/jobs/drain`. */\n runOnce(): Promise<number>;\n /** Registered after construction — how `tasks` from config and internal producers meet. */\n register<P = unknown>(task: string, handler: JobHandler<P>): void;\n isRunning(): boolean;\n}\n\nexport function createJobQueue(store: JobStore, options: JobQueueOptions = {}): JobQueue {\n const concurrency = options.concurrency ?? DEFAULT_CONCURRENCY;\n const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n const visibilityTimeoutMs = options.visibilityTimeoutMs ?? DEFAULT_VISIBILITY_TIMEOUT_MS;\n const defaultMaxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;\n const backoff = options.backoff ?? defaultBackoff;\n\n const handlers = new Map<string, JobHandler<never>>();\n for (const [task, handler] of Object.entries(options.tasks ?? {})) {\n handlers.set(task, handler);\n }\n\n // Identifies this process in `locked_by`. Purely diagnostic — the claim is\n // enforced by the row lock, not by this — but \"which pod had it when it\n // stopped\" is the first question anyone asks of a stuck job.\n const workerId = `${process.pid}-${randomUUID().slice(0, 8)}`;\n\n let timer: NodeJS.Timeout | null = null;\n let running = false;\n let draining = false;\n /** Resolves when the in-flight poll finishes, so `stop()` can wait for it. */\n let inFlight: Promise<unknown> = Promise.resolve();\n let lastReapAt = 0;\n\n async function runJob(job: JobRecord): Promise<void> {\n const handler = handlers.get(job.task);\n\n if (!handler) {\n // Not a failure. A rolling deploy runs old and new code at once, and\n // an instance that has not been updated yet must not burn the\n // attempts of a job belonging to one that has. Give the row back\n // and let a peer — or this process after its next deploy — take it.\n //\n // The attempt increment from the claim is deliberately not undone:\n // a task nobody in the fleet implements would otherwise cycle\n // forever, and this way it dead-letters after `maxAttempts` with an\n // error naming the task.\n logger.warn(`[jobs] No handler registered for task \"${job.task}\" — returning the job to the queue`);\n await store.fail(\n job.id,\n `No handler registered for task \"${job.task}\"`,\n job.attempts < job.maxAttempts ? new Date(Date.now() + backoff(job.attempts)) : null\n );\n return;\n }\n\n try {\n await handler({\n id: job.id,\n task: job.task,\n payload: job.payload as never,\n attempt: job.attempts,\n maxAttempts: job.maxAttempts\n } as never);\n await store.complete(job.id);\n } catch (error) {\n const message = error instanceof Error ? (error.stack ?? error.message) : String(error);\n const willRetry = job.attempts < job.maxAttempts;\n\n // Truncated, because `last_error` holds a stack and a queue that\n // accumulates megabytes of them is its own outage.\n await store.fail(job.id, message.slice(0, 4_000), willRetry ? new Date(Date.now() + backoff(job.attempts)) : null);\n\n if (willRetry) {\n logger.warn(`[jobs] \"${job.task}\" failed on attempt ${job.attempts}/${job.maxAttempts}; retrying`, { jobId: job.id });\n } else {\n // The last attempt is an error, not a warning: nothing else will\n // touch this job, and if nobody looks at the table it is simply\n // lost work.\n logger.error(`[jobs] \"${job.task}\" failed permanently after ${job.attempts} attempts`, { jobId: job.id, error: message });\n }\n }\n }\n\n async function poll(): Promise<number> {\n // The reaper, on its own cadence.\n const now = Date.now();\n if (now - lastReapAt > visibilityTimeoutMs * REAP_INTERVAL_FACTOR) {\n lastReapAt = now;\n try {\n await store.reapExpired(visibilityTimeoutMs);\n } catch (error) {\n logger.error(\"[jobs] Failed to reclaim expired jobs\", { error });\n }\n }\n\n const jobs = await store.claim(concurrency, workerId);\n if (jobs.length === 0) return 0;\n\n // Settled, not `all`: `runJob` handles its own errors, but a store\n // write failing inside it must not abandon this batch's siblings.\n await Promise.allSettled(jobs.map(runJob));\n return jobs.length;\n }\n\n function schedule(delayMs: number): void {\n if (!running) return;\n timer = setTimeout(() => {\n void tick();\n }, delayMs);\n // Never hold the process open. A queue with nothing to do should not be\n // the reason `rebase dev` will not exit.\n timer.unref?.();\n }\n\n async function tick(): Promise<void> {\n if (!running) return;\n const work = (async () => {\n try {\n return await poll();\n } catch (error) {\n logger.error(\"[jobs] Poll failed\", { error });\n return 0;\n }\n })();\n inFlight = work;\n const count = await work;\n\n // A full batch means there is probably more waiting, so go straight\n // back rather than sleeping through a backlog.\n schedule(count >= concurrency ? 0 : pollIntervalMs);\n }\n\n return {\n start(): void {\n if (running) return;\n running = true;\n logger.info(`[jobs] Worker started (concurrency ${concurrency}, poll ${pollIntervalMs}ms)`);\n schedule(0);\n },\n\n async stop(): Promise<void> {\n running = false;\n if (timer) {\n clearTimeout(timer);\n timer = null;\n }\n if (draining) return;\n draining = true;\n // Jobs in flight keep their claim until they finish or the\n // visibility timeout expires, so waiting here is what turns a\n // graceful shutdown into \"no job runs twice\".\n await inFlight.catch(() => undefined);\n draining = false;\n },\n\n runOnce(): Promise<number> {\n return poll();\n },\n\n register<P = unknown>(task: string, handler: JobHandler<P>): void {\n if (handlers.has(task)) {\n logger.warn(`[jobs] Task \"${task}\" was already registered; the later handler wins`);\n }\n handlers.set(task, handler as JobHandler<never>);\n },\n\n isRunning(): boolean {\n return running;\n },\n\n async enqueue<P = unknown>(task: string, payload?: P, enqueueOptions: EnqueueOptions = {}): Promise<string | null> {\n return store.insert({\n task,\n payload: payload ?? null,\n runAt: new Date(Date.now() + (enqueueOptions.delayMs ?? 0)),\n maxAttempts: enqueueOptions.maxAttempts ?? defaultMaxAttempts,\n idempotencyKey: enqueueOptions.idempotencyKey\n });\n }\n };\n}\n","export { createJobStore } from \"./job-store\";\nexport type { JobStore } from \"./job-store\";\nexport { createJobQueue, defaultBackoff } from \"./job-queue\";\nexport type { JobQueue } from \"./job-queue\";\nexport type {\n EnqueueOptions,\n JobContext,\n JobHandler,\n JobQueueClient,\n JobQueueOptions,\n JobRecord,\n JobStatus\n} from \"./types\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAgBA,IAAM,QAAQ;;AAGd,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"process";
3
- __createRequire(import.meta.url);
4
- import { i as __toESM, n as __exportAll, r as __require, t as __commonJSMin } from "./rolldown-runtime-DSJWtz9O.js";
5
- import "./src-CrCxd8km.js";
6
- import { t as logger } from "./logger-DfvF_8r-.js";
1
+ import { createRequire as __rebaseCreateRequire } from "module";
2
+ import __rebaseProcess from "process";
3
+ globalThis.process ??= __rebaseProcess;
4
+ __rebaseCreateRequire(import.meta.url);
5
+ import { i as __toESM, n as __exportAll, r as __require, t as __commonJSMin } from "./rolldown-runtime-dW7B1o5h.js";
6
+ import "./src-6VqGNeDB.js";
7
+ import { t as logger } from "./logger-DS03e908.js";
7
8
  import { createHash, createPrivateKey, createPublicKey, randomBytes } from "crypto";
8
9
  import path from "node:path";
9
10
  //#region ../types/src/types/storage_source.ts
@@ -4691,7 +4692,7 @@ function verifyAccessToken(token) {
4691
4692
  logger.error("[JWT] Verification failed: a purpose-scoped token is not an access token", { purpose: decoded.purpose });
4692
4693
  return null;
4693
4694
  }
4694
- const id = decoded.uid || decoded.userId || decoded.sub;
4695
+ const id = decoded.uid || decoded.sub;
4695
4696
  if (!id) {
4696
4697
  logger.error("[JWT] Verification failed: missing id in payload", { detail: decoded });
4697
4698
  return null;
@@ -4860,4 +4861,4 @@ function verifyDownloadToken(token) {
4860
4861
  //#endregion
4861
4862
  export { canonicalStorageKey as C, findStorageSuffixCollision as D, DEFAULT_STORAGE_SOURCE_KEY as E, normalizeStorageSources as O, canonicalStorageId as S, require_jsonwebtoken as T, verifyMfaPendingToken as _, generateMfaPendingToken as a, InvalidStorageKeyError as b, getJwks as c, hasAsymmetricSigningKey as d, hashRefreshToken as f, verifyDownloadToken as g, verifyAccessToken as h, generateDownloadToken as i, storageEnvSuffix as k, getRefreshTokenExpiry as l, jwt_exports as m, configureJwt as n, generateRefreshToken as o, isJwtConfigured as p, generateAccessToken as r, getAccessTokenExpiry as s, MAX_COOKIE_AGE_MS as t, getRefreshTokenTtlMs as u, normalizePemFromEnv as v, tryCanonicalStorageKey as w, canonicalStorageBucket as x, InvalidStorageBucketError as y };
4862
4863
 
4863
- //# sourceMappingURL=jwt-DxH9fLPt.js.map
4864
+ //# sourceMappingURL=jwt-CpMqxVct.js.map