@pithy-sh/payments 0.1.1 → 0.1.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 (402) hide show
  1. package/dist/admin/coverage.d.ts +35 -0
  2. package/dist/admin/coverage.d.ts.map +1 -0
  3. package/dist/admin/coverage.js +56 -0
  4. package/dist/admin/read.d.ts +222 -0
  5. package/dist/admin/read.d.ts.map +1 -0
  6. package/dist/admin/read.js +159 -0
  7. package/dist/audit/actions.d.ts +211 -0
  8. package/dist/audit/actions.d.ts.map +1 -0
  9. package/dist/audit/actions.js +211 -0
  10. package/dist/capability.d.ts +72 -0
  11. package/dist/capability.d.ts.map +1 -0
  12. package/dist/capability.js +228 -0
  13. package/dist/client/api.d.ts +435 -0
  14. package/dist/client/api.d.ts.map +1 -0
  15. package/dist/client/api.js +415 -0
  16. package/dist/client/checkout.d.ts +144 -0
  17. package/dist/client/checkout.d.ts.map +1 -0
  18. package/dist/client/checkout.js +173 -0
  19. package/dist/client/fixtures/price-preview-de.js +89 -0
  20. package/dist/client/fixtures/price-preview-gb.js +89 -0
  21. package/dist/client/fixtures/price-preview-jp-yen.js +89 -0
  22. package/dist/client/fixtures/price-preview-us-country-only.js +89 -0
  23. package/dist/client/fixtures/price-preview-us-new-york.js +89 -0
  24. package/dist/client/fixtures/pricePreview.d.ts +54 -0
  25. package/dist/client/fixtures/pricePreview.d.ts.map +1 -0
  26. package/dist/client/fixtures/pricePreview.js +61 -0
  27. package/dist/client/hooks.d.ts +235 -0
  28. package/dist/client/hooks.d.ts.map +1 -0
  29. package/dist/client/hooks.js +428 -0
  30. package/dist/client/paddle.d.ts +433 -0
  31. package/dist/client/paddle.d.ts.map +1 -0
  32. package/dist/client/paddle.js +391 -0
  33. package/dist/client/paddleCache.d.ts +123 -0
  34. package/dist/client/paddleCache.d.ts.map +1 -0
  35. package/dist/client/paddleCache.js +159 -0
  36. package/dist/client/paddlePrices.d.ts +94 -0
  37. package/dist/client/paddlePrices.d.ts.map +1 -0
  38. package/dist/client/paddlePrices.js +55 -0
  39. package/dist/client/paddlePricesTag.d.ts +127 -0
  40. package/dist/client/paddlePricesTag.d.ts.map +1 -0
  41. package/dist/client/paddlePricesTag.js +207 -0
  42. package/dist/client/projection.d.ts +128 -0
  43. package/dist/client/projection.d.ts.map +1 -0
  44. package/dist/client/projection.js +0 -0
  45. package/dist/client/test-utils/cacheStore.d.ts +18 -0
  46. package/dist/client/test-utils/cacheStore.d.ts.map +1 -0
  47. package/dist/client/test-utils/cacheStore.js +42 -0
  48. package/dist/client/wholeUnits.d.ts +18 -0
  49. package/dist/client/wholeUnits.d.ts.map +1 -0
  50. package/dist/client/wholeUnits.js +120 -0
  51. package/dist/config/config.d.ts +238 -0
  52. package/dist/config/config.d.ts.map +1 -0
  53. package/dist/config/config.js +249 -0
  54. package/dist/data/discount.d.ts +127 -0
  55. package/dist/data/discount.d.ts.map +1 -0
  56. package/dist/data/discount.js +91 -0
  57. package/dist/data/entitlement.d.ts +43 -0
  58. package/dist/data/entitlement.d.ts.map +1 -0
  59. package/dist/data/entitlement.js +43 -0
  60. package/dist/data/money.d.ts +15 -0
  61. package/dist/data/money.d.ts.map +1 -0
  62. package/dist/data/money.js +76 -0
  63. package/dist/data/pause.d.ts +133 -0
  64. package/dist/data/pause.d.ts.map +1 -0
  65. package/dist/data/pause.js +63 -0
  66. package/dist/data/providerAccount.d.ts +40 -0
  67. package/dist/data/providerAccount.d.ts.map +1 -0
  68. package/dist/data/providerAccount.js +34 -0
  69. package/dist/data/purchase.d.ts +107 -0
  70. package/dist/data/purchase.d.ts.map +1 -0
  71. package/dist/data/purchase.js +77 -0
  72. package/dist/data/rail.d.ts +74 -0
  73. package/dist/data/rail.d.ts.map +1 -0
  74. package/dist/data/rail.js +74 -0
  75. package/dist/data/reconcileRun.d.ts +120 -0
  76. package/dist/data/reconcileRun.d.ts.map +1 -0
  77. package/dist/data/reconcileRun.js +120 -0
  78. package/dist/data/renderMoney.d.ts +85 -0
  79. package/dist/data/renderMoney.d.ts.map +1 -0
  80. package/dist/data/renderMoney.js +146 -0
  81. package/dist/data/status.d.ts +58 -0
  82. package/dist/data/status.d.ts.map +1 -0
  83. package/dist/data/status.js +68 -0
  84. package/dist/data/subject.d.ts +111 -0
  85. package/dist/data/subject.d.ts.map +1 -0
  86. package/dist/data/subject.js +125 -0
  87. package/dist/data/subscription.d.ts +624 -0
  88. package/dist/data/subscription.d.ts.map +1 -0
  89. package/dist/data/subscription.js +497 -0
  90. package/dist/data/syncCursor.d.ts +33 -0
  91. package/dist/data/syncCursor.d.ts.map +1 -0
  92. package/dist/data/syncCursor.js +29 -0
  93. package/dist/data/tables.d.ts +37 -0
  94. package/dist/data/tables.d.ts.map +1 -0
  95. package/dist/data/tables.js +44 -0
  96. package/dist/data/webhookEvent.d.ts +134 -0
  97. package/dist/data/webhookEvent.d.ts.map +1 -0
  98. package/dist/data/webhookEvent.js +128 -0
  99. package/dist/entitlement/manual.d.ts +101 -0
  100. package/dist/entitlement/manual.d.ts.map +1 -0
  101. package/dist/entitlement/manual.js +84 -0
  102. package/dist/entitlement/resolver.d.ts +60 -0
  103. package/dist/entitlement/resolver.d.ts.map +1 -0
  104. package/dist/entitlement/resolver.js +101 -0
  105. package/dist/entitlement/subjectSeam.d.ts +102 -0
  106. package/dist/entitlement/subjectSeam.d.ts.map +1 -0
  107. package/dist/entitlement/subjectSeam.js +85 -0
  108. package/dist/error/errors.d.ts +247 -0
  109. package/dist/error/errors.d.ts.map +1 -0
  110. package/dist/error/errors.js +326 -0
  111. package/dist/grants/apply.d.ts +74 -0
  112. package/dist/grants/apply.d.ts.map +1 -0
  113. package/dist/grants/apply.js +148 -0
  114. package/dist/grants/clawback.d.ts +43 -0
  115. package/dist/grants/clawback.d.ts.map +1 -0
  116. package/dist/grants/clawback.js +71 -0
  117. package/dist/grants/ledgerSeam.d.ts +89 -0
  118. package/dist/grants/ledgerSeam.d.ts.map +1 -0
  119. package/dist/grants/ledgerSeam.js +66 -0
  120. package/dist/http/dispatch.d.ts +35 -0
  121. package/dist/http/dispatch.d.ts.map +1 -0
  122. package/dist/http/dispatch.js +40 -0
  123. package/dist/http/guards.d.ts +43 -0
  124. package/dist/http/guards.d.ts.map +1 -0
  125. package/dist/http/guards.js +52 -0
  126. package/dist/http/manifestConfig.d.ts +38 -0
  127. package/dist/http/manifestConfig.d.ts.map +1 -0
  128. package/dist/http/manifestConfig.js +49 -0
  129. package/dist/http/responses.d.ts +1455 -0
  130. package/dist/http/responses.d.ts.map +1 -0
  131. package/dist/http/responses.js +728 -0
  132. package/dist/http/routes.d.ts +315 -0
  133. package/dist/http/routes.d.ts.map +1 -0
  134. package/dist/http/routes.js +1966 -0
  135. package/dist/http/schemas.d.ts +404 -0
  136. package/dist/http/schemas.d.ts.map +1 -0
  137. package/dist/http/schemas.js +359 -0
  138. package/dist/http/scopes.d.ts +160 -0
  139. package/dist/http/scopes.d.ts.map +1 -0
  140. package/dist/http/scopes.js +240 -0
  141. package/dist/http/view.d.ts +96 -0
  142. package/dist/http/view.d.ts.map +1 -0
  143. package/dist/http/view.js +170 -0
  144. package/dist/http/webhookGuard.d.ts +109 -0
  145. package/dist/http/webhookGuard.d.ts.map +1 -0
  146. package/dist/http/webhookGuard.js +206 -0
  147. package/dist/index.d.ts +64 -0
  148. package/dist/index.d.ts.map +1 -0
  149. package/dist/index.js +43 -0
  150. package/dist/migrations/0001_purchases.d.ts +35 -0
  151. package/dist/migrations/0001_purchases.d.ts.map +1 -0
  152. package/dist/migrations/0001_purchases.js +80 -0
  153. package/dist/pricing/location.d.ts +138 -0
  154. package/dist/pricing/location.d.ts.map +1 -0
  155. package/dist/pricing/location.js +81 -0
  156. package/dist/pricing/visitor.d.ts +56 -0
  157. package/dist/pricing/visitor.d.ts.map +1 -0
  158. package/dist/pricing/visitor.js +35 -0
  159. package/dist/projection/event.d.ts +66 -0
  160. package/dist/projection/event.d.ts.map +1 -0
  161. package/dist/projection/event.js +45 -0
  162. package/dist/projection/orphans.d.ts +114 -0
  163. package/dist/projection/orphans.d.ts.map +1 -0
  164. package/dist/projection/orphans.js +162 -0
  165. package/dist/projection/owner.d.ts +121 -0
  166. package/dist/projection/owner.d.ts.map +1 -0
  167. package/dist/projection/owner.js +114 -0
  168. package/dist/projection/resolve.d.ts +30 -0
  169. package/dist/projection/resolve.d.ts.map +1 -0
  170. package/dist/projection/resolve.js +42 -0
  171. package/dist/projection/writer.d.ts +103 -0
  172. package/dist/projection/writer.d.ts.map +1 -0
  173. package/dist/projection/writer.js +280 -0
  174. package/dist/provision/resolvePaymentsConfig.d.ts +48 -0
  175. package/dist/provision/resolvePaymentsConfig.d.ts.map +1 -0
  176. package/dist/provision/resolvePaymentsConfig.js +41 -0
  177. package/dist/rails/apple/certs.d.ts +7 -0
  178. package/dist/rails/apple/certs.d.ts.map +1 -0
  179. package/dist/rails/apple/certs.js +8 -0
  180. package/dist/rails/apple/der.d.ts +107 -0
  181. package/dist/rails/apple/der.d.ts.map +1 -0
  182. package/dist/rails/apple/der.js +217 -0
  183. package/dist/rails/apple/fixtures/chain.d.ts +117 -0
  184. package/dist/rails/apple/fixtures/chain.d.ts.map +1 -0
  185. package/dist/rails/apple/fixtures/chain.js +289 -0
  186. package/dist/rails/apple/http.d.ts +74 -0
  187. package/dist/rails/apple/http.d.ts.map +1 -0
  188. package/dist/rails/apple/http.js +32 -0
  189. package/dist/rails/apple/jws.d.ts +17 -0
  190. package/dist/rails/apple/jws.d.ts.map +1 -0
  191. package/dist/rails/apple/jws.js +75 -0
  192. package/dist/rails/apple/notification.d.ts +116 -0
  193. package/dist/rails/apple/notification.d.ts.map +1 -0
  194. package/dist/rails/apple/notification.js +199 -0
  195. package/dist/rails/apple/rail.d.ts +25 -0
  196. package/dist/rails/apple/rail.d.ts.map +1 -0
  197. package/dist/rails/apple/rail.js +68 -0
  198. package/dist/rails/apple/serverApi.d.ts +120 -0
  199. package/dist/rails/apple/serverApi.d.ts.map +1 -0
  200. package/dist/rails/apple/serverApi.js +244 -0
  201. package/dist/rails/apple/verify.d.ts +36 -0
  202. package/dist/rails/apple/verify.d.ts.map +1 -0
  203. package/dist/rails/apple/verify.js +46 -0
  204. package/dist/rails/apple/x509.d.ts +85 -0
  205. package/dist/rails/apple/x509.d.ts.map +1 -0
  206. package/dist/rails/apple/x509.js +273 -0
  207. package/dist/rails/contract.d.ts +871 -0
  208. package/dist/rails/contract.d.ts.map +1 -0
  209. package/dist/rails/contract.js +60 -0
  210. package/dist/rails/google/fixtures/push.d.ts +71 -0
  211. package/dist/rails/google/fixtures/push.d.ts.map +1 -0
  212. package/dist/rails/google/fixtures/push.js +107 -0
  213. package/dist/rails/google/http.d.ts +77 -0
  214. package/dist/rails/google/http.d.ts.map +1 -0
  215. package/dist/rails/google/http.js +39 -0
  216. package/dist/rails/google/jwt.d.ts +53 -0
  217. package/dist/rails/google/jwt.d.ts.map +1 -0
  218. package/dist/rails/google/jwt.js +94 -0
  219. package/dist/rails/google/oidc.d.ts +112 -0
  220. package/dist/rails/google/oidc.d.ts.map +1 -0
  221. package/dist/rails/google/oidc.js +179 -0
  222. package/dist/rails/google/playApi.d.ts +188 -0
  223. package/dist/rails/google/playApi.d.ts.map +1 -0
  224. package/dist/rails/google/playApi.js +350 -0
  225. package/dist/rails/google/rail.d.ts +57 -0
  226. package/dist/rails/google/rail.d.ts.map +1 -0
  227. package/dist/rails/google/rail.js +70 -0
  228. package/dist/rails/google/rtdn.d.ts +105 -0
  229. package/dist/rails/google/rtdn.d.ts.map +1 -0
  230. package/dist/rails/google/rtdn.js +179 -0
  231. package/dist/rails/google/verify.d.ts +20 -0
  232. package/dist/rails/google/verify.d.ts.map +1 -0
  233. package/dist/rails/google/verify.js +76 -0
  234. package/dist/rails/lemonSqueezy/api.d.ts +92 -0
  235. package/dist/rails/lemonSqueezy/api.d.ts.map +1 -0
  236. package/dist/rails/lemonSqueezy/api.js +121 -0
  237. package/dist/rails/lemonSqueezy/checkout.d.ts +52 -0
  238. package/dist/rails/lemonSqueezy/checkout.d.ts.map +1 -0
  239. package/dist/rails/lemonSqueezy/checkout.js +84 -0
  240. package/dist/rails/lemonSqueezy/discounts.d.ts +50 -0
  241. package/dist/rails/lemonSqueezy/discounts.d.ts.map +1 -0
  242. package/dist/rails/lemonSqueezy/discounts.js +75 -0
  243. package/dist/rails/lemonSqueezy/fixtures/events.d.ts +56 -0
  244. package/dist/rails/lemonSqueezy/fixtures/events.d.ts.map +1 -0
  245. package/dist/rails/lemonSqueezy/fixtures/events.js +144 -0
  246. package/dist/rails/lemonSqueezy/objects.d.ts +229 -0
  247. package/dist/rails/lemonSqueezy/objects.d.ts.map +1 -0
  248. package/dist/rails/lemonSqueezy/objects.js +379 -0
  249. package/dist/rails/lemonSqueezy/portal.d.ts +35 -0
  250. package/dist/rails/lemonSqueezy/portal.d.ts.map +1 -0
  251. package/dist/rails/lemonSqueezy/portal.js +18 -0
  252. package/dist/rails/lemonSqueezy/pricing.d.ts +25 -0
  253. package/dist/rails/lemonSqueezy/pricing.d.ts.map +1 -0
  254. package/dist/rails/lemonSqueezy/pricing.js +54 -0
  255. package/dist/rails/lemonSqueezy/rail.d.ts +41 -0
  256. package/dist/rails/lemonSqueezy/rail.d.ts.map +1 -0
  257. package/dist/rails/lemonSqueezy/rail.js +67 -0
  258. package/dist/rails/lemonSqueezy/read.d.ts +28 -0
  259. package/dist/rails/lemonSqueezy/read.d.ts.map +1 -0
  260. package/dist/rails/lemonSqueezy/read.js +37 -0
  261. package/dist/rails/lemonSqueezy/refresh.d.ts +42 -0
  262. package/dist/rails/lemonSqueezy/refresh.d.ts.map +1 -0
  263. package/dist/rails/lemonSqueezy/refresh.js +40 -0
  264. package/dist/rails/lemonSqueezy/signature.d.ts +56 -0
  265. package/dist/rails/lemonSqueezy/signature.d.ts.map +1 -0
  266. package/dist/rails/lemonSqueezy/signature.js +92 -0
  267. package/dist/rails/lemonSqueezy/verify.d.ts +33 -0
  268. package/dist/rails/lemonSqueezy/verify.d.ts.map +1 -0
  269. package/dist/rails/lemonSqueezy/verify.js +41 -0
  270. package/dist/rails/lemonSqueezy/webhook.d.ts +53 -0
  271. package/dist/rails/lemonSqueezy/webhook.d.ts.map +1 -0
  272. package/dist/rails/lemonSqueezy/webhook.js +179 -0
  273. package/dist/rails/paddle/adjustments.d.ts +60 -0
  274. package/dist/rails/paddle/adjustments.d.ts.map +1 -0
  275. package/dist/rails/paddle/adjustments.js +66 -0
  276. package/dist/rails/paddle/api.d.ts +138 -0
  277. package/dist/rails/paddle/api.d.ts.map +1 -0
  278. package/dist/rails/paddle/api.js +152 -0
  279. package/dist/rails/paddle/checkout.d.ts +69 -0
  280. package/dist/rails/paddle/checkout.d.ts.map +1 -0
  281. package/dist/rails/paddle/checkout.js +100 -0
  282. package/dist/rails/paddle/discounts.d.ts +67 -0
  283. package/dist/rails/paddle/discounts.d.ts.map +1 -0
  284. package/dist/rails/paddle/discounts.js +73 -0
  285. package/dist/rails/paddle/events.d.ts +140 -0
  286. package/dist/rails/paddle/events.d.ts.map +1 -0
  287. package/dist/rails/paddle/events.js +183 -0
  288. package/dist/rails/paddle/fixtures/browserForged.d.ts +42 -0
  289. package/dist/rails/paddle/fixtures/browserForged.d.ts.map +1 -0
  290. package/dist/rails/paddle/fixtures/browserForged.js +52 -0
  291. package/dist/rails/paddle/fixtures/pausedSubscription.d.ts +78 -0
  292. package/dist/rails/paddle/fixtures/pausedSubscription.d.ts.map +1 -0
  293. package/dist/rails/paddle/fixtures/pausedSubscription.js +76 -0
  294. package/dist/rails/paddle/objects.d.ts +519 -0
  295. package/dist/rails/paddle/objects.d.ts.map +1 -0
  296. package/dist/rails/paddle/objects.js +588 -0
  297. package/dist/rails/paddle/portal.d.ts +45 -0
  298. package/dist/rails/paddle/portal.d.ts.map +1 -0
  299. package/dist/rails/paddle/portal.js +42 -0
  300. package/dist/rails/paddle/rail.d.ts +55 -0
  301. package/dist/rails/paddle/rail.d.ts.map +1 -0
  302. package/dist/rails/paddle/rail.js +117 -0
  303. package/dist/rails/paddle/read.d.ts +53 -0
  304. package/dist/rails/paddle/read.d.ts.map +1 -0
  305. package/dist/rails/paddle/read.js +66 -0
  306. package/dist/rails/paddle/recorded.d.ts +49 -0
  307. package/dist/rails/paddle/recorded.d.ts.map +1 -0
  308. package/dist/rails/paddle/recorded.js +79 -0
  309. package/dist/rails/paddle/refresh.d.ts +54 -0
  310. package/dist/rails/paddle/refresh.d.ts.map +1 -0
  311. package/dist/rails/paddle/refresh.js +94 -0
  312. package/dist/rails/paddle/refund.d.ts +82 -0
  313. package/dist/rails/paddle/refund.d.ts.map +1 -0
  314. package/dist/rails/paddle/refund.js +175 -0
  315. package/dist/rails/paddle/signature.d.ts +83 -0
  316. package/dist/rails/paddle/signature.d.ts.map +1 -0
  317. package/dist/rails/paddle/signature.js +163 -0
  318. package/dist/rails/paddle/subscription.d.ts +137 -0
  319. package/dist/rails/paddle/subscription.d.ts.map +1 -0
  320. package/dist/rails/paddle/subscription.js +538 -0
  321. package/dist/rails/paddle/verify.d.ts +46 -0
  322. package/dist/rails/paddle/verify.d.ts.map +1 -0
  323. package/dist/rails/paddle/verify.js +43 -0
  324. package/dist/rails/paddle/webhook.d.ts +98 -0
  325. package/dist/rails/paddle/webhook.d.ts.map +1 -0
  326. package/dist/rails/paddle/webhook.js +181 -0
  327. package/dist/rails/providers.d.ts +73 -0
  328. package/dist/rails/providers.d.ts.map +1 -0
  329. package/dist/rails/providers.js +62 -0
  330. package/dist/rails/stripe/api.d.ts +118 -0
  331. package/dist/rails/stripe/api.d.ts.map +1 -0
  332. package/dist/rails/stripe/api.js +153 -0
  333. package/dist/rails/stripe/checkout.d.ts +60 -0
  334. package/dist/rails/stripe/checkout.d.ts.map +1 -0
  335. package/dist/rails/stripe/checkout.js +86 -0
  336. package/dist/rails/stripe/discounts.d.ts +50 -0
  337. package/dist/rails/stripe/discounts.d.ts.map +1 -0
  338. package/dist/rails/stripe/discounts.js +87 -0
  339. package/dist/rails/stripe/fixtures/events.d.ts +64 -0
  340. package/dist/rails/stripe/fixtures/events.d.ts.map +1 -0
  341. package/dist/rails/stripe/fixtures/events.js +67 -0
  342. package/dist/rails/stripe/objects.d.ts +237 -0
  343. package/dist/rails/stripe/objects.d.ts.map +1 -0
  344. package/dist/rails/stripe/objects.js +389 -0
  345. package/dist/rails/stripe/portal.d.ts +29 -0
  346. package/dist/rails/stripe/portal.d.ts.map +1 -0
  347. package/dist/rails/stripe/portal.js +41 -0
  348. package/dist/rails/stripe/pricing.d.ts +25 -0
  349. package/dist/rails/stripe/pricing.d.ts.map +1 -0
  350. package/dist/rails/stripe/pricing.js +68 -0
  351. package/dist/rails/stripe/rail.d.ts +28 -0
  352. package/dist/rails/stripe/rail.d.ts.map +1 -0
  353. package/dist/rails/stripe/rail.js +81 -0
  354. package/dist/rails/stripe/refresh.d.ts +33 -0
  355. package/dist/rails/stripe/refresh.d.ts.map +1 -0
  356. package/dist/rails/stripe/refresh.js +21 -0
  357. package/dist/rails/stripe/signature.d.ts +71 -0
  358. package/dist/rails/stripe/signature.d.ts.map +1 -0
  359. package/dist/rails/stripe/signature.js +92 -0
  360. package/dist/rails/stripe/verify.d.ts +15 -0
  361. package/dist/rails/stripe/verify.d.ts.map +1 -0
  362. package/dist/rails/stripe/verify.js +63 -0
  363. package/dist/rails/stripe/webhook.d.ts +26 -0
  364. package/dist/rails/stripe/webhook.d.ts.map +1 -0
  365. package/dist/rails/stripe/webhook.js +31 -0
  366. package/dist/secret/registry.d.ts +192 -0
  367. package/dist/secret/registry.d.ts.map +1 -0
  368. package/dist/secret/registry.js +154 -0
  369. package/dist/seeds/example.d.ts +3 -0
  370. package/dist/seeds/example.d.ts.map +1 -0
  371. package/dist/seeds/example.js +171 -0
  372. package/dist/version.generated.d.ts +5 -0
  373. package/dist/version.generated.d.ts.map +1 -0
  374. package/dist/version.generated.js +7 -0
  375. package/dist/workflows/paddleSweep.d.ts +175 -0
  376. package/dist/workflows/paddleSweep.d.ts.map +1 -0
  377. package/dist/workflows/paddleSweep.js +433 -0
  378. package/dist/workflows/railAccess.d.ts +48 -0
  379. package/dist/workflows/railAccess.d.ts.map +1 -0
  380. package/dist/workflows/railAccess.js +36 -0
  381. package/dist/workflows/reconcile.d.ts +161 -0
  382. package/dist/workflows/reconcile.d.ts.map +1 -0
  383. package/dist/workflows/reconcile.js +352 -0
  384. package/dist/workflows/report.d.ts +28 -0
  385. package/dist/workflows/report.d.ts.map +1 -0
  386. package/dist/workflows/report.js +54 -0
  387. package/dist/workflows/retryPolicy.d.ts +29 -0
  388. package/dist/workflows/retryPolicy.d.ts.map +1 -0
  389. package/dist/workflows/retryPolicy.js +37 -0
  390. package/dist/workflows/specs.d.ts +102 -0
  391. package/dist/workflows/specs.d.ts.map +1 -0
  392. package/dist/workflows/specs.js +84 -0
  393. package/dist/workflows/worker.d.ts +62 -0
  394. package/dist/workflows/worker.d.ts.map +1 -0
  395. package/dist/workflows/worker.js +92 -0
  396. package/dist/workflows/workerConfig.d.ts +46 -0
  397. package/dist/workflows/workerConfig.d.ts.map +1 -0
  398. package/dist/workflows/workerConfig.js +38 -0
  399. package/package.json +22 -13
  400. package/pithy.manifest.json +19 -13
  401. package/src/version.generated.ts +1 -1
  402. package/src/client/paddlePrices.iife.ts +0 -43
@@ -0,0 +1,36 @@
1
+ import { railCredentials } from "../secret/registry.js";
2
+ import { mintAppleApiToken } from "../rails/apple/serverApi.js";
3
+ import { mintPlayAccessToken } from "../rails/google/playApi.js";
4
+ import { resolveRailProvider } from "../rails/providers.js";
5
+ //#region src/workflows/railAccess.ts
6
+ /**
7
+ * Build one page's rail access, minting each rail's batch token on first use.
8
+ *
9
+ * Lazy per rail: a page holding only Stripe purchases never signs an Apple assertion, and a project that
10
+ * enabled one rail never pays for the other two. The mint happens inside `providerFor`, so a rail whose
11
+ * credentials are missing raises `payments/rail_not_configured` at the same place a disabled rail does — one
12
+ * refusal for the caller to handle rather than two.
13
+ */
14
+ function batchedRailAccess(options) {
15
+ const { config, credentials, now } = options;
16
+ const base = options.trust ?? {};
17
+ const built = /* @__PURE__ */ new Map();
18
+ async function build(rail) {
19
+ const trust = { ...base };
20
+ if (rail === "apple" && trust.appleApiToken === void 0) trust.appleApiToken = await mintAppleApiToken(railCredentials(credentials, "apple"), { now });
21
+ if (rail === "google" && trust.googleAccessToken === void 0) trust.googleAccessToken = await mintPlayAccessToken(railCredentials(credentials, "google"), {
22
+ now,
23
+ transport: base.googleTransport
24
+ });
25
+ return resolveRailProvider(rail, config, credentials, trust);
26
+ }
27
+ return { providerFor(rail) {
28
+ const existing = built.get(rail);
29
+ if (existing) return existing;
30
+ const pending = build(rail);
31
+ built.set(rail, pending);
32
+ return pending;
33
+ } };
34
+ }
35
+ //#endregion
36
+ export { batchedRailAccess };
@@ -0,0 +1,161 @@
1
+ import type { D1Database } from "@cloudflare/workers-types";
2
+ import { type AuditEmit } from "@pithy-sh/core/src/audit/recorder";
3
+ import type { PaymentsConfig } from "../config/config";
4
+ import { type PurchaseEnvironment } from "../data/purchase";
5
+ import type { PurchaseProjection } from "../projection/writer";
6
+ import type { PaddleSweepReport } from "./paddleSweep";
7
+ import type { ReconcileRailAccess } from "./railAccess";
8
+ import { type PaymentsReconcileParams } from "./specs";
9
+ /**
10
+ * The reconciliation pass: asking each store what it thinks, and repairing what disagrees.
11
+ *
12
+ * **Why this exists at all.** A webhook-only system rots silently. A deploy that drops requests, a Pub/Sub push
13
+ * subscription pointing at a retired URL, a rotated signing key, a store outage — each loses events with
14
+ * nothing surfacing anywhere. And one drift needs no incident to explain it: a subscription can simply lapse,
15
+ * with the store deciding not to renew and telling nobody. The read path already rechecks `expiresAt` so a
16
+ * lapsed row stops granting, but the row itself stays wrong, the renewal that *did* happen stays unheard, and
17
+ * neither is visible until somebody complains.
18
+ *
19
+ * **Why a Workflow rather than a scheduled pass.** The work is hundreds of calls to rate-limited third-party
20
+ * APIs that fail part way. A plain `scheduled()` handler is wall-clock bounded and restarts from nothing; a
21
+ * Workflow step is journalled, so a run that dies on page forty resumes at page forty rather than re-asking
22
+ * Apple about four thousand subscriptions. That is durable execution earning its place, not decorating a cron.
23
+ *
24
+ * ## The shape of a run
25
+ *
26
+ * Pure over injected dependencies — the step runner, the rails, the clock, and the audit emitter all arrive as
27
+ * parameters — so the whole of it, resuming included, is tested against real D1 with fake rails and no
28
+ * network. `workflows/worker.ts` is the durable shell that supplies the real ones.
29
+ *
30
+ * **Pagination is keyset, never offset**, and that is the correctness argument for the file. The selection
31
+ * matches subscriptions that are near expiry *or* have not been verified lately, and a reconciled row stops
32
+ * matching both — so the result set shrinks underneath the scan. With `LIMIT/OFFSET` that is the classic skip
33
+ * bug: page two starts at row 100 of a set that just lost its first 100 members, and half the catalog is never
34
+ * reconciled, silently. Reading `id > cursor` in id order cannot skip and cannot repeat, whatever the
35
+ * predicate does to the rows behind the cursor.
36
+ *
37
+ * **Each page is one `step.do`, named from a page counter.** Step names must be identical across replays or
38
+ * the journal is unusable, so they are derived from a counter and never from a timestamp or a row id.
39
+ *
40
+ * ## Who a repaired purchase belongs to
41
+ *
42
+ * **Off the row, always.** A pass has no request, so there is no `c.var.auth` and no adopter
43
+ * `resolveSubject` — `entitlement/subjectSeam.ts` resolves a subject from a Hono context, and this file
44
+ * never has one. A rail's `refresh` answer carries no subject either, by construction. So every subject the
45
+ * pass touches is the pair already stored on the purchase it read, and the narrowing a support pass takes is
46
+ * the pair a human typed. See `workflows/worker.ts` for why that is structural rather than an omission.
47
+ *
48
+ * ## What is asked, and what is left alone
49
+ *
50
+ * **Subscriptions only.** A consumable or a non-consumable is bought once and does not drift: the single thing
51
+ * that changes one is a refund, which arrives as its own notification on every rail. Asking a store about
52
+ * every one-time purchase forever would be the bulk of the cost and none of the value.
53
+ *
54
+ * **Not the terminal states.** `expired`, `never_paid`, `refunded`, and `revoked` are states we learned *from a
55
+ * store*, and re-asking cannot change them: a resubscription is a new transaction and therefore a new row on all
56
+ * three rails. Excluding them is also what stops a catalog's dead subscriptions being re-fetched every night
57
+ * forever. A row that says `active` with an expiry in the past is **not** terminal — that is precisely the row
58
+ * whose renewal we may have missed, and it is the one this pass exists to ask about.
59
+ *
60
+ * ## What a failure means
61
+ *
62
+ * A store that **cannot be reached** (`payments/provider_unavailable`) fails the step, so the Workflow retries
63
+ * it — the page is idempotent, so a retry re-asks and re-projects to the same place. Every other refusal is
64
+ * about one purchase or one rail and is counted rather than thrown: an unmapped store state, a SKU no longer
65
+ * in the catalog, a rail switched off since the row was written. A pass that aborted on the first of those
66
+ * would leave the rest of the catalog unreconciled to preserve a tally.
67
+ */
68
+ /** The Workflow step runner, structurally. Injectable, so a test can run a body with no durable execution. */
69
+ export interface ReconcileStep {
70
+ /** Run a named step, or return its journalled result if this instance already completed it. */
71
+ do<T>(name: string, callback: () => Promise<T>): Promise<T>;
72
+ }
73
+ /** What one reconciliation run needs. Every one is a seam. */
74
+ export interface ReconcileDeps {
75
+ /** The app database the `pithy_payments_*` tables live in. */
76
+ d1: D1Database;
77
+ /** The resolved catalog — the writer's product lookup and the rails' enablement come from it. */
78
+ config: PaymentsConfig;
79
+ /** This deployment's store environment. A refreshed sandbox purchase is refused in production, as ever. */
80
+ environment: PurchaseEnvironment;
81
+ /** One page's rail access, built fresh per step so each page mints its own batch tokens. */
82
+ railAccess(now: Date): ReconcileRailAccess;
83
+ /** The clock. */
84
+ now(): Date;
85
+ /** The run id minter, injected so a test is deterministic. Defaults to `crypto.randomUUID`. */
86
+ newId?: () => string;
87
+ /**
88
+ * The audit seam. Drift is emitted through it, because repeated drift on one rail is the signal that the
89
+ * webhook path is broken and a tally in a log line is not something anybody queries. Defaults to core's
90
+ * no-op, which is what a host worker with no audit capability composed gets.
91
+ */
92
+ emit?: AuditEmit;
93
+ /**
94
+ * Fulfillment for a repaired purchase — the ledger credit a `grants` product asks for.
95
+ *
96
+ * A pass that repaired a subscription renewal and did not credit its coins would leave the entitlement right
97
+ * and the balance wrong, and nothing else would ever fix it: the renewal the webhook lost is the same
98
+ * renewal this pass discovered, so whoever discovers a period is the only one who can pay for it.
99
+ *
100
+ * Injectable, and defaults to {@link fulfillPurchase}. Safe to run over an already-credited purchase — the
101
+ * ref is a pure function of the purchase id and the currency, and the ledger's `ref` is UNIQUE, so a credit
102
+ * the webhook already applied is a no-op rather than a double.
103
+ */
104
+ fulfill?: (projection: PurchaseProjection) => Promise<unknown>;
105
+ /**
106
+ * The Paddle events sweep, or undefined to skip it.
107
+ *
108
+ * Injected rather than built here, because it needs Paddle's credentials and its own transport and this
109
+ * module deliberately knows nothing about any single rail — `railAccess` is the seam every other rail is
110
+ * reached through, and the sweep is not a rail method. The Workflow worker supplies it.
111
+ *
112
+ * Undefined on a project that does not sell through Paddle, which is why the step below is skipped
113
+ * entirely rather than run and reported as empty: an empty tally for a rail nobody uses reads as a rail
114
+ * that found nothing, which is a different statement.
115
+ */
116
+ sweepPaddle?: () => Promise<PaddleSweepReport>;
117
+ }
118
+ /** The tally a run reports. */
119
+ export interface ReconcileReport {
120
+ /**
121
+ * The run's id — the row this pass wrote to `pithy_payments_reconcile_runs`, and the value every repair it
122
+ * audited carries as `runId`. Minted before the first page so the events can name it.
123
+ */
124
+ runId: string;
125
+ /** Pages read — one durable step each. */
126
+ pages: number;
127
+ /** Purchases examined. */
128
+ scanned: number;
129
+ /** Purchases whose stored state already matched the store's. */
130
+ unchanged: number;
131
+ /** Purchases whose stored state disagreed. The number that matters: a rising one means webhooks are lost. */
132
+ drifted: number;
133
+ /**
134
+ * Purchases a later period of the same subscription has replaced, settled on this pass.
135
+ *
136
+ * Counted apart from `drifted` deliberately. A rail answers about the family's **current** transaction
137
+ * whichever row it is asked about, so an old period compared against its own successor always looks like
138
+ * drift — and while these were counted as drift, `drifted` measured how long the catalog had been selling
139
+ * rather than whether webhooks were arriving. Settling makes the old row terminal, so it leaves the scan and
140
+ * this number falls back to zero once a catalog's history has been walked through once.
141
+ */
142
+ superseded: number;
143
+ /** Purchases no store could be asked about. */
144
+ skipped: number;
145
+ /**
146
+ * What the Paddle events sweep found, or undefined when it did not run.
147
+ *
148
+ * Undefined rather than a zeroed tally, and the difference is a statement: a rail nobody sells through
149
+ * did not sweep, where a rail that swept and found nothing is a healthy integration. Collapsing the two
150
+ * would make "the webhooks are fine" indistinguishable from "we never looked".
151
+ */
152
+ swept?: PaddleSweepReport;
153
+ /** Purchases a store refused to answer for. */
154
+ failed: number;
155
+ /** Whether the run stopped at its page cap with more of the catalog unexamined. */
156
+ truncated: boolean;
157
+ /** Whether this run only reported. */
158
+ dryRun: boolean;
159
+ }
160
+ export declare function reconcilePayments(deps: ReconcileDeps, step: ReconcileStep, params?: PaymentsReconcileParams): Promise<ReconcileReport>;
161
+ //# sourceMappingURL=reconcile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reconcile.d.ts","sourceRoot":"","sources":["../../src/workflows/reconcile.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,KAAK,SAAS,EAAY,MAAM,mCAAmC,CAAC;AAI7E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAoB,KAAK,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAK9E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAgE,KAAK,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAErH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AAEH,8GAA8G;AAC9G,MAAM,WAAW,aAAa;IAC5B,+FAA+F;IAC/F,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAC7D;AAcD,8DAA8D;AAC9D,MAAM,WAAW,aAAa;IAC5B,8DAA8D;IAC9D,EAAE,EAAE,UAAU,CAAC;IACf,iGAAiG;IACjG,MAAM,EAAE,cAAc,CAAC;IACvB,2GAA2G;IAC3G,WAAW,EAAE,mBAAmB,CAAC;IACjC,4FAA4F;IAC5F,UAAU,CAAC,GAAG,EAAE,IAAI,GAAG,mBAAmB,CAAC;IAC3C,iBAAiB;IACjB,GAAG,IAAI,IAAI,CAAC;IACZ,+FAA+F;IAC/F,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC;IACrB;;;;OAIG;IACH,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB;;;;;;;;;;OAUG;IACH,OAAO,CAAC,EAAE,CAAC,UAAU,EAAE,kBAAkB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAC/D;;;;;;;;;;OAUG;IACH,WAAW,CAAC,EAAE,MAAM,OAAO,CAAC,iBAAiB,CAAC,CAAC;CAChD;AAyBD,+BAA+B;AAC/B,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IACd,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAC;IACd,0BAA0B;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAC;IAClB,6GAA6G;IAC7G,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;;;;OAQG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB,+CAA+C;IAC/C,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,iBAAiB,CAAC;IAC1B,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAC;IACf,mFAAmF;IACnF,SAAS,EAAE,OAAO,CAAC;IACnB,sCAAsC;IACtC,MAAM,EAAE,OAAO,CAAC;CACjB;AA0PD,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,aAAa,EACnB,IAAI,EAAE,aAAa,EACnB,MAAM,GAAE,uBAA4B,GACnC,OAAO,CAAC,eAAe,CAAC,CAyL1B"}
@@ -0,0 +1,352 @@
1
+ import { PaymentsPurchase } from "../data/purchase.js";
2
+ import { recordReconcileRun } from "../data/reconcileRun.js";
3
+ import { PAYMENTS_PURCHASES_TABLE, paymentsDatabase } from "../data/tables.js";
4
+ import { PaymentsAuditActions } from "../audit/actions.js";
5
+ import { fulfillPurchase } from "../grants/apply.js";
6
+ import { projectPurchase } from "../projection/writer.js";
7
+ import "./specs.js";
8
+ import { PithyError } from "@pithy-sh/core/src/error/pithyError";
9
+ import { SQLiteDate } from "@pithy-sh/core/src/data/codecs";
10
+ import { noopEmit } from "@pithy-sh/core/src/audit/recorder";
11
+ //#region src/workflows/reconcile.ts
12
+ /** Purchases per page when the caller names none. Bounded so one page fits inside one minted token's life. */
13
+ const DEFAULT_PAGE_SIZE = 100;
14
+ /** Pages per run when the caller names none. Ten thousand purchases is a long pass and a sane ceiling. */
15
+ const DEFAULT_MAX_PAGES = 100;
16
+ /**
17
+ * The statuses a pass never re-asks about. Each was reported by a store and cannot become anything else on the
18
+ * same transaction — see the module doc on why `active` with a past expiry is deliberately not among them.
19
+ */
20
+ const TERMINAL_STATUSES = [
21
+ "expired",
22
+ "never_paid",
23
+ "refunded",
24
+ "revoked"
25
+ ];
26
+ /** The state a drift comparison reads off a row or a refreshed event. */
27
+ function stateOf(value) {
28
+ return {
29
+ status: value.status,
30
+ expiresAt: value.expiresAt?.getTime() ?? null
31
+ };
32
+ }
33
+ /** Whether the store's answer says something different from what is stored. */
34
+ function drifted(before, after) {
35
+ return before.status !== after.status || before.expiresAt !== after.expiresAt;
36
+ }
37
+ /**
38
+ * One page of purchases to reconcile.
39
+ *
40
+ * The predicate is the pass's whole selection policy: a live subscription that is near or past its expiry, or
41
+ * one nothing has verified lately. `id > cursor` in id order is what makes it resumable — see the module doc.
42
+ */
43
+ async function readPage(deps, params, cursor, cutoffs, limit) {
44
+ let query = paymentsDatabase(deps.d1).selectFrom(PAYMENTS_PURCHASES_TABLE).selectAll().where("type", "=", "subscription").where("status", "not in", [...TERMINAL_STATUSES]).where((eb) => eb.or([eb("expiresAt", "<", SQLiteDate.encode(cutoffs.expiring)), eb("updatedAt", "<", SQLiteDate.encode(cutoffs.stale))])).orderBy("id").limit(limit);
45
+ if (cursor !== null) query = query.where("id", ">", cursor);
46
+ /**
47
+ * The holder narrowing — **both halves in one guard, or no narrowing at all**.
48
+ *
49
+ * The schema already refuses half a subject, so this is the second line rather than the first. It is
50
+ * written as one condition because the failure it prevents is the tempting one: two independent `if`s
51
+ * would let `subjectId` alone reach the query, and `where subject_id = 'acme'` matches the user *and* the
52
+ * organization called `acme` — nothing keeps those id spaces apart. The support pass would then reconcile
53
+ * a stranger's purchases and report the count as the account it was asked about.
54
+ *
55
+ * Widening when only one half arrives is the safe direction: a pass over the whole catalog is slow and
56
+ * correct, where a pass over the wrong holder is fast and wrong.
57
+ */
58
+ if (params.subjectType !== void 0 && params.subjectId !== void 0) query = query.where("subjectType", "=", params.subjectType).where("subjectId", "=", params.subjectId);
59
+ if (params.rail !== void 0) query = query.where("rail", "=", params.rail);
60
+ return (await query.execute()).map((row) => PaymentsPurchase.parse(row));
61
+ }
62
+ /**
63
+ * Reconcile one purchase: ask its rail, compare, and project any disagreement through the **same writer** the
64
+ * webhook and the client submission use.
65
+ *
66
+ * Projecting through the writer rather than updating the row is what keeps the whole design's central claim
67
+ * true — one idempotent projection, three triggers, one row. It also means a repair re-derives the entitlement
68
+ * rows in the same D1 transaction, so a subscription found to have lapsed stops granting in the same commit
69
+ * that records the lapse.
70
+ */
71
+ async function reconcileOne(deps, runId, access, purchase, now, dryRun) {
72
+ let refreshed;
73
+ try {
74
+ refreshed = await (await access.providerFor(purchase.rail)).refresh(purchase, { now });
75
+ } catch (cause) {
76
+ if (cause instanceof PithyError && cause.payload.code === "payments/provider_unavailable") throw cause;
77
+ return "failed";
78
+ }
79
+ if (refreshed === void 0) return "skipped";
80
+ /**
81
+ * The store answered about a **different** transaction, which means this row is a period that has ended and
82
+ * the one that replaced it is elsewhere in the table.
83
+ *
84
+ * Every rail's `refresh` looks up the subscription family, not the row it was handed — it has to, because a
85
+ * renewal is a new transaction and the family is the only handle a store offers. So comparing this row
86
+ * against that answer compares two different purchases, and it always disagrees: the old period's expiry is
87
+ * in the past and the new one's is not. That disagreement is not drift, and counting it as drift is what made
88
+ * a year-old subscriber contribute twelve permanent "repairs" every night.
89
+ *
90
+ * Settling the row once is what ends it. Projecting it `expired` at its own expiry puts it in
91
+ * `TERMINAL_STATUSES`, so the next pass does not select it, does not ask the store about it, and does not
92
+ * report it — and the entitlement is unaffected, because the successor grants it and the derivation reads
93
+ * every purchase for the key.
94
+ */
95
+ if (refreshed.providerTransactionId !== purchase.providerTransactionId) {
96
+ if (dryRun) return "superseded";
97
+ try {
98
+ await projectPurchase(deps.d1, {
99
+ ...purchase,
100
+ status: "expired",
101
+ providerEventAt: now,
102
+ payload: { supersededBy: refreshed.providerTransactionId }
103
+ }, {
104
+ config: deps.config,
105
+ environment: deps.environment,
106
+ now
107
+ });
108
+ } catch (cause) {
109
+ if (cause instanceof PithyError) return "failed";
110
+ throw cause;
111
+ }
112
+ return "superseded";
113
+ }
114
+ const before = stateOf(purchase);
115
+ if (dryRun) return drifted(before, stateOf({
116
+ status: refreshed.status,
117
+ expiresAt: refreshed.expiresAt ?? null
118
+ })) ? "drifted" : "unchanged";
119
+ let projection;
120
+ try {
121
+ projection = await projectPurchase(deps.d1, {
122
+ ...refreshed,
123
+ subjectType: purchase.subjectType,
124
+ subjectId: purchase.subjectId
125
+ }, {
126
+ config: deps.config,
127
+ environment: deps.environment,
128
+ now
129
+ });
130
+ } catch (cause) {
131
+ if (cause instanceof PithyError) return "failed";
132
+ throw cause;
133
+ }
134
+ const after = stateOf(projection.purchase);
135
+ if (!drifted(before, after)) return "unchanged";
136
+ await emitDrift(deps, runId, purchase, before, after);
137
+ /**
138
+ * Fulfillment, on drift only.
139
+ *
140
+ * Every rail dates a refresh `now` — it has to, or the monotonic write rule would discard the repair — so
141
+ * the writer reports `updated` for every row a pass touches. Keying fulfillment on that outcome would mean
142
+ * ten thousand ledger calls per pass, each one a no-op against a ref that already exists. Drift is the
143
+ * signal that actually distinguishes a repair from a confirmation, and a repair is the only one that can owe
144
+ * anybody coins.
145
+ */
146
+ const fulfill = deps.fulfill ?? ((repaired) => fulfillPurchase(deps.d1, repaired, {
147
+ config: deps.config,
148
+ emit: deps.emit,
149
+ now: () => now.getTime()
150
+ }));
151
+ try {
152
+ await fulfill(projection);
153
+ } catch (cause) {
154
+ if (cause instanceof PithyError) return "failed";
155
+ throw cause;
156
+ }
157
+ return "drifted";
158
+ }
159
+ /** Record a repaired disagreement. The one audit event that is about this deployment rather than a caller. */
160
+ async function emitDrift(deps, runId, purchase, before, after) {
161
+ await (deps.emit ?? noopEmit)({
162
+ action: PaymentsAuditActions.purchaseReconciled,
163
+ outcome: "success",
164
+ severity: "warning",
165
+ actorType: "service",
166
+ actorId: PaymentsAuditActions.purchaseReconciled,
167
+ resourceType: "purchase",
168
+ resourceId: purchase.id,
169
+ metadata: {
170
+ runId,
171
+ rail: purchase.rail,
172
+ productId: purchase.productId,
173
+ from: before.status,
174
+ to: after.status,
175
+ expiryChanged: before.expiresAt !== after.expiresAt
176
+ }
177
+ });
178
+ }
179
+ /**
180
+ * Run one reconciliation pass. Idempotent by construction — a second run over a reconciled catalog finds every
181
+ * row already matching and writes nothing — so a retried Workflow step, a manual trigger after a cron, and a
182
+ * nervous operator running it twice all reach the same place.
183
+ */
184
+ /** What a sweep that could not run reports. Never written to a report — see `ReconcileReport.swept`. */
185
+ const EMPTY_SWEEP = {
186
+ read: 0,
187
+ projected: 0,
188
+ ignored: 0,
189
+ orphaned: [],
190
+ duplicate: 0,
191
+ failed: 0,
192
+ quarantined: [],
193
+ cursor: null,
194
+ gap: null
195
+ };
196
+ async function reconcilePayments(deps, step, params = {}) {
197
+ const limit = params.pageSize ?? DEFAULT_PAGE_SIZE;
198
+ const maxPages = params.maxPages ?? DEFAULT_MAX_PAGES;
199
+ const dryRun = params.dryRun === true;
200
+ /**
201
+ * The run's identity and its clock, minted **once, in one journalled step**, and read back from the step's
202
+ * return value.
203
+ *
204
+ * **Why a step.** A Workflow does not resume inside the step it died in; it re-executes this body from the
205
+ * top and serves every completed step from the journal. So anything the body *computes* answers differently
206
+ * on a resume, while everything the journal holds does not — and a run's id and its start instant are both
207
+ * things every page behind the interruption has already been written under.
208
+ *
209
+ * The id was moved here first (#328): a second mint made the run record name an id no repair carried, which
210
+ * broke the runs table's only join. The clock was left in the body one release longer, and it was the same
211
+ * defect one field over (#331) — `startedAt` was recomputed on resume, so a run interrupted at nine and
212
+ * resumed at three claimed to have begun six hours *after* the repairs it names. A pass's start instant is
213
+ * not "when this attempt got going"; it is when the pass got going.
214
+ *
215
+ * It is also the comparison instant every page judges against, and journalling it is right there too: the
216
+ * pages already run were selected and dated against it, and a resume that widened the windows underneath
217
+ * them would make one pass two different queries.
218
+ *
219
+ * Epoch milliseconds rather than a `Date`, because a journal round-trips JSON and a `Date` would come back
220
+ * a string on the resume and an object on the first pass — a difference that shows up as a type error six
221
+ * frames away rather than here.
222
+ */
223
+ const context = await step.do("start-run", async () => ({
224
+ runId: (deps.newId ?? (() => crypto.randomUUID()))(),
225
+ nowMs: deps.now().getTime()
226
+ }));
227
+ const runId = context.runId;
228
+ const now = new Date(context.nowMs);
229
+ const cutoffs = {
230
+ expiring: new Date(now.getTime() + (params.expiringWithinSeconds ?? 172800) * 1e3),
231
+ stale: /* @__PURE__ */ new Date(now.getTime() - (params.staleAfterSeconds ?? 604800) * 1e3)
232
+ };
233
+ const report = {
234
+ runId,
235
+ pages: 0,
236
+ scanned: 0,
237
+ unchanged: 0,
238
+ drifted: 0,
239
+ superseded: 0,
240
+ skipped: 0,
241
+ failed: 0,
242
+ truncated: false,
243
+ dryRun
244
+ };
245
+ /**
246
+ * The Paddle events sweep, before the pages — the repair `refresh` cannot make.
247
+ *
248
+ * `refresh` re-reads rows this deployment already holds, so a purchase whose webhook was never delivered
249
+ * and which no client submitted is invisible to it forever. Paddle publishes an account-wide event
250
+ * stream, so it can be found; no other rail here can do this.
251
+ *
252
+ * **First, so what it discovers is then reconciled by the same run.** A subscription the sweep projects
253
+ * is a row the pages below will consider, which is one pass rather than two.
254
+ *
255
+ * Its own `step.do`, so a transient fault retries the sweep rather than losing a whole run's pages. It
256
+ * is skipped entirely when the pass was narrowed to another rail — `--rail stripe` means Stripe.
257
+ */
258
+ if (deps.sweepPaddle && (params.rail === void 0 || params.rail === "paddle")) {
259
+ const swept = await step.do("sweep-paddle", async () => await deps.sweepPaddle?.() ?? EMPTY_SWEEP);
260
+ report.swept = swept;
261
+ if (swept.gap !== null) await (deps.emit ?? noopEmit)({
262
+ action: PaymentsAuditActions.purchaseReconciled,
263
+ outcome: "failure",
264
+ severity: "warning",
265
+ actorType: "service",
266
+ actorId: "paddle",
267
+ metadata: {
268
+ rail: "paddle",
269
+ runId,
270
+ reason: "sweep_gap",
271
+ detail: swept.gap
272
+ }
273
+ });
274
+ }
275
+ let cursor = null;
276
+ let page = 0;
277
+ for (;;) {
278
+ page += 1;
279
+ const after = cursor;
280
+ const result = await step.do(`page-${String(page).padStart(6, "0")}`, async () => {
281
+ const rows = await readPage(deps, params, after, cutoffs, limit);
282
+ if (rows.length === 0) return {
283
+ cursor: after,
284
+ scanned: 0,
285
+ unchanged: 0,
286
+ drifted: 0,
287
+ superseded: 0,
288
+ skipped: 0,
289
+ failed: 0,
290
+ done: true
291
+ };
292
+ const access = deps.railAccess(now);
293
+ const tally = {
294
+ unchanged: 0,
295
+ drifted: 0,
296
+ superseded: 0,
297
+ skipped: 0,
298
+ failed: 0
299
+ };
300
+ for (const purchase of rows) tally[await reconcileOne(deps, runId, access, purchase, now, dryRun)] += 1;
301
+ return {
302
+ cursor: rows[rows.length - 1].id,
303
+ scanned: rows.length,
304
+ ...tally,
305
+ done: rows.length < limit
306
+ };
307
+ });
308
+ report.pages = page;
309
+ report.scanned += result.scanned;
310
+ report.unchanged += result.unchanged;
311
+ report.drifted += result.drifted;
312
+ report.superseded += result.superseded;
313
+ report.skipped += result.skipped;
314
+ report.failed += result.failed;
315
+ cursor = result.cursor;
316
+ if (result.done) break;
317
+ if (page >= maxPages) {
318
+ report.truncated = true;
319
+ break;
320
+ }
321
+ }
322
+ /**
323
+ * The run, kept.
324
+ *
325
+ * **A run that repaired nothing is written too**, and that is the load-bearing half: *"it ran and found
326
+ * nothing"* is the answer to *"is this integration healthy"*, and a table holding only the exceptional
327
+ * passes makes silence ambiguous with a cron that stopped firing — which is the failure the table exists
328
+ * to make visible.
329
+ *
330
+ * Its own `step.do`, so a D1 hiccup retries the write rather than losing the record of a pass that did
331
+ * real work. The writer is idempotent on the run's id, so a replayed step lands on the row it already
332
+ * wrote instead of counting one pass twice. It is the last step and never the first: a row claiming a
333
+ * finish time before the pass had one would be a lie the health read would then repeat.
334
+ *
335
+ * `finishedAt` is read from the clock again rather than reused from `now`, because `now` is the pass's
336
+ * comparison instant and a run over ten thousand purchases is not instantaneous.
337
+ */
338
+ await step.do("record-run", async () => {
339
+ await recordReconcileRun(deps.d1, {
340
+ id: runId,
341
+ startedAt: now,
342
+ finishedAt: deps.now(),
343
+ environment: deps.environment,
344
+ rail: params.rail ?? null,
345
+ report
346
+ }, { now: deps.now() });
347
+ return null;
348
+ });
349
+ return report;
350
+ }
351
+ //#endregion
352
+ export { reconcilePayments };
@@ -0,0 +1,28 @@
1
+ import type { AuditEmit } from "@pithy-sh/core/src/audit/recorder";
2
+ import type { Logger } from "@pithy-sh/core/src/logger/logger";
3
+ import type { ReconcileReport } from "./reconcile";
4
+ /**
5
+ * The audit emitter a standalone host can honestly offer: a structured record per repaired purchase.
6
+ *
7
+ * A host worker composes no capabilities, so there is no `@pithy-sh/audit` recorder to reach and no
8
+ * `c.var.emit` to inherit — writing to `pithy_audit_events` from here would mean this package reaching into
9
+ * another capability's tables, which is exactly what the seam exists to prevent. A logged event is what the
10
+ * seam degrades to everywhere else it is uncomposed, and it keeps the drift visible where an operator reads
11
+ * this worker's output. The event shape is core's, so a recorder wired in later takes the identical input.
12
+ *
13
+ * It takes the run's logger rather than building its own, so every event carries the instance the repair
14
+ * happened in — without that, a trail read out of Workers Logs cannot be attributed to a pass.
15
+ */
16
+ export declare function auditLogEmit(log: Logger): AuditEmit;
17
+ /**
18
+ * The tally, as one record. The run's only visible output: a pass whose findings are invisible is a pass
19
+ * nobody can tell has stopped working.
20
+ *
21
+ * Three levels, and the distinction is the point of logging it at all. `failed` counts purchases a store
22
+ * refused to answer for — a failure that was observed, so an operator has something to chase. `drifted`
23
+ * and `truncated` are the degraded-but-continuing pair: the first says webhooks are being dropped and the
24
+ * repair is covering for them, the second says the page cap stopped the pass with catalog left unread. A
25
+ * clean pass is routine, and a nightly job that reports routine at `warn` teaches an operator to ignore it.
26
+ */
27
+ export declare function logReconcileReport(log: Logger, report: ReconcileReport): void;
28
+ //# sourceMappingURL=report.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"report.d.ts","sourceRoot":"","sources":["../../src/workflows/report.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAE/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAqBnD;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAQnD;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,IAAI,CAG7E"}
@@ -0,0 +1,54 @@
1
+ //#region src/workflows/report.ts
2
+ /**
3
+ * What a reconcile pass says about itself: its tally, and each repair it had to make.
4
+ *
5
+ * Both are pure functions of a logger and a value, and both used to live in `worker.ts` beside the
6
+ * Workflow class that calls them. That module imports `cloudflare:workers`, which resolves in workerd
7
+ * and nowhere else, so everything it exported was reachable only from inside the Workers runtime — a
8
+ * Node-side caller taking one of these would have taken the whole runtime module with it and failed
9
+ * with `Could not load pithy.config.ts`, naming the config rather than the import.
10
+ *
11
+ * That is #172 and #180, twice, and neither was noticed until somebody accepted the offer. The shape
12
+ * both were fixed into is this one: a sibling module with no runtime import, which the runtime module
13
+ * imports from. `configEntrypoints.test.ts` states the invariant and holds every runtime module to it.
14
+ *
15
+ * Nothing here needs a binding, a request, or a Workers global, which is why its tests are node tests.
16
+ */
17
+ /** An audit event ranks itself; this is that rank as a log level. */
18
+ const AUDIT_LEVEL = {
19
+ info: "info",
20
+ warning: "warn",
21
+ critical: "error"
22
+ };
23
+ /**
24
+ * The audit emitter a standalone host can honestly offer: a structured record per repaired purchase.
25
+ *
26
+ * A host worker composes no capabilities, so there is no `@pithy-sh/audit` recorder to reach and no
27
+ * `c.var.emit` to inherit — writing to `pithy_audit_events` from here would mean this package reaching into
28
+ * another capability's tables, which is exactly what the seam exists to prevent. A logged event is what the
29
+ * seam degrades to everywhere else it is uncomposed, and it keeps the drift visible where an operator reads
30
+ * this worker's output. The event shape is core's, so a recorder wired in later takes the identical input.
31
+ *
32
+ * It takes the run's logger rather than building its own, so every event carries the instance the repair
33
+ * happened in — without that, a trail read out of Workers Logs cannot be attributed to a pass.
34
+ */
35
+ function auditLogEmit(log) {
36
+ return async (event) => {
37
+ log[AUDIT_LEVEL[event.severity ?? "info"]]("audit event, no recorder composed", { ...event });
38
+ };
39
+ }
40
+ /**
41
+ * The tally, as one record. The run's only visible output: a pass whose findings are invisible is a pass
42
+ * nobody can tell has stopped working.
43
+ *
44
+ * Three levels, and the distinction is the point of logging it at all. `failed` counts purchases a store
45
+ * refused to answer for — a failure that was observed, so an operator has something to chase. `drifted`
46
+ * and `truncated` are the degraded-but-continuing pair: the first says webhooks are being dropped and the
47
+ * repair is covering for them, the second says the page cap stopped the pass with catalog left unread. A
48
+ * clean pass is routine, and a nightly job that reports routine at `warn` teaches an operator to ignore it.
49
+ */
50
+ function logReconcileReport(log, report) {
51
+ log[report.failed > 0 ? "error" : report.drifted > 0 || report.truncated ? "warn" : "info"]("reconcile pass complete", { ...report });
52
+ }
53
+ //#endregion
54
+ export { auditLogEmit, logReconcileReport };