@getstrata/core 2.2.60 → 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 +22 -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 -256
  278. package/dist/entries/queue/failedJobRepository.js +17 -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 -257
  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 -1386
  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 +10435 -11351
  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,1085 +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 currentSqlDialect2() {
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 = currentSqlDialect2();
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;
851
- }
852
- }
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 { currentSqlDialect, 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 ? {} : {
936
- payload: currentSqlDialect().driver === "pgsql" ? values.payload : JSON.stringify(values.payload)
937
- },
938
- ...values.failed_at === undefined ? {} : { failed_at: sqlTimestamp(values.failed_at) }
939
- };
940
- return hydrateFailure(await super.create(stored));
941
- }
942
- async findAll(options = {}) {
943
- const rows = await super.findAll(options);
944
- return options.select?.length ? rows : rows.map(hydrateFailure);
945
- }
946
- }
947
- function hydrateFailure(row) {
948
- const result = { ...row };
949
- if (typeof row.payload === "string")
950
- result.payload = JSON.parse(row.payload);
951
- if ("failed_at" in row && !(row.failed_at instanceof Date)) {
952
- result.failed_at = new Date(row.failed_at);
953
- }
954
- return result;
955
- }
956
- var failedJobRepository_default = FailedJobRepository;
957
-
958
- // ../../src/core/queue/failedJobService.ts
959
- class FailedJobService {
960
- repository;
961
- constructor(repository) {
962
- this.repository = repository;
963
- }
964
- async recordFailure(input) {
965
- return await this.repository.create({
966
- job_name: input.jobName,
967
- job_id: input.jobId ?? null,
968
- payload: input.payload,
969
- exception: input.exception,
970
- failed_at: new Date
971
- });
972
- }
973
- listRecent(limit = 50) {
974
- return this.repository.findAll({
975
- limit,
976
- orderBy: { column: "failed_at", direction: "DESC" }
977
- });
978
- }
979
- async retry(id, enqueue) {
980
- if (typeof enqueue !== "function")
981
- throw new Error("Failed-job retry requires an admission callback.");
982
- const failedJob = await this.repository.findByIdOrThrow(id, (jobId) => new Error(`Failed job ${jobId} not found.`));
983
- await enqueue(failedJob);
984
- await this.repository.deleteById(id);
985
- return failedJob;
986
- }
987
- async delete(id) {
988
- const deleted = await this.repository.deleteById(id);
989
- if (!deleted) {
990
- throw new Error(`Failed job ${id} not found.`);
991
- }
992
- }
993
- async flush() {
994
- const jobs = await this.repository.findAll();
995
- let deleted = 0;
996
- for (const job of jobs) {
997
- if (await this.repository.deleteById(job.id)) {
998
- deleted += 1;
999
- }
1000
- }
1001
- return deleted;
1002
- }
1003
- }
1004
- var failedJobService_default = FailedJobService;
1005
-
1006
- // ../../src/core/queue/jobRegistry.ts
1007
- class JobRegistry {
1008
- factories = new Map;
1009
- instances = new WeakMap;
1010
- register(name, factory) {
1011
- this.factories.set(name, factory);
1012
- }
1013
- resolveName(job) {
1014
- return this.instances.get(job);
1015
- }
1016
- track(name, job) {
1017
- this.instances.set(job, name);
1018
- return job;
1019
- }
1020
- create(name) {
1021
- const factory = this.factories.get(name);
1022
- if (!factory) {
1023
- return;
1024
- }
1025
- return factory();
1026
- }
1027
- names() {
1028
- return [...this.factories.keys()];
60
+ ancestors.delete(value);
1029
61
  }
1030
62
  }
1031
- var JOB_REGISTRY_KEY = Symbol.for("@getstrata/jobRegistry");
1032
- function readSharedJobRegistry() {
1033
- const globalRegistry = globalThis[JOB_REGISTRY_KEY];
1034
- if (globalRegistry) {
1035
- return globalRegistry;
1036
- }
1037
- const registry = new JobRegistry;
1038
- globalThis[JOB_REGISTRY_KEY] = registry;
1039
- return registry;
1040
- }
1041
- var jobRegistry = readSharedJobRegistry();
1042
-
1043
- // ../../src/core/queue/jobRunner.ts
1044
- import { setTimeout as delay } from "timers/promises";
1045
63
 
1046
64
  // ../../src/core/queue/queueConfig.ts
1047
- function resolveQueueConfig() {
1048
- const driver = process.env.QUEUE_DRIVER;
1049
- const maxAttempts = Number(process.env.QUEUE_MAX_ATTEMPTS ?? "3");
1050
- const backoffMs = Number(process.env.QUEUE_BACKOFF_MS ?? "1000");
1051
- return {
1052
- driver: driver === "redis" || driver === "async" || driver === "sync" ? driver : "sync",
1053
- maxAttempts: Number.isFinite(maxAttempts) && maxAttempts > 0 ? maxAttempts : 3,
1054
- backoffMs: Number.isFinite(backoffMs) && backoffMs >= 0 ? backoffMs : 1000
1055
- };
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 });
1056
83
  }
1057
- var queueConfig = {
1058
- get driver() {
1059
- return resolveQueueConfig().driver;
1060
- },
1061
- get maxAttempts() {
1062
- return resolveQueueConfig().maxAttempts;
1063
- },
1064
- get backoffMs() {
1065
- return resolveQueueConfig().backoffMs;
1066
- }
1067
- };
1068
- function resolveRedisQueueTransport() {
1069
- const transport = process.env.QUEUE_REDIS_TRANSPORT ?? "lists";
84
+ function resolveRedisQueueTransport(env = process.env) {
85
+ const transport = env.QUEUE_REDIS_TRANSPORT ?? "lists";
1070
86
  if (transport !== "lists" && transport !== "streams")
1071
87
  throw new Error('QUEUE_REDIS_TRANSPORT must be "lists" or "streams".');
1072
88
  return transport;
1073
89
  }
1074
- function readQueueVisibilityMs() {
1075
- const value = Number(process.env.QUEUE_VISIBILITY_MS ?? "60000");
1076
- if (!Number.isSafeInteger(value) || value < 30)
1077
- 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.");
1078
94
  return value;
1079
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
+ }
1080
115
 
1081
116
  // ../../src/core/queue/jobRunner.ts
1082
- 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) {
1083
136
  let parsed;
1084
137
  try {
1085
138
  parsed = JSON.parse(rawPayload);
@@ -1092,15 +145,27 @@ function parseQueueJobEnvelope(rawPayload) {
1092
145
  return null;
1093
146
  }
1094
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;
1095
160
  if (typeof envelope.name !== "string" || envelope.name.length === 0) {
1096
161
  console.error("[QueueWorker] Ignoring queue payload without job name");
1097
162
  return null;
1098
163
  }
1099
- if (!jobRegistry.create(envelope.name)) {
164
+ if (!registry.has(envelope.name)) {
1100
165
  console.error(`[QueueWorker] Ignoring unknown job name: ${envelope.name}`);
1101
166
  return null;
1102
167
  }
1103
- 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))) {
1104
169
  console.error("[QueueWorker] Ignoring queue payload with invalid payload object");
1105
170
  return null;
1106
171
  }
@@ -1115,16 +180,18 @@ function parseQueueJobEnvelope(rawPayload) {
1115
180
  return {
1116
181
  ...envelope.cancellable ? { cancellable: true } : {},
1117
182
  ...envelope.deadlineAtMs === undefined ? {} : { deadlineAtMs: envelope.deadlineAtMs },
183
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1118
184
  jobId: envelope.jobId,
1119
185
  name: envelope.name,
1120
186
  payload: envelope.payload ?? {},
1121
187
  attempts: typeof envelope.attempts === "number" ? envelope.attempts : 0
1122
188
  };
1123
189
  }
1124
- 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());
1125
193
  options.context?.signal.throwIfAborted();
1126
- const job = jobRegistry.create(envelope.name);
1127
- if (!job) {
194
+ if (!registry.has(envelope.name)) {
1128
195
  throw new Error(`Unknown job "${envelope.name}".`);
1129
196
  }
1130
197
  const attempts = envelope.attempts ?? 0;
@@ -1132,14 +199,23 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
1132
199
  jobId: envelope.jobId ?? crypto.randomUUID(),
1133
200
  signal: new AbortController().signal
1134
201
  };
202
+ let job;
1135
203
  try {
1136
- 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
+ });
1137
211
  } catch (error) {
1138
212
  options.context?.signal.throwIfAborted();
213
+ if (!job)
214
+ throw error;
1139
215
  const nextAttempt = attempts + 1;
1140
- const maxAttempts = job.maxAttempts ?? queueConfig.maxAttempts;
216
+ const maxAttempts = job.maxAttempts ?? retryPolicy.maxAttempts;
1141
217
  if (nextAttempt < maxAttempts) {
1142
- const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
218
+ const backoffMs = job.backoffMs ?? retryPolicy.backoffMs;
1143
219
  if (options.deferRetry) {
1144
220
  await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
1145
221
  return;
@@ -1148,11 +224,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
1148
224
  await runQueueJob({
1149
225
  ...envelope,
1150
226
  attempts: nextAttempt
1151
- }, failedJobs, { ...options, context });
227
+ }, failedJobs, registry, { ...options, context, retryPolicy });
1152
228
  return;
1153
229
  }
1154
230
  await failedJobs.recordFailure({
1155
231
  jobName: envelope.name,
232
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1156
233
  jobId: context.jobId,
1157
234
  payload: envelope.payload,
1158
235
  exception: error instanceof Error ? error.stack ?? error.message : String(error)
@@ -1162,9 +239,6 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
1162
239
  }
1163
240
  }
1164
241
 
1165
- // ../../src/core/queue/redisQueue.ts
1166
- var {RedisClient: RedisClient2 } = globalThis.Bun;
1167
-
1168
242
  // ../../src/core/queue/redisListScripts.ts
1169
243
  var SCRIPT_HEADER = `
1170
244
  local function checkType(key, expected)
@@ -1323,14 +397,14 @@ function envFlagEnabled(value) {
1323
397
  }
1324
398
 
1325
399
  // ../../src/core/runtime/appKeyPrefix.ts
1326
- function appKeyPrefix() {
1327
- return process.env.APP_KEY_PREFIX?.trim() || "strata";
400
+ function appKeyPrefix(env = process.env) {
401
+ return env.APP_KEY_PREFIX?.trim() || "strata";
1328
402
  }
1329
- function appCookieName(kind) {
1330
- return `${appKeyPrefix()}_${kind}`;
403
+ function appCookieName(kind, env = process.env) {
404
+ return `${appKeyPrefix(env)}_${kind}`;
1331
405
  }
1332
- function appDevSecret(kind) {
1333
- return `${appKeyPrefix()}-dev-${kind}`;
406
+ function appDevSecret(kind, env = process.env) {
407
+ return `${appKeyPrefix(env)}-dev-${kind}`;
1334
408
  }
1335
409
  function requireConfiguredSecret(names, devKind, env = process.env) {
1336
410
  for (const name of names) {
@@ -1342,50 +416,18 @@ function requireConfiguredSecret(names, devKind, env = process.env) {
1342
416
  if (isProductionEnv(env)) {
1343
417
  throw new Error(`${names[0]} must be set outside development. Do not derive secrets from the app name.`);
1344
418
  }
1345
- return appDevSecret(devKind);
419
+ return appDevSecret(devKind, env);
1346
420
  }
1347
421
  function namespacedRedisKey(kind) {
1348
422
  return `${appKeyPrefix()}:${kind}`;
1349
423
  }
1350
- function smtpEhloHost() {
1351
- 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`;
1352
426
  const safe = raw.replace(/[^a-zA-Z0-9.-]/g, "");
1353
427
  return safe || "strata.local";
1354
428
  }
1355
- function siemEventType() {
1356
- return process.env.SIEM_EVENT_TYPE?.trim() || `${appKeyPrefix()}.audit`;
1357
- }
1358
- function appUserAgent() {
1359
- return process.env.APP_USER_AGENT?.trim() || appKeyPrefix();
1360
- }
1361
- function otelServiceName() {
1362
- return process.env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix()}-api`;
1363
- }
1364
- function webhookSignatureHeader() {
1365
- return process.env.WEBHOOK_SIGNATURE_HEADER?.trim() || `x-${appKeyPrefix()}-signature`;
1366
- }
1367
- function appDisplayName() {
1368
- return process.env.APP_NAME?.trim() || "Strata";
1369
- }
1370
- function appEnv() {
1371
- return process.env.APP_ENV?.trim() || "local";
1372
- }
1373
- function appUrl() {
1374
- return (process.env.APP_URL?.trim() || "http://localhost:3000").replace(/\/$/, "");
1375
- }
1376
- function apiPrefix() {
1377
- const raw = process.env.API_PREFIX?.trim() || "/api/v1";
1378
- const withSlash = raw.startsWith("/") ? raw : `/${raw}`;
1379
- const trimmed = withSlash.replace(/\/+$/, "");
1380
- return trimmed || "/api/v1";
1381
- }
1382
- function sdkClientClassName() {
1383
- const override = process.env.APP_SDK_CLASS?.trim();
1384
- if (override && /^[A-Za-z_][A-Za-z0-9_]*$/.test(override)) {
1385
- return override;
1386
- }
1387
- const fromName = appDisplayName().replace(/[^A-Za-z0-9]/g, "");
1388
- return fromName ? `${fromName}Client` : "AppClient";
429
+ function otelServiceName(env = process.env) {
430
+ return env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix(env)}-api`;
1389
431
  }
1390
432
 
1391
433
  // ../../src/core/queue/redisQueueKeys.ts
@@ -1398,25 +440,19 @@ function queueHighKey() {
1398
440
  function queueLowKey() {
1399
441
  return namespacedRedisKey("queue:low");
1400
442
  }
1401
- var QUEUE_LIST_KEY = queueListKey();
1402
- var QUEUE_HIGH_KEY = queueHighKey();
1403
- 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";
1404
446
  function defaultQueueKeys() {
1405
447
  return [queueHighKey(), queueListKey(), queueLowKey()];
1406
448
  }
1407
- var QUEUE_KEYS = defaultQueueKeys();
1408
- function queueKeyForPriority(priority = "default") {
1409
- switch (priority) {
1410
- case "high":
1411
- return queueHighKey();
1412
- case "low":
1413
- return queueLowKey();
1414
- default:
1415
- return queueListKey();
1416
- }
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];
1417
452
  }
1418
453
 
1419
454
  // ../../src/core/queue/redisStreams.ts
455
+ import { QueueHandoffError } from "@getstrata/core/queue/queueHandoff";
1420
456
  var {RedisClient } = globalThis.Bun;
1421
457
 
1422
458
  // ../../src/core/queue/redisStreamsScripts.ts
@@ -1629,7 +665,7 @@ function watchStreamDeadline(client, deadlineAtMs, remainingMs, expire, fail) {
1629
665
  clearTimeout(timer);
1630
666
  };
1631
667
  }
1632
- async function scheduleStreamRetry(client, key, reservation, envelope, delayMs) {
668
+ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1633
669
  if (!Number.isSafeInteger(delayMs) || delayMs < 0 || delayMs > Number.MAX_SAFE_INTEGER / 2)
1634
670
  throw new Error("Invalid Streams retry delay");
1635
671
  const scheduled = await client.send("EVAL", [
@@ -1641,7 +677,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
1641
677
  STREAM_GROUP,
1642
678
  reservation.id,
1643
679
  reservation.owner,
1644
- String(readQueueVisibilityMs()),
680
+ String(visibilityMs),
1645
681
  String(delayMs),
1646
682
  JSON.stringify(envelope),
1647
683
  envelope.deadlineAtMs === undefined ? "" : String(envelope.deadlineAtMs),
@@ -1650,7 +686,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
1650
686
  if (Number(scheduled) !== 1)
1651
687
  throw new Error("Queue lease lost while scheduling retry");
1652
688
  }
1653
- async function pollStreamPriorities(client, queueKeys, owner, cursors) {
689
+ async function pollStreamPriorities(client, queueKeys, owner, cursors, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1654
690
  const keys = [];
1655
691
  for (const key of queueKeys)
1656
692
  keys.push(streamQueueKey(key), streamRetryKey(key));
@@ -1660,7 +696,7 @@ async function pollStreamPriorities(client, queueKeys, owner, cursors) {
1660
696
  ...keys,
1661
697
  STREAM_GROUP,
1662
698
  owner,
1663
- String(readQueueVisibilityMs()),
699
+ String(visibilityMs),
1664
700
  ...cursors
1665
701
  ]);
1666
702
  if (!Array.isArray(raw))
@@ -1711,7 +747,7 @@ async function ensureStreamGroup(client, key) {
1711
747
  throw error;
1712
748
  }
1713
749
  }
1714
- async function updateStreamReservation(client, key, reservation, action, controlKey) {
750
+ async function updateStreamReservation(client, key, reservation, action, controlKey, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1715
751
  return Number(await client.send("EVAL", [
1716
752
  UPDATE_STREAM,
1717
753
  "3",
@@ -1721,41 +757,46 @@ async function updateStreamReservation(client, key, reservation, action, control
1721
757
  STREAM_GROUP,
1722
758
  reservation.id,
1723
759
  reservation.owner,
1724
- String(readQueueVisibilityMs()),
760
+ String(visibilityMs),
1725
761
  action,
1726
762
  reservation.payload,
1727
763
  controlKey ? "1" : "0"
1728
764
  ])) === 1;
1729
765
  }
1730
766
  class RedisStreamsQueue {
767
+ registry;
768
+ queueKeys;
1731
769
  client;
1732
- constructor(redisUrl) {
770
+ constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
771
+ this.registry = registry;
772
+ this.queueKeys = queueKeys;
773
+ this.queueKeys = Object.freeze([...queueKeys]);
1733
774
  this.client = new RedisClient(redisUrl);
1734
775
  }
1735
776
  close() {
1736
777
  this.client.close();
1737
778
  }
1738
- async dispatch(job, payload) {
1739
- await this.enqueue(job, payload);
779
+ async dispatch(job, payload, options) {
780
+ await this.admit(job, payload, {}, undefined, options?.handoff);
1740
781
  }
1741
782
  async enqueue(job, payload, options = {}) {
1742
783
  return this.admit(job, payload, options);
1743
784
  }
1744
- async replay(job, payload, jobId) {
1745
- 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)}/`));
1746
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)))
1747
788
  throw new Error("Replay identity does not belong to this Streams namespace");
1748
- await this.admit(job, payload, {}, jobId);
789
+ await this.admit(job, payload, {}, jobId, options?.handoff);
1749
790
  }
1750
- async admit(job, payload, options, replayId) {
791
+ async admit(job, payload, options, replayId, handoff) {
1751
792
  if (options.cancellable !== undefined && typeof options.cancellable !== "boolean")
1752
793
  throw new Error("cancellable must be a boolean");
1753
794
  if (options.timeoutMs !== undefined && (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs <= 0 || options.timeoutMs > MAX_TIMER_MS))
1754
795
  throw new Error("Streams timeoutMs must be an integer between 1 and 2147483647");
1755
- const name = jobRegistry.resolveName(job);
796
+ const name = this.registry.resolveName(job);
1756
797
  if (!name)
1757
798
  throw new Error("Job is not registered with the queue worker registry.");
1758
- const key = queueKeyForPriority(job.priority);
799
+ const key = queueKeyForPriority(job.priority, this.queueKeys);
1759
800
  await assertLegacyDrained(this.client, key);
1760
801
  const jobId = replayId ?? `${streamQueueKey(key)}/${crypto.randomUUID()}`;
1761
802
  const deadlineAtMs = options.timeoutMs === undefined ? undefined : await readStreamTime(this.client) + options.timeoutMs;
@@ -1763,6 +804,7 @@ class RedisStreamsQueue {
1763
804
  name,
1764
805
  payload,
1765
806
  attempts: 0,
807
+ ...handoff === undefined ? {} : { handoff },
1766
808
  jobId,
1767
809
  deadlineAtMs,
1768
810
  ...options.cancellable ? { cancellable: true } : {}
@@ -1780,7 +822,7 @@ class RedisStreamsQueue {
1780
822
  return jobId;
1781
823
  }
1782
824
  async cancel(jobId) {
1783
- const key = defaultQueueKeys().find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
825
+ const key = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
1784
826
  if (!key)
1785
827
  throw new Error("Invalid cancellable Streams job ID");
1786
828
  return cancelStreamJob(this.client, key, jobId);
@@ -1790,8 +832,11 @@ class RedisStreamsQueue {
1790
832
  class RedisStreamsWorker {
1791
833
  redisUrl;
1792
834
  failedJobs;
835
+ registry;
1793
836
  timeoutSeconds;
1794
837
  queueKeys;
838
+ visibilityMs;
839
+ retryPolicy;
1795
840
  client;
1796
841
  owner = crypto.randomUUID();
1797
842
  cursors = new Map;
@@ -1800,12 +845,16 @@ class RedisStreamsWorker {
1800
845
  running = false;
1801
846
  stopping = false;
1802
847
  active;
1803
- constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = defaultQueueKeys()) {
848
+ constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
1804
849
  this.redisUrl = redisUrl;
1805
850
  this.failedJobs = failedJobs;
851
+ this.registry = registry;
1806
852
  this.timeoutSeconds = timeoutSeconds;
1807
853
  this.queueKeys = queueKeys;
1808
- readQueueVisibilityMs();
854
+ this.visibilityMs = visibilityMs;
855
+ this.queueKeys = Object.freeze([...queueKeys]);
856
+ this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
857
+ readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
1809
858
  this.client = new RedisClient(redisUrl);
1810
859
  }
1811
860
  requestStop() {
@@ -1844,7 +893,7 @@ class RedisStreamsWorker {
1844
893
  do {
1845
894
  if (this.stopping)
1846
895
  return false;
1847
- 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);
1848
897
  this.queueKeys.forEach((key, index) => {
1849
898
  const cursor = polled.cursors[index];
1850
899
  if (cursor)
@@ -1864,9 +913,15 @@ class RedisStreamsWorker {
1864
913
  return false;
1865
914
  }
1866
915
  async handle(key, reservation) {
1867
- const envelope = parseQueueJobEnvelope(reservation.payload);
916
+ const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
1868
917
  if (!envelope) {
1869
- 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);
1870
925
  return;
1871
926
  }
1872
927
  envelope.jobId ??= `${streamQueueKey(key)}/${reservation.id}`;
@@ -1875,7 +930,7 @@ class RedisStreamsWorker {
1875
930
  try {
1876
931
  controlKey = streamControlKey(key, envelope.jobId);
1877
932
  } catch {
1878
- await updateStreamReservation(this.client, key, reservation, "quarantine");
933
+ await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
1879
934
  return;
1880
935
  }
1881
936
  }
@@ -1884,10 +939,11 @@ class RedisStreamsWorker {
1884
939
  await this.failedJobs.recordFailure({
1885
940
  jobName: envelope.name,
1886
941
  jobId: envelope.jobId,
942
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1887
943
  payload: envelope.payload,
1888
944
  exception: cancellationError.message
1889
945
  });
1890
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
946
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
1891
947
  return;
1892
948
  }
1893
949
  const remainingMs = envelope.deadlineAtMs === undefined ? undefined : envelope.deadlineAtMs - await readStreamTime(this.client);
@@ -1896,15 +952,17 @@ class RedisStreamsWorker {
1896
952
  await this.failedJobs.recordFailure({
1897
953
  jobName: envelope.name,
1898
954
  jobId: envelope.jobId,
955
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1899
956
  payload: envelope.payload,
1900
957
  exception: deadlineError.message
1901
958
  });
1902
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
959
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
1903
960
  return;
1904
961
  }
1905
962
  const controller = new AbortController;
1906
963
  this.active = controller;
1907
964
  let acknowledge = false;
965
+ let invalidHandoff = false;
1908
966
  let released = false;
1909
967
  let ownershipLost = false;
1910
968
  let renewal;
@@ -1924,7 +982,7 @@ class RedisStreamsWorker {
1924
982
  return;
1925
983
  renewal = (async () => {
1926
984
  try {
1927
- if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey))
985
+ if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey, this.visibilityMs))
1928
986
  loseOwnership(new Error("Queue lease lost"));
1929
987
  else if (controlKey && await readStreamCancellation(this.client, controlKey)) {
1930
988
  cancellationRequested = true;
@@ -1937,9 +995,10 @@ class RedisStreamsWorker {
1937
995
  })().finally(() => {
1938
996
  renewal = undefined;
1939
997
  });
1940
- }, Math.max(1, Math.floor(readQueueVisibilityMs() / 3)));
998
+ }, Math.max(1, Math.floor(this.visibilityMs / 3)));
1941
999
  try {
1942
- await runQueueJob(envelope, this.failedJobs, {
1000
+ await runQueueJob(envelope, this.failedJobs, this.registry, {
1001
+ retryPolicy: this.retryPolicy,
1943
1002
  context: {
1944
1003
  jobId: envelope.jobId,
1945
1004
  signal: controller.signal,
@@ -1947,7 +1006,7 @@ class RedisStreamsWorker {
1947
1006
  },
1948
1007
  deferRetry: async (next, delayMs) => {
1949
1008
  controller.signal.throwIfAborted();
1950
- await scheduleStreamRetry(this.client, key, reservation, next, delayMs);
1009
+ await scheduleStreamRetry(this.client, key, reservation, next, delayMs, this.visibilityMs);
1951
1010
  released = true;
1952
1011
  },
1953
1012
  onFailureRecorded: () => {
@@ -1957,18 +1016,26 @@ class RedisStreamsWorker {
1957
1016
  });
1958
1017
  acknowledge = true;
1959
1018
  } catch (error) {
1960
- console.error("[StreamsWorker] Job failed:", error);
1019
+ if (error instanceof QueueHandoffError)
1020
+ invalidHandoff = true;
1021
+ else
1022
+ console.error("[StreamsWorker] Job failed:", error);
1961
1023
  } finally {
1962
1024
  stopDeadlineWatch?.();
1963
1025
  clearInterval(heartbeat);
1964
1026
  await renewal;
1965
1027
  this.active = undefined;
1966
1028
  }
1029
+ if (invalidHandoff && !ownershipLost && !released) {
1030
+ await updateStreamReservation(this.client, key, reservation, "quarantine", controlKey, this.visibilityMs);
1031
+ return;
1032
+ }
1967
1033
  if (!ownershipLost && !released && controlKey && (cancellationRequested || await readStreamCancellation(this.client, controlKey))) {
1968
1034
  if (!failureRecorded)
1969
1035
  await this.failedJobs.recordFailure({
1970
1036
  jobName: envelope.name,
1971
1037
  jobId: envelope.jobId,
1038
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1972
1039
  payload: envelope.payload,
1973
1040
  exception: cancellationError.message
1974
1041
  });
@@ -1980,13 +1047,14 @@ class RedisStreamsWorker {
1980
1047
  await this.failedJobs.recordFailure({
1981
1048
  jobName: envelope.name,
1982
1049
  jobId: envelope.jobId,
1050
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
1983
1051
  payload: envelope.payload,
1984
1052
  exception: deadlineError.message
1985
1053
  });
1986
1054
  acknowledge = true;
1987
1055
  }
1988
1056
  if (acknowledge && !ownershipLost && !released)
1989
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
1057
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
1990
1058
  }
1991
1059
  async run() {
1992
1060
  this.running = true;
@@ -2018,7 +1086,7 @@ async function migrateLegacyQueueToStreams(client, options) {
2018
1086
  if (!Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 1000)
2019
1087
  throw new Error("Queue conversion batchSize must be between 1 and 1000.");
2020
1088
  let converted = 0;
2021
- for (const key of options.queueKeys ?? defaultQueueKeys())
1089
+ for (const key of [...options.queueKeys ?? QUEUE_KEYS])
2022
1090
  converted += Number(await client.send("EVAL", [
2023
1091
  CONVERT_LIST,
2024
1092
  "4",
@@ -2051,7 +1119,7 @@ function reservationExpired(lease, now, visibilityMs) {
2051
1119
  function queueInvalidKey(queueKey) {
2052
1120
  return `${queueKey}:invalid`;
2053
1121
  }
2054
- async function reserveQueueJob(client, queueKey, owner) {
1122
+ async function reserveQueueJob(client, queueKey, owner, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
2055
1123
  const raw = await client.send("EVAL", [
2056
1124
  RESERVE_JOB,
2057
1125
  "3",
@@ -2060,14 +1128,14 @@ async function reserveQueueJob(client, queueKey, owner) {
2060
1128
  queueProcessingLeaseKey(queueKey),
2061
1129
  crypto.randomUUID(),
2062
1130
  owner,
2063
- String(readQueueVisibilityMs())
1131
+ String(visibilityMs)
2064
1132
  ]);
2065
1133
  if (typeof raw !== "string")
2066
1134
  return null;
2067
1135
  const reservation = JSON.parse(raw);
2068
1136
  return { ...reservation, record: raw };
2069
1137
  }
2070
- async function updateQueueReservation(client, queueKey, reservation, action) {
1138
+ async function updateQueueReservation(client, queueKey, reservation, action, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
2071
1139
  return Number(await client.send("EVAL", [
2072
1140
  RENEW_OR_ACK,
2073
1141
  "3",
@@ -2078,11 +1146,11 @@ async function updateQueueReservation(client, queueKey, reservation, action) {
2078
1146
  reservation.owner,
2079
1147
  reservation.record,
2080
1148
  action,
2081
- String(readQueueVisibilityMs()),
1149
+ String(visibilityMs),
2082
1150
  reservation.payload
2083
1151
  ])) === 1;
2084
1152
  }
2085
- async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim) {
1153
+ async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
2086
1154
  const keys = [];
2087
1155
  for (const key of queueKeys) {
2088
1156
  keys.push(key, queueProcessingKey(key), queueProcessingLeaseKey(key));
@@ -2092,7 +1160,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
2092
1160
  String(keys.length),
2093
1161
  ...keys,
2094
1162
  owner,
2095
- String(readQueueVisibilityMs()),
1163
+ String(visibilityMs),
2096
1164
  reservationId,
2097
1165
  reclaim ? "1" : "0"
2098
1166
  ]);
@@ -2117,7 +1185,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
2117
1185
  const reservation = JSON.parse(record);
2118
1186
  return { queueKey, reservation: { ...reservation, record } };
2119
1187
  }
2120
- async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
1188
+ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
2121
1189
  let reclaimed = 0;
2122
1190
  for (const key of queueKeys) {
2123
1191
  reclaimed += Number(await client.send("EVAL", [
@@ -2126,31 +1194,46 @@ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
2126
1194
  key,
2127
1195
  queueProcessingKey(key),
2128
1196
  queueProcessingLeaseKey(key),
2129
- String(readQueueVisibilityMs())
1197
+ String(visibilityMs)
2130
1198
  ]));
2131
1199
  }
2132
1200
  return reclaimed;
2133
1201
  }
2134
1202
 
2135
1203
  class RedisQueue {
1204
+ registry;
1205
+ queueKeys;
2136
1206
  client;
2137
- constructor(redisUrl) {
1207
+ constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
1208
+ this.registry = registry;
1209
+ this.queueKeys = queueKeys;
1210
+ this.queueKeys = Object.freeze([...queueKeys]);
2138
1211
  this.client = new RedisClient2(redisUrl);
2139
1212
  }
2140
1213
  close() {
2141
1214
  this.client.close();
2142
1215
  }
2143
- async dispatch(job, payload) {
2144
- 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);
2145
1226
  if (!name) {
2146
1227
  throw new Error("Job is not registered with the queue worker registry.");
2147
1228
  }
2148
1229
  const envelope = {
2149
1230
  name,
1231
+ jobId: jobId ?? crypto.randomUUID(),
2150
1232
  payload,
2151
- attempts: 0
1233
+ attempts: 0,
1234
+ ...options?.handoff === undefined ? {} : { handoff: options.handoff }
2152
1235
  };
2153
- const queueKey = queueKeyForPriority(job.priority);
1236
+ const queueKey = queueKeyForPriority(job.priority, this.queueKeys);
2154
1237
  await this.client.lpush(queueKey, JSON.stringify(envelope));
2155
1238
  }
2156
1239
  }
@@ -2158,17 +1241,25 @@ class RedisQueue {
2158
1241
  class QueueWorker {
2159
1242
  redisUrl;
2160
1243
  failedJobs;
1244
+ registry;
2161
1245
  timeoutSeconds;
2162
1246
  queueKeys;
1247
+ visibilityMs;
1248
+ retryPolicy;
2163
1249
  owner = crypto.randomUUID();
2164
1250
  running = false;
2165
1251
  stopping = false;
2166
1252
  client;
2167
- constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = QUEUE_KEYS) {
1253
+ constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
2168
1254
  this.redisUrl = redisUrl;
2169
1255
  this.failedJobs = failedJobs;
1256
+ this.registry = registry;
2170
1257
  this.timeoutSeconds = timeoutSeconds;
2171
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) });
2172
1263
  this.client = new RedisClient2(redisUrl);
2173
1264
  }
2174
1265
  requestStop() {
@@ -2183,7 +1274,7 @@ class QueueWorker {
2183
1274
  do {
2184
1275
  if (this.stopping)
2185
1276
  return null;
2186
- 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);
2187
1278
  reclaim = false;
2188
1279
  if (reserved)
2189
1280
  return reserved;
@@ -2200,12 +1291,13 @@ class QueueWorker {
2200
1291
  if (!reserved)
2201
1292
  return false;
2202
1293
  const { queueKey, reservation } = reserved;
2203
- const envelope = parseQueueJobEnvelope(reservation.payload);
1294
+ const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
2204
1295
  if (!envelope) {
2205
- await updateQueueReservation(this.client, queueKey, reservation, "quarantine");
1296
+ await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
2206
1297
  return true;
2207
1298
  }
2208
1299
  let acknowledge = false;
1300
+ let invalidHandoff = false;
2209
1301
  let ownershipLost = false;
2210
1302
  let renewal;
2211
1303
  const heartbeat = setInterval(() => {
@@ -2213,7 +1305,7 @@ class QueueWorker {
2213
1305
  return;
2214
1306
  renewal = (async () => {
2215
1307
  try {
2216
- if (!await updateQueueReservation(this.client, queueKey, reservation, "renew")) {
1308
+ if (!await updateQueueReservation(this.client, queueKey, reservation, "renew", this.visibilityMs)) {
2217
1309
  ownershipLost = true;
2218
1310
  }
2219
1311
  } catch (error) {
@@ -2223,22 +1315,30 @@ class QueueWorker {
2223
1315
  })().finally(() => {
2224
1316
  renewal = undefined;
2225
1317
  });
2226
- }, Math.max(1, Math.floor(readQueueVisibilityMs() / 3)));
1318
+ }, Math.max(1, Math.floor(this.visibilityMs / 3)));
2227
1319
  try {
2228
- await runQueueJob(envelope, this.failedJobs, {
1320
+ await runQueueJob(envelope, this.failedJobs, this.registry, {
1321
+ retryPolicy: this.retryPolicy,
2229
1322
  onFailureRecorded: () => {
2230
1323
  acknowledge = true;
2231
1324
  }
2232
1325
  });
2233
1326
  acknowledge = true;
2234
1327
  } catch (error) {
2235
- console.error("[QueueWorker] Job failed:", error);
1328
+ if (error instanceof QueueHandoffError2)
1329
+ invalidHandoff = true;
1330
+ else
1331
+ console.error("[QueueWorker] Job failed:", error);
2236
1332
  } finally {
2237
1333
  clearInterval(heartbeat);
2238
1334
  await renewal;
2239
1335
  }
1336
+ if (invalidHandoff && !ownershipLost) {
1337
+ await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
1338
+ return true;
1339
+ }
2240
1340
  if (acknowledge && !ownershipLost) {
2241
- await updateQueueReservation(this.client, queueKey, reservation, "ack");
1341
+ await updateQueueReservation(this.client, queueKey, reservation, "ack", this.visibilityMs);
2242
1342
  }
2243
1343
  return true;
2244
1344
  }
@@ -2267,12 +1367,13 @@ class QueueWorker {
2267
1367
  this.client.close();
2268
1368
  }
2269
1369
  }
2270
- async function countPendingQueueJobs(redisUrl) {
1370
+ async function countPendingQueueJobs(redisUrl, config = resolveQueueRuntimeConfig({})) {
1371
+ const { transport, queueKeys } = snapshotQueueRuntimeConfig(config);
2271
1372
  const client = new RedisClient2(redisUrl);
2272
1373
  try {
2273
1374
  let total = 0;
2274
- for (const queueKey of resolveRedisQueueTransport() === "streams" ? defaultQueueKeys() : QUEUE_KEYS) {
2275
- 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);
2276
1377
  }
2277
1378
  return total;
2278
1379
  } finally {
@@ -2280,195 +1381,744 @@ async function countPendingQueueJobs(redisUrl) {
2280
1381
  }
2281
1382
  }
2282
1383
 
2283
- // ../../src/core/queue/index.ts
2284
- class Job {
2285
- static jobName;
2286
- maxAttempts;
2287
- backoffMs;
2288
- priority;
2289
- constructor() {
2290
- const jobName = this.constructor.jobName;
2291
- if (jobName) {
2292
- 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.");
2293
1491
  }
1492
+ clauses.push(`${column} @@ plainto_tsquery('english', ${pushParam(dialect, params, operator.tsMatch)})`);
2294
1493
  }
1494
+ return clauses;
2295
1495
  }
2296
-
2297
- class SyncQueue {
2298
- async dispatch(job, payload) {
2299
- 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)}`);
2300
1516
  }
1517
+ return clauses.join(" AND ");
2301
1518
  }
2302
-
2303
- class AsyncQueue {
2304
- active = new Set;
2305
- closing;
2306
- closed = false;
2307
- assertOpen() {
2308
- if (this.closed)
2309
- throw new Error("Async queue is closed.");
2310
- }
2311
- enqueue(task, onError) {
2312
- this.assertOpen();
2313
- const running = new Promise((resolve) => {
2314
- setTimeout(() => {
2315
- Promise.resolve().then(task).catch(onError).finally(resolve);
2316
- }, 0);
2317
- });
2318
- this.active.add(running);
2319
- running.then(() => {
2320
- this.active.delete(running);
2321
- });
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}`;
2322
1564
  }
2323
- async dispatch(job, payload) {
2324
- this.enqueue(() => job.handle(payload), (error) => {
2325
- console.error("[AsyncQueue] Job failed:", error);
2326
- });
1565
+ if (!result) {
1566
+ return "";
2327
1567
  }
2328
- close() {
2329
- this.closing ??= (async () => {
2330
- while (this.active.size)
2331
- await Promise.all([...this.active]);
2332
- this.closed = true;
2333
- })();
2334
- 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 });
2335
1574
  }
1575
+ nodes.push(...whereNodes);
1576
+ const combined = buildWhereGroupClause(dialect, tableName, nodes, params);
1577
+ return {
1578
+ clause: combined ? ` WHERE ${combined}` : "",
1579
+ params
1580
+ };
2336
1581
  }
2337
- function createQueue(driver) {
2338
- 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";
2339
1590
  }
2340
-
2341
- // ../../src/core/queue/resilientQueue.ts
2342
- class ResilientQueue extends AsyncQueue {
2343
- failedJobs;
2344
- asyncDispatch;
2345
- constructor(failedJobs, asyncDispatch = false) {
2346
- super();
2347
- this.failedJobs = failedJobs;
2348
- this.asyncDispatch = asyncDispatch;
1591
+ function appendSoftDeleteScope(dialect, table, options, clauses) {
1592
+ const column = resolveSoftDeleteColumn(table);
1593
+ if (!column) {
1594
+ return;
2349
1595
  }
2350
- async dispatch(job, payload) {
2351
- this.assertOpen();
2352
- const name = jobRegistry.resolveName(job);
2353
- if (!name) {
2354
- 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.");
2355
1641
  }
2356
- const envelope = {
2357
- name,
2358
- payload,
2359
- attempts: 0
2360
- };
2361
- if (this.asyncDispatch) {
2362
- this.enqueue(() => runQueueJob(envelope, this.failedJobs), (error) => {
2363
- console.error("[ResilientQueue] Job failed:", error);
2364
- });
2365
- 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}`;
2366
1648
  }
2367
- 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 "";
2368
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(", ")}` : "";
2369
1660
  }
2370
-
2371
- // ../../src/core/queue/publicQueue.ts
2372
- function createFailedJobService() {
2373
- return new failedJobService_default(new failedJobRepository_default);
2374
- }
2375
- function createTrackedJob(name, job) {
2376
- return jobRegistry.track(name, job);
2377
- }
2378
- function createProductionQueue(driver, options = {}) {
2379
- options.registerJobs?.();
2380
- const failedJobs = options.failedJobs ?? createFailedJobService();
2381
- if (driver === "redis") {
2382
- if (!options.redisUrl) {
2383
- 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;
2384
1704
  }
2385
- 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.`);
2386
1877
  }
2387
- 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
+ };
2388
1886
  }
2389
- function createQueueWorker(redisUrl, failedJobs = createFailedJobService()) {
2390
- 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
+ };
2391
1907
  }
2392
-
2393
- // ../../src/core/queue/createAppQueue.ts
2394
- var FAILED_JOB_SERVICE_TOKEN = "core.failedJobs";
2395
- function createAppQueue(driver, redisUrl, failedJobs = createFailedJobService(), registerJobs) {
2396
- return createProductionQueue(driver, {
2397
- redisUrl,
2398
- failedJobs,
2399
- 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]
2400
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
+ };
2401
2033
  }
2402
2034
 
2403
- // ../../src/core/queue/failedJobMetrics.ts
2404
- 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
+ });
2405
2043
 
2406
- // ../../src/core/database/readOnlyCollector.ts
2407
- init_connectionContext();
2408
- init_dialect();
2409
- init_repositoryConnection();
2410
- init_transaction();
2411
- 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;
2412
2071
  if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 5000)
2413
- 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.");
2414
2073
  let active;
2415
2074
  let closed = false;
2416
2075
  return {
2417
2076
  async collect() {
2418
2077
  if (closed)
2419
- throw new Error(`${name} collector is closed.`);
2420
- if (hasActiveTransaction() || hasActiveDatabaseConnection())
2421
- throw new Error(`${name} require an independent transaction.`);
2422
- const driver = currentSqlDialect2().driver;
2423
- const pool = resolveRepositoryConnection();
2424
- if (driver !== "pgsql" && driver !== "sqlite" && driver !== "mysql")
2425
- throw new Error(`${name} require Postgres or an interruptible SQLite/MySQL adapter.`);
2078
+ throw new Error("Failed-job metrics collector is closed.");
2426
2079
  if (active)
2427
- throw new Error(`${name} collection is still settling.`);
2428
- const deadline = performance.now() + timeoutMs;
2429
- let expired = false;
2430
- const acquisition = new AbortController;
2431
- let timer;
2432
- const remaining = () => {
2433
- const value = Math.floor(deadline - performance.now());
2434
- if (expired || value < 1)
2435
- throw new Error(`${name} collection timed out.`);
2436
- return value;
2437
- };
2438
- const work = driver === "pgsql" ? runInTransaction(async () => {
2439
- await repositoryConnection.unsafe("SELECT set_config('statement_timeout', $1, true)", [
2440
- `${remaining()}ms`
2441
- ]);
2442
- await repositoryConnection.unsafe("SET TRANSACTION READ ONLY");
2443
- remaining();
2444
- 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.");
2445
2097
  remaining();
2446
- return result;
2447
- }, { acquisitionSignal: acquisition.signal }) : (async () => {
2448
- if (!pool.observeReadOnly)
2449
- throw new Error(`${name} require Postgres or an interruptible SQLite/MySQL adapter.`);
2450
- return pool.observeReadOnly(async (connection) => await runWithDatabaseConnection(connection, async () => {
2451
- remaining();
2452
- const result = await collect(remaining);
2453
- remaining();
2454
- return result;
2455
- }), { signal: acquisition.signal, timeoutMs: remaining() });
2456
- })();
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
+ });
2457
2111
  active = work;
2458
2112
  work.finally(() => {
2459
2113
  if (active === work)
2460
2114
  active = undefined;
2461
2115
  }).catch(() => {});
2116
+ let timer;
2462
2117
  try {
2463
2118
  return await Promise.race([
2464
2119
  work,
2465
2120
  new Promise((_, reject) => {
2466
- timer = setTimeout(() => {
2467
- expired = true;
2468
- const error = new Error(`${name} collection timed out.`);
2469
- acquisition.abort(error);
2470
- reject(error);
2471
- }, timeoutMs);
2121
+ timer = setTimeout(() => reject(new Error("Failed-job metrics collection timed out.")), timeoutMs);
2472
2122
  })
2473
2123
  ]);
2474
2124
  } finally {
@@ -2482,54 +2132,6 @@ function createReadOnlyCollector(name, timeoutMs, collect) {
2482
2132
  }
2483
2133
  };
2484
2134
  }
2485
-
2486
- // ../../src/core/queue/failedJobMetrics.ts
2487
- init_repositoryConnection();
2488
- var schemaQuery = `SELECT c.relkind = 'r' AND c.relpersistence = 'p' AND NOT c.relrowsecurity AND EXISTS (
2489
- SELECT 1 FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = i.indkey[0]
2490
- WHERE i.indrelid = c.oid AND i.indisprimary AND i.indisvalid AND i.indisready
2491
- AND i.indnkeyatts = 1 AND a.attname = 'id' AND NOT a.attisdropped
2492
- AND a.atttypid IN ('int2'::regtype, 'int4'::regtype, 'int8'::regtype)
2493
- ) AS supported FROM pg_class c WHERE c.oid = 'failed_job'::regclass`;
2494
- var sqliteSchemaQuery = `SELECT 1 AS supported FROM main.sqlite_schema
2495
- WHERE name='failed_job' AND type='table' AND rootpage > 0
2496
- AND (SELECT COUNT(*) FROM pragma_table_xinfo('failed_job', 'main') WHERE pk > 0) = 1
2497
- AND EXISTS (SELECT 1 FROM pragma_table_xinfo('failed_job', 'main')
2498
- WHERE name='id' AND pk=1 AND upper(type)='INTEGER' AND hidden=0) LIMIT 1`;
2499
- var mysqlSchemaQuery = `SELECT 1 AS supported FROM information_schema.TABLES t
2500
- WHERE t.TABLE_SCHEMA=DATABASE() AND t.TABLE_NAME='failed_job' AND t.TABLE_TYPE='BASE TABLE' AND t.ENGINE='InnoDB'
2501
- AND (SELECT COUNT(*) FROM information_schema.STATISTICS i WHERE i.TABLE_SCHEMA=t.TABLE_SCHEMA
2502
- AND i.TABLE_NAME=t.TABLE_NAME AND i.INDEX_NAME='PRIMARY')=1
2503
- AND EXISTS(SELECT 1 FROM information_schema.STATISTICS i JOIN information_schema.COLUMNS c
2504
- ON c.TABLE_SCHEMA=i.TABLE_SCHEMA AND c.TABLE_NAME=i.TABLE_NAME AND c.COLUMN_NAME=i.COLUMN_NAME
2505
- WHERE i.TABLE_SCHEMA=t.TABLE_SCHEMA AND i.TABLE_NAME=t.TABLE_NAME AND i.INDEX_NAME='PRIMARY'
2506
- AND i.SEQ_IN_INDEX=1 AND i.COLUMN_NAME='id' AND c.DATA_TYPE IN ('tinyint','smallint','mediumint','int','bigint')) LIMIT 1`;
2507
- function createFailedJobMetricsCollector(options = {}) {
2508
- const sampleLimit = options.sampleLimit ?? 500;
2509
- if (!Number.isSafeInteger(sampleLimit) || sampleLimit < 1 || sampleLimit > 1000)
2510
- throw new TypeError("Failed-job metrics sample limit must be between 1 and 1000.");
2511
- const repository = new failedJobRepository_default;
2512
- return createReadOnlyCollector("Failed-job metrics", options.timeoutMs ?? 1000, async (remaining) => {
2513
- const driver = currentSqlDialect2().driver;
2514
- const postgres = driver === "pgsql";
2515
- if (postgres)
2516
- await repositoryConnection.unsafe('LOCK TABLE "failed_job" IN ACCESS SHARE MODE');
2517
- if (driver === "mysql")
2518
- await repositoryConnection.unsafe("SELECT id FROM failed_job LIMIT 0");
2519
- const schema = await repositoryConnection.unsafe(postgres ? schemaQuery : driver === "mysql" ? mysqlSchemaQuery : sqliteSchemaQuery);
2520
- if (schema.length !== 1 || schema[0]?.supported !== (postgres ? true : 1))
2521
- throw new Error("Failed-job metrics require the standard global table with an ID primary key and no RLS.");
2522
- remaining();
2523
- const rows = await repository.query().orderBy({ column: "id", direction: "ASC" }).project(["id"], sampleLimit + 1);
2524
- if (rows.length > sampleLimit + 1 || rows.some((row) => row.id == null))
2525
- throw new Error("Invalid failed-job metrics response.");
2526
- return {
2527
- sampleLimit,
2528
- count: Math.min(rows.length, sampleLimit),
2529
- capped: rows.length > sampleLimit
2530
- };
2531
- });
2532
- }
2533
2135
  function renderFailedJobMetrics(snapshot) {
2534
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)
2535
2137
  throw new TypeError("Invalid failed-job metrics snapshot.");
@@ -2546,13 +2148,15 @@ strata_queue_failed_jobs_sample_capped ${Number(snapshot.capped)}
2546
2148
  }
2547
2149
 
2548
2150
  // ../../src/core/queue/queueMetrics.ts
2549
- 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.");
2550
2154
  const client = new RedisClient3(redisUrl);
2551
2155
  let high;
2552
2156
  let defaultQueue;
2553
2157
  let low;
2554
2158
  try {
2555
- 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);
2556
2160
  [high, defaultQueue, low] = await Promise.all([
2557
2161
  readDepth(defaultQueueKeys()[0]),
2558
2162
  readDepth(defaultQueueKeys()[1]),
@@ -2568,9 +2172,9 @@ async function readRedisQueueDepth(redisUrl) {
2568
2172
  total: Number(high ?? 0) + Number(defaultQueue ?? 0) + Number(low ?? 0)
2569
2173
  };
2570
2174
  }
2571
- async function collectQueueMetrics() {
2572
- const driver = queueConfig.driver;
2573
- const failedJobs = createFailedJobService();
2175
+ async function collectQueueMetrics(failedJobs, options = { driver: "sync" }) {
2176
+ const { driver, redisUrl } = options;
2177
+ const transport = options.transport ?? "lists";
2574
2178
  const failedCount = (await failedJobs.listRecent(1000)).length;
2575
2179
  if (driver !== "redis") {
2576
2180
  return {
@@ -2584,7 +2188,6 @@ async function collectQueueMetrics() {
2584
2188
  failedCount
2585
2189
  };
2586
2190
  }
2587
- const redisUrl = process.env.REDIS_URL;
2588
2191
  if (!redisUrl) {
2589
2192
  return {
2590
2193
  driver,
@@ -2599,7 +2202,7 @@ async function collectQueueMetrics() {
2599
2202
  }
2600
2203
  return {
2601
2204
  driver,
2602
- pending: await readRedisQueueDepth(redisUrl),
2205
+ pending: await readRedisQueueDepth(redisUrl, transport),
2603
2206
  failedCount
2604
2207
  };
2605
2208
  }
@@ -2645,7 +2248,7 @@ return {ready+inflight, ready, inflight, 0, 0, 0, redis.call('LLEN', KEYS[3]), '
2645
2248
  async function readRedisQueueSnapshot(redisUrl, options = {}) {
2646
2249
  if (!redisUrl.trim())
2647
2250
  throw new TypeError("Queue metrics require a Redis URL.");
2648
- const transport = options.transport ?? resolveRedisQueueTransport();
2251
+ const transport = options.transport ?? "lists";
2649
2252
  if (transport !== "lists" && transport !== "streams")
2650
2253
  throw new TypeError("Invalid queue metrics transport.");
2651
2254
  const timeoutMs = options.timeoutMs ?? 1000;