@getstrata/core 2.2.59 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (330) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/core/audit/exportAuditLogs.d.ts +12 -3
  3. package/dist/core/audit/exportStore.d.ts +6 -2
  4. package/dist/core/audit/siemFormatter.d.ts +2 -2
  5. package/dist/core/auth/abilityCatalog.d.ts +1 -9
  6. package/dist/core/auth/authContext.d.ts +2 -0
  7. package/dist/core/auth/authOutcome.d.ts +32 -0
  8. package/dist/core/auth/authReader.d.ts +22 -0
  9. package/dist/core/auth/basicAuthGuard.d.ts +19 -3
  10. package/dist/core/auth/emailVerification.d.ts +1 -1
  11. package/dist/core/auth/guard.d.ts +27 -6
  12. package/dist/core/auth/intendedUrlCookie.d.ts +13 -7
  13. package/dist/core/auth/jwt.d.ts +5 -4
  14. package/dist/core/auth/jwtGuard.d.ts +7 -6
  15. package/dist/core/auth/membershipContext.d.ts +10 -7
  16. package/dist/core/auth/membershipContextMiddleware.d.ts +3 -3
  17. package/dist/core/auth/membershipMiddleware.d.ts +1 -2
  18. package/dist/core/auth/membershipServiceImplementation.d.ts +1 -1
  19. package/dist/core/auth/oauth/oidcIdToken.d.ts +25 -16
  20. package/dist/core/auth/oauth/oidcProvider.d.ts +6 -4
  21. package/dist/core/auth/oauth/providers.d.ts +3 -1
  22. package/dist/core/auth/oneTimeToken.d.ts +2 -3
  23. package/dist/core/auth/passwordConfirmCookie.d.ts +15 -6
  24. package/dist/core/auth/passwordLogin.d.ts +5 -5
  25. package/dist/core/auth/pendingMfaCookie.d.ts +2 -2
  26. package/dist/core/auth/policy.d.ts +23 -10
  27. package/dist/core/auth/requireAuthCapability.d.ts +3 -0
  28. package/dist/core/auth/resolveMembershipService.d.ts +3 -2
  29. package/dist/core/auth/saml/samlAssertionReplay.d.ts +4 -4
  30. package/dist/core/auth/saml/samlServiceProvider.d.ts +11 -7
  31. package/dist/core/auth/scimAuthMiddleware.d.ts +10 -3
  32. package/dist/core/auth/sessionCookie.d.ts +20 -9
  33. package/dist/core/auth/sessionGuard.d.ts +14 -3
  34. package/dist/core/cache/createCacheStore.d.ts +6 -3
  35. package/dist/core/cache/redisCacheStore.d.ts +5 -1
  36. package/dist/core/cache/repository.d.ts +5 -1
  37. package/dist/core/cache/simpleCache.d.ts +2 -1
  38. package/dist/core/cache/tokens.d.ts +8 -0
  39. package/dist/core/config/applicationConfig.d.ts +21 -0
  40. package/dist/core/config/configSnapshot.d.ts +3 -0
  41. package/dist/core/config/envSchema.d.ts +3 -2
  42. package/dist/core/contracts/application.d.ts +153 -0
  43. package/dist/core/contracts/authUserDirectory.d.ts +24 -7
  44. package/dist/core/contracts/container.d.ts +20 -23
  45. package/dist/core/contracts/di.d.ts +4 -2
  46. package/dist/core/contracts/executionContext.d.ts +5 -0
  47. package/dist/core/contracts/executionOperations.d.ts +10 -0
  48. package/dist/core/contracts/executionOwnership.d.ts +38 -0
  49. package/dist/core/contracts/persistence.d.ts +20 -0
  50. package/dist/core/contracts/serviceTokens.d.ts +32 -18
  51. package/dist/core/contracts/services.d.ts +63 -0
  52. package/dist/core/contracts/token.d.ts +19 -0
  53. package/dist/core/crypto/fieldEncryption.d.ts +19 -7
  54. package/dist/core/crypto/fieldEncryptionRotation.d.ts +21 -5
  55. package/dist/core/crypto/mfaSecret.d.ts +10 -3
  56. package/dist/core/database/baseRepository.d.ts +14 -10
  57. package/dist/core/database/bindings.d.ts +6 -0
  58. package/dist/core/database/bunSql.d.ts +3 -12
  59. package/dist/core/database/definitions.d.ts +209 -0
  60. package/dist/core/database/dialect.d.ts +5 -24
  61. package/dist/core/database/driverPorts.d.ts +116 -0
  62. package/dist/core/database/errors.d.ts +47 -7
  63. package/dist/core/database/factory.d.ts +51 -31
  64. package/dist/core/database/index.d.ts +9 -10
  65. package/dist/core/database/keyset.d.ts +2 -1
  66. package/dist/core/database/legacyNativeTransaction.d.ts +5 -0
  67. package/dist/core/database/manager.d.ts +12 -0
  68. package/dist/core/database/model.d.ts +91 -97
  69. package/dist/core/database/modelQuery.d.ts +9 -5
  70. package/dist/core/database/modelRegistry.d.ts +52 -33
  71. package/dist/core/database/mysqlConnection.d.ts +1 -19
  72. package/dist/core/database/mysqlNative.d.ts +38 -0
  73. package/dist/core/database/nativeTransactions.d.ts +16 -0
  74. package/dist/core/database/ormInternal.d.ts +85 -0
  75. package/dist/core/database/ormRuntime.d.ts +4 -0
  76. package/dist/core/database/ownedReadOnlyObservation.d.ts +16 -0
  77. package/dist/core/database/postgresNative.d.ts +11 -0
  78. package/dist/core/database/query.d.ts +24 -23
  79. package/dist/core/database/queryPlanSnapshot.d.ts +7 -0
  80. package/dist/core/database/rawMysql.d.ts +7 -0
  81. package/dist/core/database/rawPostgres.d.ts +23 -0
  82. package/dist/core/database/rawSqlite.d.ts +6 -0
  83. package/dist/core/database/relationQueries.d.ts +3 -2
  84. package/dist/core/database/relationQuery.d.ts +24 -6
  85. package/dist/core/database/repositoryQuery.d.ts +6 -1
  86. package/dist/core/database/schema/driver.d.ts +1 -1
  87. package/dist/core/database/seeders/types.d.ts +1 -1
  88. package/dist/core/database/session.d.ts +13 -0
  89. package/dist/core/database/sessionFactory.d.ts +9 -0
  90. package/dist/core/database/sqlDialects.d.ts +22 -0
  91. package/dist/core/database/tokens.d.ts +7 -0
  92. package/dist/core/database/transactionSession.d.ts +43 -0
  93. package/dist/core/errors/http.d.ts +4 -1
  94. package/dist/core/events/{eventBus.d.ts → eventBusClass.d.ts} +1 -3
  95. package/dist/core/events/index.d.ts +2 -3
  96. package/dist/core/events/outbox/index.d.ts +44 -13
  97. package/dist/core/events/outbox/metrics.d.ts +4 -4
  98. package/dist/core/events/outbox/workerTransport.d.ts +17 -0
  99. package/dist/core/http/authMiddleware.d.ts +2 -2
  100. package/dist/core/http/authorizeMiddleware.d.ts +2 -2
  101. package/dist/core/http/bodySizeLimitMiddleware.d.ts +1 -1
  102. package/dist/core/http/conditionalResponse.d.ts +7 -2
  103. package/dist/core/http/contentSecurityPolicy.d.ts +4 -3
  104. package/dist/core/http/corsMiddleware.d.ts +2 -1
  105. package/dist/core/http/csrfMiddleware.d.ts +6 -1
  106. package/dist/core/http/csrfToken.d.ts +17 -7
  107. package/dist/core/http/databaseError.d.ts +4 -0
  108. package/dist/core/http/etag.d.ts +5 -2
  109. package/dist/core/http/flashMiddleware.d.ts +1 -1
  110. package/dist/core/http/flashSession.d.ts +13 -7
  111. package/dist/core/http/index.d.ts +1 -1
  112. package/dist/core/http/loginThrottleMiddleware.d.ts +3 -3
  113. package/dist/core/http/memoryThrottleMiddleware.d.ts +2 -2
  114. package/dist/core/http/parseMultipartUpload.d.ts +3 -2
  115. package/dist/core/http/requestMetaContext.d.ts +5 -0
  116. package/dist/core/http/requireAbilityMiddleware.d.ts +1 -1
  117. package/dist/core/http/requireAuthMiddleware.d.ts +2 -2
  118. package/dist/core/http/requirePasswordConfirmMiddleware.d.ts +2 -1
  119. package/dist/core/http/requireVerifiedMiddleware.d.ts +2 -2
  120. package/dist/core/http/requireWebAuthMiddleware.d.ts +2 -2
  121. package/dist/core/http/response.d.ts +4 -3
  122. package/dist/core/http/scimThrottleMiddleware.d.ts +4 -3
  123. package/dist/core/http/securedRouteModelBinding.d.ts +10 -3
  124. package/dist/core/http/securityHeadersMiddleware.d.ts +1 -2
  125. package/dist/core/http/signedUrl.d.ts +8 -6
  126. package/dist/core/http/throttleMiddleware.d.ts +9 -5
  127. package/dist/core/http/uploads.d.ts +9 -3
  128. package/dist/core/http/webErrorResponse.d.ts +8 -2
  129. package/dist/core/jobs/exportAuditLogsJob.d.ts +5 -2
  130. package/dist/core/jobs/invalidateCacheTagsJob.d.ts +5 -4
  131. package/dist/core/lifecycle/gracefulShutdown.d.ts +2 -5
  132. package/dist/core/logging/requestLoggingMiddleware.d.ts +1 -1
  133. package/dist/core/mail/mailer.d.ts +7 -5
  134. package/dist/core/openapi/generator.d.ts +12 -4
  135. package/dist/core/openapi/registeredRoute.d.ts +4 -4
  136. package/dist/core/queue/createAppQueue.d.ts +9 -4
  137. package/dist/core/queue/failedJobMetrics.d.ts +11 -2
  138. package/dist/core/queue/failedJobRepository.d.ts +6 -3
  139. package/dist/core/queue/failedJobService.d.ts +1 -0
  140. package/dist/core/queue/index.d.ts +16 -6
  141. package/dist/core/queue/jobRegistry.d.ts +3 -2
  142. package/dist/core/queue/jobRunner.d.ts +10 -4
  143. package/dist/core/queue/publicQueue.d.ts +18 -9
  144. package/dist/core/queue/queueConfig.d.ts +20 -10
  145. package/dist/core/queue/queueHandoff.d.ts +31 -0
  146. package/dist/core/queue/queueMetrics.d.ts +10 -3
  147. package/dist/core/queue/queueServices.d.ts +28 -0
  148. package/dist/core/queue/queueWorkerTransport.d.ts +28 -0
  149. package/dist/core/queue/redisQueue.d.ts +19 -10
  150. package/dist/core/queue/redisQueueKeys.d.ts +5 -5
  151. package/dist/core/queue/redisStreams.d.ts +17 -10
  152. package/dist/core/queue/resilientQueue.d.ts +10 -4
  153. package/dist/core/queue/types.d.ts +1 -0
  154. package/dist/core/runtime/appKeyPrefix.d.ts +5 -5
  155. package/dist/core/runtime/application.d.ts +7 -0
  156. package/dist/core/runtime/executionContext.d.ts +9 -0
  157. package/dist/core/runtime/executionTasks.d.ts +49 -0
  158. package/dist/core/runtime/resourceJournal.d.ts +67 -0
  159. package/dist/core/runtime/serviceGraph.d.ts +14 -0
  160. package/dist/core/security/oauthState.d.ts +13 -9
  161. package/dist/core/security/publicReads.d.ts +2 -2
  162. package/dist/core/security/safeFetch.d.ts +1 -0
  163. package/dist/core/security/safeUrl.d.ts +3 -0
  164. package/dist/core/security/scimTenantTokens.d.ts +1 -1
  165. package/dist/core/security/tokenExpiry.d.ts +1 -1
  166. package/dist/core/security/webhookSignature.d.ts +1 -1
  167. package/dist/core/storage/storage.d.ts +24 -16
  168. package/dist/core/tenant/databaseTenantContext.d.ts +5 -7
  169. package/dist/core/tenant/resolveTenant.d.ts +2 -2
  170. package/dist/core/tenant/scopedDatabasePolicy.d.ts +8 -0
  171. package/dist/core/tenant/tenantDatabaseScope.d.ts +3 -7
  172. package/dist/core/tenant/tenantMiddleware.d.ts +15 -4
  173. package/dist/core/tracing/otel.d.ts +4 -6
  174. package/dist/core/tracing/tracingMiddleware.d.ts +3 -3
  175. package/dist/core/view/etaViewEngine.d.ts +1 -1
  176. package/dist/core/view/index.d.ts +5 -4
  177. package/dist/core/view/tokens.d.ts +8 -0
  178. package/dist/core/view/viewRenderer.d.ts +2 -2
  179. package/dist/core/view/webErrorView.d.ts +8 -7
  180. package/dist/core/view/webLayoutData.d.ts +6 -4
  181. package/dist/entries/audit/exportAuditLogs.js +87 -1024
  182. package/dist/entries/audit/siemFormatter.js +4 -90
  183. package/dist/entries/auth/abilityCatalog.js +28 -0
  184. package/dist/entries/auth/accessControl.js +5 -16
  185. package/dist/entries/auth/accountStateMigration.js +216 -236
  186. package/dist/entries/auth/authOutcome.js +80 -0
  187. package/dist/entries/auth/authReader.js +68 -0
  188. package/dist/entries/auth/basicAuthGuard.js +144 -1249
  189. package/dist/entries/auth/emailVerification.js +2 -2
  190. package/dist/entries/auth/intendedUrlCookie.js +35 -59
  191. package/dist/entries/auth/jwt.js +22 -47
  192. package/dist/entries/auth/jwtGuard.js +99 -231
  193. package/dist/entries/auth/membershipMiddleware.js +71 -10
  194. package/dist/entries/auth/membershipScope.js +16 -22
  195. package/dist/entries/auth/membershipService.js +11 -28
  196. package/dist/entries/auth/oauth/oidcProvider.js +59 -64
  197. package/dist/entries/auth/oauth/providers.js +12 -95
  198. package/dist/entries/auth/oauth/samlProvider.js +53 -1080
  199. package/dist/entries/auth/oneTimeToken.js +27 -289
  200. package/dist/entries/auth/passwordConfirmCookie.js +40 -63
  201. package/dist/entries/auth/passwordLogin.js +94 -399
  202. package/dist/entries/auth/pendingMfaCookie.js +6 -6
  203. package/dist/entries/auth/policy.js +22 -56
  204. package/dist/entries/auth/saml/samlServiceProvider.js +55 -1084
  205. package/dist/entries/auth/scimAuthMiddleware.js +16 -3358
  206. package/dist/entries/auth/sessionCookie.js +58 -73
  207. package/dist/entries/auth/sessionGuard.js +95 -248
  208. package/dist/entries/auth/tokenHash.js +11 -43
  209. package/dist/entries/cache/createCacheStore.js +126 -117
  210. package/dist/entries/cache/repository.js +25 -0
  211. package/dist/entries/cache/simpleCache.js +12 -7
  212. package/dist/entries/cache/tokens.js +9 -0
  213. package/dist/entries/config/applicationConfig.js +163 -0
  214. package/dist/entries/config/envSchema.js +21 -4
  215. package/dist/entries/contracts/application.js +1 -0
  216. package/dist/entries/contracts/authUserDirectory.js +10 -0
  217. package/dist/entries/contracts/container.js +99 -26
  218. package/dist/entries/contracts/di.js +3 -0
  219. package/dist/entries/contracts/executionContext.js +7 -0
  220. package/dist/entries/contracts/executionOperations.js +7 -0
  221. package/dist/entries/contracts/executionOwnership.js +7 -0
  222. package/dist/entries/contracts/membership.js +1 -0
  223. package/dist/entries/contracts/persistence.js +1 -0
  224. package/dist/entries/contracts/serviceTokens.js +26 -116
  225. package/dist/entries/contracts/services.js +98 -0
  226. package/dist/entries/contracts/token.js +41 -0
  227. package/dist/entries/crypto/fieldEncryption.js +49 -50
  228. package/dist/entries/crypto/mfaSecret.js +86 -82
  229. package/dist/entries/database/acquisitionMetrics.js +107 -0
  230. package/dist/entries/database/baseRepository.d.ts +2 -0
  231. package/dist/entries/database/bindings.js +2082 -0
  232. package/dist/entries/database/definitions.js +175 -0
  233. package/dist/entries/database/errors.js +114 -89
  234. package/dist/entries/database/factory.js +67 -28
  235. package/dist/entries/database/migrations.js +172 -224
  236. package/dist/entries/database/model.d.ts +1 -0
  237. package/dist/entries/database/ownedReadOnlyObservation.js +1282 -0
  238. package/dist/entries/database/query.js +159 -161
  239. package/dist/entries/database/repositoryQuery.d.ts +1 -0
  240. package/dist/entries/database/repositoryQuery.js +1 -1373
  241. package/dist/entries/database/schema.js +216 -236
  242. package/dist/entries/database/session.js +1 -0
  243. package/dist/entries/database/sqliteConnection.js +525 -83
  244. package/dist/entries/database/tokens.js +9 -0
  245. package/dist/entries/errors/http.js +118 -1
  246. package/dist/entries/http/authMiddleware.js +8 -5
  247. package/dist/entries/http/authorizeMiddleware.js +1 -1
  248. package/dist/entries/http/bodySizeLimitMiddleware.js +2 -2
  249. package/dist/entries/http/clientIp.js +2 -2
  250. package/dist/entries/http/conditionalResponse.js +19 -7
  251. package/dist/entries/http/corsMiddleware.js +6 -101
  252. package/dist/entries/http/csrfMiddleware.js +92 -106
  253. package/dist/entries/http/csrfToken.js +58 -94
  254. package/dist/entries/http/etag.js +7 -4
  255. package/dist/entries/http/flashMiddleware.js +57 -99
  256. package/dist/entries/http/flashSession.js +41 -68
  257. package/dist/entries/http/memoryThrottleMiddleware.js +43 -186
  258. package/dist/entries/http/metricsMiddleware.js +2 -16
  259. package/dist/entries/http/parseMultipartUpload.js +15 -29
  260. package/dist/entries/http/requireAbilityMiddleware.js +2 -2
  261. package/dist/entries/http/requirePasswordConfirmMiddleware.js +19 -31
  262. package/dist/entries/http/requireVerifiedMiddleware.js +4 -7
  263. package/dist/entries/http/requireWebAuthMiddleware.js +2 -2
  264. package/dist/entries/http/response.js +67 -233
  265. package/dist/entries/http/routeMiddleware.js +0 -14
  266. package/dist/entries/http/scimThrottleMiddleware.js +43 -237
  267. package/dist/entries/http/securedRouteModelBinding.js +37 -25
  268. package/dist/entries/http/securityHeadersMiddleware.js +8 -25
  269. package/dist/entries/http/signedUrl.js +20 -110
  270. package/dist/entries/http/throttleMiddleware.js +41 -185
  271. package/dist/entries/http/uploads.js +9 -24
  272. package/dist/entries/jobs/exportAuditLogsJob.js +17 -1498
  273. package/dist/entries/jobs/invalidateCacheTagsJob.js +12 -47
  274. package/dist/entries/logging/requestLoggingMiddleware.js +4 -4
  275. package/dist/entries/mail/mailer.js +68 -78
  276. package/dist/entries/openapi/generator.js +33 -100
  277. package/dist/entries/queue/createAppQueue.js +416 -254
  278. package/dist/entries/queue/failedJobRepository.js +19 -8
  279. package/dist/entries/queue/failedJobService.js +1 -0
  280. package/dist/entries/queue/jobRunner.js +141 -66
  281. package/dist/entries/queue/publicQueue.js +411 -255
  282. package/dist/entries/queue/queueConfig.js +61 -0
  283. package/dist/entries/queue/queueHandoff.js +318 -0
  284. package/dist/entries/queue/queueMetrics.js +989 -1384
  285. package/dist/entries/queue/queueServices.js +133 -0
  286. package/dist/entries/queue/queueWorkerTransport.js +1703 -0
  287. package/dist/entries/queue/redisQueue.js +282 -180
  288. package/dist/entries/queue.js +6 -45
  289. package/dist/entries/runtime/application.js +1585 -0
  290. package/dist/entries/runtime/asyncContextStore.js +0 -16
  291. package/dist/entries/security/oauthState.js +35 -67
  292. package/dist/entries/security/publicReads.js +4 -4
  293. package/dist/entries/security/safeFetch.js +1 -0
  294. package/dist/entries/security/scimTenantTokens.js +3 -3
  295. package/dist/entries/security/tokenExpiry.js +2 -2
  296. package/dist/entries/security/totp.js +1 -87
  297. package/dist/entries/security/webhookSignature.js +2 -90
  298. package/dist/entries/storage/storage.js +120 -45
  299. package/dist/entries/tenant/databaseTenantContext.js +6 -907
  300. package/dist/entries/tenant/publicTenantHost.js +78 -0
  301. package/dist/entries/tenant/resolveTenant.js +14 -0
  302. package/dist/entries/tenant/scopedDatabasePolicy.js +75 -0
  303. package/dist/entries/tenant/tenancyConfig.js +1 -19
  304. package/dist/entries/tenant/tenantDatabaseScope.js +3 -892
  305. package/dist/entries/tracing/tracingMiddleware.js +104 -173
  306. package/dist/entries/view/tokens.js +11 -0
  307. package/dist/framework/public-api.d.ts +62 -44
  308. package/dist/index.js +10433 -11347
  309. package/dist/internal/ormInternal.js +117 -0
  310. package/dist/internal/ormRuntime.js +5000 -0
  311. package/package.json +158 -46
  312. package/dist/core/database/bindConnection.d.ts +0 -1
  313. package/dist/core/database/boundConnection.d.ts +0 -5
  314. package/dist/core/database/defaultConnection.d.ts +0 -7
  315. package/dist/core/database/namedConnections.d.ts +0 -15
  316. package/dist/core/database/readOnlyCollector.d.ts +0 -7
  317. package/dist/core/database/repositoryConnection.d.ts +0 -4
  318. package/dist/core/database/transaction.d.ts +0 -23
  319. package/dist/core/database/transactionControl.d.ts +0 -6
  320. package/dist/core/events/deferredModelEvents.d.ts +0 -7
  321. package/dist/core/facades/index.d.ts +0 -10
  322. package/dist/core/runtime/applicationRegistry.d.ts +0 -18
  323. package/dist/entries/database/bindConnection.js +0 -1
  324. package/dist/entries/database/boundConnection.js +0 -1
  325. package/dist/entries/database/defaultConnection.js +0 -1
  326. package/dist/entries/database/namedConnections.js +0 -1
  327. package/dist/entries/database/repositoryConnection.js +0 -1
  328. package/dist/entries/database/transaction.js +0 -1
  329. package/dist/entries/facades.js +0 -711
  330. package/dist/entries/runtime/applicationRegistry.js +0 -1
@@ -1,84 +1,135 @@
1
1
  // @bun
2
2
  // ../../src/core/queue/redisQueue.ts
3
+ import { QueueHandoffError as QueueHandoffError2 } from "@getstrata/core/queue/queueHandoff";
3
4
  var {RedisClient: RedisClient2 } = globalThis.Bun;
4
5
 
5
- // ../../src/core/queue/jobRegistry.ts
6
- class JobRegistry {
7
- factories = new Map;
8
- instances = new WeakMap;
9
- register(name, factory) {
10
- this.factories.set(name, factory);
6
+ // ../../src/core/queue/jobRunner.ts
7
+ import { setTimeout as delay } from "timers/promises";
8
+
9
+ // ../../src/core/config/configSnapshot.ts
10
+ function copyApplicationConfig(value, ancestors = new Set) {
11
+ if (value === null || typeof value === "string" || typeof value === "boolean")
12
+ return value;
13
+ if (typeof value === "number" && Number.isFinite(value))
14
+ return value;
15
+ if (typeof value !== "object" || ancestors.has(value)) {
16
+ throw new TypeError("Application config must contain acyclic plain data.");
11
17
  }
12
- resolveName(job) {
13
- return this.instances.get(job);
18
+ const array = Array.isArray(value);
19
+ const prototype = Object.getPrototypeOf(value);
20
+ if (prototype !== (array ? Array.prototype : Object.prototype) && !(prototype === null && !array)) {
21
+ throw new TypeError("Application config must contain only plain objects and arrays.");
14
22
  }
15
- track(name, job) {
16
- this.instances.set(job, name);
17
- return job;
23
+ if (Object.getOwnPropertySymbols(value).length > 0) {
24
+ throw new TypeError("Application config cannot contain symbol keys.");
18
25
  }
19
- create(name) {
20
- const factory = this.factories.get(name);
21
- if (!factory) {
22
- return;
26
+ const descriptors = Object.getOwnPropertyDescriptors(value);
27
+ for (const [key, descriptor] of Object.entries(descriptors)) {
28
+ if (!(array && key === "length") && (!descriptor.enumerable || !("value" in descriptor))) {
29
+ throw new TypeError("Application config cannot contain accessors or hidden properties.");
23
30
  }
24
- return factory();
25
31
  }
26
- names() {
27
- return [...this.factories.keys()];
28
- }
29
- }
30
- var JOB_REGISTRY_KEY = Symbol.for("@getstrata/jobRegistry");
31
- function readSharedJobRegistry() {
32
- const globalRegistry = globalThis[JOB_REGISTRY_KEY];
33
- if (globalRegistry) {
34
- return globalRegistry;
32
+ ancestors.add(value);
33
+ try {
34
+ if (array) {
35
+ const length = descriptors.length.value;
36
+ if (Object.keys(descriptors).length !== length + 1) {
37
+ throw new TypeError("Application config arrays must be dense and have no extra properties.");
38
+ }
39
+ const result = [];
40
+ for (let index = 0;index < length; index++) {
41
+ const descriptor = descriptors[String(index)];
42
+ if (!descriptor)
43
+ throw new TypeError("Application config arrays must be dense.");
44
+ result.push(copyApplicationConfig(descriptor.value, ancestors));
45
+ }
46
+ return Object.freeze(result);
47
+ }
48
+ const result = {};
49
+ for (const [key, descriptor] of Object.entries(descriptors)) {
50
+ Object.defineProperty(result, key, {
51
+ value: copyApplicationConfig(descriptor.value, ancestors),
52
+ enumerable: true
53
+ });
54
+ }
55
+ return Object.freeze(result);
56
+ } finally {
57
+ ancestors.delete(value);
35
58
  }
36
- const registry = new JobRegistry;
37
- globalThis[JOB_REGISTRY_KEY] = registry;
38
- return registry;
39
59
  }
40
- var jobRegistry = readSharedJobRegistry();
41
-
42
- // ../../src/core/queue/jobRunner.ts
43
- import { setTimeout as delay } from "timers/promises";
44
60
 
45
61
  // ../../src/core/queue/queueConfig.ts
46
- function resolveQueueConfig() {
47
- const driver = process.env.QUEUE_DRIVER;
48
- const maxAttempts = Number(process.env.QUEUE_MAX_ATTEMPTS ?? "3");
49
- const backoffMs = Number(process.env.QUEUE_BACKOFF_MS ?? "1000");
50
- return {
51
- driver: driver === "redis" || driver === "async" || driver === "sync" ? driver : "sync",
52
- maxAttempts: Number.isFinite(maxAttempts) && maxAttempts > 0 ? maxAttempts : 3,
53
- backoffMs: Number.isFinite(backoffMs) && backoffMs >= 0 ? backoffMs : 1000
54
- };
62
+ var DEFAULT_QUEUE_VISIBILITY_MS = 60000;
63
+ function snapshotQueueRetryPolicy(policy) {
64
+ const { maxAttempts, backoffMs } = policy;
65
+ if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1)
66
+ throw new TypeError("QUEUE_MAX_ATTEMPTS must be a positive safe integer.");
67
+ if (!Number.isSafeInteger(backoffMs) || backoffMs < 0 || backoffMs > 2147483647)
68
+ throw new TypeError("QUEUE_BACKOFF_MS must be a non-negative integer within the timer range.");
69
+ return Object.freeze({ maxAttempts, backoffMs });
55
70
  }
56
- var queueConfig = {
57
- get driver() {
58
- return resolveQueueConfig().driver;
59
- },
60
- get maxAttempts() {
61
- return resolveQueueConfig().maxAttempts;
62
- },
63
- get backoffMs() {
64
- return resolveQueueConfig().backoffMs;
65
- }
66
- };
67
- function resolveRedisQueueTransport() {
68
- const transport = process.env.QUEUE_REDIS_TRANSPORT ?? "lists";
71
+ function resolveQueueConfig(env = process.env) {
72
+ const driver = env.QUEUE_DRIVER ?? "sync";
73
+ if (!["sync", "async", "redis"].includes(driver))
74
+ throw new TypeError("Unknown queue driver.");
75
+ const policy = snapshotQueueRetryPolicy({
76
+ maxAttempts: Number(env.QUEUE_MAX_ATTEMPTS ?? "3"),
77
+ backoffMs: Number(env.QUEUE_BACKOFF_MS ?? "1000")
78
+ });
79
+ return Object.freeze({ driver, ...policy });
80
+ }
81
+ function resolveRedisQueueTransport(env = process.env) {
82
+ const transport = env.QUEUE_REDIS_TRANSPORT ?? "lists";
69
83
  if (transport !== "lists" && transport !== "streams")
70
84
  throw new Error('QUEUE_REDIS_TRANSPORT must be "lists" or "streams".');
71
85
  return transport;
72
86
  }
73
- function readQueueVisibilityMs() {
74
- const value = Number(process.env.QUEUE_VISIBILITY_MS ?? "60000");
75
- if (!Number.isSafeInteger(value) || value < 30)
76
- throw new Error("QUEUE_VISIBILITY_MS must be an integer of at least 30 milliseconds.");
87
+ function readQueueVisibilityMs(env = process.env) {
88
+ const value = Number(env.QUEUE_VISIBILITY_MS ?? "60000");
89
+ if (!Number.isSafeInteger(value) || value < 30 || value > 2147483647)
90
+ throw new Error("QUEUE_VISIBILITY_MS must be an integer from 30 through 2147483647 milliseconds.");
77
91
  return value;
78
92
  }
93
+ function snapshotQueueRuntimeConfig(config) {
94
+ if (config.transport !== "lists" && config.transport !== "streams" || typeof config.visibilityMs !== "number")
95
+ throw new TypeError("Queue runtime requires explicit transport and numeric visibility.");
96
+ if (!Array.isArray(config.queueKeys) || config.queueKeys.length !== 3 || config.queueKeys.some((key) => typeof key !== "string" || !key.length) || new Set(config.queueKeys).size !== 3)
97
+ throw new TypeError("Queue runtime requires three distinct nonempty priority keys.");
98
+ return Object.freeze({
99
+ queueKeys: Object.freeze([...config.queueKeys]),
100
+ transport: resolveRedisQueueTransport({ QUEUE_REDIS_TRANSPORT: config.transport }),
101
+ visibilityMs: readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(config.visibilityMs) })
102
+ });
103
+ }
104
+ function resolveQueueRuntimeConfig(env = process.env) {
105
+ const prefix = env.APP_KEY_PREFIX?.trim() || "strata";
106
+ return snapshotQueueRuntimeConfig({
107
+ transport: resolveRedisQueueTransport(env),
108
+ visibilityMs: readQueueVisibilityMs(env),
109
+ queueKeys: [`${prefix}:queue:high`, `${prefix}:queue:default`, `${prefix}:queue:low`]
110
+ });
111
+ }
79
112
 
80
113
  // ../../src/core/queue/jobRunner.ts
81
- function parseQueueJobEnvelope(rawPayload) {
114
+ function snapshotQueueJobEnvelope(input) {
115
+ if (!input || typeof input !== "object" || Array.isArray(input) || ![Object.prototype, null].includes(Object.getPrototypeOf(input)))
116
+ throw new TypeError("Queue envelope must be plain data.");
117
+ const data = {};
118
+ for (const key of Reflect.ownKeys(input)) {
119
+ if (typeof key !== "string" || !["name", "payload", "attempts", "jobId", "deadlineAtMs", "cancellable", "handoff"].includes(key))
120
+ throw new TypeError("Unknown queue envelope field.");
121
+ const descriptor = Object.getOwnPropertyDescriptor(input, key);
122
+ if (!descriptor || !("value" in descriptor) || !descriptor.enumerable)
123
+ throw new TypeError("Queue envelope must contain own data fields.");
124
+ if (descriptor.value !== undefined)
125
+ Object.defineProperty(data, key, { value: descriptor.value, enumerable: true });
126
+ }
127
+ const captured = copyApplicationConfig(data);
128
+ if (typeof captured.name !== "string" || !captured.name || !captured.payload || typeof captured.payload !== "object" || Array.isArray(captured.payload))
129
+ throw new TypeError("Queue envelope requires a job name and payload record.");
130
+ return captured;
131
+ }
132
+ function parseQueueJobEnvelope(rawPayload, registry) {
82
133
  let parsed;
83
134
  try {
84
135
  parsed = JSON.parse(rawPayload);
@@ -91,15 +142,27 @@ function parseQueueJobEnvelope(rawPayload) {
91
142
  return null;
92
143
  }
93
144
  const envelope = parsed;
145
+ if (Object.keys(parsed).some((key) => ![
146
+ "name",
147
+ "payload",
148
+ "attempts",
149
+ "jobId",
150
+ "deadlineAtMs",
151
+ "cancellable",
152
+ "handoff"
153
+ ].includes(key)))
154
+ return null;
155
+ if (envelope.handoff !== undefined && (typeof envelope.handoff !== "string" || !envelope.handoff.length || envelope.handoff.length > 32768))
156
+ return null;
94
157
  if (typeof envelope.name !== "string" || envelope.name.length === 0) {
95
158
  console.error("[QueueWorker] Ignoring queue payload without job name");
96
159
  return null;
97
160
  }
98
- if (!jobRegistry.create(envelope.name)) {
161
+ if (!registry.has(envelope.name)) {
99
162
  console.error(`[QueueWorker] Ignoring unknown job name: ${envelope.name}`);
100
163
  return null;
101
164
  }
102
- if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null)) {
165
+ if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null || Array.isArray(envelope.payload))) {
103
166
  console.error("[QueueWorker] Ignoring queue payload with invalid payload object");
104
167
  return null;
105
168
  }
@@ -114,16 +177,18 @@ function parseQueueJobEnvelope(rawPayload) {
114
177
  return {
115
178
  ...envelope.cancellable ? { cancellable: true } : {},
116
179
  ...envelope.deadlineAtMs === undefined ? {} : { deadlineAtMs: envelope.deadlineAtMs },
180
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
117
181
  jobId: envelope.jobId,
118
182
  name: envelope.name,
119
183
  payload: envelope.payload ?? {},
120
184
  attempts: typeof envelope.attempts === "number" ? envelope.attempts : 0
121
185
  };
122
186
  }
123
- async function runQueueJob(envelope, failedJobs, options = {}) {
187
+ async function runQueueJob(envelope, failedJobs, registry, options = {}) {
188
+ envelope = snapshotQueueJobEnvelope(envelope);
189
+ const retryPolicy = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
124
190
  options.context?.signal.throwIfAborted();
125
- const job = jobRegistry.create(envelope.name);
126
- if (!job) {
191
+ if (!registry.has(envelope.name)) {
127
192
  throw new Error(`Unknown job "${envelope.name}".`);
128
193
  }
129
194
  const attempts = envelope.attempts ?? 0;
@@ -131,14 +196,23 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
131
196
  jobId: envelope.jobId ?? crypto.randomUUID(),
132
197
  signal: new AbortController().signal
133
198
  };
199
+ let job;
134
200
  try {
135
- await job.handle(envelope.payload, { ...context, attempt: attempts + 1 });
201
+ await failedJobs.runAttempt(envelope, { ...context, attempt: attempts + 1 }, (scoped) => {
202
+ scoped.signal.throwIfAborted();
203
+ job = registry.create(envelope.name);
204
+ if (!job)
205
+ throw new Error(`Unknown job "${envelope.name}".`);
206
+ return job.handle(envelope.payload, scoped);
207
+ });
136
208
  } catch (error) {
137
209
  options.context?.signal.throwIfAborted();
210
+ if (!job)
211
+ throw error;
138
212
  const nextAttempt = attempts + 1;
139
- const maxAttempts = job.maxAttempts ?? queueConfig.maxAttempts;
213
+ const maxAttempts = job.maxAttempts ?? retryPolicy.maxAttempts;
140
214
  if (nextAttempt < maxAttempts) {
141
- const backoffMs = job.backoffMs ?? queueConfig.backoffMs;
215
+ const backoffMs = job.backoffMs ?? retryPolicy.backoffMs;
142
216
  if (options.deferRetry) {
143
217
  await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
144
218
  return;
@@ -147,11 +221,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
147
221
  await runQueueJob({
148
222
  ...envelope,
149
223
  attempts: nextAttempt
150
- }, failedJobs, { ...options, context });
224
+ }, failedJobs, registry, { ...options, context, retryPolicy });
151
225
  return;
152
226
  }
153
227
  await failedJobs.recordFailure({
154
228
  jobName: envelope.name,
229
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
155
230
  jobId: context.jobId,
156
231
  payload: envelope.payload,
157
232
  exception: error instanceof Error ? error.stack ?? error.message : String(error)
@@ -319,14 +394,14 @@ function envFlagEnabled(value) {
319
394
  }
320
395
 
321
396
  // ../../src/core/runtime/appKeyPrefix.ts
322
- function appKeyPrefix() {
323
- return process.env.APP_KEY_PREFIX?.trim() || "strata";
397
+ function appKeyPrefix(env = process.env) {
398
+ return env.APP_KEY_PREFIX?.trim() || "strata";
324
399
  }
325
- function appCookieName(kind) {
326
- return `${appKeyPrefix()}_${kind}`;
400
+ function appCookieName(kind, env = process.env) {
401
+ return `${appKeyPrefix(env)}_${kind}`;
327
402
  }
328
- function appDevSecret(kind) {
329
- return `${appKeyPrefix()}-dev-${kind}`;
403
+ function appDevSecret(kind, env = process.env) {
404
+ return `${appKeyPrefix(env)}-dev-${kind}`;
330
405
  }
331
406
  function requireConfiguredSecret(names, devKind, env = process.env) {
332
407
  for (const name of names) {
@@ -338,50 +413,18 @@ function requireConfiguredSecret(names, devKind, env = process.env) {
338
413
  if (isProductionEnv(env)) {
339
414
  throw new Error(`${names[0]} must be set outside development. Do not derive secrets from the app name.`);
340
415
  }
341
- return appDevSecret(devKind);
416
+ return appDevSecret(devKind, env);
342
417
  }
343
418
  function namespacedRedisKey(kind) {
344
419
  return `${appKeyPrefix()}:${kind}`;
345
420
  }
346
- function smtpEhloHost() {
347
- const raw = process.env.MAIL_EHLO?.trim() || `${appKeyPrefix()}.local`;
421
+ function smtpEhloHost(env = process.env) {
422
+ const raw = env.MAIL_EHLO?.trim() || `${appKeyPrefix(env)}.local`;
348
423
  const safe = raw.replace(/[^a-zA-Z0-9.-]/g, "");
349
424
  return safe || "strata.local";
350
425
  }
351
- function siemEventType() {
352
- return process.env.SIEM_EVENT_TYPE?.trim() || `${appKeyPrefix()}.audit`;
353
- }
354
- function appUserAgent() {
355
- return process.env.APP_USER_AGENT?.trim() || appKeyPrefix();
356
- }
357
- function otelServiceName() {
358
- return process.env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix()}-api`;
359
- }
360
- function webhookSignatureHeader() {
361
- return process.env.WEBHOOK_SIGNATURE_HEADER?.trim() || `x-${appKeyPrefix()}-signature`;
362
- }
363
- function appDisplayName() {
364
- return process.env.APP_NAME?.trim() || "Strata";
365
- }
366
- function appEnv() {
367
- return process.env.APP_ENV?.trim() || "local";
368
- }
369
- function appUrl() {
370
- return (process.env.APP_URL?.trim() || "http://localhost:3000").replace(/\/$/, "");
371
- }
372
- function apiPrefix() {
373
- const raw = process.env.API_PREFIX?.trim() || "/api/v1";
374
- const withSlash = raw.startsWith("/") ? raw : `/${raw}`;
375
- const trimmed = withSlash.replace(/\/+$/, "");
376
- return trimmed || "/api/v1";
377
- }
378
- function sdkClientClassName() {
379
- const override = process.env.APP_SDK_CLASS?.trim();
380
- if (override && /^[A-Za-z_][A-Za-z0-9_]*$/.test(override)) {
381
- return override;
382
- }
383
- const fromName = appDisplayName().replace(/[^A-Za-z0-9]/g, "");
384
- return fromName ? `${fromName}Client` : "AppClient";
426
+ function otelServiceName(env = process.env) {
427
+ return env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix(env)}-api`;
385
428
  }
386
429
 
387
430
  // ../../src/core/queue/redisQueueKeys.ts
@@ -394,25 +437,19 @@ function queueHighKey() {
394
437
  function queueLowKey() {
395
438
  return namespacedRedisKey("queue:low");
396
439
  }
397
- var QUEUE_LIST_KEY = queueListKey();
398
- var QUEUE_HIGH_KEY = queueHighKey();
399
- var QUEUE_LOW_KEY = queueLowKey();
440
+ var QUEUE_LIST_KEY = "strata:queue:default";
441
+ var QUEUE_HIGH_KEY = "strata:queue:high";
442
+ var QUEUE_LOW_KEY = "strata:queue:low";
400
443
  function defaultQueueKeys() {
401
444
  return [queueHighKey(), queueListKey(), queueLowKey()];
402
445
  }
403
- var QUEUE_KEYS = defaultQueueKeys();
404
- function queueKeyForPriority(priority = "default") {
405
- switch (priority) {
406
- case "high":
407
- return queueHighKey();
408
- case "low":
409
- return queueLowKey();
410
- default:
411
- return queueListKey();
412
- }
446
+ var QUEUE_KEYS = Object.freeze([QUEUE_HIGH_KEY, QUEUE_LIST_KEY, QUEUE_LOW_KEY]);
447
+ function queueKeyForPriority(priority = "default", keys = defaultQueueKeys()) {
448
+ return priority === "high" ? keys[0] : priority === "low" ? keys[2] : keys[1];
413
449
  }
414
450
 
415
451
  // ../../src/core/queue/redisStreams.ts
452
+ import { QueueHandoffError } from "@getstrata/core/queue/queueHandoff";
416
453
  var {RedisClient } = globalThis.Bun;
417
454
 
418
455
  // ../../src/core/queue/redisStreamsScripts.ts
@@ -625,7 +662,7 @@ function watchStreamDeadline(client, deadlineAtMs, remainingMs, expire, fail) {
625
662
  clearTimeout(timer);
626
663
  };
627
664
  }
628
- async function scheduleStreamRetry(client, key, reservation, envelope, delayMs) {
665
+ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
629
666
  if (!Number.isSafeInteger(delayMs) || delayMs < 0 || delayMs > Number.MAX_SAFE_INTEGER / 2)
630
667
  throw new Error("Invalid Streams retry delay");
631
668
  const scheduled = await client.send("EVAL", [
@@ -637,7 +674,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
637
674
  STREAM_GROUP,
638
675
  reservation.id,
639
676
  reservation.owner,
640
- String(readQueueVisibilityMs()),
677
+ String(visibilityMs),
641
678
  String(delayMs),
642
679
  JSON.stringify(envelope),
643
680
  envelope.deadlineAtMs === undefined ? "" : String(envelope.deadlineAtMs),
@@ -646,7 +683,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
646
683
  if (Number(scheduled) !== 1)
647
684
  throw new Error("Queue lease lost while scheduling retry");
648
685
  }
649
- async function pollStreamPriorities(client, queueKeys, owner, cursors) {
686
+ async function pollStreamPriorities(client, queueKeys, owner, cursors, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
650
687
  const keys = [];
651
688
  for (const key of queueKeys)
652
689
  keys.push(streamQueueKey(key), streamRetryKey(key));
@@ -656,7 +693,7 @@ async function pollStreamPriorities(client, queueKeys, owner, cursors) {
656
693
  ...keys,
657
694
  STREAM_GROUP,
658
695
  owner,
659
- String(readQueueVisibilityMs()),
696
+ String(visibilityMs),
660
697
  ...cursors
661
698
  ]);
662
699
  if (!Array.isArray(raw))
@@ -707,7 +744,7 @@ async function ensureStreamGroup(client, key) {
707
744
  throw error;
708
745
  }
709
746
  }
710
- async function updateStreamReservation(client, key, reservation, action, controlKey) {
747
+ async function updateStreamReservation(client, key, reservation, action, controlKey, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
711
748
  return Number(await client.send("EVAL", [
712
749
  UPDATE_STREAM,
713
750
  "3",
@@ -717,41 +754,46 @@ async function updateStreamReservation(client, key, reservation, action, control
717
754
  STREAM_GROUP,
718
755
  reservation.id,
719
756
  reservation.owner,
720
- String(readQueueVisibilityMs()),
757
+ String(visibilityMs),
721
758
  action,
722
759
  reservation.payload,
723
760
  controlKey ? "1" : "0"
724
761
  ])) === 1;
725
762
  }
726
763
  class RedisStreamsQueue {
764
+ registry;
765
+ queueKeys;
727
766
  client;
728
- constructor(redisUrl) {
767
+ constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
768
+ this.registry = registry;
769
+ this.queueKeys = queueKeys;
770
+ this.queueKeys = Object.freeze([...queueKeys]);
729
771
  this.client = new RedisClient(redisUrl);
730
772
  }
731
773
  close() {
732
774
  this.client.close();
733
775
  }
734
- async dispatch(job, payload) {
735
- await this.enqueue(job, payload);
776
+ async dispatch(job, payload, options) {
777
+ await this.admit(job, payload, {}, undefined, options?.handoff);
736
778
  }
737
779
  async enqueue(job, payload, options = {}) {
738
780
  return this.admit(job, payload, options);
739
781
  }
740
- async replay(job, payload, jobId) {
741
- const prefix = defaultQueueKeys().find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
782
+ async replay(job, payload, jobId, options) {
783
+ const prefix = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
742
784
  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)))
743
785
  throw new Error("Replay identity does not belong to this Streams namespace");
744
- await this.admit(job, payload, {}, jobId);
786
+ await this.admit(job, payload, {}, jobId, options?.handoff);
745
787
  }
746
- async admit(job, payload, options, replayId) {
788
+ async admit(job, payload, options, replayId, handoff) {
747
789
  if (options.cancellable !== undefined && typeof options.cancellable !== "boolean")
748
790
  throw new Error("cancellable must be a boolean");
749
791
  if (options.timeoutMs !== undefined && (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs <= 0 || options.timeoutMs > MAX_TIMER_MS))
750
792
  throw new Error("Streams timeoutMs must be an integer between 1 and 2147483647");
751
- const name = jobRegistry.resolveName(job);
793
+ const name = this.registry.resolveName(job);
752
794
  if (!name)
753
795
  throw new Error("Job is not registered with the queue worker registry.");
754
- const key = queueKeyForPriority(job.priority);
796
+ const key = queueKeyForPriority(job.priority, this.queueKeys);
755
797
  await assertLegacyDrained(this.client, key);
756
798
  const jobId = replayId ?? `${streamQueueKey(key)}/${crypto.randomUUID()}`;
757
799
  const deadlineAtMs = options.timeoutMs === undefined ? undefined : await readStreamTime(this.client) + options.timeoutMs;
@@ -759,6 +801,7 @@ class RedisStreamsQueue {
759
801
  name,
760
802
  payload,
761
803
  attempts: 0,
804
+ ...handoff === undefined ? {} : { handoff },
762
805
  jobId,
763
806
  deadlineAtMs,
764
807
  ...options.cancellable ? { cancellable: true } : {}
@@ -776,7 +819,7 @@ class RedisStreamsQueue {
776
819
  return jobId;
777
820
  }
778
821
  async cancel(jobId) {
779
- const key = defaultQueueKeys().find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
822
+ const key = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
780
823
  if (!key)
781
824
  throw new Error("Invalid cancellable Streams job ID");
782
825
  return cancelStreamJob(this.client, key, jobId);
@@ -786,8 +829,11 @@ class RedisStreamsQueue {
786
829
  class RedisStreamsWorker {
787
830
  redisUrl;
788
831
  failedJobs;
832
+ registry;
789
833
  timeoutSeconds;
790
834
  queueKeys;
835
+ visibilityMs;
836
+ retryPolicy;
791
837
  client;
792
838
  owner = crypto.randomUUID();
793
839
  cursors = new Map;
@@ -796,12 +842,16 @@ class RedisStreamsWorker {
796
842
  running = false;
797
843
  stopping = false;
798
844
  active;
799
- constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = defaultQueueKeys()) {
845
+ constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
800
846
  this.redisUrl = redisUrl;
801
847
  this.failedJobs = failedJobs;
848
+ this.registry = registry;
802
849
  this.timeoutSeconds = timeoutSeconds;
803
850
  this.queueKeys = queueKeys;
804
- readQueueVisibilityMs();
851
+ this.visibilityMs = visibilityMs;
852
+ this.queueKeys = Object.freeze([...queueKeys]);
853
+ this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
854
+ readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
805
855
  this.client = new RedisClient(redisUrl);
806
856
  }
807
857
  requestStop() {
@@ -840,7 +890,7 @@ class RedisStreamsWorker {
840
890
  do {
841
891
  if (this.stopping)
842
892
  return false;
843
- const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"));
893
+ const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"), this.visibilityMs);
844
894
  this.queueKeys.forEach((key, index) => {
845
895
  const cursor = polled.cursors[index];
846
896
  if (cursor)
@@ -860,9 +910,15 @@ class RedisStreamsWorker {
860
910
  return false;
861
911
  }
862
912
  async handle(key, reservation) {
863
- const envelope = parseQueueJobEnvelope(reservation.payload);
913
+ const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
864
914
  if (!envelope) {
865
- await updateStreamReservation(this.client, key, reservation, "quarantine");
915
+ await this.failedJobs.recordFailure({
916
+ jobName: "queue.invalid-envelope",
917
+ jobId: `${streamQueueKey(key)}/${reservation.id}`,
918
+ payload: { raw: reservation.payload },
919
+ exception: "Malformed or unknown queue envelope"
920
+ });
921
+ await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
866
922
  return;
867
923
  }
868
924
  envelope.jobId ??= `${streamQueueKey(key)}/${reservation.id}`;
@@ -871,7 +927,7 @@ class RedisStreamsWorker {
871
927
  try {
872
928
  controlKey = streamControlKey(key, envelope.jobId);
873
929
  } catch {
874
- await updateStreamReservation(this.client, key, reservation, "quarantine");
930
+ await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
875
931
  return;
876
932
  }
877
933
  }
@@ -880,10 +936,11 @@ class RedisStreamsWorker {
880
936
  await this.failedJobs.recordFailure({
881
937
  jobName: envelope.name,
882
938
  jobId: envelope.jobId,
939
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
883
940
  payload: envelope.payload,
884
941
  exception: cancellationError.message
885
942
  });
886
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
943
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
887
944
  return;
888
945
  }
889
946
  const remainingMs = envelope.deadlineAtMs === undefined ? undefined : envelope.deadlineAtMs - await readStreamTime(this.client);
@@ -892,15 +949,17 @@ class RedisStreamsWorker {
892
949
  await this.failedJobs.recordFailure({
893
950
  jobName: envelope.name,
894
951
  jobId: envelope.jobId,
952
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
895
953
  payload: envelope.payload,
896
954
  exception: deadlineError.message
897
955
  });
898
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
956
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
899
957
  return;
900
958
  }
901
959
  const controller = new AbortController;
902
960
  this.active = controller;
903
961
  let acknowledge = false;
962
+ let invalidHandoff = false;
904
963
  let released = false;
905
964
  let ownershipLost = false;
906
965
  let renewal;
@@ -920,7 +979,7 @@ class RedisStreamsWorker {
920
979
  return;
921
980
  renewal = (async () => {
922
981
  try {
923
- if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey))
982
+ if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey, this.visibilityMs))
924
983
  loseOwnership(new Error("Queue lease lost"));
925
984
  else if (controlKey && await readStreamCancellation(this.client, controlKey)) {
926
985
  cancellationRequested = true;
@@ -933,9 +992,10 @@ class RedisStreamsWorker {
933
992
  })().finally(() => {
934
993
  renewal = undefined;
935
994
  });
936
- }, Math.max(1, Math.floor(readQueueVisibilityMs() / 3)));
995
+ }, Math.max(1, Math.floor(this.visibilityMs / 3)));
937
996
  try {
938
- await runQueueJob(envelope, this.failedJobs, {
997
+ await runQueueJob(envelope, this.failedJobs, this.registry, {
998
+ retryPolicy: this.retryPolicy,
939
999
  context: {
940
1000
  jobId: envelope.jobId,
941
1001
  signal: controller.signal,
@@ -943,7 +1003,7 @@ class RedisStreamsWorker {
943
1003
  },
944
1004
  deferRetry: async (next, delayMs) => {
945
1005
  controller.signal.throwIfAborted();
946
- await scheduleStreamRetry(this.client, key, reservation, next, delayMs);
1006
+ await scheduleStreamRetry(this.client, key, reservation, next, delayMs, this.visibilityMs);
947
1007
  released = true;
948
1008
  },
949
1009
  onFailureRecorded: () => {
@@ -953,18 +1013,26 @@ class RedisStreamsWorker {
953
1013
  });
954
1014
  acknowledge = true;
955
1015
  } catch (error) {
956
- console.error("[StreamsWorker] Job failed:", error);
1016
+ if (error instanceof QueueHandoffError)
1017
+ invalidHandoff = true;
1018
+ else
1019
+ console.error("[StreamsWorker] Job failed:", error);
957
1020
  } finally {
958
1021
  stopDeadlineWatch?.();
959
1022
  clearInterval(heartbeat);
960
1023
  await renewal;
961
1024
  this.active = undefined;
962
1025
  }
1026
+ if (invalidHandoff && !ownershipLost && !released) {
1027
+ await updateStreamReservation(this.client, key, reservation, "quarantine", controlKey, this.visibilityMs);
1028
+ return;
1029
+ }
963
1030
  if (!ownershipLost && !released && controlKey && (cancellationRequested || await readStreamCancellation(this.client, controlKey))) {
964
1031
  if (!failureRecorded)
965
1032
  await this.failedJobs.recordFailure({
966
1033
  jobName: envelope.name,
967
1034
  jobId: envelope.jobId,
1035
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
968
1036
  payload: envelope.payload,
969
1037
  exception: cancellationError.message
970
1038
  });
@@ -976,13 +1044,14 @@ class RedisStreamsWorker {
976
1044
  await this.failedJobs.recordFailure({
977
1045
  jobName: envelope.name,
978
1046
  jobId: envelope.jobId,
1047
+ ...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
979
1048
  payload: envelope.payload,
980
1049
  exception: deadlineError.message
981
1050
  });
982
1051
  acknowledge = true;
983
1052
  }
984
1053
  if (acknowledge && !ownershipLost && !released)
985
- await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
1054
+ await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
986
1055
  }
987
1056
  async run() {
988
1057
  this.running = true;
@@ -1014,7 +1083,7 @@ async function migrateLegacyQueueToStreams(client, options) {
1014
1083
  if (!Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 1000)
1015
1084
  throw new Error("Queue conversion batchSize must be between 1 and 1000.");
1016
1085
  let converted = 0;
1017
- for (const key of options.queueKeys ?? defaultQueueKeys())
1086
+ for (const key of [...options.queueKeys ?? QUEUE_KEYS])
1018
1087
  converted += Number(await client.send("EVAL", [
1019
1088
  CONVERT_LIST,
1020
1089
  "4",
@@ -1047,7 +1116,7 @@ function reservationExpired(lease, now, visibilityMs) {
1047
1116
  function queueInvalidKey(queueKey) {
1048
1117
  return `${queueKey}:invalid`;
1049
1118
  }
1050
- async function reserveQueueJob(client, queueKey, owner) {
1119
+ async function reserveQueueJob(client, queueKey, owner, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1051
1120
  const raw = await client.send("EVAL", [
1052
1121
  RESERVE_JOB,
1053
1122
  "3",
@@ -1056,14 +1125,14 @@ async function reserveQueueJob(client, queueKey, owner) {
1056
1125
  queueProcessingLeaseKey(queueKey),
1057
1126
  crypto.randomUUID(),
1058
1127
  owner,
1059
- String(readQueueVisibilityMs())
1128
+ String(visibilityMs)
1060
1129
  ]);
1061
1130
  if (typeof raw !== "string")
1062
1131
  return null;
1063
1132
  const reservation = JSON.parse(raw);
1064
1133
  return { ...reservation, record: raw };
1065
1134
  }
1066
- async function updateQueueReservation(client, queueKey, reservation, action) {
1135
+ async function updateQueueReservation(client, queueKey, reservation, action, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1067
1136
  return Number(await client.send("EVAL", [
1068
1137
  RENEW_OR_ACK,
1069
1138
  "3",
@@ -1074,11 +1143,11 @@ async function updateQueueReservation(client, queueKey, reservation, action) {
1074
1143
  reservation.owner,
1075
1144
  reservation.record,
1076
1145
  action,
1077
- String(readQueueVisibilityMs()),
1146
+ String(visibilityMs),
1078
1147
  reservation.payload
1079
1148
  ])) === 1;
1080
1149
  }
1081
- async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim) {
1150
+ async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1082
1151
  const keys = [];
1083
1152
  for (const key of queueKeys) {
1084
1153
  keys.push(key, queueProcessingKey(key), queueProcessingLeaseKey(key));
@@ -1088,7 +1157,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
1088
1157
  String(keys.length),
1089
1158
  ...keys,
1090
1159
  owner,
1091
- String(readQueueVisibilityMs()),
1160
+ String(visibilityMs),
1092
1161
  reservationId,
1093
1162
  reclaim ? "1" : "0"
1094
1163
  ]);
@@ -1113,7 +1182,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
1113
1182
  const reservation = JSON.parse(record);
1114
1183
  return { queueKey, reservation: { ...reservation, record } };
1115
1184
  }
1116
- async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
1185
+ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
1117
1186
  let reclaimed = 0;
1118
1187
  for (const key of queueKeys) {
1119
1188
  reclaimed += Number(await client.send("EVAL", [
@@ -1122,31 +1191,46 @@ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
1122
1191
  key,
1123
1192
  queueProcessingKey(key),
1124
1193
  queueProcessingLeaseKey(key),
1125
- String(readQueueVisibilityMs())
1194
+ String(visibilityMs)
1126
1195
  ]));
1127
1196
  }
1128
1197
  return reclaimed;
1129
1198
  }
1130
1199
 
1131
1200
  class RedisQueue {
1201
+ registry;
1202
+ queueKeys;
1132
1203
  client;
1133
- constructor(redisUrl) {
1204
+ constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
1205
+ this.registry = registry;
1206
+ this.queueKeys = queueKeys;
1207
+ this.queueKeys = Object.freeze([...queueKeys]);
1134
1208
  this.client = new RedisClient2(redisUrl);
1135
1209
  }
1136
1210
  close() {
1137
1211
  this.client.close();
1138
1212
  }
1139
- async dispatch(job, payload) {
1140
- const name = jobRegistry.resolveName(job);
1213
+ async dispatch(job, payload, options) {
1214
+ await this.admit(job, payload, options);
1215
+ }
1216
+ async replay(job, payload, jobId, options) {
1217
+ if (typeof jobId !== "string" || !jobId.length)
1218
+ throw new TypeError("Replay identity is required.");
1219
+ await this.admit(job, payload, options, jobId);
1220
+ }
1221
+ async admit(job, payload, options, jobId) {
1222
+ const name = this.registry.resolveName(job);
1141
1223
  if (!name) {
1142
1224
  throw new Error("Job is not registered with the queue worker registry.");
1143
1225
  }
1144
1226
  const envelope = {
1145
1227
  name,
1228
+ jobId: jobId ?? crypto.randomUUID(),
1146
1229
  payload,
1147
- attempts: 0
1230
+ attempts: 0,
1231
+ ...options?.handoff === undefined ? {} : { handoff: options.handoff }
1148
1232
  };
1149
- const queueKey = queueKeyForPriority(job.priority);
1233
+ const queueKey = queueKeyForPriority(job.priority, this.queueKeys);
1150
1234
  await this.client.lpush(queueKey, JSON.stringify(envelope));
1151
1235
  }
1152
1236
  }
@@ -1154,17 +1238,25 @@ class RedisQueue {
1154
1238
  class QueueWorker {
1155
1239
  redisUrl;
1156
1240
  failedJobs;
1241
+ registry;
1157
1242
  timeoutSeconds;
1158
1243
  queueKeys;
1244
+ visibilityMs;
1245
+ retryPolicy;
1159
1246
  owner = crypto.randomUUID();
1160
1247
  running = false;
1161
1248
  stopping = false;
1162
1249
  client;
1163
- constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = QUEUE_KEYS) {
1250
+ constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
1164
1251
  this.redisUrl = redisUrl;
1165
1252
  this.failedJobs = failedJobs;
1253
+ this.registry = registry;
1166
1254
  this.timeoutSeconds = timeoutSeconds;
1167
1255
  this.queueKeys = queueKeys;
1256
+ this.visibilityMs = visibilityMs;
1257
+ this.queueKeys = Object.freeze([...queueKeys]);
1258
+ this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
1259
+ readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
1168
1260
  this.client = new RedisClient2(redisUrl);
1169
1261
  }
1170
1262
  requestStop() {
@@ -1179,7 +1271,7 @@ class QueueWorker {
1179
1271
  do {
1180
1272
  if (this.stopping)
1181
1273
  return null;
1182
- const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim);
1274
+ const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim, this.visibilityMs);
1183
1275
  reclaim = false;
1184
1276
  if (reserved)
1185
1277
  return reserved;
@@ -1196,12 +1288,13 @@ class QueueWorker {
1196
1288
  if (!reserved)
1197
1289
  return false;
1198
1290
  const { queueKey, reservation } = reserved;
1199
- const envelope = parseQueueJobEnvelope(reservation.payload);
1291
+ const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
1200
1292
  if (!envelope) {
1201
- await updateQueueReservation(this.client, queueKey, reservation, "quarantine");
1293
+ await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
1202
1294
  return true;
1203
1295
  }
1204
1296
  let acknowledge = false;
1297
+ let invalidHandoff = false;
1205
1298
  let ownershipLost = false;
1206
1299
  let renewal;
1207
1300
  const heartbeat = setInterval(() => {
@@ -1209,7 +1302,7 @@ class QueueWorker {
1209
1302
  return;
1210
1303
  renewal = (async () => {
1211
1304
  try {
1212
- if (!await updateQueueReservation(this.client, queueKey, reservation, "renew")) {
1305
+ if (!await updateQueueReservation(this.client, queueKey, reservation, "renew", this.visibilityMs)) {
1213
1306
  ownershipLost = true;
1214
1307
  }
1215
1308
  } catch (error) {
@@ -1219,22 +1312,30 @@ class QueueWorker {
1219
1312
  })().finally(() => {
1220
1313
  renewal = undefined;
1221
1314
  });
1222
- }, Math.max(1, Math.floor(readQueueVisibilityMs() / 3)));
1315
+ }, Math.max(1, Math.floor(this.visibilityMs / 3)));
1223
1316
  try {
1224
- await runQueueJob(envelope, this.failedJobs, {
1317
+ await runQueueJob(envelope, this.failedJobs, this.registry, {
1318
+ retryPolicy: this.retryPolicy,
1225
1319
  onFailureRecorded: () => {
1226
1320
  acknowledge = true;
1227
1321
  }
1228
1322
  });
1229
1323
  acknowledge = true;
1230
1324
  } catch (error) {
1231
- console.error("[QueueWorker] Job failed:", error);
1325
+ if (error instanceof QueueHandoffError2)
1326
+ invalidHandoff = true;
1327
+ else
1328
+ console.error("[QueueWorker] Job failed:", error);
1232
1329
  } finally {
1233
1330
  clearInterval(heartbeat);
1234
1331
  await renewal;
1235
1332
  }
1333
+ if (invalidHandoff && !ownershipLost) {
1334
+ await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
1335
+ return true;
1336
+ }
1236
1337
  if (acknowledge && !ownershipLost) {
1237
- await updateQueueReservation(this.client, queueKey, reservation, "ack");
1338
+ await updateQueueReservation(this.client, queueKey, reservation, "ack", this.visibilityMs);
1238
1339
  }
1239
1340
  return true;
1240
1341
  }
@@ -1263,12 +1364,13 @@ class QueueWorker {
1263
1364
  this.client.close();
1264
1365
  }
1265
1366
  }
1266
- async function countPendingQueueJobs(redisUrl) {
1367
+ async function countPendingQueueJobs(redisUrl, config = resolveQueueRuntimeConfig({})) {
1368
+ const { transport, queueKeys } = snapshotQueueRuntimeConfig(config);
1267
1369
  const client = new RedisClient2(redisUrl);
1268
1370
  try {
1269
1371
  let total = 0;
1270
- for (const queueKey of resolveRedisQueueTransport() === "streams" ? defaultQueueKeys() : QUEUE_KEYS) {
1271
- total += resolveRedisQueueTransport() === "streams" ? await readStreamQueueDepth(client, queueKey) : await client.llen(queueKey);
1372
+ for (const queueKey of queueKeys) {
1373
+ total += transport === "streams" ? await readStreamQueueDepth(client, queueKey) : await client.llen(queueKey);
1272
1374
  }
1273
1375
  return total;
1274
1376
  } finally {