@getstrata/core 2.2.59 → 3.0.3

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 (330) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/core/audit/exportAuditLogs.d.ts +12 -3
  3. package/dist/core/audit/exportStore.d.ts +6 -2
  4. package/dist/core/audit/siemFormatter.d.ts +2 -2
  5. package/dist/core/auth/abilityCatalog.d.ts +1 -9
  6. package/dist/core/auth/authContext.d.ts +2 -0
  7. package/dist/core/auth/authOutcome.d.ts +32 -0
  8. package/dist/core/auth/authReader.d.ts +22 -0
  9. package/dist/core/auth/basicAuthGuard.d.ts +19 -3
  10. package/dist/core/auth/emailVerification.d.ts +1 -1
  11. package/dist/core/auth/guard.d.ts +27 -6
  12. package/dist/core/auth/intendedUrlCookie.d.ts +13 -7
  13. package/dist/core/auth/jwt.d.ts +5 -4
  14. package/dist/core/auth/jwtGuard.d.ts +7 -6
  15. package/dist/core/auth/membershipContext.d.ts +10 -7
  16. package/dist/core/auth/membershipContextMiddleware.d.ts +3 -3
  17. package/dist/core/auth/membershipMiddleware.d.ts +1 -2
  18. package/dist/core/auth/membershipServiceImplementation.d.ts +1 -1
  19. package/dist/core/auth/oauth/oidcIdToken.d.ts +25 -16
  20. package/dist/core/auth/oauth/oidcProvider.d.ts +6 -4
  21. package/dist/core/auth/oauth/providers.d.ts +3 -1
  22. package/dist/core/auth/oneTimeToken.d.ts +2 -3
  23. package/dist/core/auth/passwordConfirmCookie.d.ts +15 -6
  24. package/dist/core/auth/passwordLogin.d.ts +5 -5
  25. package/dist/core/auth/pendingMfaCookie.d.ts +2 -2
  26. package/dist/core/auth/policy.d.ts +23 -10
  27. package/dist/core/auth/requireAuthCapability.d.ts +3 -0
  28. package/dist/core/auth/resolveMembershipService.d.ts +3 -2
  29. package/dist/core/auth/saml/samlAssertionReplay.d.ts +4 -4
  30. package/dist/core/auth/saml/samlServiceProvider.d.ts +11 -7
  31. package/dist/core/auth/scimAuthMiddleware.d.ts +10 -3
  32. package/dist/core/auth/sessionCookie.d.ts +20 -9
  33. package/dist/core/auth/sessionGuard.d.ts +14 -3
  34. package/dist/core/cache/createCacheStore.d.ts +6 -3
  35. package/dist/core/cache/redisCacheStore.d.ts +5 -1
  36. package/dist/core/cache/repository.d.ts +5 -1
  37. package/dist/core/cache/simpleCache.d.ts +2 -1
  38. package/dist/core/cache/tokens.d.ts +8 -0
  39. package/dist/core/config/applicationConfig.d.ts +21 -0
  40. package/dist/core/config/configSnapshot.d.ts +3 -0
  41. package/dist/core/config/envSchema.d.ts +3 -2
  42. package/dist/core/contracts/application.d.ts +153 -0
  43. package/dist/core/contracts/authUserDirectory.d.ts +24 -7
  44. package/dist/core/contracts/container.d.ts +20 -23
  45. package/dist/core/contracts/di.d.ts +4 -2
  46. package/dist/core/contracts/executionContext.d.ts +5 -0
  47. package/dist/core/contracts/executionOperations.d.ts +10 -0
  48. package/dist/core/contracts/executionOwnership.d.ts +38 -0
  49. package/dist/core/contracts/persistence.d.ts +20 -0
  50. package/dist/core/contracts/serviceTokens.d.ts +32 -18
  51. package/dist/core/contracts/services.d.ts +63 -0
  52. package/dist/core/contracts/token.d.ts +19 -0
  53. package/dist/core/crypto/fieldEncryption.d.ts +19 -7
  54. package/dist/core/crypto/fieldEncryptionRotation.d.ts +21 -5
  55. package/dist/core/crypto/mfaSecret.d.ts +10 -3
  56. package/dist/core/database/baseRepository.d.ts +14 -10
  57. package/dist/core/database/bindings.d.ts +6 -0
  58. package/dist/core/database/bunSql.d.ts +3 -12
  59. package/dist/core/database/definitions.d.ts +209 -0
  60. package/dist/core/database/dialect.d.ts +5 -24
  61. package/dist/core/database/driverPorts.d.ts +116 -0
  62. package/dist/core/database/errors.d.ts +47 -7
  63. package/dist/core/database/factory.d.ts +51 -31
  64. package/dist/core/database/index.d.ts +9 -10
  65. package/dist/core/database/keyset.d.ts +2 -1
  66. package/dist/core/database/legacyNativeTransaction.d.ts +5 -0
  67. package/dist/core/database/manager.d.ts +12 -0
  68. package/dist/core/database/model.d.ts +91 -97
  69. package/dist/core/database/modelQuery.d.ts +9 -5
  70. package/dist/core/database/modelRegistry.d.ts +52 -33
  71. package/dist/core/database/mysqlConnection.d.ts +1 -19
  72. package/dist/core/database/mysqlNative.d.ts +38 -0
  73. package/dist/core/database/nativeTransactions.d.ts +16 -0
  74. package/dist/core/database/ormInternal.d.ts +85 -0
  75. package/dist/core/database/ormRuntime.d.ts +4 -0
  76. package/dist/core/database/ownedReadOnlyObservation.d.ts +16 -0
  77. package/dist/core/database/postgresNative.d.ts +11 -0
  78. package/dist/core/database/query.d.ts +24 -23
  79. package/dist/core/database/queryPlanSnapshot.d.ts +7 -0
  80. package/dist/core/database/rawMysql.d.ts +7 -0
  81. package/dist/core/database/rawPostgres.d.ts +23 -0
  82. package/dist/core/database/rawSqlite.d.ts +6 -0
  83. package/dist/core/database/relationQueries.d.ts +3 -2
  84. package/dist/core/database/relationQuery.d.ts +24 -6
  85. package/dist/core/database/repositoryQuery.d.ts +6 -1
  86. package/dist/core/database/schema/driver.d.ts +1 -1
  87. package/dist/core/database/seeders/types.d.ts +1 -1
  88. package/dist/core/database/session.d.ts +13 -0
  89. package/dist/core/database/sessionFactory.d.ts +9 -0
  90. package/dist/core/database/sqlDialects.d.ts +22 -0
  91. package/dist/core/database/tokens.d.ts +7 -0
  92. package/dist/core/database/transactionSession.d.ts +43 -0
  93. package/dist/core/errors/http.d.ts +4 -1
  94. package/dist/core/events/{eventBus.d.ts → eventBusClass.d.ts} +1 -3
  95. package/dist/core/events/index.d.ts +2 -3
  96. package/dist/core/events/outbox/index.d.ts +44 -13
  97. package/dist/core/events/outbox/metrics.d.ts +4 -4
  98. package/dist/core/events/outbox/workerTransport.d.ts +17 -0
  99. package/dist/core/http/authMiddleware.d.ts +2 -2
  100. package/dist/core/http/authorizeMiddleware.d.ts +2 -2
  101. package/dist/core/http/bodySizeLimitMiddleware.d.ts +1 -1
  102. package/dist/core/http/conditionalResponse.d.ts +7 -2
  103. package/dist/core/http/contentSecurityPolicy.d.ts +4 -3
  104. package/dist/core/http/corsMiddleware.d.ts +2 -1
  105. package/dist/core/http/csrfMiddleware.d.ts +6 -1
  106. package/dist/core/http/csrfToken.d.ts +17 -7
  107. package/dist/core/http/databaseError.d.ts +4 -0
  108. package/dist/core/http/etag.d.ts +5 -2
  109. package/dist/core/http/flashMiddleware.d.ts +1 -1
  110. package/dist/core/http/flashSession.d.ts +13 -7
  111. package/dist/core/http/index.d.ts +1 -1
  112. package/dist/core/http/loginThrottleMiddleware.d.ts +3 -3
  113. package/dist/core/http/memoryThrottleMiddleware.d.ts +2 -2
  114. package/dist/core/http/parseMultipartUpload.d.ts +3 -2
  115. package/dist/core/http/requestMetaContext.d.ts +5 -0
  116. package/dist/core/http/requireAbilityMiddleware.d.ts +1 -1
  117. package/dist/core/http/requireAuthMiddleware.d.ts +2 -2
  118. package/dist/core/http/requirePasswordConfirmMiddleware.d.ts +2 -1
  119. package/dist/core/http/requireVerifiedMiddleware.d.ts +2 -2
  120. package/dist/core/http/requireWebAuthMiddleware.d.ts +2 -2
  121. package/dist/core/http/response.d.ts +4 -3
  122. package/dist/core/http/scimThrottleMiddleware.d.ts +4 -3
  123. package/dist/core/http/securedRouteModelBinding.d.ts +10 -3
  124. package/dist/core/http/securityHeadersMiddleware.d.ts +1 -2
  125. package/dist/core/http/signedUrl.d.ts +8 -6
  126. package/dist/core/http/throttleMiddleware.d.ts +9 -5
  127. package/dist/core/http/uploads.d.ts +9 -3
  128. package/dist/core/http/webErrorResponse.d.ts +8 -2
  129. package/dist/core/jobs/exportAuditLogsJob.d.ts +5 -2
  130. package/dist/core/jobs/invalidateCacheTagsJob.d.ts +5 -4
  131. package/dist/core/lifecycle/gracefulShutdown.d.ts +2 -5
  132. package/dist/core/logging/requestLoggingMiddleware.d.ts +1 -1
  133. package/dist/core/mail/mailer.d.ts +7 -5
  134. package/dist/core/openapi/generator.d.ts +12 -4
  135. package/dist/core/openapi/registeredRoute.d.ts +4 -4
  136. package/dist/core/queue/createAppQueue.d.ts +9 -4
  137. package/dist/core/queue/failedJobMetrics.d.ts +11 -2
  138. package/dist/core/queue/failedJobRepository.d.ts +6 -3
  139. package/dist/core/queue/failedJobService.d.ts +1 -0
  140. package/dist/core/queue/index.d.ts +16 -6
  141. package/dist/core/queue/jobRegistry.d.ts +3 -2
  142. package/dist/core/queue/jobRunner.d.ts +10 -4
  143. package/dist/core/queue/publicQueue.d.ts +18 -9
  144. package/dist/core/queue/queueConfig.d.ts +20 -10
  145. package/dist/core/queue/queueHandoff.d.ts +31 -0
  146. package/dist/core/queue/queueMetrics.d.ts +10 -3
  147. package/dist/core/queue/queueServices.d.ts +28 -0
  148. package/dist/core/queue/queueWorkerTransport.d.ts +28 -0
  149. package/dist/core/queue/redisQueue.d.ts +19 -10
  150. package/dist/core/queue/redisQueueKeys.d.ts +5 -5
  151. package/dist/core/queue/redisStreams.d.ts +17 -10
  152. package/dist/core/queue/resilientQueue.d.ts +10 -4
  153. package/dist/core/queue/types.d.ts +1 -0
  154. package/dist/core/runtime/appKeyPrefix.d.ts +5 -5
  155. package/dist/core/runtime/application.d.ts +7 -0
  156. package/dist/core/runtime/executionContext.d.ts +9 -0
  157. package/dist/core/runtime/executionTasks.d.ts +49 -0
  158. package/dist/core/runtime/resourceJournal.d.ts +67 -0
  159. package/dist/core/runtime/serviceGraph.d.ts +14 -0
  160. package/dist/core/security/oauthState.d.ts +13 -9
  161. package/dist/core/security/publicReads.d.ts +2 -2
  162. package/dist/core/security/safeFetch.d.ts +1 -0
  163. package/dist/core/security/safeUrl.d.ts +3 -0
  164. package/dist/core/security/scimTenantTokens.d.ts +1 -1
  165. package/dist/core/security/tokenExpiry.d.ts +1 -1
  166. package/dist/core/security/webhookSignature.d.ts +1 -1
  167. package/dist/core/storage/storage.d.ts +24 -16
  168. package/dist/core/tenant/databaseTenantContext.d.ts +5 -7
  169. package/dist/core/tenant/resolveTenant.d.ts +2 -2
  170. package/dist/core/tenant/scopedDatabasePolicy.d.ts +8 -0
  171. package/dist/core/tenant/tenantDatabaseScope.d.ts +3 -7
  172. package/dist/core/tenant/tenantMiddleware.d.ts +15 -4
  173. package/dist/core/tracing/otel.d.ts +4 -6
  174. package/dist/core/tracing/tracingMiddleware.d.ts +3 -3
  175. package/dist/core/view/etaViewEngine.d.ts +1 -1
  176. package/dist/core/view/index.d.ts +5 -4
  177. package/dist/core/view/tokens.d.ts +8 -0
  178. package/dist/core/view/viewRenderer.d.ts +2 -2
  179. package/dist/core/view/webErrorView.d.ts +8 -7
  180. package/dist/core/view/webLayoutData.d.ts +6 -4
  181. package/dist/entries/audit/exportAuditLogs.js +87 -1024
  182. package/dist/entries/audit/siemFormatter.js +4 -90
  183. package/dist/entries/auth/abilityCatalog.js +28 -0
  184. package/dist/entries/auth/accessControl.js +5 -16
  185. package/dist/entries/auth/accountStateMigration.js +216 -236
  186. package/dist/entries/auth/authOutcome.js +80 -0
  187. package/dist/entries/auth/authReader.js +68 -0
  188. package/dist/entries/auth/basicAuthGuard.js +144 -1249
  189. package/dist/entries/auth/emailVerification.js +2 -2
  190. package/dist/entries/auth/intendedUrlCookie.js +35 -59
  191. package/dist/entries/auth/jwt.js +22 -47
  192. package/dist/entries/auth/jwtGuard.js +99 -231
  193. package/dist/entries/auth/membershipMiddleware.js +71 -10
  194. package/dist/entries/auth/membershipScope.js +16 -22
  195. package/dist/entries/auth/membershipService.js +11 -28
  196. package/dist/entries/auth/oauth/oidcProvider.js +59 -64
  197. package/dist/entries/auth/oauth/providers.js +12 -95
  198. package/dist/entries/auth/oauth/samlProvider.js +53 -1080
  199. package/dist/entries/auth/oneTimeToken.js +27 -289
  200. package/dist/entries/auth/passwordConfirmCookie.js +40 -63
  201. package/dist/entries/auth/passwordLogin.js +94 -399
  202. package/dist/entries/auth/pendingMfaCookie.js +6 -6
  203. package/dist/entries/auth/policy.js +22 -56
  204. package/dist/entries/auth/saml/samlServiceProvider.js +55 -1084
  205. package/dist/entries/auth/scimAuthMiddleware.js +16 -3358
  206. package/dist/entries/auth/sessionCookie.js +58 -73
  207. package/dist/entries/auth/sessionGuard.js +95 -248
  208. package/dist/entries/auth/tokenHash.js +11 -43
  209. package/dist/entries/cache/createCacheStore.js +126 -117
  210. package/dist/entries/cache/repository.js +25 -0
  211. package/dist/entries/cache/simpleCache.js +12 -7
  212. package/dist/entries/cache/tokens.js +9 -0
  213. package/dist/entries/config/applicationConfig.js +163 -0
  214. package/dist/entries/config/envSchema.js +21 -4
  215. package/dist/entries/contracts/application.js +1 -0
  216. package/dist/entries/contracts/authUserDirectory.js +10 -0
  217. package/dist/entries/contracts/container.js +99 -26
  218. package/dist/entries/contracts/di.js +3 -0
  219. package/dist/entries/contracts/executionContext.js +7 -0
  220. package/dist/entries/contracts/executionOperations.js +7 -0
  221. package/dist/entries/contracts/executionOwnership.js +7 -0
  222. package/dist/entries/contracts/membership.js +1 -0
  223. package/dist/entries/contracts/persistence.js +1 -0
  224. package/dist/entries/contracts/serviceTokens.js +26 -116
  225. package/dist/entries/contracts/services.js +98 -0
  226. package/dist/entries/contracts/token.js +41 -0
  227. package/dist/entries/crypto/fieldEncryption.js +49 -50
  228. package/dist/entries/crypto/mfaSecret.js +86 -82
  229. package/dist/entries/database/acquisitionMetrics.js +107 -0
  230. package/dist/entries/database/baseRepository.d.ts +2 -0
  231. package/dist/entries/database/bindings.js +2082 -0
  232. package/dist/entries/database/definitions.js +175 -0
  233. package/dist/entries/database/errors.js +114 -89
  234. package/dist/entries/database/factory.js +67 -28
  235. package/dist/entries/database/migrations.js +172 -224
  236. package/dist/entries/database/model.d.ts +1 -0
  237. package/dist/entries/database/ownedReadOnlyObservation.js +1282 -0
  238. package/dist/entries/database/query.js +159 -161
  239. package/dist/entries/database/repositoryQuery.d.ts +1 -0
  240. package/dist/entries/database/repositoryQuery.js +1 -1373
  241. package/dist/entries/database/schema.js +216 -236
  242. package/dist/entries/database/session.js +1 -0
  243. package/dist/entries/database/sqliteConnection.js +525 -83
  244. package/dist/entries/database/tokens.js +9 -0
  245. package/dist/entries/errors/http.js +118 -1
  246. package/dist/entries/http/authMiddleware.js +8 -5
  247. package/dist/entries/http/authorizeMiddleware.js +1 -1
  248. package/dist/entries/http/bodySizeLimitMiddleware.js +2 -2
  249. package/dist/entries/http/clientIp.js +2 -2
  250. package/dist/entries/http/conditionalResponse.js +19 -7
  251. package/dist/entries/http/corsMiddleware.js +6 -101
  252. package/dist/entries/http/csrfMiddleware.js +92 -106
  253. package/dist/entries/http/csrfToken.js +58 -94
  254. package/dist/entries/http/etag.js +7 -4
  255. package/dist/entries/http/flashMiddleware.js +57 -99
  256. package/dist/entries/http/flashSession.js +41 -68
  257. package/dist/entries/http/memoryThrottleMiddleware.js +43 -186
  258. package/dist/entries/http/metricsMiddleware.js +2 -16
  259. package/dist/entries/http/parseMultipartUpload.js +15 -29
  260. package/dist/entries/http/requireAbilityMiddleware.js +2 -2
  261. package/dist/entries/http/requirePasswordConfirmMiddleware.js +19 -31
  262. package/dist/entries/http/requireVerifiedMiddleware.js +4 -7
  263. package/dist/entries/http/requireWebAuthMiddleware.js +2 -2
  264. package/dist/entries/http/response.js +67 -233
  265. package/dist/entries/http/routeMiddleware.js +0 -14
  266. package/dist/entries/http/scimThrottleMiddleware.js +43 -237
  267. package/dist/entries/http/securedRouteModelBinding.js +37 -25
  268. package/dist/entries/http/securityHeadersMiddleware.js +8 -25
  269. package/dist/entries/http/signedUrl.js +20 -110
  270. package/dist/entries/http/throttleMiddleware.js +41 -185
  271. package/dist/entries/http/uploads.js +9 -24
  272. package/dist/entries/jobs/exportAuditLogsJob.js +17 -1498
  273. package/dist/entries/jobs/invalidateCacheTagsJob.js +12 -47
  274. package/dist/entries/logging/requestLoggingMiddleware.js +4 -4
  275. package/dist/entries/mail/mailer.js +68 -78
  276. package/dist/entries/openapi/generator.js +33 -100
  277. package/dist/entries/queue/createAppQueue.js +416 -254
  278. package/dist/entries/queue/failedJobRepository.js +19 -8
  279. package/dist/entries/queue/failedJobService.js +1 -0
  280. package/dist/entries/queue/jobRunner.js +141 -66
  281. package/dist/entries/queue/publicQueue.js +411 -255
  282. package/dist/entries/queue/queueConfig.js +61 -0
  283. package/dist/entries/queue/queueHandoff.js +318 -0
  284. package/dist/entries/queue/queueMetrics.js +989 -1384
  285. package/dist/entries/queue/queueServices.js +133 -0
  286. package/dist/entries/queue/queueWorkerTransport.js +1703 -0
  287. package/dist/entries/queue/redisQueue.js +282 -180
  288. package/dist/entries/queue.js +6 -45
  289. package/dist/entries/runtime/application.js +1585 -0
  290. package/dist/entries/runtime/asyncContextStore.js +0 -16
  291. package/dist/entries/security/oauthState.js +35 -67
  292. package/dist/entries/security/publicReads.js +4 -4
  293. package/dist/entries/security/safeFetch.js +1 -0
  294. package/dist/entries/security/scimTenantTokens.js +3 -3
  295. package/dist/entries/security/tokenExpiry.js +2 -2
  296. package/dist/entries/security/totp.js +1 -87
  297. package/dist/entries/security/webhookSignature.js +2 -90
  298. package/dist/entries/storage/storage.js +120 -45
  299. package/dist/entries/tenant/databaseTenantContext.js +6 -907
  300. package/dist/entries/tenant/publicTenantHost.js +78 -0
  301. package/dist/entries/tenant/resolveTenant.js +14 -0
  302. package/dist/entries/tenant/scopedDatabasePolicy.js +75 -0
  303. package/dist/entries/tenant/tenancyConfig.js +1 -19
  304. package/dist/entries/tenant/tenantDatabaseScope.js +3 -892
  305. package/dist/entries/tracing/tracingMiddleware.js +104 -173
  306. package/dist/entries/view/tokens.js +11 -0
  307. package/dist/framework/public-api.d.ts +62 -44
  308. package/dist/index.js +10433 -11347
  309. package/dist/internal/ormInternal.js +117 -0
  310. package/dist/internal/ormRuntime.js +5000 -0
  311. package/package.json +158 -46
  312. package/dist/core/database/bindConnection.d.ts +0 -1
  313. package/dist/core/database/boundConnection.d.ts +0 -5
  314. package/dist/core/database/defaultConnection.d.ts +0 -7
  315. package/dist/core/database/namedConnections.d.ts +0 -15
  316. package/dist/core/database/readOnlyCollector.d.ts +0 -7
  317. package/dist/core/database/repositoryConnection.d.ts +0 -4
  318. package/dist/core/database/transaction.d.ts +0 -23
  319. package/dist/core/database/transactionControl.d.ts +0 -6
  320. package/dist/core/events/deferredModelEvents.d.ts +0 -7
  321. package/dist/core/facades/index.d.ts +0 -10
  322. package/dist/core/runtime/applicationRegistry.d.ts +0 -18
  323. package/dist/entries/database/bindConnection.js +0 -1
  324. package/dist/entries/database/boundConnection.js +0 -1
  325. package/dist/entries/database/defaultConnection.js +0 -1
  326. package/dist/entries/database/namedConnections.js +0 -1
  327. package/dist/entries/database/repositoryConnection.js +0 -1
  328. package/dist/entries/database/transaction.js +0 -1
  329. package/dist/entries/facades.js +0 -711
  330. package/dist/entries/runtime/applicationRegistry.js +0 -1
@@ -1,1083 +1,138 @@
1
1
  // @bun
2
- var __esm = (fn, res, err) => () => {
3
- if (fn)
4
- try {
5
- res = fn(fn = 0);
6
- } catch (e) {
7
- err = [e];
8
- }
9
- if (err)
10
- throw err[0];
11
- return res;
12
- };
13
-
14
- // ../../src/core/runtime/asyncContextStore.ts
15
- import { AsyncLocalStorage } from "async_hooks";
16
- function createAsyncContextStore(key) {
17
- const symbol = Symbol.for(key);
18
- const globalRecord = globalThis;
19
- const existing = globalRecord[symbol];
20
- if (existing) {
21
- return existing;
22
- }
23
- const store = new AsyncLocalStorage;
24
- globalRecord[symbol] = store;
25
- return store;
26
- }
27
- var init_asyncContextStore = () => {};
28
-
29
- // ../../src/core/database/schema/driver.ts
30
- function normalizeConnectionName(connection) {
31
- const normalized = connection.trim().toLowerCase();
32
- if (normalized === "pgsql" || normalized === "postgres" || normalized === "postgresql") {
33
- return "pgsql";
34
- }
35
- if (normalized === "mysql" || normalized === "mariadb") {
36
- return "mysql";
37
- }
38
- if (normalized === "sqlite") {
39
- return "sqlite";
40
- }
41
- throw new Error(`Unsupported DB_CONNECTION: ${connection}`);
42
- }
43
- function resolveDriverFromUrl(url) {
44
- const normalized = url.trim().toLowerCase();
45
- if (normalized.startsWith("postgres://") || normalized.startsWith("postgresql://") || normalized.startsWith("postgres:")) {
46
- return "pgsql";
47
- }
48
- if (normalized.startsWith("mysql://") || normalized.startsWith("mysql:")) {
49
- return "mysql";
50
- }
51
- if (normalized.startsWith("sqlite:")) {
52
- return "sqlite";
53
- }
54
- return null;
55
- }
56
- function resolveDatabaseDriver(options = {}) {
57
- const connection = options.connection ?? process.env.DB_CONNECTION;
58
- if (connection) {
59
- return normalizeConnectionName(connection);
60
- }
61
- const url = options.url ?? process.env.DATABASE_URL ?? "";
62
- const fromUrl = resolveDriverFromUrl(url);
63
- if (fromUrl) {
64
- return fromUrl;
65
- }
66
- return "pgsql";
67
- }
68
- var init_driver = () => {};
69
-
70
- // ../../src/core/database/dialect.ts
71
- function assertSafeIdentifier(identifier) {
72
- if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(identifier)) {
73
- throw new Error(`Invalid SQL identifier: ${identifier}`);
74
- }
75
- return identifier;
76
- }
77
- function quoteIdentifierFor(dialect, column) {
78
- return dialect.quoteIdentifier(column);
79
- }
80
- function dialectFor(driver) {
81
- return dialects[driver];
82
- }
83
- function currentSqlDialect() {
84
- return dialectContext.getStore() ?? dialectOverride ?? dialectFor(resolveDatabaseDriver());
85
- }
86
- var postgresDialect, mysqlDialect, sqliteDialect, dialects, dialectContext, dialectOverride = null;
87
- var init_dialect = __esm(() => {
88
- init_asyncContextStore();
89
- init_driver();
90
- postgresDialect = {
91
- driver: "pgsql",
92
- placeholder(index) {
93
- return `$${index}`;
94
- },
95
- quoteIdentifier(identifier) {
96
- return `"${assertSafeIdentifier(identifier)}"`;
97
- },
98
- nowExpression() {
99
- return "NOW()";
100
- },
101
- epochMillisecondsExpression() {
102
- return "CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT)";
103
- },
104
- rowLockClause(skipLocked = false) {
105
- return ` FOR UPDATE${skipLocked ? " SKIP LOCKED" : ""}`;
106
- },
107
- timestampValue(value) {
108
- return value.toISOString();
109
- },
110
- returningClause(columns) {
111
- return ` RETURNING ${columns}`;
112
- },
113
- ilikeOperator() {
114
- return "ILIKE";
115
- },
116
- nullsLastSuffix() {
117
- return " NULLS LAST";
118
- },
119
- castToText(expression) {
120
- return `${expression}::text`;
121
- },
122
- upsertSuffix(conflictColumns, updateColumns) {
123
- const target = conflictColumns.map((column) => quoteIdentifierFor(this, column)).join(", ");
124
- if (updateColumns.length === 0) {
125
- return ` ON CONFLICT (${target}) DO NOTHING`;
126
- }
127
- const assignments = updateColumns.map((column) => {
128
- const quoted = quoteIdentifierFor(this, column);
129
- return `${quoted} = excluded.${quoted}`;
130
- }).join(", ");
131
- return ` ON CONFLICT (${target}) DO UPDATE SET ${assignments}`;
132
- }
133
- };
134
- mysqlDialect = {
135
- driver: "mysql",
136
- placeholder() {
137
- return "?";
138
- },
139
- quoteIdentifier(identifier) {
140
- return `\`${assertSafeIdentifier(identifier)}\``;
141
- },
142
- nowExpression() {
143
- return "CURRENT_TIMESTAMP";
144
- },
145
- epochMillisecondsExpression() {
146
- return "CAST(UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000 AS UNSIGNED)";
147
- },
148
- rowLockClause(skipLocked = false) {
149
- return ` FOR UPDATE${skipLocked ? " SKIP LOCKED" : ""}`;
150
- },
151
- timestampValue(value) {
152
- return value.toISOString().slice(0, 19).replace("T", " ");
153
- },
154
- returningClause() {
155
- return "";
156
- },
157
- ilikeOperator() {
158
- return "LIKE";
159
- },
160
- nullsLastSuffix() {
161
- return "";
162
- },
163
- castToText(expression) {
164
- return `CAST(${expression} AS CHAR)`;
165
- },
166
- upsertSuffix(conflictColumns, updateColumns) {
167
- if (updateColumns.length === 0) {
168
- const anchor = quoteIdentifierFor(this, conflictColumns[0] ?? "");
169
- return ` ON DUPLICATE KEY UPDATE ${anchor} = ${anchor}`;
170
- }
171
- const assignments = updateColumns.map((column) => {
172
- const quoted = quoteIdentifierFor(this, column);
173
- return `${quoted} = VALUES(${quoted})`;
174
- }).join(", ");
175
- return ` ON DUPLICATE KEY UPDATE ${assignments}`;
176
- }
177
- };
178
- sqliteDialect = {
179
- driver: "sqlite",
180
- placeholder() {
181
- return "?";
182
- },
183
- quoteIdentifier(identifier) {
184
- return `"${assertSafeIdentifier(identifier)}"`;
185
- },
186
- nowExpression() {
187
- return "strftime('%Y-%m-%dT%H:%M:%fZ', 'now')";
188
- },
189
- epochMillisecondsExpression() {
190
- return "CAST((julianday('now') - 2440587.5) * 86400000 AS INTEGER)";
191
- },
192
- rowLockClause() {
193
- return "";
194
- },
195
- timestampValue(value) {
196
- return value.toISOString();
197
- },
198
- returningClause(columns) {
199
- return ` RETURNING ${columns}`;
200
- },
201
- ilikeOperator() {
202
- return "LIKE";
203
- },
204
- nullsLastSuffix() {
205
- return "";
206
- },
207
- castToText(expression) {
208
- return `CAST(${expression} AS TEXT)`;
209
- },
210
- upsertSuffix(conflictColumns, updateColumns) {
211
- const target = conflictColumns.map((column) => quoteIdentifierFor(this, column)).join(", ");
212
- if (updateColumns.length === 0) {
213
- return ` ON CONFLICT (${target}) DO NOTHING`;
214
- }
215
- const assignments = updateColumns.map((column) => {
216
- const quoted = quoteIdentifierFor(this, column);
217
- return `${quoted} = excluded.${quoted}`;
218
- }).join(", ");
219
- return ` ON CONFLICT (${target}) DO UPDATE SET ${assignments}`;
220
- }
221
- };
222
- dialects = {
223
- pgsql: postgresDialect,
224
- mysql: mysqlDialect,
225
- sqlite: sqliteDialect
226
- };
227
- dialectContext = createAsyncContextStore("@getstrata/sqlDialect");
228
- });
229
-
230
- // ../../src/core/database/connectionContext.ts
231
- function runWithDatabaseConnection(connection, callback) {
232
- return activeConnection.run(connection, callback);
233
- }
234
- function getActiveDatabaseConnection(fallback) {
235
- return activeConnection.getStore() ?? fallback;
236
- }
237
- function hasActiveDatabaseConnection() {
238
- return activeConnection.getStore() !== undefined;
239
- }
240
- var activeConnection;
241
- var init_connectionContext = __esm(() => {
242
- init_asyncContextStore();
243
- activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
244
- });
245
-
246
- // ../../src/core/database/boundConnection.ts
247
- function boundConnectionState() {
248
- const globalRecord = globalThis;
249
- const existing = globalRecord[BOUND_CONNECTION_KEY];
250
- if (existing) {
251
- return existing;
252
- }
253
- const created = { connection: null };
254
- globalRecord[BOUND_CONNECTION_KEY] = created;
255
- return created;
256
- }
257
- function getBoundDatabaseConnection() {
258
- return boundConnectionState().connection;
259
- }
260
- var BOUND_CONNECTION_KEY;
261
- var init_boundConnection = __esm(() => {
262
- BOUND_CONNECTION_KEY = Symbol.for("@getstrata/boundDatabaseConnection");
263
- });
264
-
265
- // ../../src/core/database/queryProxy.ts
266
- var POOL_CONNECTION_METHODS;
267
- var init_queryProxy = __esm(() => {
268
- init_connectionContext();
269
- POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
270
- });
271
-
272
- // ../../src/core/database/defaultConnection.ts
273
- function defaultConnectionState() {
274
- const globalRecord = globalThis;
275
- const existing = globalRecord[DEFAULT_CONNECTION_KEY];
276
- if (existing) {
277
- return existing;
278
- }
279
- const created = { pool: null, query: null };
280
- globalRecord[DEFAULT_CONNECTION_KEY] = created;
281
- return created;
282
- }
283
- function getDefaultDatabasePool() {
284
- const pool = defaultConnectionState().pool;
285
- if (!pool) {
286
- throw new Error("Default database pool is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
287
- }
288
- return pool;
289
- }
290
- function getDefaultDatabaseQuery() {
291
- const query = defaultConnectionState().query;
292
- if (!query) {
293
- throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
294
- }
295
- return query;
296
- }
297
- var DEFAULT_CONNECTION_KEY;
298
- var init_defaultConnection = __esm(() => {
299
- init_queryProxy();
300
- DEFAULT_CONNECTION_KEY = Symbol.for("@getstrata/defaultDatabaseConnection");
301
- });
2
+ // ../../src/core/queue/queueMetrics.ts
3
+ var {RedisClient: RedisClient3 } = globalThis.Bun;
302
4
 
303
- // ../../src/core/database/repositoryConnection.ts
304
- function resolveRepositoryConnection() {
305
- return getActiveDatabaseConnection(getBoundDatabaseConnection() ?? getDefaultDatabaseQuery());
306
- }
307
- function compileTaggedSql(strings, values) {
308
- const dialect = currentSqlDialect();
309
- let query = strings[0] ?? "";
310
- const params = [];
311
- for (let index = 0;index < values.length; index += 1) {
312
- params.push(values[index]);
313
- query += `${dialect.placeholder(params.length)}${strings[index + 1] ?? ""}`;
314
- }
315
- return { query, params };
316
- }
317
- function isTaggedStrings(value) {
318
- if (!Array.isArray(value) || typeof value[0] !== "string") {
319
- return false;
320
- }
321
- return typeof value.raw !== "undefined";
322
- }
323
- function runOnConnection(connection, args) {
324
- if (typeof connection === "function") {
325
- return connection(...args);
326
- }
327
- if (typeof connection.unsafe !== "function") {
328
- throw new Error("Database connection cannot run SQL.");
329
- }
330
- const strings = args[0];
331
- if (!isTaggedStrings(strings)) {
332
- throw new Error("Database connection cannot run tagged SQL.");
333
- }
334
- const compiled = compileTaggedSql(strings, args.slice(1));
335
- return connection.unsafe(compiled.query, compiled.params);
336
- }
337
- var repositoryConnection;
338
- var init_repositoryConnection = __esm(() => {
339
- init_boundConnection();
340
- init_connectionContext();
341
- init_defaultConnection();
342
- init_dialect();
343
- repositoryConnection = new Proxy(function repositoryConnection() {}, {
344
- apply(_target, _thisArg, args) {
345
- return runOnConnection(resolveRepositoryConnection(), args);
346
- },
347
- get(_target, property) {
348
- const connection = resolveRepositoryConnection();
349
- const value = connection[property];
350
- return typeof value === "function" ? value.bind(connection) : value;
351
- }
352
- });
353
- });
5
+ // ../../src/core/queue/redisQueue.ts
6
+ import { QueueHandoffError as QueueHandoffError2 } from "@getstrata/core/queue/queueHandoff";
7
+ var {RedisClient: RedisClient2 } = globalThis.Bun;
354
8
 
355
- // ../../src/core/tenant/tenantContext.ts
356
- function runWithTenant(tenant, callback) {
357
- return tenantContext.run(tenant, callback);
358
- }
359
- function currentTenant() {
360
- return tenantContext.getStore() ?? null;
361
- }
362
- var tenantContext;
363
- var init_tenantContext = __esm(() => {
364
- init_asyncContextStore();
365
- tenantContext = createAsyncContextStore("@getstrata/tenantContext");
366
- });
9
+ // ../../src/core/queue/jobRunner.ts
10
+ import { setTimeout as delay } from "timers/promises";
367
11
 
368
- // ../../src/core/events/eventBus.ts
369
- class EventBus {
370
- constructor() {}
371
- transactionalListeners = new Map;
372
- listenTransactional(event, listener) {
373
- const handlers = this.transactionalListeners.get(event) ?? new Set;
374
- handlers.add(listener);
375
- this.transactionalListeners.set(event, handlers);
376
- return () => {
377
- handlers.delete(listener);
378
- if (!handlers.size)
379
- this.transactionalListeners.delete(event);
380
- };
381
- }
382
- hasTransactionalListeners(event) {
383
- return (this.transactionalListeners.get(event)?.size ?? 0) > 0;
384
- }
385
- async dispatchTransactional(event, payload) {
386
- for (const listener of this.transactionalListeners.get(event) ?? [])
387
- await listener(payload);
388
- }
389
- listeners = new Map;
390
- on(event, listener) {
391
- return this.listen(event, listener);
392
- }
393
- listen(event, listener) {
394
- const handlers = this.listeners.get(event) ?? new Set;
395
- handlers.add(listener);
396
- this.listeners.set(event, handlers);
397
- return () => {
398
- handlers.delete(listener);
399
- if (handlers.size === 0) {
400
- this.listeners.delete(event);
401
- }
402
- };
403
- }
404
- async emit(event, payload) {
405
- await this.dispatch(event, payload);
406
- }
407
- async dispatch(event, payload) {
408
- const handlers = this.listeners.get(event);
409
- if (!handlers || handlers.size === 0) {
410
- return;
411
- }
412
- for (const handler of handlers) {
413
- await handler(payload);
12
+ // ../../src/core/config/configSnapshot.ts
13
+ function copyApplicationConfig(value, ancestors = new Set) {
14
+ if (value === null || typeof value === "string" || typeof value === "boolean")
15
+ return value;
16
+ if (typeof value === "number" && Number.isFinite(value))
17
+ return value;
18
+ if (typeof value !== "object" || ancestors.has(value)) {
19
+ throw new TypeError("Application config must contain acyclic plain data.");
20
+ }
21
+ const array = Array.isArray(value);
22
+ const prototype = Object.getPrototypeOf(value);
23
+ if (prototype !== (array ? Array.prototype : Object.prototype) && !(prototype === null && !array)) {
24
+ throw new TypeError("Application config must contain only plain objects and arrays.");
25
+ }
26
+ if (Object.getOwnPropertySymbols(value).length > 0) {
27
+ throw new TypeError("Application config cannot contain symbol keys.");
28
+ }
29
+ const descriptors = Object.getOwnPropertyDescriptors(value);
30
+ for (const [key, descriptor] of Object.entries(descriptors)) {
31
+ if (!(array && key === "length") && (!descriptor.enumerable || !("value" in descriptor))) {
32
+ throw new TypeError("Application config cannot contain accessors or hidden properties.");
414
33
  }
415
34
  }
416
- }
417
- function readSharedEventBus() {
418
- const globalBus = globalThis[EVENT_BUS_KEY];
419
- if (globalBus) {
420
- return globalBus;
421
- }
422
- const bus = new EventBus;
423
- globalThis[EVENT_BUS_KEY] = bus;
424
- return bus;
425
- }
426
- var EVENT_BUS_KEY, eventBus;
427
- var init_eventBus = __esm(() => {
428
- EVENT_BUS_KEY = Symbol.for("@getstrata/eventBus");
429
- eventBus = readSharedEventBus();
430
- });
431
-
432
- // ../../src/core/database/transactionControl.ts
433
- function unwrapTransactionRollback(error, depth = 0) {
434
- if (error instanceof TransactionRollback) {
435
- return error;
436
- }
437
- if (depth >= 5 || !(error instanceof Error) || error.cause === undefined) {
438
- return null;
439
- }
440
- return unwrapTransactionRollback(error.cause, depth + 1);
441
- }
442
- function hasActiveTransaction() {
443
- return hasActiveDatabaseConnection() && (transactionScopes.getStore()?.rollback.length ?? 0) > 0;
444
- }
445
- function requestTransactionRollback() {
446
- const state = transactionScopes.getStore();
447
- const index = state ? state.rollback.length - 1 : -1;
448
- if (!state || index < 0) {
449
- return;
450
- }
451
- state.rollback[index] = true;
452
- }
453
- function scopeRequestsRollback() {
454
- const state = transactionScopes.getStore();
455
- if (!state || state.rollback.length === 0) {
456
- return false;
457
- }
458
- return state.rollback[state.rollback.length - 1] === true;
459
- }
460
- async function commitOrRollbackScope(result) {
461
- if (scopeRequestsRollback()) {
462
- throw new TransactionRollback(result);
463
- }
464
- return result;
465
- }
466
- async function settleTransaction(work) {
35
+ ancestors.add(value);
467
36
  try {
468
- return await work();
469
- } catch (error) {
470
- const rollback = unwrapTransactionRollback(error);
471
- if (rollback) {
472
- return rollback.result;
473
- }
474
- throw error;
475
- }
476
- }
477
- async function runWithTransactionScope(callback) {
478
- const existing = transactionScopes.getStore();
479
- if (existing) {
480
- existing.rollback.push(false);
481
- try {
482
- return await callback();
483
- } finally {
484
- existing.rollback.pop();
485
- }
486
- }
487
- const state = { rollback: [false] };
488
- return await transactionScopes.run(state, callback);
489
- }
490
- var TransactionRollback, transactionScopes;
491
- var init_transactionControl = __esm(() => {
492
- init_asyncContextStore();
493
- init_connectionContext();
494
- TransactionRollback = class TransactionRollback extends Error {
495
- result;
496
- constructor(result) {
497
- super("Transaction rolled back");
498
- this.name = "TransactionRollback";
499
- this.result = result;
500
- }
501
- };
502
- transactionScopes = createAsyncContextStore("@getstrata/transactionScopes");
503
- });
504
-
505
- // ../../src/core/tenant/tenancyConfig.ts
506
- function readTenancyDriver(env = process.env) {
507
- const raw = env.TENANCY_DRIVER?.trim();
508
- if (raw === undefined || raw === "") {
509
- return "rls";
510
- }
511
- if (TENANCY_DRIVERS.includes(raw)) {
512
- return raw;
513
- }
514
- throw new Error(`TENANCY_DRIVER must be one of ${TENANCY_DRIVERS.join(", ")}; received "${raw}".`);
515
- }
516
- function isTenancyEnabled(env = process.env) {
517
- return readTenancyDriver(env) !== "none";
518
- }
519
- function isRlsTenancy(env = process.env) {
520
- return readTenancyDriver(env) === "rls";
521
- }
522
- var TENANCY_DRIVERS;
523
- var init_tenancyConfig = __esm(() => {
524
- TENANCY_DRIVERS = ["rls", "column", "none"];
525
- });
526
-
527
- // ../../src/core/tenant/databaseTenantContext.ts
528
- import { getDefaultDatabasePool as getDefaultDatabasePool2 } from "@getstrata/core/database/defaultConnection";
529
- import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
530
- async function applyBypassToTransaction(transaction, bypass) {
531
- await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, [
532
- bypass ? "true" : "false"
533
- ]);
534
- }
535
- function stringifyBypassIdentifier(identifier) {
536
- if (typeof identifier === "number") {
537
- return String(identifier);
538
- }
539
- return identifier.trim();
540
- }
541
- async function applyIdentifierToTransaction(transaction, identifier) {
542
- await transaction.unsafe(`SELECT set_config('app.bypass_identifier', $1, true)`, [
543
- stringifyBypassIdentifier(identifier)
544
- ]);
545
- }
546
- function assertBypassIdentifier(identifier) {
547
- if (typeof identifier === "number") {
548
- if (!Number.isInteger(identifier) || identifier <= 0) {
549
- throw new Error("RLS bypass requires a caller-supplied identifier that pins the row.");
550
- }
551
- return;
552
- }
553
- if (typeof identifier === "string" && identifier.trim() !== "") {
554
- return;
555
- }
556
- throw new Error("RLS bypass requires a caller-supplied identifier that pins the row.");
557
- }
558
- async function runWithScopedTenantTransaction(apply, callback) {
559
- if (!isRlsTenancy()) {
560
- return await callback();
561
- }
562
- if (hasActiveDatabaseConnection()) {
563
- const connection = getActiveDatabaseConnection(getDefaultDatabasePool2());
564
- return await runInTransaction(async () => {
565
- const [previous] = await connection.unsafe(`SELECT current_setting('app.tenant_id', true) AS tenant_id,
566
- current_setting('app.bypass_rls', true) AS bypass_rls,
567
- current_setting('app.bypass_identifier', true) AS bypass_identifier`);
568
- await apply(connection);
569
- const result = await callback();
570
- await connection.unsafe(`SELECT set_config('app.tenant_id', $1, true),
571
- set_config('app.bypass_rls', $2, true), set_config('app.bypass_identifier', $3, true)`, [previous?.tenant_id ?? "", previous?.bypass_rls ?? "", previous?.bypass_identifier ?? ""]);
572
- return result;
573
- });
574
- }
575
- const pool = getDefaultDatabasePool2();
576
- const begin = pool.begin;
577
- if (typeof begin !== "function") {
578
- throw new Error("RLS migration bypass requires a pool that supports begin(). Session-scoped set_config is not used on pooled connections.");
579
- }
580
- return await settleTransaction(() => runWithDeferredModelEvents(async () => {
581
- return await begin.bind(pool)(AsyncLocalStorage2.bind(async (transaction) => {
582
- await apply(transaction);
583
- return await runWithDatabaseConnection(transaction, () => runWithTransactionScope(async () => commitOrRollbackScope(await callback())));
584
- }));
585
- }));
586
- }
587
- async function runWithMigrationBypass(callback) {
588
- return await runWithScopedTenantTransaction((transaction) => applyBypassToTransaction(transaction, true), callback);
589
- }
590
- async function runWithMigrationBypassForIdentifier(identifier, callback) {
591
- assertBypassIdentifier(identifier);
592
- return await runWithScopedTenantTransaction((transaction) => applyIdentifierToTransaction(transaction, identifier), callback);
593
- }
594
- var init_databaseTenantContext = __esm(() => {
595
- init_connectionContext();
596
- init_transaction();
597
- init_transactionControl();
598
- init_deferredModelEvents();
599
- init_tenancyConfig();
600
- });
601
-
602
- // ../../src/core/tenant/tenantDatabaseScope.ts
603
- import { getDefaultDatabasePool as getDefaultDatabasePool3 } from "@getstrata/core/database/defaultConnection";
604
- import { AsyncLocalStorage as AsyncLocalStorage3 } from "async_hooks";
605
- async function applyTenantContextToTransaction(transaction, tenantId) {
606
- await transaction.unsafe(`SELECT set_config('app.tenant_id', $1, true)`, [String(tenantId)]);
607
- await transaction.unsafe(`SELECT set_config('app.bypass_rls', $1, true)`, ["false"]);
608
- }
609
- async function runWithTenantDatabase(tenant, callback) {
610
- const outerTenant = currentTenant();
611
- if (hasActiveDatabaseConnection() && outerTenant && outerTenant.id !== tenant.id) {
612
- throw new Error("Cannot switch tenants inside an active database transaction.");
613
- }
614
- if (!isTenancyEnabled() || !isRlsTenancy()) {
615
- return await runWithTenant(tenant, callback);
616
- }
617
- if (hasActiveDatabaseConnection()) {
618
- const activeConnection = getActiveDatabaseConnection(getDefaultDatabasePool3());
619
- return await runWithScopedTenantTransaction(() => applyTenantContextToTransaction(activeConnection, tenant.id), () => runWithTenant(tenant, callback));
620
- }
621
- const pool = getDefaultDatabasePool3();
622
- const begin = pool.begin;
623
- if (typeof begin !== "function") {
624
- throw new Error("RLS tenant scope requires a pool that supports begin(). Session-scoped set_config is not used on pooled connections.");
625
- }
626
- return await settleTransaction(() => runWithDeferredModelEvents(async () => {
627
- return await begin.bind(pool)(AsyncLocalStorage3.bind(async (transaction) => {
628
- await applyTenantContextToTransaction(transaction, tenant.id);
629
- return await runWithDatabaseConnection(transaction, async () => {
630
- return await runWithTransactionScope(async () => commitOrRollbackScope(await runWithTenant(tenant, callback)));
631
- });
632
- }));
633
- }));
634
- }
635
- function isInsideTenantDatabaseScope(tenantId = currentTenant()?.id) {
636
- return hasActiveDatabaseConnection() && currentTenant()?.id === tenantId;
637
- }
638
- var init_tenantDatabaseScope = __esm(() => {
639
- init_connectionContext();
640
- init_transactionControl();
641
- init_deferredModelEvents();
642
- init_databaseTenantContext();
643
- init_tenancyConfig();
644
- init_tenantContext();
645
- });
646
-
647
- // ../../src/core/events/deferredModelEvents.ts
648
- function sameTenant(left, right) {
649
- if (left === null && right === null) {
650
- return true;
651
- }
652
- if (left === null || right === null) {
653
- return false;
654
- }
655
- return left.id === right.id;
656
- }
657
- function groupQueuedModelEvents(queued) {
658
- const groups = [];
659
- for (const item of queued) {
660
- const lastGroup = groups[groups.length - 1];
661
- const last = lastGroup?.[lastGroup.length - 1];
662
- if (!lastGroup || !last || !sameTenant(last.tenant, item.tenant) || "callback" in last !== "callback" in item) {
663
- groups.push([item]);
664
- } else {
665
- lastGroup.push(item);
666
- }
667
- }
668
- return groups;
669
- }
670
- async function dispatchEventBatch(batch) {
671
- for (const item of batch) {
672
- if ("callback" in item)
673
- await item.callback();
674
- else
675
- await eventBus.dispatch(item.event, item.payload);
676
- }
677
- }
678
- async function dispatchQueuedModelEvents(queued) {
679
- if (queued.length === 0) {
680
- return;
681
- }
682
- await Promise.resolve().then(() => init_tenantDatabaseScope());
683
- for (const batch of groupQueuedModelEvents(queued)) {
684
- const tenant = batch[0]?.tenant ?? null;
685
- if (tenant) {
686
- if (batch.some((item) => ("callback" in item))) {
687
- await runWithTenant(tenant, () => dispatchEventBatch(batch));
688
- } else {
689
- await runWithTenantDatabase(tenant, () => dispatchEventBatch(batch));
690
- }
691
- } else {
692
- await dispatchEventBatch(batch);
693
- }
694
- }
695
- }
696
- async function dispatchModelEvent(event, payload) {
697
- await eventBus.dispatchTransactional(event, payload);
698
- const state = deferredModelEvents.getStore();
699
- const frame = state?.frames[state.frames.length - 1];
700
- if (frame) {
701
- frame.push({
702
- event,
703
- payload,
704
- tenant: currentTenant()
705
- });
706
- return;
707
- }
708
- await eventBus.dispatch(event, payload);
709
- }
710
- async function runWithDeferredModelEvents(callback) {
711
- const existing = deferredModelEvents.getStore();
712
- if (existing) {
713
- existing.frames.push([]);
714
- existing.rollbackFrames.push([]);
715
- try {
716
- const result = await callback();
717
- const nested = existing.frames.pop() ?? [];
718
- const rollback = existing.rollbackFrames.pop() ?? [];
719
- existing.rollbackFrames.at(-1)?.push(...rollback);
720
- const parent = existing.frames[existing.frames.length - 1];
721
- if (parent) {
722
- parent.push(...nested);
37
+ if (array) {
38
+ const length = descriptors.length.value;
39
+ if (Object.keys(descriptors).length !== length + 1) {
40
+ throw new TypeError("Application config arrays must be dense and have no extra properties.");
723
41
  }
724
- return result;
725
- } catch (error) {
726
- existing.frames.pop();
727
- for (const rollback of (existing.rollbackFrames.pop() ?? []).reverse())
728
- rollback();
729
- throw error;
730
- }
731
- }
732
- const state = { frames: [[]], rollbackFrames: [[]] };
733
- let result;
734
- try {
735
- result = await deferredModelEvents.run(state, callback);
736
- } catch (error) {
737
- for (const rollback of (state.rollbackFrames[0] ?? []).reverse())
738
- rollback();
739
- throw error;
740
- }
741
- await dispatchQueuedModelEvents(state.frames[0] ?? []);
742
- return result;
743
- }
744
- var deferredModelEvents;
745
- var init_deferredModelEvents = __esm(() => {
746
- init_asyncContextStore();
747
- init_tenantContext();
748
- init_eventBus();
749
- deferredModelEvents = createAsyncContextStore("@getstrata/deferredModelEvents");
750
- });
751
-
752
- // ../../src/core/database/acquisitionMetrics.ts
753
- async function observeTransactionAcquisition(metrics, acquire, signal) {
754
- const state = states.get(metrics);
755
- if (!state)
756
- throw new TypeError("Transaction acquisition metrics must be created by the framework factory.");
757
- const started = performance.now();
758
- state.inflight++;
759
- let outcome = "acquired";
760
- try {
761
- return await acquire();
762
- } catch (error) {
763
- outcome = signal?.aborted ? "aborted" : "failed";
764
- throw error;
765
- } finally {
766
- state.inflight--;
767
- const elapsed = Math.max(0, performance.now() - started) / 1000;
768
- const histogram = state.outcomes[outcomes.indexOf(outcome)];
769
- if (histogram) {
770
- histogram.count++;
771
- histogram.sumSeconds += elapsed;
772
- for (let index = 0;index < ACQUISITION_BUCKETS_SECONDS.length; index++) {
773
- const bound = ACQUISITION_BUCKETS_SECONDS[index];
774
- if (bound !== undefined && elapsed <= bound)
775
- histogram.buckets[index] = (histogram.buckets[index] ?? 0) + 1;
42
+ const result = [];
43
+ for (let index = 0;index < length; index++) {
44
+ const descriptor = descriptors[String(index)];
45
+ if (!descriptor)
46
+ throw new TypeError("Application config arrays must be dense.");
47
+ result.push(copyApplicationConfig(descriptor.value, ancestors));
776
48
  }
49
+ return Object.freeze(result);
777
50
  }
778
- }
779
- }
780
- var ACQUISITION_BUCKETS_SECONDS, outcomes, metricsBrand, states;
781
- var init_acquisitionMetrics = __esm(() => {
782
- ACQUISITION_BUCKETS_SECONDS = [
783
- 0.001,
784
- 0.005,
785
- 0.01,
786
- 0.025,
787
- 0.05,
788
- 0.1,
789
- 0.25,
790
- 0.5,
791
- 1,
792
- 2.5,
793
- 5,
794
- 10
795
- ];
796
- outcomes = ["acquired", "failed", "aborted"];
797
- metricsBrand = Symbol("transaction acquisition metrics");
798
- states = new WeakMap;
799
- });
800
-
801
- // ../../src/core/database/connection.ts
802
- function createDatabaseConnection(source) {
803
- return {
804
- async unsafe(query, params = []) {
805
- return await source.unsafe(query, params);
51
+ const result = {};
52
+ for (const [key, descriptor] of Object.entries(descriptors)) {
53
+ Object.defineProperty(result, key, {
54
+ value: copyApplicationConfig(descriptor.value, ancestors),
55
+ enumerable: true
56
+ });
806
57
  }
807
- };
808
- }
809
- var init_connection = () => {};
810
-
811
- // ../../src/core/database/transaction.ts
812
- import { AsyncLocalStorage as AsyncLocalStorage4 } from "async_hooks";
813
- function scopedConnection(source) {
814
- const connection = createDatabaseConnection(source);
815
- transactionAliases.set(connection, source);
816
- return connection;
817
- }
818
- function isActiveTransactionConnection(connection) {
819
- if (!hasActiveTransaction() || !hasActiveDatabaseConnection())
820
- return false;
821
- const active = getActiveDatabaseConnection(connection);
822
- return connection === active || transactionAliases.get(connection) === active;
823
- }
824
- async function runInSavepoint(connection, operation) {
825
- const inherited = transactionFrame.getStore();
826
- let parent = inherited?.connection === connection ? inherited : outerFrames.get(connection);
827
- if (!parent) {
828
- parent = { connection, childActive: false };
829
- outerFrames.set(connection, parent);
830
- }
831
- if (parent.childActive) {
832
- throw new Error("Concurrent nested transactions on one connection are not supported. Await each transaction before starting the next.");
833
- }
834
- parent.childActive = true;
835
- const name = `strata_${crypto.randomUUID().replaceAll("-", "")}`;
836
- try {
837
- return await settleTransaction(() => runWithDeferredModelEvents(async () => {
838
- await connection.unsafe(`SAVEPOINT ${name}`);
839
- try {
840
- const result = await transactionFrame.run({ connection, childActive: false }, () => runWithTransactionScope(async () => commitOrRollbackScope(await operation(scopedConnection(connection)))));
841
- await connection.unsafe(`RELEASE SAVEPOINT ${name}`);
842
- return result;
843
- } catch (error) {
844
- await connection.unsafe(`ROLLBACK TO SAVEPOINT ${name}`);
845
- await connection.unsafe(`RELEASE SAVEPOINT ${name}`);
846
- throw error;
847
- }
848
- }));
58
+ return Object.freeze(result);
849
59
  } finally {
850
- parent.childActive = false;
60
+ ancestors.delete(value);
851
61
  }
852
62
  }
853
- function supportsTransactions(connection) {
854
- return typeof connection.begin === "function";
855
- }
856
- async function runInTransaction(operation, options = {}) {
857
- const signal = options.acquisitionSignal;
858
- signal?.throwIfAborted();
859
- const admittedOperation = (connection) => {
860
- signal?.throwIfAborted();
861
- return operation(connection);
862
- };
863
- const pool = options.connection ?? resolveRepositoryConnection();
864
- if (hasActiveDatabaseConnection()) {
865
- const active = getActiveDatabaseConnection(pool);
866
- if (options.connection && options.connection !== active && options.connection !== transactionRoots.get(active) && transactionAliases.get(options.connection) !== active)
867
- throw new Error("Cannot switch database connections within an active transaction.");
868
- return await runInSavepoint(active, admittedOperation);
869
- }
870
- if (!supportsTransactions(pool)) {
871
- throw new Error("Active database connection does not support transactions. Bind a client with begin() via bindDatabaseConnection.");
872
- }
873
- return await settleTransaction(() => runWithDeferredModelEvents(async () => {
874
- let reserved;
875
- if (signal || options.acquisitionMetrics) {
876
- if (!pool.reserve)
877
- throw new Error(signal ? "Active database connection does not support cancellable transaction acquisition." : "Active database connection does not support observable transaction acquisition.");
878
- const reserve = pool.reserve.bind(pool);
879
- reserved = options.acquisitionMetrics ? await observeTransactionAcquisition(options.acquisitionMetrics, () => reserve({ signal }), signal) : await reserve({ signal });
880
- }
881
- const connection = reserved ?? pool;
882
- try {
883
- signal?.throwIfAborted();
884
- if (!supportsTransactions(connection))
885
- throw new Error("Reserved database connection does not support transactions.");
886
- return await connection.begin(AsyncLocalStorage4.bind(async (transaction) => {
887
- transactionRoots.set(transaction, pool);
888
- return await runWithDatabaseConnection(transaction, () => transactionFrame.run({ connection: transaction, childActive: false }, () => runWithTransactionScope(async () => commitOrRollbackScope(await admittedOperation(scopedConnection(transaction))))));
889
- }));
890
- } finally {
891
- await reserved?.release();
892
- }
893
- }));
894
- }
895
- var transactionFrame, outerFrames, transactionRoots, transactionAliases;
896
- var init_transaction = __esm(() => {
897
- init_deferredModelEvents();
898
- init_asyncContextStore();
899
- init_acquisitionMetrics();
900
- init_connection();
901
- init_connectionContext();
902
- init_repositoryConnection();
903
- init_transactionControl();
904
- init_acquisitionMetrics();
905
- transactionFrame = createAsyncContextStore("@getstrata/transactionFrame");
906
- outerFrames = new WeakMap;
907
- transactionRoots = new WeakMap;
908
- transactionAliases = new WeakMap;
909
- });
910
-
911
- // ../../src/core/queue/queueMetrics.ts
912
- var {RedisClient: RedisClient3 } = globalThis.Bun;
913
-
914
- // ../../src/core/queue/failedJobRepository.ts
915
- import { BaseRepository } from "@getstrata/core/database/baseRepository";
916
- import { sqlTimestamp } from "@getstrata/core/database/dialect";
917
-
918
- // ../../src/core/queue/failedJobTable.ts
919
- import { defineTable } from "@getstrata/core/database/table";
920
- var failedJobTable = defineTable({
921
- name: "failed_job",
922
- primaryKey: "id",
923
- columns: ["id", "job_name", "job_id", "payload", "exception", "failed_at"],
924
- defaultOrderBy: { column: "failed_at", direction: "DESC" }
925
- });
926
-
927
- // ../../src/core/queue/failedJobRepository.ts
928
- class FailedJobRepository extends BaseRepository {
929
- constructor() {
930
- super(failedJobTable);
931
- }
932
- async create(values) {
933
- const stored = {
934
- ...values,
935
- ...values.payload === undefined ? {} : { payload: JSON.stringify(values.payload) },
936
- ...values.failed_at === undefined ? {} : { failed_at: sqlTimestamp(values.failed_at) }
937
- };
938
- return hydrateFailure(await super.create(stored));
939
- }
940
- async findAll(options = {}) {
941
- const rows = await super.findAll(options);
942
- return options.select?.length ? rows : rows.map(hydrateFailure);
943
- }
944
- }
945
- function hydrateFailure(row) {
946
- const result = { ...row };
947
- if (typeof row.payload === "string")
948
- result.payload = JSON.parse(row.payload);
949
- if ("failed_at" in row && !(row.failed_at instanceof Date)) {
950
- result.failed_at = new Date(row.failed_at);
951
- }
952
- return result;
953
- }
954
- var failedJobRepository_default = FailedJobRepository;
955
-
956
- // ../../src/core/queue/failedJobService.ts
957
- class FailedJobService {
958
- repository;
959
- constructor(repository) {
960
- this.repository = repository;
961
- }
962
- async recordFailure(input) {
963
- return await this.repository.create({
964
- job_name: input.jobName,
965
- job_id: input.jobId ?? null,
966
- payload: input.payload,
967
- exception: input.exception,
968
- failed_at: new Date
969
- });
970
- }
971
- listRecent(limit = 50) {
972
- return this.repository.findAll({
973
- limit,
974
- orderBy: { column: "failed_at", direction: "DESC" }
975
- });
976
- }
977
- async retry(id, enqueue) {
978
- if (typeof enqueue !== "function")
979
- throw new Error("Failed-job retry requires an admission callback.");
980
- const failedJob = await this.repository.findByIdOrThrow(id, (jobId) => new Error(`Failed job ${jobId} not found.`));
981
- await enqueue(failedJob);
982
- await this.repository.deleteById(id);
983
- return failedJob;
984
- }
985
- async delete(id) {
986
- const deleted = await this.repository.deleteById(id);
987
- if (!deleted) {
988
- throw new Error(`Failed job ${id} not found.`);
989
- }
990
- }
991
- async flush() {
992
- const jobs = await this.repository.findAll();
993
- let deleted = 0;
994
- for (const job of jobs) {
995
- if (await this.repository.deleteById(job.id)) {
996
- deleted += 1;
997
- }
998
- }
999
- return deleted;
1000
- }
1001
- }
1002
- var failedJobService_default = FailedJobService;
1003
-
1004
- // ../../src/core/queue/jobRegistry.ts
1005
- class JobRegistry {
1006
- factories = new Map;
1007
- instances = new WeakMap;
1008
- register(name, factory) {
1009
- this.factories.set(name, factory);
1010
- }
1011
- resolveName(job) {
1012
- return this.instances.get(job);
1013
- }
1014
- track(name, job) {
1015
- this.instances.set(job, name);
1016
- return job;
1017
- }
1018
- create(name) {
1019
- const factory = this.factories.get(name);
1020
- if (!factory) {
1021
- return;
1022
- }
1023
- return factory();
1024
- }
1025
- names() {
1026
- return [...this.factories.keys()];
1027
- }
1028
- }
1029
- var JOB_REGISTRY_KEY = Symbol.for("@getstrata/jobRegistry");
1030
- function readSharedJobRegistry() {
1031
- const globalRegistry = globalThis[JOB_REGISTRY_KEY];
1032
- if (globalRegistry) {
1033
- return globalRegistry;
1034
- }
1035
- const registry = new JobRegistry;
1036
- globalThis[JOB_REGISTRY_KEY] = registry;
1037
- return registry;
1038
- }
1039
- var jobRegistry = readSharedJobRegistry();
1040
-
1041
- // ../../src/core/queue/jobRunner.ts
1042
- import { setTimeout as delay } from "timers/promises";
1043
63
 
1044
64
  // ../../src/core/queue/queueConfig.ts
1045
- function resolveQueueConfig() {
1046
- const driver = process.env.QUEUE_DRIVER;
1047
- const maxAttempts = Number(process.env.QUEUE_MAX_ATTEMPTS ?? "3");
1048
- const backoffMs = Number(process.env.QUEUE_BACKOFF_MS ?? "1000");
1049
- return {
1050
- driver: driver === "redis" || driver === "async" || driver === "sync" ? driver : "sync",
1051
- maxAttempts: Number.isFinite(maxAttempts) && maxAttempts > 0 ? maxAttempts : 3,
1052
- backoffMs: Number.isFinite(backoffMs) && backoffMs >= 0 ? backoffMs : 1000
1053
- };
65
+ var DEFAULT_QUEUE_VISIBILITY_MS = 60000;
66
+ function snapshotQueueRetryPolicy(policy) {
67
+ const { maxAttempts, backoffMs } = policy;
68
+ if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1)
69
+ throw new TypeError("QUEUE_MAX_ATTEMPTS must be a positive safe integer.");
70
+ if (!Number.isSafeInteger(backoffMs) || backoffMs < 0 || backoffMs > 2147483647)
71
+ throw new TypeError("QUEUE_BACKOFF_MS must be a non-negative integer within the timer range.");
72
+ return Object.freeze({ maxAttempts, backoffMs });
73
+ }
74
+ function resolveQueueConfig(env = process.env) {
75
+ const driver = env.QUEUE_DRIVER ?? "sync";
76
+ if (!["sync", "async", "redis"].includes(driver))
77
+ throw new TypeError("Unknown queue driver.");
78
+ const policy = snapshotQueueRetryPolicy({
79
+ maxAttempts: Number(env.QUEUE_MAX_ATTEMPTS ?? "3"),
80
+ backoffMs: Number(env.QUEUE_BACKOFF_MS ?? "1000")
81
+ });
82
+ return Object.freeze({ driver, ...policy });
1054
83
  }
1055
- var queueConfig = {
1056
- get driver() {
1057
- return resolveQueueConfig().driver;
1058
- },
1059
- get maxAttempts() {
1060
- return resolveQueueConfig().maxAttempts;
1061
- },
1062
- get backoffMs() {
1063
- return resolveQueueConfig().backoffMs;
1064
- }
1065
- };
1066
- function resolveRedisQueueTransport() {
1067
- const transport = process.env.QUEUE_REDIS_TRANSPORT ?? "lists";
84
+ function resolveRedisQueueTransport(env = process.env) {
85
+ const transport = env.QUEUE_REDIS_TRANSPORT ?? "lists";
1068
86
  if (transport !== "lists" && transport !== "streams")
1069
87
  throw new Error('QUEUE_REDIS_TRANSPORT must be "lists" or "streams".');
1070
88
  return transport;
1071
89
  }
1072
- function readQueueVisibilityMs() {
1073
- const value = Number(process.env.QUEUE_VISIBILITY_MS ?? "60000");
1074
- if (!Number.isSafeInteger(value) || value < 30)
1075
- throw new Error("QUEUE_VISIBILITY_MS must be an integer of at least 30 milliseconds.");
90
+ function readQueueVisibilityMs(env = process.env) {
91
+ const value = Number(env.QUEUE_VISIBILITY_MS ?? "60000");
92
+ if (!Number.isSafeInteger(value) || value < 30 || value > 2147483647)
93
+ throw new Error("QUEUE_VISIBILITY_MS must be an integer from 30 through 2147483647 milliseconds.");
1076
94
  return value;
1077
95
  }
96
+ function snapshotQueueRuntimeConfig(config) {
97
+ if (config.transport !== "lists" && config.transport !== "streams" || typeof config.visibilityMs !== "number")
98
+ throw new TypeError("Queue runtime requires explicit transport and numeric visibility.");
99
+ if (!Array.isArray(config.queueKeys) || config.queueKeys.length !== 3 || config.queueKeys.some((key) => typeof key !== "string" || !key.length) || new Set(config.queueKeys).size !== 3)
100
+ throw new TypeError("Queue runtime requires three distinct nonempty priority keys.");
101
+ return Object.freeze({
102
+ queueKeys: Object.freeze([...config.queueKeys]),
103
+ transport: resolveRedisQueueTransport({ QUEUE_REDIS_TRANSPORT: config.transport }),
104
+ visibilityMs: readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(config.visibilityMs) })
105
+ });
106
+ }
107
+ function resolveQueueRuntimeConfig(env = process.env) {
108
+ const prefix = env.APP_KEY_PREFIX?.trim() || "strata";
109
+ return snapshotQueueRuntimeConfig({
110
+ transport: resolveRedisQueueTransport(env),
111
+ visibilityMs: readQueueVisibilityMs(env),
112
+ queueKeys: [`${prefix}:queue:high`, `${prefix}:queue:default`, `${prefix}:queue:low`]
113
+ });
114
+ }
1078
115
 
1079
116
  // ../../src/core/queue/jobRunner.ts
1080
- function parseQueueJobEnvelope(rawPayload) {
117
+ function snapshotQueueJobEnvelope(input) {
118
+ if (!input || typeof input !== "object" || Array.isArray(input) || ![Object.prototype, null].includes(Object.getPrototypeOf(input)))
119
+ throw new TypeError("Queue envelope must be plain data.");
120
+ const data = {};
121
+ for (const key of Reflect.ownKeys(input)) {
122
+ if (typeof key !== "string" || !["name", "payload", "attempts", "jobId", "deadlineAtMs", "cancellable", "handoff"].includes(key))
123
+ throw new TypeError("Unknown queue envelope field.");
124
+ const descriptor = Object.getOwnPropertyDescriptor(input, key);
125
+ if (!descriptor || !("value" in descriptor) || !descriptor.enumerable)
126
+ throw new TypeError("Queue envelope must contain own data fields.");
127
+ if (descriptor.value !== undefined)
128
+ Object.defineProperty(data, key, { value: descriptor.value, enumerable: true });
129
+ }
130
+ const captured = copyApplicationConfig(data);
131
+ if (typeof captured.name !== "string" || !captured.name || !captured.payload || typeof captured.payload !== "object" || Array.isArray(captured.payload))
132
+ throw new TypeError("Queue envelope requires a job name and payload record.");
133
+ return captured;
134
+ }
135
+ function parseQueueJobEnvelope(rawPayload, registry) {
1081
136
  let parsed;
1082
137
  try {
1083
138
  parsed = JSON.parse(rawPayload);
@@ -1090,15 +145,27 @@ function parseQueueJobEnvelope(rawPayload) {
1090
145
  return null;
1091
146
  }
1092
147
  const envelope = parsed;
148
+ if (Object.keys(parsed).some((key) => ![
149
+ "name",
150
+ "payload",
151
+ "attempts",
152
+ "jobId",
153
+ "deadlineAtMs",
154
+ "cancellable",
155
+ "handoff"
156
+ ].includes(key)))
157
+ return null;
158
+ if (envelope.handoff !== undefined && (typeof envelope.handoff !== "string" || !envelope.handoff.length || envelope.handoff.length > 32768))
159
+ return null;
1093
160
  if (typeof envelope.name !== "string" || envelope.name.length === 0) {
1094
161
  console.error("[QueueWorker] Ignoring queue payload without job name");
1095
162
  return null;
1096
163
  }
1097
- if (!jobRegistry.create(envelope.name)) {
164
+ if (!registry.has(envelope.name)) {
1098
165
  console.error(`[QueueWorker] Ignoring unknown job name: ${envelope.name}`);
1099
166
  return null;
1100
167
  }
1101
- if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null)) {
168
+ if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null || Array.isArray(envelope.payload))) {
1102
169
  console.error("[QueueWorker] Ignoring queue payload with invalid payload object");
1103
170
  return null;
1104
171
  }
@@ -1113,16 +180,18 @@ function parseQueueJobEnvelope(rawPayload) {
1113
180
  return {
1114
181
  ...envelope.cancellable ? { cancellable: true } : {},
1115
182
  ...envelope.deadlineAtMs === undefined ? {} : { deadlineAtMs: envelope.deadlineAtMs },
183
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1116
184
  jobId: envelope.jobId,
1117
185
  name: envelope.name,
1118
186
  payload: envelope.payload ?? {},
1119
187
  attempts: typeof envelope.attempts === "number" ? envelope.attempts : 0
1120
188
  };
1121
189
  }
1122
- async function runQueueJob(envelope, failedJobs, options = {}) {
190
+ async function runQueueJob(envelope, failedJobs, registry, options = {}) {
191
+ envelope = snapshotQueueJobEnvelope(envelope);
192
+ const retryPolicy = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
1123
193
  options.context?.signal.throwIfAborted();
1124
- const job = jobRegistry.create(envelope.name);
1125
- if (!job) {
194
+ if (!registry.has(envelope.name)) {
1126
195
  throw new Error(`Unknown job "${envelope.name}".`);
1127
196
  }
1128
197
  const attempts = envelope.attempts ?? 0;
@@ -1130,14 +199,23 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
1130
199
  jobId: envelope.jobId ?? crypto.randomUUID(),
1131
200
  signal: new AbortController().signal
1132
201
  };
202
+ let job;
1133
203
  try {
1134
- await job.handle(envelope.payload, { ...context, attempt: attempts + 1 });
204
+ await failedJobs.runAttempt(envelope, { ...context, attempt: attempts + 1 }, (scoped) => {
205
+ scoped.signal.throwIfAborted();
206
+ job = registry.create(envelope.name);
207
+ if (!job)
208
+ throw new Error(`Unknown job "${envelope.name}".`);
209
+ return job.handle(envelope.payload, scoped);
210
+ });
1135
211
  } catch (error) {
1136
212
  options.context?.signal.throwIfAborted();
213
+ if (!job)
214
+ throw error;
1137
215
  const nextAttempt = attempts + 1;
1138
- const maxAttempts = job.maxAttempts ?? queueConfig.maxAttempts;
216
+ const maxAttempts = job.maxAttempts ?? retryPolicy.maxAttempts;
1139
217
  if (nextAttempt < maxAttempts) {
1140
- const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
218
+ const backoffMs = job.backoffMs ?? retryPolicy.backoffMs;
1141
219
  if (options.deferRetry) {
1142
220
  await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
1143
221
  return;
@@ -1146,11 +224,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
1146
224
  await runQueueJob({
1147
225
  ...envelope,
1148
226
  attempts: nextAttempt
1149
- }, failedJobs, { ...options, context });
227
+ }, failedJobs, registry, { ...options, context, retryPolicy });
1150
228
  return;
1151
229
  }
1152
230
  await failedJobs.recordFailure({
1153
231
  jobName: envelope.name,
232
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1154
233
  jobId: context.jobId,
1155
234
  payload: envelope.payload,
1156
235
  exception: error instanceof Error ? error.stack ?? error.message : String(error)
@@ -1160,9 +239,6 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
1160
239
  }
1161
240
  }
1162
241
 
1163
- // ../../src/core/queue/redisQueue.ts
1164
- var {RedisClient: RedisClient2 } = globalThis.Bun;
1165
-
1166
242
  // ../../src/core/queue/redisListScripts.ts
1167
243
  var SCRIPT_HEADER = `
1168
244
  local function checkType(key, expected)
@@ -1321,14 +397,14 @@ function envFlagEnabled(value) {
1321
397
  }
1322
398
 
1323
399
  // ../../src/core/runtime/appKeyPrefix.ts
1324
- function appKeyPrefix() {
1325
- return process.env.APP_KEY_PREFIX?.trim() || "strata";
400
+ function appKeyPrefix(env = process.env) {
401
+ return env.APP_KEY_PREFIX?.trim() || "strata";
1326
402
  }
1327
- function appCookieName(kind) {
1328
- return `${appKeyPrefix()}_${kind}`;
403
+ function appCookieName(kind, env = process.env) {
404
+ return `${appKeyPrefix(env)}_${kind}`;
1329
405
  }
1330
- function appDevSecret(kind) {
1331
- return `${appKeyPrefix()}-dev-${kind}`;
406
+ function appDevSecret(kind, env = process.env) {
407
+ return `${appKeyPrefix(env)}-dev-${kind}`;
1332
408
  }
1333
409
  function requireConfiguredSecret(names, devKind, env = process.env) {
1334
410
  for (const name of names) {
@@ -1340,50 +416,18 @@ function requireConfiguredSecret(names, devKind, env = process.env) {
1340
416
  if (isProductionEnv(env)) {
1341
417
  throw new Error(`${names[0]} must be set outside development. Do not derive secrets from the app name.`);
1342
418
  }
1343
- return appDevSecret(devKind);
419
+ return appDevSecret(devKind, env);
1344
420
  }
1345
421
  function namespacedRedisKey(kind) {
1346
422
  return `${appKeyPrefix()}:${kind}`;
1347
423
  }
1348
- function smtpEhloHost() {
1349
- const raw = process.env.MAIL_EHLO?.trim() || `${appKeyPrefix()}.local`;
424
+ function smtpEhloHost(env = process.env) {
425
+ const raw = env.MAIL_EHLO?.trim() || `${appKeyPrefix(env)}.local`;
1350
426
  const safe = raw.replace(/[^a-zA-Z0-9.-]/g, "");
1351
427
  return safe || "strata.local";
1352
428
  }
1353
- function siemEventType() {
1354
- return process.env.SIEM_EVENT_TYPE?.trim() || `${appKeyPrefix()}.audit`;
1355
- }
1356
- function appUserAgent() {
1357
- return process.env.APP_USER_AGENT?.trim() || appKeyPrefix();
1358
- }
1359
- function otelServiceName() {
1360
- return process.env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix()}-api`;
1361
- }
1362
- function webhookSignatureHeader() {
1363
- return process.env.WEBHOOK_SIGNATURE_HEADER?.trim() || `x-${appKeyPrefix()}-signature`;
1364
- }
1365
- function appDisplayName() {
1366
- return process.env.APP_NAME?.trim() || "Strata";
1367
- }
1368
- function appEnv() {
1369
- return process.env.APP_ENV?.trim() || "local";
1370
- }
1371
- function appUrl() {
1372
- return (process.env.APP_URL?.trim() || "http://localhost:3000").replace(/\/$/, "");
1373
- }
1374
- function apiPrefix() {
1375
- const raw = process.env.API_PREFIX?.trim() || "/api/v1";
1376
- const withSlash = raw.startsWith("/") ? raw : `/${raw}`;
1377
- const trimmed = withSlash.replace(/\/+$/, "");
1378
- return trimmed || "/api/v1";
1379
- }
1380
- function sdkClientClassName() {
1381
- const override = process.env.APP_SDK_CLASS?.trim();
1382
- if (override && /^[A-Za-z_][A-Za-z0-9_]*$/.test(override)) {
1383
- return override;
1384
- }
1385
- const fromName = appDisplayName().replace(/[^A-Za-z0-9]/g, "");
1386
- return fromName ? `${fromName}Client` : "AppClient";
429
+ function otelServiceName(env = process.env) {
430
+ return env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix(env)}-api`;
1387
431
  }
1388
432
 
1389
433
  // ../../src/core/queue/redisQueueKeys.ts
@@ -1396,25 +440,19 @@ function queueHighKey() {
1396
440
  function queueLowKey() {
1397
441
  return namespacedRedisKey("queue:low");
1398
442
  }
1399
- var QUEUE_LIST_KEY = queueListKey();
1400
- var QUEUE_HIGH_KEY = queueHighKey();
1401
- var QUEUE_LOW_KEY = queueLowKey();
443
+ var QUEUE_LIST_KEY = "strata:queue:default";
444
+ var QUEUE_HIGH_KEY = "strata:queue:high";
445
+ var QUEUE_LOW_KEY = "strata:queue:low";
1402
446
  function defaultQueueKeys() {
1403
447
  return [queueHighKey(), queueListKey(), queueLowKey()];
1404
448
  }
1405
- var QUEUE_KEYS = defaultQueueKeys();
1406
- function queueKeyForPriority(priority = "default") {
1407
- switch (priority) {
1408
- case "high":
1409
- return queueHighKey();
1410
- case "low":
1411
- return queueLowKey();
1412
- default:
1413
- return queueListKey();
1414
- }
449
+ var QUEUE_KEYS = Object.freeze([QUEUE_HIGH_KEY, QUEUE_LIST_KEY, QUEUE_LOW_KEY]);
450
+ function queueKeyForPriority(priority = "default", keys = defaultQueueKeys()) {
451
+ return priority === "high" ? keys[0] : priority === "low" ? keys[2] : keys[1];
1415
452
  }
1416
453
 
1417
454
  // ../../src/core/queue/redisStreams.ts
455
+ import { QueueHandoffError } from "@getstrata/core/queue/queueHandoff";
1418
456
  var {RedisClient } = globalThis.Bun;
1419
457
 
1420
458
  // ../../src/core/queue/redisStreamsScripts.ts
@@ -1627,7 +665,7 @@ function watchStreamDeadline(client, deadlineAtMs, remainingMs, expire, fail) {
1627
665
  clearTimeout(timer);
1628
666
  };
1629
667
  }
1630
- async function scheduleStreamRetry(client, key, reservation, envelope, delayMs) {
668
+ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1631
669
  if (!Number.isSafeInteger(delayMs) || delayMs < 0 || delayMs > Number.MAX_SAFE_INTEGER / 2)
1632
670
  throw new Error("Invalid Streams retry delay");
1633
671
  const scheduled = await client.send("EVAL", [
@@ -1639,7 +677,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
1639
677
  STREAM_GROUP,
1640
678
  reservation.id,
1641
679
  reservation.owner,
1642
- String(readQueueVisibilityMs()),
680
+ String(visibilityMs),
1643
681
  String(delayMs),
1644
682
  JSON.stringify(envelope),
1645
683
  envelope.deadlineAtMs === undefined ? "" : String(envelope.deadlineAtMs),
@@ -1648,7 +686,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
1648
686
  if (Number(scheduled) !== 1)
1649
687
  throw new Error("Queue lease lost while scheduling retry");
1650
688
  }
1651
- async function pollStreamPriorities(client, queueKeys, owner, cursors) {
689
+ async function pollStreamPriorities(client, queueKeys, owner, cursors, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1652
690
  const keys = [];
1653
691
  for (const key of queueKeys)
1654
692
  keys.push(streamQueueKey(key), streamRetryKey(key));
@@ -1658,7 +696,7 @@ async function pollStreamPriorities(client, queueKeys, owner, cursors) {
1658
696
  ...keys,
1659
697
  STREAM_GROUP,
1660
698
  owner,
1661
- String(readQueueVisibilityMs()),
699
+ String(visibilityMs),
1662
700
  ...cursors
1663
701
  ]);
1664
702
  if (!Array.isArray(raw))
@@ -1709,7 +747,7 @@ async function ensureStreamGroup(client, key) {
1709
747
  throw error;
1710
748
  }
1711
749
  }
1712
- async function updateStreamReservation(client, key, reservation, action, controlKey) {
750
+ async function updateStreamReservation(client, key, reservation, action, controlKey, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1713
751
  return Number(await client.send("EVAL", [
1714
752
  UPDATE_STREAM,
1715
753
  "3",
@@ -1719,41 +757,46 @@ async function updateStreamReservation(client, key, reservation, action, control
1719
757
  STREAM_GROUP,
1720
758
  reservation.id,
1721
759
  reservation.owner,
1722
- String(readQueueVisibilityMs()),
760
+ String(visibilityMs),
1723
761
  action,
1724
762
  reservation.payload,
1725
763
  controlKey ? "1" : "0"
1726
764
  ])) === 1;
1727
765
  }
1728
766
  class RedisStreamsQueue {
767
+ registry;
768
+ queueKeys;
1729
769
  client;
1730
- constructor(redisUrl) {
770
+ constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
771
+ this.registry = registry;
772
+ this.queueKeys = queueKeys;
773
+ this.queueKeys = Object.freeze([...queueKeys]);
1731
774
  this.client = new RedisClient(redisUrl);
1732
775
  }
1733
776
  close() {
1734
777
  this.client.close();
1735
778
  }
1736
- async dispatch(job, payload) {
1737
- await this.enqueue(job, payload);
779
+ async dispatch(job, payload, options) {
780
+ await this.admit(job, payload, {}, undefined, options?.handoff);
1738
781
  }
1739
782
  async enqueue(job, payload, options = {}) {
1740
783
  return this.admit(job, payload, options);
1741
784
  }
1742
- async replay(job, payload, jobId) {
1743
- const prefix = defaultQueueKeys().find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
785
+ async replay(job, payload, jobId, options) {
786
+ const prefix = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
1744
787
  if (!prefix || !/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9]+-[0-9]+)$/.test(jobId.slice(streamQueueKey(prefix).length + 1)))
1745
788
  throw new Error("Replay identity does not belong to this Streams namespace");
1746
- await this.admit(job, payload, {}, jobId);
789
+ await this.admit(job, payload, {}, jobId, options?.handoff);
1747
790
  }
1748
- async admit(job, payload, options, replayId) {
791
+ async admit(job, payload, options, replayId, handoff) {
1749
792
  if (options.cancellable !== undefined && typeof options.cancellable !== "boolean")
1750
793
  throw new Error("cancellable must be a boolean");
1751
794
  if (options.timeoutMs !== undefined && (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs <= 0 || options.timeoutMs > MAX_TIMER_MS))
1752
795
  throw new Error("Streams timeoutMs must be an integer between 1 and 2147483647");
1753
- const name = jobRegistry.resolveName(job);
796
+ const name = this.registry.resolveName(job);
1754
797
  if (!name)
1755
798
  throw new Error("Job is not registered with the queue worker registry.");
1756
- const key = queueKeyForPriority(job.priority);
799
+ const key = queueKeyForPriority(job.priority, this.queueKeys);
1757
800
  await assertLegacyDrained(this.client, key);
1758
801
  const jobId = replayId ?? `${streamQueueKey(key)}/${crypto.randomUUID()}`;
1759
802
  const deadlineAtMs = options.timeoutMs === undefined ? undefined : await readStreamTime(this.client) + options.timeoutMs;
@@ -1761,6 +804,7 @@ class RedisStreamsQueue {
1761
804
  name,
1762
805
  payload,
1763
806
  attempts: 0,
807
+ ...handoff === undefined ? {} : { handoff },
1764
808
  jobId,
1765
809
  deadlineAtMs,
1766
810
  ...options.cancellable ? { cancellable: true } : {}
@@ -1778,7 +822,7 @@ class RedisStreamsQueue {
1778
822
  return jobId;
1779
823
  }
1780
824
  async cancel(jobId) {
1781
- const key = defaultQueueKeys().find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
825
+ const key = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
1782
826
  if (!key)
1783
827
  throw new Error("Invalid cancellable Streams job ID");
1784
828
  return cancelStreamJob(this.client, key, jobId);
@@ -1788,8 +832,11 @@ class RedisStreamsQueue {
1788
832
  class RedisStreamsWorker {
1789
833
  redisUrl;
1790
834
  failedJobs;
835
+ registry;
1791
836
  timeoutSeconds;
1792
837
  queueKeys;
838
+ visibilityMs;
839
+ retryPolicy;
1793
840
  client;
1794
841
  owner = crypto.randomUUID();
1795
842
  cursors = new Map;
@@ -1798,12 +845,16 @@ class RedisStreamsWorker {
1798
845
  running = false;
1799
846
  stopping = false;
1800
847
  active;
1801
- constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = defaultQueueKeys()) {
848
+ constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
1802
849
  this.redisUrl = redisUrl;
1803
850
  this.failedJobs = failedJobs;
851
+ this.registry = registry;
1804
852
  this.timeoutSeconds = timeoutSeconds;
1805
853
  this.queueKeys = queueKeys;
1806
- readQueueVisibilityMs();
854
+ this.visibilityMs = visibilityMs;
855
+ this.queueKeys = Object.freeze([...queueKeys]);
856
+ this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
857
+ readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
1807
858
  this.client = new RedisClient(redisUrl);
1808
859
  }
1809
860
  requestStop() {
@@ -1842,7 +893,7 @@ class RedisStreamsWorker {
1842
893
  do {
1843
894
  if (this.stopping)
1844
895
  return false;
1845
- const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"));
896
+ const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"), this.visibilityMs);
1846
897
  this.queueKeys.forEach((key, index) => {
1847
898
  const cursor = polled.cursors[index];
1848
899
  if (cursor)
@@ -1862,9 +913,15 @@ class RedisStreamsWorker {
1862
913
  return false;
1863
914
  }
1864
915
  async handle(key, reservation) {
1865
- const envelope = parseQueueJobEnvelope(reservation.payload);
916
+ const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
1866
917
  if (!envelope) {
1867
- await updateStreamReservation(this.client, key, reservation, "quarantine");
918
+ await this.failedJobs.recordFailure({
919
+ jobName: "queue.invalid-envelope",
920
+ jobId: `${streamQueueKey(key)}/${reservation.id}`,
921
+ payload: { raw: reservation.payload },
922
+ exception: "Malformed or unknown queue envelope"
923
+ });
924
+ await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
1868
925
  return;
1869
926
  }
1870
927
  envelope.jobId ??= `${streamQueueKey(key)}/${reservation.id}`;
@@ -1873,7 +930,7 @@ class RedisStreamsWorker {
1873
930
  try {
1874
931
  controlKey = streamControlKey(key, envelope.jobId);
1875
932
  } catch {
1876
- await updateStreamReservation(this.client, key, reservation, "quarantine");
933
+ await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
1877
934
  return;
1878
935
  }
1879
936
  }
@@ -1882,10 +939,11 @@ class RedisStreamsWorker {
1882
939
  await this.failedJobs.recordFailure({
1883
940
  jobName: envelope.name,
1884
941
  jobId: envelope.jobId,
942
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1885
943
  payload: envelope.payload,
1886
944
  exception: cancellationError.message
1887
945
  });
1888
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
946
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
1889
947
  return;
1890
948
  }
1891
949
  const remainingMs = envelope.deadlineAtMs === undefined ? undefined : envelope.deadlineAtMs - await readStreamTime(this.client);
@@ -1894,15 +952,17 @@ class RedisStreamsWorker {
1894
952
  await this.failedJobs.recordFailure({
1895
953
  jobName: envelope.name,
1896
954
  jobId: envelope.jobId,
955
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1897
956
  payload: envelope.payload,
1898
957
  exception: deadlineError.message
1899
958
  });
1900
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
959
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
1901
960
  return;
1902
961
  }
1903
962
  const controller = new AbortController;
1904
963
  this.active = controller;
1905
964
  let acknowledge = false;
965
+ let invalidHandoff = false;
1906
966
  let released = false;
1907
967
  let ownershipLost = false;
1908
968
  let renewal;
@@ -1922,7 +982,7 @@ class RedisStreamsWorker {
1922
982
  return;
1923
983
  renewal = (async () => {
1924
984
  try {
1925
- if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey))
985
+ if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey, this.visibilityMs))
1926
986
  loseOwnership(new Error("Queue lease lost"));
1927
987
  else if (controlKey && await readStreamCancellation(this.client, controlKey)) {
1928
988
  cancellationRequested = true;
@@ -1935,9 +995,10 @@ class RedisStreamsWorker {
1935
995
  })().finally(() => {
1936
996
  renewal = undefined;
1937
997
  });
1938
- }, Math.max(1, Math.floor(readQueueVisibilityMs() / 3)));
998
+ }, Math.max(1, Math.floor(this.visibilityMs / 3)));
1939
999
  try {
1940
- await runQueueJob(envelope, this.failedJobs, {
1000
+ await runQueueJob(envelope, this.failedJobs, this.registry, {
1001
+ retryPolicy: this.retryPolicy,
1941
1002
  context: {
1942
1003
  jobId: envelope.jobId,
1943
1004
  signal: controller.signal,
@@ -1945,7 +1006,7 @@ class RedisStreamsWorker {
1945
1006
  },
1946
1007
  deferRetry: async (next, delayMs) => {
1947
1008
  controller.signal.throwIfAborted();
1948
- await scheduleStreamRetry(this.client, key, reservation, next, delayMs);
1009
+ await scheduleStreamRetry(this.client, key, reservation, next, delayMs, this.visibilityMs);
1949
1010
  released = true;
1950
1011
  },
1951
1012
  onFailureRecorded: () => {
@@ -1955,18 +1016,26 @@ class RedisStreamsWorker {
1955
1016
  });
1956
1017
  acknowledge = true;
1957
1018
  } catch (error) {
1958
- console.error("[StreamsWorker] Job failed:", error);
1019
+ if (error instanceof QueueHandoffError)
1020
+ invalidHandoff = true;
1021
+ else
1022
+ console.error("[StreamsWorker] Job failed:", error);
1959
1023
  } finally {
1960
1024
  stopDeadlineWatch?.();
1961
1025
  clearInterval(heartbeat);
1962
1026
  await renewal;
1963
1027
  this.active = undefined;
1964
1028
  }
1029
+ if (invalidHandoff && !ownershipLost && !released) {
1030
+ await updateStreamReservation(this.client, key, reservation, "quarantine", controlKey, this.visibilityMs);
1031
+ return;
1032
+ }
1965
1033
  if (!ownershipLost && !released && controlKey && (cancellationRequested || await readStreamCancellation(this.client, controlKey))) {
1966
1034
  if (!failureRecorded)
1967
1035
  await this.failedJobs.recordFailure({
1968
1036
  jobName: envelope.name,
1969
1037
  jobId: envelope.jobId,
1038
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1970
1039
  payload: envelope.payload,
1971
1040
  exception: cancellationError.message
1972
1041
  });
@@ -1978,13 +1047,14 @@ class RedisStreamsWorker {
1978
1047
  await this.failedJobs.recordFailure({
1979
1048
  jobName: envelope.name,
1980
1049
  jobId: envelope.jobId,
1050
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1981
1051
  payload: envelope.payload,
1982
1052
  exception: deadlineError.message
1983
1053
  });
1984
1054
  acknowledge = true;
1985
1055
  }
1986
1056
  if (acknowledge && !ownershipLost && !released)
1987
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
1057
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
1988
1058
  }
1989
1059
  async run() {
1990
1060
  this.running = true;
@@ -2016,7 +1086,7 @@ async function migrateLegacyQueueToStreams(client, options) {
2016
1086
  if (!Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 1000)
2017
1087
  throw new Error("Queue conversion batchSize must be between 1 and 1000.");
2018
1088
  let converted = 0;
2019
- for (const key of options.queueKeys ?? defaultQueueKeys())
1089
+ for (const key of [...options.queueKeys ?? QUEUE_KEYS])
2020
1090
  converted += Number(await client.send("EVAL", [
2021
1091
  CONVERT_LIST,
2022
1092
  "4",
@@ -2049,7 +1119,7 @@ function reservationExpired(lease, now, visibilityMs) {
2049
1119
  function queueInvalidKey(queueKey) {
2050
1120
  return `${queueKey}:invalid`;
2051
1121
  }
2052
- async function reserveQueueJob(client, queueKey, owner) {
1122
+ async function reserveQueueJob(client, queueKey, owner, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
2053
1123
  const raw = await client.send("EVAL", [
2054
1124
  RESERVE_JOB,
2055
1125
  "3",
@@ -2058,14 +1128,14 @@ async function reserveQueueJob(client, queueKey, owner) {
2058
1128
  queueProcessingLeaseKey(queueKey),
2059
1129
  crypto.randomUUID(),
2060
1130
  owner,
2061
- String(readQueueVisibilityMs())
1131
+ String(visibilityMs)
2062
1132
  ]);
2063
1133
  if (typeof raw !== "string")
2064
1134
  return null;
2065
1135
  const reservation = JSON.parse(raw);
2066
1136
  return { ...reservation, record: raw };
2067
1137
  }
2068
- async function updateQueueReservation(client, queueKey, reservation, action) {
1138
+ async function updateQueueReservation(client, queueKey, reservation, action, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
2069
1139
  return Number(await client.send("EVAL", [
2070
1140
  RENEW_OR_ACK,
2071
1141
  "3",
@@ -2076,11 +1146,11 @@ async function updateQueueReservation(client, queueKey, reservation, action) {
2076
1146
  reservation.owner,
2077
1147
  reservation.record,
2078
1148
  action,
2079
- String(readQueueVisibilityMs()),
1149
+ String(visibilityMs),
2080
1150
  reservation.payload
2081
1151
  ])) === 1;
2082
1152
  }
2083
- async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim) {
1153
+ async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
2084
1154
  const keys = [];
2085
1155
  for (const key of queueKeys) {
2086
1156
  keys.push(key, queueProcessingKey(key), queueProcessingLeaseKey(key));
@@ -2090,7 +1160,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
2090
1160
  String(keys.length),
2091
1161
  ...keys,
2092
1162
  owner,
2093
- String(readQueueVisibilityMs()),
1163
+ String(visibilityMs),
2094
1164
  reservationId,
2095
1165
  reclaim ? "1" : "0"
2096
1166
  ]);
@@ -2115,7 +1185,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
2115
1185
  const reservation = JSON.parse(record);
2116
1186
  return { queueKey, reservation: { ...reservation, record } };
2117
1187
  }
2118
- async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
1188
+ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
2119
1189
  let reclaimed = 0;
2120
1190
  for (const key of queueKeys) {
2121
1191
  reclaimed += Number(await client.send("EVAL", [
@@ -2124,31 +1194,46 @@ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
2124
1194
  key,
2125
1195
  queueProcessingKey(key),
2126
1196
  queueProcessingLeaseKey(key),
2127
- String(readQueueVisibilityMs())
1197
+ String(visibilityMs)
2128
1198
  ]));
2129
1199
  }
2130
1200
  return reclaimed;
2131
1201
  }
2132
1202
 
2133
1203
  class RedisQueue {
1204
+ registry;
1205
+ queueKeys;
2134
1206
  client;
2135
- constructor(redisUrl) {
1207
+ constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
1208
+ this.registry = registry;
1209
+ this.queueKeys = queueKeys;
1210
+ this.queueKeys = Object.freeze([...queueKeys]);
2136
1211
  this.client = new RedisClient2(redisUrl);
2137
1212
  }
2138
1213
  close() {
2139
1214
  this.client.close();
2140
1215
  }
2141
- async dispatch(job, payload) {
2142
- const name = jobRegistry.resolveName(job);
1216
+ async dispatch(job, payload, options) {
1217
+ await this.admit(job, payload, options);
1218
+ }
1219
+ async replay(job, payload, jobId, options) {
1220
+ if (typeof jobId !== "string" || !jobId.length)
1221
+ throw new TypeError("Replay identity is required.");
1222
+ await this.admit(job, payload, options, jobId);
1223
+ }
1224
+ async admit(job, payload, options, jobId) {
1225
+ const name = this.registry.resolveName(job);
2143
1226
  if (!name) {
2144
1227
  throw new Error("Job is not registered with the queue worker registry.");
2145
1228
  }
2146
1229
  const envelope = {
2147
1230
  name,
1231
+ jobId: jobId ?? crypto.randomUUID(),
2148
1232
  payload,
2149
- attempts: 0
1233
+ attempts: 0,
1234
+ ...options?.handoff === undefined ? {} : { handoff: options.handoff }
2150
1235
  };
2151
- const queueKey = queueKeyForPriority(job.priority);
1236
+ const queueKey = queueKeyForPriority(job.priority, this.queueKeys);
2152
1237
  await this.client.lpush(queueKey, JSON.stringify(envelope));
2153
1238
  }
2154
1239
  }
@@ -2156,17 +1241,25 @@ class RedisQueue {
2156
1241
  class QueueWorker {
2157
1242
  redisUrl;
2158
1243
  failedJobs;
1244
+ registry;
2159
1245
  timeoutSeconds;
2160
1246
  queueKeys;
1247
+ visibilityMs;
1248
+ retryPolicy;
2161
1249
  owner = crypto.randomUUID();
2162
1250
  running = false;
2163
1251
  stopping = false;
2164
1252
  client;
2165
- constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = QUEUE_KEYS) {
1253
+ constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
2166
1254
  this.redisUrl = redisUrl;
2167
1255
  this.failedJobs = failedJobs;
1256
+ this.registry = registry;
2168
1257
  this.timeoutSeconds = timeoutSeconds;
2169
1258
  this.queueKeys = queueKeys;
1259
+ this.visibilityMs = visibilityMs;
1260
+ this.queueKeys = Object.freeze([...queueKeys]);
1261
+ this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
1262
+ readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
2170
1263
  this.client = new RedisClient2(redisUrl);
2171
1264
  }
2172
1265
  requestStop() {
@@ -2181,7 +1274,7 @@ class QueueWorker {
2181
1274
  do {
2182
1275
  if (this.stopping)
2183
1276
  return null;
2184
- const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim);
1277
+ const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim, this.visibilityMs);
2185
1278
  reclaim = false;
2186
1279
  if (reserved)
2187
1280
  return reserved;
@@ -2198,12 +1291,13 @@ class QueueWorker {
2198
1291
  if (!reserved)
2199
1292
  return false;
2200
1293
  const { queueKey, reservation } = reserved;
2201
- const envelope = parseQueueJobEnvelope(reservation.payload);
1294
+ const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
2202
1295
  if (!envelope) {
2203
- await updateQueueReservation(this.client, queueKey, reservation, "quarantine");
1296
+ await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
2204
1297
  return true;
2205
1298
  }
2206
1299
  let acknowledge = false;
1300
+ let invalidHandoff = false;
2207
1301
  let ownershipLost = false;
2208
1302
  let renewal;
2209
1303
  const heartbeat = setInterval(() => {
@@ -2211,7 +1305,7 @@ class QueueWorker {
2211
1305
  return;
2212
1306
  renewal = (async () => {
2213
1307
  try {
2214
- if (!await updateQueueReservation(this.client, queueKey, reservation, "renew")) {
1308
+ if (!await updateQueueReservation(this.client, queueKey, reservation, "renew", this.visibilityMs)) {
2215
1309
  ownershipLost = true;
2216
1310
  }
2217
1311
  } catch (error) {
@@ -2221,22 +1315,30 @@ class QueueWorker {
2221
1315
  })().finally(() => {
2222
1316
  renewal = undefined;
2223
1317
  });
2224
- }, Math.max(1, Math.floor(readQueueVisibilityMs() / 3)));
1318
+ }, Math.max(1, Math.floor(this.visibilityMs / 3)));
2225
1319
  try {
2226
- await runQueueJob(envelope, this.failedJobs, {
1320
+ await runQueueJob(envelope, this.failedJobs, this.registry, {
1321
+ retryPolicy: this.retryPolicy,
2227
1322
  onFailureRecorded: () => {
2228
1323
  acknowledge = true;
2229
1324
  }
2230
1325
  });
2231
1326
  acknowledge = true;
2232
1327
  } catch (error) {
2233
- console.error("[QueueWorker] Job failed:", error);
1328
+ if (error instanceof QueueHandoffError2)
1329
+ invalidHandoff = true;
1330
+ else
1331
+ console.error("[QueueWorker] Job failed:", error);
2234
1332
  } finally {
2235
1333
  clearInterval(heartbeat);
2236
1334
  await renewal;
2237
1335
  }
1336
+ if (invalidHandoff && !ownershipLost) {
1337
+ await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
1338
+ return true;
1339
+ }
2238
1340
  if (acknowledge && !ownershipLost) {
2239
- await updateQueueReservation(this.client, queueKey, reservation, "ack");
1341
+ await updateQueueReservation(this.client, queueKey, reservation, "ack", this.visibilityMs);
2240
1342
  }
2241
1343
  return true;
2242
1344
  }
@@ -2265,12 +1367,13 @@ class QueueWorker {
2265
1367
  this.client.close();
2266
1368
  }
2267
1369
  }
2268
- async function countPendingQueueJobs(redisUrl) {
1370
+ async function countPendingQueueJobs(redisUrl, config = resolveQueueRuntimeConfig({})) {
1371
+ const { transport, queueKeys } = snapshotQueueRuntimeConfig(config);
2269
1372
  const client = new RedisClient2(redisUrl);
2270
1373
  try {
2271
1374
  let total = 0;
2272
- for (const queueKey of resolveRedisQueueTransport() === "streams" ? defaultQueueKeys() : QUEUE_KEYS) {
2273
- total += resolveRedisQueueTransport() === "streams" ? await readStreamQueueDepth(client, queueKey) : await client.llen(queueKey);
1375
+ for (const queueKey of queueKeys) {
1376
+ total += transport === "streams" ? await readStreamQueueDepth(client, queueKey) : await client.llen(queueKey);
2274
1377
  }
2275
1378
  return total;
2276
1379
  } finally {
@@ -2278,195 +1381,744 @@ async function countPendingQueueJobs(redisUrl) {
2278
1381
  }
2279
1382
  }
2280
1383
 
2281
- // ../../src/core/queue/index.ts
2282
- class Job {
2283
- static jobName;
2284
- maxAttempts;
2285
- backoffMs;
2286
- priority;
2287
- constructor() {
2288
- const jobName = this.constructor.jobName;
2289
- if (jobName) {
2290
- jobRegistry.track(jobName, this);
1384
+ // ../../src/core/database/query.ts
1385
+ function quoteIdentifier(dialect, identifier) {
1386
+ return dialect.quoteIdentifier(identifier);
1387
+ }
1388
+ function returningSuffix(dialect, columns) {
1389
+ return dialect.returningClause(columns);
1390
+ }
1391
+ function qualifyColumn(dialect, tableName, column) {
1392
+ return `${quoteIdentifier(dialect, tableName)}.${quoteIdentifier(dialect, column)}`;
1393
+ }
1394
+ function resolveQualifiedColumn(dialect, defaultTable, columnName) {
1395
+ if (columnName.includes(".")) {
1396
+ const [table, column] = columnName.split(".", 2);
1397
+ if (!table || !column) {
1398
+ throw new Error(`Invalid qualified column: ${columnName}`);
1399
+ }
1400
+ return qualifyColumn(dialect, table, column);
1401
+ }
1402
+ return qualifyColumn(dialect, defaultTable, columnName);
1403
+ }
1404
+ function parseQualifiedColumn(reference) {
1405
+ const [table, column] = reference.split(".", 2);
1406
+ if (!table || !column) {
1407
+ throw new Error(`Join columns must be qualified as table.column: ${reference}`);
1408
+ }
1409
+ return { table, column };
1410
+ }
1411
+ function normalizeDirection(direction = "ASC") {
1412
+ return direction.toUpperCase() === "DESC" ? "DESC" : "ASC";
1413
+ }
1414
+ function isQueryOperator(value) {
1415
+ return value !== null && !Array.isArray(value) && !(value instanceof Date) && typeof value === "object";
1416
+ }
1417
+ function pushParam(dialect, values, value) {
1418
+ values.push(value);
1419
+ return dialect.placeholder(values.length);
1420
+ }
1421
+ var SUPPORTED_OPERATORS = new Set([
1422
+ "eq",
1423
+ "ne",
1424
+ "in",
1425
+ "notIn",
1426
+ "gt",
1427
+ "gte",
1428
+ "lt",
1429
+ "lte",
1430
+ "isNull",
1431
+ "ilike",
1432
+ "tsMatch"
1433
+ ]);
1434
+ function buildInClause(dialect, column, values, params, negated = false) {
1435
+ if (values.length === 0) {
1436
+ return negated ? "1 = 1" : "1 = 0";
1437
+ }
1438
+ const placeholders = values.map((value) => pushParam(dialect, params, value)).join(", ");
1439
+ return `${column}${negated ? " NOT" : ""} IN (${placeholders})`;
1440
+ }
1441
+ function buildOperatorClauses(dialect, column, operator, params) {
1442
+ const clauses = [];
1443
+ const unsupported = Object.keys(operator).filter((key) => !SUPPORTED_OPERATORS.has(key));
1444
+ if (unsupported.length > 0) {
1445
+ throw new Error(`Unsupported query operator(s) for ${column}: ${unsupported.join(", ")}. Supported: ${[...SUPPORTED_OPERATORS].join(", ")}.`);
1446
+ }
1447
+ if (operator.isNull === true) {
1448
+ clauses.push(`${column} IS NULL`);
1449
+ }
1450
+ if (operator.isNull === false) {
1451
+ clauses.push(`${column} IS NOT NULL`);
1452
+ }
1453
+ if (operator.eq !== undefined) {
1454
+ if (operator.eq === null) {
1455
+ clauses.push(`${column} IS NULL`);
1456
+ } else {
1457
+ clauses.push(`${column} = ${pushParam(dialect, params, operator.eq)}`);
1458
+ }
1459
+ }
1460
+ if (operator.ne !== undefined) {
1461
+ if (operator.ne === null) {
1462
+ clauses.push(`${column} IS NOT NULL`);
1463
+ } else {
1464
+ clauses.push(`${column} <> ${pushParam(dialect, params, operator.ne)}`);
1465
+ }
1466
+ }
1467
+ if (operator.in !== undefined) {
1468
+ clauses.push(buildInClause(dialect, column, operator.in, params));
1469
+ }
1470
+ if (operator.notIn !== undefined) {
1471
+ clauses.push(buildInClause(dialect, column, operator.notIn, params, true));
1472
+ }
1473
+ if (operator.gt !== undefined) {
1474
+ clauses.push(`${column} > ${pushParam(dialect, params, operator.gt)}`);
1475
+ }
1476
+ if (operator.gte !== undefined) {
1477
+ clauses.push(`${column} >= ${pushParam(dialect, params, operator.gte)}`);
1478
+ }
1479
+ if (operator.lt !== undefined) {
1480
+ clauses.push(`${column} < ${pushParam(dialect, params, operator.lt)}`);
1481
+ }
1482
+ if (operator.lte !== undefined) {
1483
+ clauses.push(`${column} <= ${pushParam(dialect, params, operator.lte)}`);
1484
+ }
1485
+ if (operator.ilike !== undefined) {
1486
+ clauses.push(`${column} ${dialect.ilikeOperator()} ${pushParam(dialect, params, operator.ilike)}`);
1487
+ }
1488
+ if (operator.tsMatch !== undefined) {
1489
+ if (dialect.driver !== "pgsql") {
1490
+ throw new Error("Full-text search (tsMatch) is only available on PostgreSQL.");
2291
1491
  }
1492
+ clauses.push(`${column} @@ plainto_tsquery('english', ${pushParam(dialect, params, operator.tsMatch)})`);
2292
1493
  }
1494
+ return clauses;
2293
1495
  }
2294
-
2295
- class SyncQueue {
2296
- async dispatch(job, payload) {
2297
- await job.handle(payload);
1496
+ function appendWhereParts(dialect, tableName, where, params) {
1497
+ const clauses = [];
1498
+ for (const [columnName, filterValue] of Object.entries(where)) {
1499
+ if (filterValue === undefined) {
1500
+ continue;
1501
+ }
1502
+ const column = resolveQualifiedColumn(dialect, tableName, columnName);
1503
+ if (Array.isArray(filterValue)) {
1504
+ clauses.push(buildInClause(dialect, column, filterValue, params));
1505
+ continue;
1506
+ }
1507
+ if (isQueryOperator(filterValue)) {
1508
+ clauses.push(...buildOperatorClauses(dialect, column, filterValue, params));
1509
+ continue;
1510
+ }
1511
+ if (filterValue === null) {
1512
+ clauses.push(`${column} IS NULL`);
1513
+ continue;
1514
+ }
1515
+ clauses.push(`${column} = ${pushParam(dialect, params, filterValue)}`);
2298
1516
  }
1517
+ return clauses.join(" AND ");
2299
1518
  }
2300
-
2301
- class AsyncQueue {
2302
- active = new Set;
2303
- closing;
2304
- closed = false;
2305
- assertOpen() {
2306
- if (this.closed)
2307
- throw new Error("Async queue is closed.");
2308
- }
2309
- enqueue(task, onError) {
2310
- this.assertOpen();
2311
- const running = new Promise((resolve) => {
2312
- setTimeout(() => {
2313
- Promise.resolve().then(task).catch(onError).finally(resolve);
2314
- }, 0);
2315
- });
2316
- this.active.add(running);
2317
- running.then(() => {
2318
- this.active.delete(running);
2319
- });
1519
+ function buildWhereClause(dialect, tableName, where = {}) {
1520
+ const params = [];
1521
+ const body = appendWhereParts(dialect, tableName, where, params);
1522
+ return {
1523
+ clause: body.length > 0 ? ` WHERE ${body}` : "",
1524
+ params
1525
+ };
1526
+ }
1527
+ function remapExistsSql(sql, existsParams, params) {
1528
+ const offset = params.length;
1529
+ params.push(...existsParams);
1530
+ return sql.replace(/\$(\d+)/g, (_match, index) => `$${offset + Number(index)}`);
1531
+ }
1532
+ function buildWhereNodeClause(dialect, tableName, node, params) {
1533
+ if ("compareRow" in node) {
1534
+ const { columns, operator, values } = node.compareRow;
1535
+ if (!Array.isArray(columns) || !columns.length || !Array.isArray(values) || columns.length !== values.length || !["lt", "gt"].includes(operator) || Array.from(columns).some((column) => typeof column !== "string" || !column.length))
1536
+ throw new TypeError("Invalid row comparison columns, values or operator.");
1537
+ return `(${columns.map((column) => qualifyColumn(dialect, tableName, column)).join(", ")}) ${operator === "lt" ? "<" : ">"} (${values.map((value) => pushParam(dialect, params, value)).join(", ")})`;
1538
+ }
1539
+ if ("exists" in node) {
1540
+ const body = remapExistsSql(node.exists.sql, node.exists.params, params);
1541
+ return `${node.exists.not ? "NOT " : ""}EXISTS (${body})`;
1542
+ }
1543
+ if ("where" in node) {
1544
+ return appendWhereParts(dialect, tableName, node.where, params);
1545
+ }
1546
+ const grouped = buildWhereGroupClause(dialect, tableName, node.group, params);
1547
+ if (!grouped) {
1548
+ return "";
1549
+ }
1550
+ return grouped.includes(" OR ") ? `(${grouped})` : grouped;
1551
+ }
1552
+ function buildWhereGroupClause(dialect, tableName, nodes, params) {
1553
+ let result = "";
1554
+ for (const node of nodes) {
1555
+ const part = buildWhereNodeClause(dialect, tableName, node, params);
1556
+ if (!part) {
1557
+ continue;
1558
+ }
1559
+ if (!result) {
1560
+ result = part;
1561
+ continue;
1562
+ }
1563
+ result = node.kind === "or" ? `${result} OR ${part}` : `${result} AND ${part}`;
2320
1564
  }
2321
- async dispatch(job, payload) {
2322
- this.enqueue(() => job.handle(payload), (error) => {
2323
- console.error("[AsyncQueue] Job failed:", error);
2324
- });
1565
+ if (!result) {
1566
+ return "";
2325
1567
  }
2326
- close() {
2327
- this.closing ??= (async () => {
2328
- while (this.active.size)
2329
- await Promise.all([...this.active]);
2330
- this.closed = true;
2331
- })();
2332
- return this.closing;
1568
+ return result;
1569
+ }
1570
+ function buildAdvancedWhereClause(dialect, tableName, where = {}, whereNodes = [], params = []) {
1571
+ const nodes = [];
1572
+ if (Object.keys(where).length > 0) {
1573
+ nodes.push({ kind: "and", where });
2333
1574
  }
1575
+ nodes.push(...whereNodes);
1576
+ const combined = buildWhereGroupClause(dialect, tableName, nodes, params);
1577
+ return {
1578
+ clause: combined ? ` WHERE ${combined}` : "",
1579
+ params
1580
+ };
2334
1581
  }
2335
- function createQueue(driver) {
2336
- return driver === "async" ? new AsyncQueue : new SyncQueue;
1582
+ function resolveSoftDeleteColumn(table) {
1583
+ if (!table.softDeletes) {
1584
+ return null;
1585
+ }
1586
+ if (table.softDeletes === true) {
1587
+ return "deleted_at";
1588
+ }
1589
+ return table.softDeletes.column ?? "deleted_at";
2337
1590
  }
2338
-
2339
- // ../../src/core/queue/resilientQueue.ts
2340
- class ResilientQueue extends AsyncQueue {
2341
- failedJobs;
2342
- asyncDispatch;
2343
- constructor(failedJobs, asyncDispatch = false) {
2344
- super();
2345
- this.failedJobs = failedJobs;
2346
- this.asyncDispatch = asyncDispatch;
1591
+ function appendSoftDeleteScope(dialect, table, options, clauses) {
1592
+ const column = resolveSoftDeleteColumn(table);
1593
+ if (!column) {
1594
+ return;
2347
1595
  }
2348
- async dispatch(job, payload) {
2349
- this.assertOpen();
2350
- const name = jobRegistry.resolveName(job);
2351
- if (!name) {
2352
- throw new Error("Job is not registered with the queue worker registry.");
1596
+ const qualifiedColumn = qualifyColumn(dialect, table.name, column);
1597
+ if (options.onlyTrashed) {
1598
+ clauses.push(`${qualifiedColumn} IS NOT NULL`);
1599
+ return;
1600
+ }
1601
+ if (!options.withTrashed) {
1602
+ clauses.push(`${qualifiedColumn} IS NULL`);
1603
+ }
1604
+ }
1605
+ function buildQueryWhereClause(dialect, table, options = {}, whereNodes = [], params = []) {
1606
+ const { clause, params: whereParams } = buildAdvancedWhereClause(dialect, table.name, options.where ?? {}, whereNodes, params);
1607
+ const softDeleteClauses = [];
1608
+ appendSoftDeleteScope(dialect, table, options, softDeleteClauses);
1609
+ if (softDeleteClauses.length === 0) {
1610
+ return { clause, params: whereParams };
1611
+ }
1612
+ const base = clause.replace(/^ WHERE /, "");
1613
+ const scope = softDeleteClauses.join(" AND ");
1614
+ return {
1615
+ clause: base ? ` WHERE (${base}) AND ${scope}` : ` WHERE ${scope}`,
1616
+ params: whereParams
1617
+ };
1618
+ }
1619
+ function isQueryOrder(value) {
1620
+ return "column" in value;
1621
+ }
1622
+ function normalizeOrderBy(orderBy) {
1623
+ if (!orderBy) {
1624
+ return [];
1625
+ }
1626
+ if (Array.isArray(orderBy)) {
1627
+ return orderBy;
1628
+ }
1629
+ if (isQueryOrder(orderBy)) {
1630
+ return [orderBy];
1631
+ }
1632
+ return Object.entries(orderBy).map(([column, direction]) => ({
1633
+ column,
1634
+ direction
1635
+ }));
1636
+ }
1637
+ function buildOrderByClause(dialect, tableName, orderBy) {
1638
+ const parts = normalizeOrderBy(orderBy).map(({ column, direction, nulls }) => {
1639
+ if (nulls !== undefined && nulls !== "first" && nulls !== "last") {
1640
+ throw new TypeError("Invalid null ordering: expected first or last.");
2353
1641
  }
2354
- const envelope = {
2355
- name,
2356
- payload,
2357
- attempts: 0
2358
- };
2359
- if (this.asyncDispatch) {
2360
- this.enqueue(() => runQueueJob(envelope, this.failedJobs), (error) => {
2361
- console.error("[ResilientQueue] Job failed:", error);
2362
- });
2363
- return;
1642
+ const reference = resolveQualifiedColumn(dialect, tableName, column);
1643
+ const ordered = `${reference} ${normalizeDirection(direction)}`;
1644
+ if (nulls === undefined)
1645
+ return ordered;
1646
+ if (dialect.driver === "mysql") {
1647
+ return `(${reference} IS NULL) ${nulls === "first" ? "DESC" : "ASC"}, ${ordered}`;
2364
1648
  }
2365
- await runQueueJob(envelope, this.failedJobs);
1649
+ return `${ordered} NULLS ${nulls.toUpperCase()}`;
1650
+ });
1651
+ return parts.length > 0 ? ` ORDER BY ${parts.join(", ")}` : "";
1652
+ }
1653
+ function buildGroupByClause(dialect, tableName, groupBy) {
1654
+ if (!groupBy) {
1655
+ return "";
2366
1656
  }
1657
+ const columns = (Array.isArray(groupBy) ? groupBy : [groupBy]).map((column) => String(column));
1658
+ const parts = columns.map((column) => resolveQualifiedColumn(dialect, tableName, column));
1659
+ return parts.length > 0 ? ` GROUP BY ${parts.join(", ")}` : "";
2367
1660
  }
2368
-
2369
- // ../../src/core/queue/publicQueue.ts
2370
- function createFailedJobService() {
2371
- return new failedJobService_default(new failedJobRepository_default);
2372
- }
2373
- function createTrackedJob(name, job) {
2374
- return jobRegistry.track(name, job);
2375
- }
2376
- function createProductionQueue(driver, options = {}) {
2377
- options.registerJobs?.();
2378
- const failedJobs = options.failedJobs ?? createFailedJobService();
2379
- if (driver === "redis") {
2380
- if (!options.redisUrl) {
2381
- throw new Error('QUEUE_DRIVER="redis" requires REDIS_URL to be set.');
1661
+ function buildHavingClause(dialect, tableName, having, params) {
1662
+ if (!having) {
1663
+ return "";
1664
+ }
1665
+ const body = appendWhereParts(dialect, tableName, having, params);
1666
+ return body.length > 0 ? ` HAVING ${body}` : "";
1667
+ }
1668
+ function buildJoinClause(dialect, joins = []) {
1669
+ return joins.map((join) => {
1670
+ const joinType = join.type === "left" ? "LEFT JOIN" : "INNER JOIN";
1671
+ const onClause = join.on.map(({ left, right }) => `${qualifyColumn(dialect, left.table, left.column)} = ${qualifyColumn(dialect, right.table, right.column)}`).join(" AND ");
1672
+ return ` ${joinType} ${quoteIdentifier(dialect, join.table)} ON ${onClause}`;
1673
+ }).join("");
1674
+ }
1675
+ function buildLimitClause(limit) {
1676
+ if (limit === undefined) {
1677
+ return "";
1678
+ }
1679
+ if (!Number.isInteger(limit) || limit <= 0) {
1680
+ throw new Error("Query limit must be a positive integer.");
1681
+ }
1682
+ return ` LIMIT ${limit}`;
1683
+ }
1684
+ function buildOffsetClause(offset) {
1685
+ if (offset === undefined) {
1686
+ return "";
1687
+ }
1688
+ if (!Number.isInteger(offset) || offset < 0) {
1689
+ throw new Error("Query offset must be a non-negative integer.");
1690
+ }
1691
+ return ` OFFSET ${offset}`;
1692
+ }
1693
+ function buildReturningColumns(dialect, table) {
1694
+ return table.columns.map((column) => qualifyColumn(dialect, table.name, column)).join(", ");
1695
+ }
1696
+ function buildSelectList(dialect, table, select, params = []) {
1697
+ if (!select || select.length === 0) {
1698
+ return buildReturningColumns(dialect, table);
1699
+ }
1700
+ return select.map((item) => {
1701
+ if (item.kind === "column") {
1702
+ const column = qualifyColumn(dialect, item.table, item.column);
1703
+ return item.as ? `${column} AS ${quoteIdentifier(dialect, item.as)}` : column;
2382
1704
  }
2383
- return resolveRedisQueueTransport() === "streams" ? new RedisStreamsQueue(options.redisUrl) : new RedisQueue(options.redisUrl);
1705
+ if (item.kind === "textColumn") {
1706
+ const column = qualifyColumn(dialect, item.table, item.column);
1707
+ const text = dialect.driver === "sqlite" ? `CASE WHEN typeof(${column}) = 'real' THEN printf('%!.26g', ${column}) ELSE ${dialect.castToText(column)} END` : dialect.castToText(column);
1708
+ return `${text} AS ${quoteIdentifier(dialect, item.as)}`;
1709
+ }
1710
+ if (item.kind === "literalText") {
1711
+ return `${dialect.castToText(pushParam(dialect, params, item.value))} AS ${quoteIdentifier(dialect, item.as)}`;
1712
+ }
1713
+ if (item.kind === "subqueryCount") {
1714
+ const body = remapExistsSql(item.sql, item.params, params);
1715
+ return `(${body}) AS ${quoteIdentifier(dialect, item.as)}`;
1716
+ }
1717
+ const column = qualifyColumn(dialect, item.table, item.column);
1718
+ const placeholder = pushParam(dialect, params, item.query);
1719
+ return `ts_rank(${column}, plainto_tsquery('english', ${placeholder})) AS ${quoteIdentifier(dialect, item.as)}`;
1720
+ }).join(", ");
1721
+ }
1722
+ function getDefinedColumnEntries(table, values, options = {}) {
1723
+ const record = values;
1724
+ const excluded = new Set(options.exclude ?? []);
1725
+ return table.columns.flatMap((column) => {
1726
+ if (excluded.has(column) || !Object.hasOwn(record, column)) {
1727
+ return [];
1728
+ }
1729
+ const value = record[column];
1730
+ if (value === undefined) {
1731
+ return [];
1732
+ }
1733
+ return [[column, value]];
1734
+ });
1735
+ }
1736
+ function buildSelectLock(dialect, table, options) {
1737
+ const lock = options.lock;
1738
+ if (!lock)
1739
+ return "";
1740
+ const driver = dialect.driver;
1741
+ if (driver === "sqlite")
1742
+ throw new Error("Row locks are not supported on SQLite; use an explicit write transaction.");
1743
+ if (lock.mode !== "update" && lock.mode !== "share")
1744
+ throw new TypeError("Invalid row lock mode.");
1745
+ if (lock.wait !== undefined && !["wait", "nowait", "skipLocked"].includes(lock.wait))
1746
+ throw new TypeError("Invalid row lock wait policy.");
1747
+ if (options.groupBy || options.having)
1748
+ throw new Error("Row locks cannot be combined with grouped/aggregate queries.");
1749
+ let target = "";
1750
+ if (lock.of !== undefined) {
1751
+ if (driver !== "pgsql")
1752
+ throw new Error("Row lock targets are supported only on PostgreSQL.");
1753
+ const tables = [table.name, ...(options.joins ?? []).map((join) => join.table)];
1754
+ if (!lock.of.length || lock.of.some((name) => !tables.includes(name)))
1755
+ throw new TypeError("Row lock targets must name tables in this query.");
1756
+ target = ` OF ${lock.of.map((column) => quoteIdentifier(dialect, column)).join(", ")}`;
1757
+ }
1758
+ const wait = lock.wait === "nowait" ? " NOWAIT" : lock.wait === "skipLocked" ? " SKIP LOCKED" : "";
1759
+ return ` FOR ${lock.mode === "update" ? "UPDATE" : "SHARE"}${target}${wait}`;
1760
+ }
1761
+ function buildSelectQuery(dialect, table, options = {}, whereNodes = []) {
1762
+ const params = [];
1763
+ const columns = buildSelectList(dialect, table, options.select, params);
1764
+ const { clause } = buildQueryWhereClause(dialect, table, options, whereNodes, params);
1765
+ const joins = buildJoinClause(dialect, options.joins);
1766
+ const groupBy = buildGroupByClause(dialect, table.name, options.groupBy);
1767
+ const havingClause = buildHavingClause(dialect, table.name, options.having, params);
1768
+ const orderBy = buildOrderByClause(dialect, table.name, options.orderBy ?? table.defaultOrderBy);
1769
+ const limit = buildLimitClause(options.limit);
1770
+ const offset = buildOffsetClause(options.offset);
1771
+ const lock = buildSelectLock(dialect, table, options);
1772
+ return {
1773
+ text: `SELECT ${columns} FROM ${quoteIdentifier(dialect, table.name)}${joins}${clause}${groupBy}${havingClause}${orderBy}${limit}${offset}${lock}`,
1774
+ params
1775
+ };
1776
+ }
1777
+ var EAGER_ROW_NUMBER = "__strata_eager_row_number";
1778
+ function buildLimitedRelationQuery(dialect, table, options, partition, source) {
1779
+ if (options.lock || options.groupBy?.length || options.having) {
1780
+ throw new Error("Limited eager relations do not support row locks, grouping or HAVING.");
1781
+ }
1782
+ if (table.columns.includes(EAGER_ROW_NUMBER) || options.select?.some((item) => ("as" in item) && item.as === EAGER_ROW_NUMBER)) {
1783
+ throw new Error(`Reserved eager relation column: ${EAGER_ROW_NUMBER}`);
1784
+ }
1785
+ buildLimitClause(options.limit);
1786
+ buildOffsetClause(options.offset);
1787
+ if (!Number.isSafeInteger((options.offset ?? 0) + (options.limit ?? 0))) {
1788
+ throw new Error("Limited eager relation bounds must fit a safe integer.");
1789
+ }
1790
+ const order = [...normalizeOrderBy(options.orderBy ?? table.defaultOrderBy)];
1791
+ if (!order.some((item) => item.column === table.primaryKey || item.column === `${table.name}.${table.primaryKey}`)) {
1792
+ order.push({ column: table.primaryKey, direction: "ASC" });
1793
+ }
1794
+ const ordering = buildOrderByClause(dialect, table.name, order);
1795
+ const params = [];
1796
+ const columns = buildSelectList(dialect, table, options.select, params);
1797
+ const rank = `ROW_NUMBER() OVER (PARTITION BY ${qualifyColumn(dialect, partition.table, partition.column)}${ordering}) AS ${quoteIdentifier(dialect, EAGER_ROW_NUMBER)}`;
1798
+ const sql = source(columns, rank, params);
1799
+ const offset = options.offset ?? 0;
1800
+ const upper = options.limit === undefined ? "" : ` AND ${quoteIdentifier(dialect, EAGER_ROW_NUMBER)} <= ${offset + options.limit}`;
1801
+ return {
1802
+ text: `SELECT * FROM (${sql}) AS ${quoteIdentifier(dialect, "__strata_eager")} WHERE ${quoteIdentifier(dialect, EAGER_ROW_NUMBER)} > ${offset}${upper} ORDER BY ${quoteIdentifier(dialect, EAGER_ROW_NUMBER)}`,
1803
+ params
1804
+ };
1805
+ }
1806
+ function buildLimitedHasManyQuery(dialect, table, options, partition) {
1807
+ return buildLimitedRelationQuery(dialect, table, options, partition, (columns, rank, params) => {
1808
+ const { clause } = buildQueryWhereClause(dialect, table, options, options.whereNodes, params);
1809
+ const owner = partition.ownerAlias ? `, ${qualifyColumn(dialect, partition.table, partition.column)} AS ${quoteIdentifier(dialect, partition.ownerAlias)}` : "";
1810
+ return `SELECT ${columns}${owner}, ${rank} FROM ${quoteIdentifier(dialect, table.name)}${buildJoinClause(dialect, options.joins)}${clause}`;
1811
+ });
1812
+ }
1813
+ function buildCountQuery(dialect, table, where = {}, options = {}, whereNodes = []) {
1814
+ const params = [];
1815
+ const { clause, params: whereParams } = buildQueryWhereClause(dialect, table, {
1816
+ where,
1817
+ withTrashed: options.withTrashed,
1818
+ onlyTrashed: options.onlyTrashed
1819
+ }, whereNodes);
1820
+ params.push(...whereParams);
1821
+ const joins = buildJoinClause(dialect, options.joins);
1822
+ const groupBy = buildGroupByClause(dialect, table.name, options.groupBy);
1823
+ return {
1824
+ text: `SELECT COUNT(*) AS count FROM ${quoteIdentifier(dialect, table.name)}${joins}${clause}${groupBy}`,
1825
+ params
1826
+ };
1827
+ }
1828
+ function buildProjectionQuery(dialect, table, expression, alias, options = {}, whereNodes = []) {
1829
+ assertSafeProjectionExpression(expression);
1830
+ const params = [];
1831
+ const { clause, params: whereParams } = buildQueryWhereClause(dialect, table, options, whereNodes);
1832
+ params.push(...whereParams);
1833
+ const joins = buildJoinClause(dialect, options.joins);
1834
+ const groupBy = buildGroupByClause(dialect, table.name, options.groupBy);
1835
+ const orderBy = buildOrderByClause(dialect, table.name, options.orderBy);
1836
+ const limit = buildLimitClause(options.limit);
1837
+ return {
1838
+ text: `SELECT ${expression} AS ${quoteIdentifier(dialect, alias)} FROM ${quoteIdentifier(dialect, table.name)}${joins}${clause}${groupBy}${orderBy}${limit}`,
1839
+ params
1840
+ };
1841
+ }
1842
+ var SAFE_PROJECTION_EXPRESSION = /^(COUNT\(\*\)|(?:"[\w_]+"(?:\."[\w_]+")?)|(?:AVG|SUM|MIN|MAX)\((?:"[\w_]+"(?:\."[\w_]+")?)\))$/;
1843
+ function assertSafeProjectionExpression(expression) {
1844
+ if (!SAFE_PROJECTION_EXPRESSION.test(expression.trim())) {
1845
+ throw new Error(`Unsafe projection expression: ${expression}`);
1846
+ }
1847
+ }
1848
+ function buildGroupedSumQuery(dialect, table, groupColumn, column, options = {}, whereNodes = options.whereNodes ?? []) {
1849
+ if (!table.columns.includes(groupColumn) || !table.columns.includes(column))
1850
+ throw new TypeError("Grouped sums require declared columns.");
1851
+ for (const key of Object.keys(options))
1852
+ if (!["where", "whereNodes", "withTrashed", "onlyTrashed"].includes(key))
1853
+ throw new TypeError("Grouped sums do not support joins, selection, ordering, pagination, grouping or locks.");
1854
+ const group = qualifyColumn(dialect, table.name, groupColumn);
1855
+ const total = dialect.castToText(`SUM(${qualifyColumn(dialect, table.name, column)})`);
1856
+ const { clause, params } = buildQueryWhereClause(dialect, table, options, whereNodes);
1857
+ return {
1858
+ text: `SELECT ${group} AS ${quoteIdentifier(dialect, "value")}, ${total} AS ${quoteIdentifier(dialect, "sum")} FROM ${quoteIdentifier(dialect, table.name)}${clause} GROUP BY ${group} ORDER BY ${group} ASC`,
1859
+ params
1860
+ };
1861
+ }
1862
+ function buildGroupedCountQuery(dialect, table, column, where = {}, options = {}) {
1863
+ const qualifiedColumn = qualifyColumn(dialect, table.name, column);
1864
+ const { clause, params } = buildQueryWhereClause(dialect, table, {
1865
+ where,
1866
+ ...options
1867
+ });
1868
+ return {
1869
+ text: `SELECT ${qualifiedColumn} AS ${quoteIdentifier(dialect, "value")}, COUNT(*) AS ${quoteIdentifier(dialect, "count")} FROM ${quoteIdentifier(dialect, table.name)}${clause} GROUP BY ${qualifiedColumn} ORDER BY ${qualifiedColumn} ASC`,
1870
+ params
1871
+ };
1872
+ }
1873
+ function buildInsertQuery(dialect, table, values) {
1874
+ const entries = getDefinedColumnEntries(table, values);
1875
+ if (entries.length === 0) {
1876
+ throw new Error(`Cannot insert into ${table.name} without any column values.`);
2384
1877
  }
2385
- return new ResilientQueue(failedJobs, driver === "async");
1878
+ const params = [];
1879
+ const columns = entries.map(([column]) => quoteIdentifier(dialect, column)).join(", ");
1880
+ const placeholders = entries.map(([, value]) => pushParam(dialect, params, value)).join(", ");
1881
+ const returningColumns = buildReturningColumns(dialect, table);
1882
+ return {
1883
+ text: `INSERT INTO ${quoteIdentifier(dialect, table.name)} (${columns}) VALUES (${placeholders})${returningSuffix(dialect, returningColumns)}`,
1884
+ params
1885
+ };
2386
1886
  }
2387
- function createQueueWorker(redisUrl, failedJobs = createFailedJobService()) {
2388
- return resolveRedisQueueTransport() === "streams" ? new RedisStreamsWorker(redisUrl, failedJobs) : new QueueWorker(redisUrl, failedJobs);
1887
+ function buildUpsertQuery(dialect, table, values, conflictColumns, updateColumns) {
1888
+ const entries = getDefinedColumnEntries(table, values);
1889
+ if (entries.length === 0) {
1890
+ throw new Error(`Cannot upsert into ${table.name} without any column values.`);
1891
+ }
1892
+ if (conflictColumns.length === 0) {
1893
+ throw new Error(`Cannot upsert into ${table.name} without any conflict columns.`);
1894
+ }
1895
+ const insertable = new Set(entries.map(([column]) => column));
1896
+ const conflict = new Set(conflictColumns);
1897
+ const updatable = (updateColumns ?? entries.map(([column]) => column).filter((c) => !conflict.has(c))).filter((column) => insertable.has(column));
1898
+ const params = [];
1899
+ const columns = entries.map(([column]) => quoteIdentifier(dialect, column)).join(", ");
1900
+ const placeholders = entries.map(([, value]) => pushParam(dialect, params, value)).join(", ");
1901
+ const returningColumns = buildReturningColumns(dialect, table);
1902
+ const suffix = dialect.upsertSuffix(conflictColumns, updatable);
1903
+ return {
1904
+ text: `INSERT INTO ${quoteIdentifier(dialect, table.name)} (${columns}) VALUES (${placeholders})${suffix}${returningSuffix(dialect, returningColumns)}`,
1905
+ params
1906
+ };
2389
1907
  }
2390
-
2391
- // ../../src/core/queue/createAppQueue.ts
2392
- var FAILED_JOB_SERVICE_TOKEN = "core.failedJobs";
2393
- function createAppQueue(driver, redisUrl, failedJobs = createFailedJobService(), registerJobs) {
2394
- return createProductionQueue(driver, {
2395
- redisUrl,
2396
- failedJobs,
2397
- registerJobs
1908
+ function buildIncrementQuery(dialect, table, id, column, amount, extra = {}) {
1909
+ if (!Number.isFinite(amount)) {
1910
+ throw new Error("Increment amount must be a finite number.");
1911
+ }
1912
+ if (!table.columns.includes(column)) {
1913
+ throw new Error(`Unknown column ${String(column)} on ${table.name}.`);
1914
+ }
1915
+ const params = [];
1916
+ const target = quoteIdentifier(dialect, column);
1917
+ const assignments = [`${target} = ${target} + ${pushParam(dialect, params, amount)}`];
1918
+ for (const [name, value] of getDefinedColumnEntries(table, extra, {
1919
+ exclude: [table.primaryKey, column]
1920
+ })) {
1921
+ assignments.push(`${quoteIdentifier(dialect, name)} = ${pushParam(dialect, params, value)}`);
1922
+ }
1923
+ const primaryKeyPlaceholder = pushParam(dialect, params, id);
1924
+ const returningColumns = buildReturningColumns(dialect, table);
1925
+ const scopeClauses = [];
1926
+ appendSoftDeleteScope(dialect, table, {}, scopeClauses);
1927
+ const scopeSuffix = scopeClauses.length > 0 ? ` AND ${scopeClauses.join(" AND ")}` : "";
1928
+ return {
1929
+ text: `UPDATE ${quoteIdentifier(dialect, table.name)} SET ${assignments.join(", ")} WHERE ${quoteIdentifier(dialect, table.primaryKey)} = ${primaryKeyPlaceholder}${scopeSuffix}${returningSuffix(dialect, returningColumns)}`,
1930
+ params
1931
+ };
1932
+ }
1933
+ function buildUpdateQuery(dialect, table, id, changes) {
1934
+ const entries = getDefinedColumnEntries(table, changes, {
1935
+ exclude: [table.primaryKey]
2398
1936
  });
1937
+ if (entries.length === 0) {
1938
+ throw new Error(`Cannot update ${table.name} without any changed column values.`);
1939
+ }
1940
+ const params = [];
1941
+ const setClause = entries.map(([column, value]) => `${quoteIdentifier(dialect, column)} = ${pushParam(dialect, params, value)}`).join(", ");
1942
+ const primaryKeyPlaceholder = pushParam(dialect, params, id);
1943
+ const returningColumns = buildReturningColumns(dialect, table);
1944
+ const scopeClauses = [];
1945
+ appendSoftDeleteScope(dialect, table, {}, scopeClauses);
1946
+ const scopeSuffix = scopeClauses.length > 0 ? ` AND ${scopeClauses.join(" AND ")}` : "";
1947
+ return {
1948
+ text: `UPDATE ${quoteIdentifier(dialect, table.name)} SET ${setClause} WHERE ${quoteIdentifier(dialect, table.primaryKey)} = ${primaryKeyPlaceholder}${scopeSuffix}${returningSuffix(dialect, returningColumns)}`,
1949
+ params
1950
+ };
1951
+ }
1952
+ function buildSoftDeleteByIdQuery(dialect, table, id, deletedAt) {
1953
+ const deletedAtColumn = resolveSoftDeleteColumn(table);
1954
+ if (!deletedAtColumn) {
1955
+ throw new Error(`Table ${table.name} does not support soft deletes.`);
1956
+ }
1957
+ const returningColumns = buildReturningColumns(dialect, table);
1958
+ const scopeClauses = [];
1959
+ appendSoftDeleteScope(dialect, table, {}, scopeClauses);
1960
+ const scopeSuffix = scopeClauses.length > 0 ? ` AND ${scopeClauses.join(" AND ")}` : "";
1961
+ const params = [];
1962
+ const deletedAtPlaceholder = pushParam(dialect, params, deletedAt);
1963
+ const idPlaceholder = pushParam(dialect, params, id);
1964
+ return {
1965
+ text: `UPDATE ${quoteIdentifier(dialect, table.name)} SET ${quoteIdentifier(dialect, deletedAtColumn)} = ${deletedAtPlaceholder} WHERE ${quoteIdentifier(dialect, table.primaryKey)} = ${idPlaceholder}${scopeSuffix}${returningSuffix(dialect, returningColumns)}`,
1966
+ params
1967
+ };
1968
+ }
1969
+ function buildRestoreByIdQuery(dialect, table, id) {
1970
+ const deletedAtColumn = resolveSoftDeleteColumn(table);
1971
+ if (!deletedAtColumn) {
1972
+ throw new Error(`Table ${table.name} does not support soft deletes.`);
1973
+ }
1974
+ const returningColumns = buildReturningColumns(dialect, table);
1975
+ const params = [];
1976
+ const deletedAtPlaceholder = pushParam(dialect, params, null);
1977
+ const idPlaceholder = pushParam(dialect, params, id);
1978
+ return {
1979
+ text: `UPDATE ${quoteIdentifier(dialect, table.name)} SET ${quoteIdentifier(dialect, deletedAtColumn)} = ${deletedAtPlaceholder} WHERE ${quoteIdentifier(dialect, table.primaryKey)} = ${idPlaceholder} AND ${qualifyColumn(dialect, table.name, deletedAtColumn)} IS NOT NULL${returningSuffix(dialect, returningColumns)}`,
1980
+ params
1981
+ };
1982
+ }
1983
+ function buildDeleteByIdQuery(dialect, table, id) {
1984
+ const params = [];
1985
+ const idPlaceholder = pushParam(dialect, params, id);
1986
+ const returning = returningSuffix(dialect, `${quoteIdentifier(dialect, table.primaryKey)} AS ${quoteIdentifier(dialect, "deleted_id")}`);
1987
+ return {
1988
+ text: `DELETE FROM ${quoteIdentifier(dialect, table.name)} WHERE ${quoteIdentifier(dialect, table.primaryKey)} = ${idPlaceholder}${returning}`,
1989
+ params
1990
+ };
1991
+ }
1992
+ function buildConditionalWriteQuery(dialect, table, operation, changes, options, returning) {
1993
+ for (const key of [
1994
+ "joins",
1995
+ "groupBy",
1996
+ "having",
1997
+ "select",
1998
+ "orderBy",
1999
+ "limit",
2000
+ "offset",
2001
+ "lock"
2002
+ ]) {
2003
+ if (options[key] !== undefined)
2004
+ throw new Error(`Conditional writes do not support ${key}.`);
2005
+ }
2006
+ const params = [];
2007
+ const predicate = buildAdvancedWhereClause(dialect, table.name, options.where ?? {}, options.whereNodes ?? []);
2008
+ if (!predicate.clause)
2009
+ throw new Error("Conditional writes require an explicit predicate.");
2010
+ if (returning !== undefined && dialect.driver === "mysql")
2011
+ throw new Error("Mutation RETURNING is not supported on MySQL.");
2012
+ const columns = returning ?? [table.primaryKey];
2013
+ if (!columns.length || columns.some((column) => !table.columns.includes(column)))
2014
+ throw new Error("RETURNING columns must be nonempty declared table columns.");
2015
+ const deletedAt = resolveSoftDeleteColumn(table);
2016
+ const values = operation === "delete" && deletedAt ? { [deletedAt]: new Date } : changes;
2017
+ let prefix;
2018
+ if (operation === "update" || deletedAt) {
2019
+ const entries = Object.entries(values).filter(([, value]) => value !== undefined);
2020
+ if (!entries.length)
2021
+ throw new Error("Conditional updates require at least one value.");
2022
+ if (entries.some(([column]) => column === table.primaryKey || !table.columns.includes(column)))
2023
+ throw new Error("Conditional updates require declared non-primary-key columns.");
2024
+ const assignments = entries.map(([column, value]) => `${quoteIdentifier(dialect, column)} = ${pushParam(dialect, params, value)}`);
2025
+ prefix = `UPDATE ${quoteIdentifier(dialect, table.name)} SET ${assignments.join(", ")}`;
2026
+ } else
2027
+ prefix = `DELETE FROM ${quoteIdentifier(dialect, table.name)}`;
2028
+ const { clause } = buildQueryWhereClause(dialect, table, options, options.whereNodes ?? [], params);
2029
+ return {
2030
+ text: prefix + clause + returningSuffix(dialect, columns.map((column) => quoteIdentifier(dialect, column)).join(", ")),
2031
+ params
2032
+ };
2399
2033
  }
2400
2034
 
2401
- // ../../src/core/queue/failedJobMetrics.ts
2402
- init_dialect();
2035
+ // ../../src/core/queue/failedJobTable.ts
2036
+ import { defineTable } from "@getstrata/core/database/table";
2037
+ var failedJobTable = defineTable({
2038
+ name: "failed_job",
2039
+ primaryKey: "id",
2040
+ columns: ["id", "job_name", "job_id", "handoff", "payload", "exception", "failed_at"],
2041
+ defaultOrderBy: { column: "failed_at", direction: "DESC" }
2042
+ });
2403
2043
 
2404
- // ../../src/core/database/readOnlyCollector.ts
2405
- init_connectionContext();
2406
- init_dialect();
2407
- init_repositoryConnection();
2408
- init_transaction();
2409
- function createReadOnlyCollector(name, timeoutMs, collect) {
2044
+ // ../../src/core/queue/failedJobMetrics.ts
2045
+ var schemaQuery = `SELECT c.relkind = 'r' AND c.relpersistence = 'p' AND NOT c.relrowsecurity AND EXISTS (
2046
+ SELECT 1 FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = i.indkey[0]
2047
+ WHERE i.indrelid = c.oid AND i.indisprimary AND i.indisvalid AND i.indisready
2048
+ AND i.indnkeyatts = 1 AND a.attname = 'id' AND NOT a.attisdropped
2049
+ AND a.atttypid IN ('int2'::regtype, 'int4'::regtype, 'int8'::regtype)
2050
+ ) AS supported FROM pg_class c WHERE c.oid = 'failed_job'::regclass`;
2051
+ var sqliteSchemaQuery = `SELECT 1 AS supported FROM main.sqlite_schema
2052
+ WHERE name='failed_job' AND type='table' AND rootpage > 0
2053
+ AND (SELECT COUNT(*) FROM pragma_table_xinfo('failed_job', 'main') WHERE pk > 0) = 1
2054
+ AND EXISTS (SELECT 1 FROM pragma_table_xinfo('failed_job', 'main')
2055
+ WHERE name='id' AND pk=1 AND upper(type)='INTEGER' AND hidden=0) LIMIT 1`;
2056
+ var mysqlSchemaQuery = `SELECT 1 AS supported FROM information_schema.TABLES t
2057
+ WHERE t.TABLE_SCHEMA=DATABASE() AND t.TABLE_NAME='failed_job' AND t.TABLE_TYPE='BASE TABLE' AND t.ENGINE='InnoDB'
2058
+ AND (SELECT COUNT(*) FROM information_schema.STATISTICS i WHERE i.TABLE_SCHEMA=t.TABLE_SCHEMA
2059
+ AND i.TABLE_NAME=t.TABLE_NAME AND i.INDEX_NAME='PRIMARY')=1
2060
+ AND EXISTS(SELECT 1 FROM information_schema.STATISTICS i JOIN information_schema.COLUMNS c
2061
+ ON c.TABLE_SCHEMA=i.TABLE_SCHEMA AND c.TABLE_NAME=i.TABLE_NAME AND c.COLUMN_NAME=i.COLUMN_NAME
2062
+ WHERE i.TABLE_SCHEMA=t.TABLE_SCHEMA AND i.TABLE_NAME=t.TABLE_NAME AND i.INDEX_NAME='PRIMARY'
2063
+ AND i.SEQ_IN_INDEX=1 AND i.COLUMN_NAME='id' AND c.DATA_TYPE IN ('tinyint','smallint','mediumint','int','bigint')) LIMIT 1`;
2064
+ function createFailedJobMetricsCollector(options) {
2065
+ if (typeof options.observeReadOnly !== "function")
2066
+ throw new TypeError("Failed-job metrics require an explicit read-only observation runner.");
2067
+ const sampleLimit = options.sampleLimit ?? 500;
2068
+ if (!Number.isSafeInteger(sampleLimit) || sampleLimit < 1 || sampleLimit > 1000)
2069
+ throw new TypeError("Failed-job metrics sample limit must be between 1 and 1000.");
2070
+ const timeoutMs = options.timeoutMs ?? 1000;
2410
2071
  if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 5000)
2411
- throw new TypeError(`${name} timeout must be between 1 and 5000 milliseconds.`);
2072
+ throw new TypeError("Failed-job metrics timeout must be between 1 and 5000 milliseconds.");
2412
2073
  let active;
2413
2074
  let closed = false;
2414
2075
  return {
2415
2076
  async collect() {
2416
2077
  if (closed)
2417
- throw new Error(`${name} collector is closed.`);
2418
- if (hasActiveTransaction() || hasActiveDatabaseConnection())
2419
- throw new Error(`${name} require an independent transaction.`);
2420
- const driver = currentSqlDialect().driver;
2421
- const pool = resolveRepositoryConnection();
2422
- if (driver !== "pgsql" && driver !== "sqlite" && driver !== "mysql")
2423
- throw new Error(`${name} require Postgres or an interruptible SQLite/MySQL adapter.`);
2078
+ throw new Error("Failed-job metrics collector is closed.");
2424
2079
  if (active)
2425
- throw new Error(`${name} collection is still settling.`);
2426
- const deadline = performance.now() + timeoutMs;
2427
- let expired = false;
2428
- const acquisition = new AbortController;
2429
- let timer;
2430
- const remaining = () => {
2431
- const value = Math.floor(deadline - performance.now());
2432
- if (expired || value < 1)
2433
- throw new Error(`${name} collection timed out.`);
2434
- return value;
2435
- };
2436
- const work = driver === "pgsql" ? runInTransaction(async () => {
2437
- await repositoryConnection.unsafe("SELECT set_config('statement_timeout', $1, true)", [
2438
- `${remaining()}ms`
2439
- ]);
2440
- await repositoryConnection.unsafe("SET TRANSACTION READ ONLY");
2441
- remaining();
2442
- const result = await collect(remaining);
2080
+ throw new Error("Failed-job metrics collection is still settling.");
2081
+ const work = options.observeReadOnly(timeoutMs, async (database, remaining) => {
2082
+ const driver = database.dialect.driver;
2083
+ const postgres = driver === "pgsql";
2084
+ if (postgres)
2085
+ await database.execute({
2086
+ text: 'LOCK TABLE "failed_job" IN ACCESS SHARE MODE',
2087
+ params: []
2088
+ });
2089
+ if (driver === "mysql")
2090
+ await database.execute({ text: "SELECT id FROM failed_job LIMIT 0", params: [] });
2091
+ const schema = await database.execute({
2092
+ text: postgres ? schemaQuery : driver === "mysql" ? mysqlSchemaQuery : sqliteSchemaQuery,
2093
+ params: []
2094
+ });
2095
+ if (schema.length !== 1 || schema[0]?.supported !== (postgres ? true : 1))
2096
+ throw new Error("Failed-job metrics require the standard global table with an ID primary key and no RLS.");
2443
2097
  remaining();
2444
- return result;
2445
- }, { acquisitionSignal: acquisition.signal }) : (async () => {
2446
- if (!pool.observeReadOnly)
2447
- throw new Error(`${name} require Postgres or an interruptible SQLite/MySQL adapter.`);
2448
- return pool.observeReadOnly(async (connection) => await runWithDatabaseConnection(connection, async () => {
2449
- remaining();
2450
- const result = await collect(remaining);
2451
- remaining();
2452
- return result;
2453
- }), { signal: acquisition.signal, timeoutMs: remaining() });
2454
- })();
2098
+ const rows = await database.execute(buildSelectQuery(database.dialect, failedJobTable, {
2099
+ select: [{ kind: "column", table: failedJobTable.name, column: "id" }],
2100
+ orderBy: { column: "id", direction: "ASC" },
2101
+ limit: sampleLimit + 1
2102
+ }));
2103
+ if (rows.length > sampleLimit + 1 || rows.some((row) => row.id == null))
2104
+ throw new Error("Invalid failed-job metrics response.");
2105
+ return {
2106
+ sampleLimit,
2107
+ count: Math.min(rows.length, sampleLimit),
2108
+ capped: rows.length > sampleLimit
2109
+ };
2110
+ });
2455
2111
  active = work;
2456
2112
  work.finally(() => {
2457
2113
  if (active === work)
2458
2114
  active = undefined;
2459
2115
  }).catch(() => {});
2116
+ let timer;
2460
2117
  try {
2461
2118
  return await Promise.race([
2462
2119
  work,
2463
2120
  new Promise((_, reject) => {
2464
- timer = setTimeout(() => {
2465
- expired = true;
2466
- const error = new Error(`${name} collection timed out.`);
2467
- acquisition.abort(error);
2468
- reject(error);
2469
- }, timeoutMs);
2121
+ timer = setTimeout(() => reject(new Error("Failed-job metrics collection timed out.")), timeoutMs);
2470
2122
  })
2471
2123
  ]);
2472
2124
  } finally {
@@ -2480,54 +2132,6 @@ function createReadOnlyCollector(name, timeoutMs, collect) {
2480
2132
  }
2481
2133
  };
2482
2134
  }
2483
-
2484
- // ../../src/core/queue/failedJobMetrics.ts
2485
- init_repositoryConnection();
2486
- var schemaQuery = `SELECT c.relkind = 'r' AND c.relpersistence = 'p' AND NOT c.relrowsecurity AND EXISTS (
2487
- SELECT 1 FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = i.indkey[0]
2488
- WHERE i.indrelid = c.oid AND i.indisprimary AND i.indisvalid AND i.indisready
2489
- AND i.indnkeyatts = 1 AND a.attname = 'id' AND NOT a.attisdropped
2490
- AND a.atttypid IN ('int2'::regtype, 'int4'::regtype, 'int8'::regtype)
2491
- ) AS supported FROM pg_class c WHERE c.oid = 'failed_job'::regclass`;
2492
- var sqliteSchemaQuery = `SELECT 1 AS supported FROM main.sqlite_schema
2493
- WHERE name='failed_job' AND type='table' AND rootpage > 0
2494
- AND (SELECT COUNT(*) FROM pragma_table_xinfo('failed_job', 'main') WHERE pk > 0) = 1
2495
- AND EXISTS (SELECT 1 FROM pragma_table_xinfo('failed_job', 'main')
2496
- WHERE name='id' AND pk=1 AND upper(type)='INTEGER' AND hidden=0) LIMIT 1`;
2497
- var mysqlSchemaQuery = `SELECT 1 AS supported FROM information_schema.TABLES t
2498
- WHERE t.TABLE_SCHEMA=DATABASE() AND t.TABLE_NAME='failed_job' AND t.TABLE_TYPE='BASE TABLE' AND t.ENGINE='InnoDB'
2499
- AND (SELECT COUNT(*) FROM information_schema.STATISTICS i WHERE i.TABLE_SCHEMA=t.TABLE_SCHEMA
2500
- AND i.TABLE_NAME=t.TABLE_NAME AND i.INDEX_NAME='PRIMARY')=1
2501
- AND EXISTS(SELECT 1 FROM information_schema.STATISTICS i JOIN information_schema.COLUMNS c
2502
- ON c.TABLE_SCHEMA=i.TABLE_SCHEMA AND c.TABLE_NAME=i.TABLE_NAME AND c.COLUMN_NAME=i.COLUMN_NAME
2503
- WHERE i.TABLE_SCHEMA=t.TABLE_SCHEMA AND i.TABLE_NAME=t.TABLE_NAME AND i.INDEX_NAME='PRIMARY'
2504
- AND i.SEQ_IN_INDEX=1 AND i.COLUMN_NAME='id' AND c.DATA_TYPE IN ('tinyint','smallint','mediumint','int','bigint')) LIMIT 1`;
2505
- function createFailedJobMetricsCollector(options = {}) {
2506
- const sampleLimit = options.sampleLimit ?? 500;
2507
- if (!Number.isSafeInteger(sampleLimit) || sampleLimit < 1 || sampleLimit > 1000)
2508
- throw new TypeError("Failed-job metrics sample limit must be between 1 and 1000.");
2509
- const repository = new failedJobRepository_default;
2510
- return createReadOnlyCollector("Failed-job metrics", options.timeoutMs ?? 1000, async (remaining) => {
2511
- const driver = currentSqlDialect().driver;
2512
- const postgres = driver === "pgsql";
2513
- if (postgres)
2514
- await repositoryConnection.unsafe('LOCK TABLE "failed_job" IN ACCESS SHARE MODE');
2515
- if (driver === "mysql")
2516
- await repositoryConnection.unsafe("SELECT id FROM failed_job LIMIT 0");
2517
- const schema = await repositoryConnection.unsafe(postgres ? schemaQuery : driver === "mysql" ? mysqlSchemaQuery : sqliteSchemaQuery);
2518
- if (schema.length !== 1 || schema[0]?.supported !== (postgres ? true : 1))
2519
- throw new Error("Failed-job metrics require the standard global table with an ID primary key and no RLS.");
2520
- remaining();
2521
- const rows = await repository.query().orderBy({ column: "id", direction: "ASC" }).project(["id"], sampleLimit + 1);
2522
- if (rows.length > sampleLimit + 1 || rows.some((row) => row.id == null))
2523
- throw new Error("Invalid failed-job metrics response.");
2524
- return {
2525
- sampleLimit,
2526
- count: Math.min(rows.length, sampleLimit),
2527
- capped: rows.length > sampleLimit
2528
- };
2529
- });
2530
- }
2531
2135
  function renderFailedJobMetrics(snapshot) {
2532
2136
  if (!Number.isSafeInteger(snapshot.sampleLimit) || snapshot.sampleLimit < 1 || snapshot.sampleLimit > 1000 || !Number.isSafeInteger(snapshot.count) || snapshot.count < 0 || snapshot.count > snapshot.sampleLimit || typeof snapshot.capped !== "boolean" || snapshot.capped && snapshot.count !== snapshot.sampleLimit)
2533
2137
  throw new TypeError("Invalid failed-job metrics snapshot.");
@@ -2544,13 +2148,15 @@ strata_queue_failed_jobs_sample_capped ${Number(snapshot.capped)}
2544
2148
  }
2545
2149
 
2546
2150
  // ../../src/core/queue/queueMetrics.ts
2547
- async function readRedisQueueDepth(redisUrl) {
2151
+ async function readRedisQueueDepth(redisUrl, transport = "lists") {
2152
+ if (transport !== "lists" && transport !== "streams")
2153
+ throw new TypeError("Invalid queue metrics transport.");
2548
2154
  const client = new RedisClient3(redisUrl);
2549
2155
  let high;
2550
2156
  let defaultQueue;
2551
2157
  let low;
2552
2158
  try {
2553
- const readDepth = async (key) => resolveRedisQueueTransport() === "streams" ? readStreamQueueDepth(client, key) : client.llen(key);
2159
+ const readDepth = async (key) => transport === "streams" ? readStreamQueueDepth(client, key) : client.llen(key);
2554
2160
  [high, defaultQueue, low] = await Promise.all([
2555
2161
  readDepth(defaultQueueKeys()[0]),
2556
2162
  readDepth(defaultQueueKeys()[1]),
@@ -2566,9 +2172,9 @@ async function readRedisQueueDepth(redisUrl) {
2566
2172
  total: Number(high ?? 0) + Number(defaultQueue ?? 0) + Number(low ?? 0)
2567
2173
  };
2568
2174
  }
2569
- async function collectQueueMetrics() {
2570
- const driver = queueConfig.driver;
2571
- const failedJobs = createFailedJobService();
2175
+ async function collectQueueMetrics(failedJobs, options = { driver: "sync" }) {
2176
+ const { driver, redisUrl } = options;
2177
+ const transport = options.transport ?? "lists";
2572
2178
  const failedCount = (await failedJobs.listRecent(1000)).length;
2573
2179
  if (driver !== "redis") {
2574
2180
  return {
@@ -2582,7 +2188,6 @@ async function collectQueueMetrics() {
2582
2188
  failedCount
2583
2189
  };
2584
2190
  }
2585
- const redisUrl = process.env.REDIS_URL;
2586
2191
  if (!redisUrl) {
2587
2192
  return {
2588
2193
  driver,
@@ -2597,7 +2202,7 @@ async function collectQueueMetrics() {
2597
2202
  }
2598
2203
  return {
2599
2204
  driver,
2600
- pending: await readRedisQueueDepth(redisUrl),
2205
+ pending: await readRedisQueueDepth(redisUrl, transport),
2601
2206
  failedCount
2602
2207
  };
2603
2208
  }
@@ -2643,7 +2248,7 @@ return {ready+inflight, ready, inflight, 0, 0, 0, redis.call('LLEN', KEYS[3]), '
2643
2248
  async function readRedisQueueSnapshot(redisUrl, options = {}) {
2644
2249
  if (!redisUrl.trim())
2645
2250
  throw new TypeError("Queue metrics require a Redis URL.");
2646
- const transport = options.transport ?? resolveRedisQueueTransport();
2251
+ const transport = options.transport ?? "lists";
2647
2252
  if (transport !== "lists" && transport !== "streams")
2648
2253
  throw new TypeError("Invalid queue metrics transport.");
2649
2254
  const timeoutMs = options.timeoutMs ?? 1000;