payment-kit 1.29.1 → 1.29.2

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 (310) hide show
  1. package/api/dev.ts +41 -2
  2. package/api/hono.d.ts +42 -0
  3. package/api/node-sqlite.d.ts +12 -0
  4. package/api/src/bootstrap.ts +36 -0
  5. package/api/src/crons/base.ts +3 -3
  6. package/api/src/crons/currency.ts +1 -1
  7. package/api/src/crons/index.ts +27 -24
  8. package/api/src/crons/metering-subscription-detection.ts +1 -1
  9. package/api/src/crons/overdue-detection.ts +2 -2
  10. package/api/src/crons/retry-pending-events.ts +6 -0
  11. package/api/src/index.ts +22 -161
  12. package/api/src/integrations/app-store/client.ts +3 -4
  13. package/api/src/integrations/app-store/handlers/subscription.ts +7 -7
  14. package/api/src/integrations/app-store/signed-data-verifier.ts +3 -2
  15. package/api/src/integrations/arcblock/token.ts +21 -7
  16. package/api/src/integrations/google-play/handlers/subscription.ts +6 -6
  17. package/api/src/integrations/google-play/handlers/voided.ts +2 -2
  18. package/api/src/integrations/google-play/verify.ts +3 -2
  19. package/api/src/integrations/iap-reconcile.ts +3 -5
  20. package/api/src/integrations/stripe/handlers/invoice.ts +2 -2
  21. package/api/src/integrations/stripe/handlers/subscription.ts +3 -3
  22. package/api/src/libs/archive/query.ts +19 -0
  23. package/api/src/libs/audit.ts +61 -4
  24. package/api/src/libs/auth.ts +99 -38
  25. package/api/src/libs/context.ts +78 -1
  26. package/api/src/libs/currency.ts +2 -2
  27. package/api/src/libs/dayjs.ts +8 -2
  28. package/api/src/libs/drivers/auth-storage.ts +118 -0
  29. package/api/src/libs/drivers/cron.ts +264 -0
  30. package/api/src/libs/drivers/db.ts +170 -0
  31. package/api/src/libs/drivers/identity.ts +81 -0
  32. package/api/src/libs/drivers/index.ts +40 -0
  33. package/api/src/libs/drivers/locks.ts +226 -0
  34. package/api/src/libs/drivers/migrate-runner.ts +70 -0
  35. package/api/src/libs/drivers/queue.ts +104 -0
  36. package/api/src/libs/drivers/secrets.ts +194 -0
  37. package/api/src/libs/env.ts +170 -54
  38. package/api/src/libs/exchange-rate/service.ts +7 -6
  39. package/api/src/libs/http-fetch-adapter.ts +50 -0
  40. package/api/src/libs/invoice.ts +1 -1
  41. package/api/src/libs/lock.ts +51 -47
  42. package/api/src/libs/logger.ts +48 -8
  43. package/api/src/libs/notification/index.ts +1 -1
  44. package/api/src/libs/notification/template/customer-credit-low-balance.ts +2 -1
  45. package/api/src/libs/notification/template/customer-revenue-succeeded.ts +1 -1
  46. package/api/src/libs/notification/template/customer-reward-succeeded.ts +1 -1
  47. package/api/src/libs/overdraft-protection.ts +1 -1
  48. package/api/src/libs/payout.ts +1 -1
  49. package/api/src/libs/queue/index.ts +259 -52
  50. package/api/src/libs/queue/runtime.ts +175 -0
  51. package/api/src/libs/resource.ts +3 -3
  52. package/api/src/libs/secrets.ts +38 -0
  53. package/api/src/libs/session.ts +3 -2
  54. package/api/src/libs/subscription.ts +5 -5
  55. package/api/src/libs/tenant.ts +92 -0
  56. package/api/src/libs/url.ts +3 -3
  57. package/api/src/libs/util.ts +21 -13
  58. package/api/src/middlewares/hono/cdn.ts +63 -0
  59. package/api/src/middlewares/hono/context.ts +73 -0
  60. package/api/src/middlewares/hono/csrf.ts +72 -0
  61. package/api/src/middlewares/hono/fallback.ts +194 -0
  62. package/api/src/middlewares/hono/pipeline.ts +73 -0
  63. package/api/src/middlewares/hono/resource-mount.ts +42 -0
  64. package/api/src/middlewares/hono/resource.ts +63 -0
  65. package/api/src/middlewares/hono/security.ts +214 -0
  66. package/api/src/middlewares/hono/session.ts +114 -0
  67. package/api/src/middlewares/hono/xss.ts +61 -0
  68. package/api/src/queues/auto-recharge.ts +12 -10
  69. package/api/src/queues/checkout-session.ts +17 -12
  70. package/api/src/queues/credit-consume.ts +40 -36
  71. package/api/src/queues/credit-grant.ts +25 -18
  72. package/api/src/queues/credit-reconciliation.ts +7 -5
  73. package/api/src/queues/discount-status.ts +9 -6
  74. package/api/src/queues/event.ts +12 -4
  75. package/api/src/queues/exchange-rate-health.ts +49 -30
  76. package/api/src/queues/invoice.ts +18 -15
  77. package/api/src/queues/notification.ts +14 -7
  78. package/api/src/queues/payment.ts +41 -28
  79. package/api/src/queues/payout.ts +9 -5
  80. package/api/src/queues/refund.ts +18 -12
  81. package/api/src/queues/subscription.ts +83 -53
  82. package/api/src/queues/token-transfer.ts +15 -10
  83. package/api/src/queues/usage-record.ts +8 -5
  84. package/api/src/queues/vendors/commission.ts +7 -5
  85. package/api/src/queues/vendors/fulfillment-coordinator.ts +17 -13
  86. package/api/src/queues/vendors/fulfillment.ts +4 -2
  87. package/api/src/queues/vendors/return-processor.ts +5 -3
  88. package/api/src/queues/vendors/return-scanner.ts +5 -4
  89. package/api/src/queues/vendors/status-check.ts +10 -7
  90. package/api/src/queues/webhook.ts +60 -32
  91. package/api/src/routes/connect/shared.ts +1 -2
  92. package/api/src/routes/connect/subscribe.ts +3 -3
  93. package/api/src/routes/{archive.ts → hono/archive.ts} +69 -64
  94. package/api/src/routes/{auto-recharge-configs.ts → hono/auto-recharge-configs.ts} +39 -28
  95. package/api/src/routes/{checkout-sessions.ts → hono/checkout-sessions.ts} +790 -923
  96. package/api/src/routes/{coupons.ts → hono/coupons.ts} +93 -76
  97. package/api/src/routes/{credit-grants.ts → hono/credit-grants.ts} +140 -126
  98. package/api/src/routes/hono/credit-tokens.ts +43 -0
  99. package/api/src/routes/{credit-transactions.ts → hono/credit-transactions.ts} +37 -29
  100. package/api/src/routes/{customers.ts → hono/customers.ts} +193 -223
  101. package/api/src/routes/{donations.ts → hono/donations.ts} +41 -32
  102. package/api/src/routes/{entitlements.ts → hono/entitlements.ts} +28 -25
  103. package/api/src/routes/{events.ts → hono/events.ts} +107 -71
  104. package/api/src/routes/{exchange-rate-providers.ts → hono/exchange-rate-providers.ts} +138 -126
  105. package/api/src/routes/hono/exchange-rates.ts +77 -0
  106. package/api/src/routes/hono/index.ts +115 -0
  107. package/api/src/routes/{integrations → hono/integrations}/app-store.ts +68 -48
  108. package/api/src/routes/{integrations → hono/integrations}/google-play.ts +78 -58
  109. package/api/src/routes/hono/integrations/stripe.ts +74 -0
  110. package/api/src/routes/{invoices.ts → hono/invoices.ts} +253 -244
  111. package/api/src/routes/{meter-events.ts → hono/meter-events.ts} +120 -110
  112. package/api/src/routes/hono/meters.ts +288 -0
  113. package/api/src/routes/hono/passports.ts +73 -0
  114. package/api/src/routes/{payment-currencies.ts → hono/payment-currencies.ts} +219 -197
  115. package/api/src/routes/{payment-intents.ts → hono/payment-intents.ts} +136 -132
  116. package/api/src/routes/{payment-links.ts → hono/payment-links.ts} +145 -128
  117. package/api/src/routes/{payment-methods.ts → hono/payment-methods.ts} +125 -93
  118. package/api/src/routes/{payment-stats.ts → hono/payment-stats.ts} +30 -25
  119. package/api/src/routes/{payouts.ts → hono/payouts.ts} +55 -47
  120. package/api/src/routes/{prices.ts → hono/prices.ts} +265 -242
  121. package/api/src/routes/{pricing-table.ts → hono/pricing-table.ts} +94 -87
  122. package/api/src/routes/{products.ts → hono/products.ts} +172 -159
  123. package/api/src/routes/{promotion-codes.ts → hono/promotion-codes.ts} +207 -185
  124. package/api/src/routes/hono/redirect.ts +24 -0
  125. package/api/src/routes/{refunds.ts → hono/refunds.ts} +96 -80
  126. package/api/src/routes/{settings.ts → hono/settings.ts} +64 -55
  127. package/api/src/routes/{subscription-items.ts → hono/subscription-items.ts} +64 -57
  128. package/api/src/routes/{subscriptions.ts → hono/subscriptions.ts} +475 -528
  129. package/api/src/routes/{tax-rates.ts → hono/tax-rates.ts} +71 -70
  130. package/api/src/routes/hono/tool.ts +69 -0
  131. package/api/src/routes/{usage-records.ts → hono/usage-records.ts} +47 -42
  132. package/api/src/routes/{vendor.ts → hono/vendor.ts} +315 -167
  133. package/api/src/routes/{webhook-attempts.ts → hono/webhook-attempts.ts} +17 -13
  134. package/api/src/routes/hono/webhook-endpoints.ts +126 -0
  135. package/api/src/service.ts +667 -0
  136. package/api/src/store/migrations/20230911-seeding.ts +2 -1
  137. package/api/src/store/migrations/20260609-remove-did-space-jobs.ts +23 -0
  138. package/api/src/store/migrations/20260610-tenant-columns.ts +40 -0
  139. package/api/src/store/migrations/20260611-tenant-backfill.ts +33 -0
  140. package/api/src/store/models/auto-recharge-config.ts +22 -10
  141. package/api/src/store/models/checkout-session.ts +15 -14
  142. package/api/src/store/models/coupon.ts +29 -20
  143. package/api/src/store/models/credit-grant.ts +38 -29
  144. package/api/src/store/models/credit-transaction.ts +32 -21
  145. package/api/src/store/models/customer.ts +19 -17
  146. package/api/src/store/models/discount.ts +11 -2
  147. package/api/src/store/models/entitlement-grant.ts +21 -9
  148. package/api/src/store/models/entitlement-product.ts +21 -9
  149. package/api/src/store/models/entitlement.ts +19 -10
  150. package/api/src/store/models/event.ts +18 -9
  151. package/api/src/store/models/exchange-rate-provider.ts +17 -4
  152. package/api/src/store/models/invoice-item.ts +18 -9
  153. package/api/src/store/models/invoice.ts +16 -8
  154. package/api/src/store/models/meter-event.ts +27 -9
  155. package/api/src/store/models/meter.ts +31 -22
  156. package/api/src/store/models/payment-currency.ts +25 -8
  157. package/api/src/store/models/payment-intent.ts +15 -6
  158. package/api/src/store/models/payment-link.ts +15 -6
  159. package/api/src/store/models/payment-method.ts +38 -22
  160. package/api/src/store/models/payment-stat.ts +18 -9
  161. package/api/src/store/models/payout.ts +15 -6
  162. package/api/src/store/models/price-quote.ts +17 -8
  163. package/api/src/store/models/price.ts +24 -12
  164. package/api/src/store/models/pricing-table.ts +29 -20
  165. package/api/src/store/models/product-vendor.ts +20 -10
  166. package/api/src/store/models/product.ts +15 -6
  167. package/api/src/store/models/promotion-code.ts +14 -6
  168. package/api/src/store/models/refund.ts +15 -6
  169. package/api/src/store/models/revenue-snapshot.ts +21 -9
  170. package/api/src/store/models/setting.ts +18 -9
  171. package/api/src/store/models/setup-intent.ts +36 -27
  172. package/api/src/store/models/subscription-item.ts +21 -9
  173. package/api/src/store/models/subscription-schedule.ts +21 -9
  174. package/api/src/store/models/subscription.ts +21 -10
  175. package/api/src/store/models/tax-rate.ts +29 -21
  176. package/api/src/store/models/usage-record.ts +11 -2
  177. package/api/src/store/models/webhook-attempt.ts +18 -9
  178. package/api/src/store/models/webhook-endpoint.ts +18 -9
  179. package/api/src/store/scoped-core.ts +55 -0
  180. package/api/src/store/scoped.ts +247 -0
  181. package/api/src/store/sequelize.ts +66 -22
  182. package/api/src/store/sql-migrations.ts +20 -0
  183. package/api/src/store/tenant-backfill.ts +260 -0
  184. package/api/src/store/tenant-model.ts +124 -0
  185. package/api/src/store/tenant-tables.ts +50 -0
  186. package/api/tests/embedded/embedded-multi-mode-d3.spec.ts +257 -0
  187. package/api/tests/fixtures/bare-query-violation.ts +13 -0
  188. package/api/tests/fixtures/core-env-violation.ts +10 -0
  189. package/api/tests/fixtures/host-read-violation.ts +19 -0
  190. package/api/tests/fixtures/tenants.ts +4 -0
  191. package/api/tests/integrations/iap-tenant.spec.ts +284 -0
  192. package/api/tests/libs/archive-query.spec.ts +26 -0
  193. package/api/tests/libs/audit-tenant.spec.ts +153 -0
  194. package/api/tests/libs/context.spec.ts +204 -0
  195. package/api/tests/libs/core-config.spec.ts +115 -0
  196. package/api/tests/libs/cron-driver-d2.spec.ts +237 -0
  197. package/api/tests/libs/crons-conservation-d2.spec.ts +52 -0
  198. package/api/tests/libs/lock-tenant.spec.ts +66 -0
  199. package/api/tests/libs/scoped.spec.ts +222 -0
  200. package/api/tests/libs/secrets-facade.spec.ts +52 -0
  201. package/api/tests/libs/tenancy-slot-authority.spec.ts +209 -0
  202. package/api/tests/libs/tenant-middleware.spec.ts +42 -0
  203. package/api/tests/libs/tenant-scanner.spec.ts +120 -0
  204. package/api/tests/middlewares/hono/cdn.spec.ts +70 -0
  205. package/api/tests/middlewares/hono/context.spec.ts +113 -0
  206. package/api/tests/middlewares/hono/csrf.spec.ts +136 -0
  207. package/api/tests/middlewares/hono/fallback.spec.ts +67 -0
  208. package/api/tests/middlewares/hono/pipeline.spec.ts +47 -0
  209. package/api/tests/middlewares/hono/security.spec.ts +181 -0
  210. package/api/tests/middlewares/hono/session.spec.ts +42 -0
  211. package/api/tests/middlewares/hono/xss.spec.ts +81 -0
  212. package/api/tests/models/tenant-backfill.spec.ts +287 -0
  213. package/api/tests/models/tenant-columns-model.spec.ts +46 -0
  214. package/api/tests/models/tenant-columns.spec.ts +161 -0
  215. package/api/tests/queues/credit-consume-batch.spec.ts +8 -1
  216. package/api/tests/queues/credit-consume.spec.ts +8 -1
  217. package/api/tests/queues/event-tenant.spec.ts +236 -0
  218. package/api/tests/queues/exchange-rate-health-tenant-d6.spec.ts +62 -0
  219. package/api/tests/queues/queue-parity.spec.ts +249 -0
  220. package/api/tests/queues/queue-runtime-surface.spec.ts +277 -0
  221. package/api/tests/queues/queue-teardown-d2.spec.ts +127 -0
  222. package/api/tests/queues/tenant-matrix-a.spec.ts +245 -0
  223. package/api/tests/queues/tenant-matrix-b.spec.ts +168 -0
  224. package/api/tests/routes/connect/hono-attach.spec.ts +107 -0
  225. package/api/tests/service/collapse.spec.ts +96 -0
  226. package/api/tests/store/tenant-crosscut.spec.ts +202 -0
  227. package/api/tests/store/tenant-model-spike.spec.ts +177 -0
  228. package/api/tests/store/tenant-model.spec.ts +162 -0
  229. package/api/tests/store/tenant-residual.spec.ts +196 -0
  230. package/api/third.d.ts +4 -0
  231. package/blocklet.yml +1 -1
  232. package/cloudflare/README.md +26 -6
  233. package/cloudflare/build.ts +28 -13
  234. package/cloudflare/did-connect-auth.ts +0 -217
  235. package/cloudflare/migrations/0006_tenant_columns.sql +46 -0
  236. package/cloudflare/migrations/0007_tenant_backfill_indexes.sql +65 -0
  237. package/cloudflare/migrations/0008_schema_parity.sql +16 -0
  238. package/cloudflare/migrations/0009_remove_did_space_jobs.sql +5 -0
  239. package/cloudflare/queue-runtime-mode.ts +13 -0
  240. package/cloudflare/run-build.js +10 -56
  241. package/cloudflare/shims/blocklet-sdk/asset-host-transformer.ts +20 -0
  242. package/cloudflare/shims/blocklet-sdk/config.ts +8 -1
  243. package/cloudflare/shims/blocklet-sdk/login.ts +12 -0
  244. package/cloudflare/shims/blocklet-sdk/service-api.ts +14 -0
  245. package/cloudflare/shims/blocklet-sdk/session.ts +4 -2
  246. package/cloudflare/shims/blocklet-sdk/util-constants.ts +8 -0
  247. package/cloudflare/shims/blocklet-sdk/util-csrf.ts +13 -0
  248. package/cloudflare/shims/blocklet-sdk/util-wallet.ts +8 -0
  249. package/cloudflare/shims/cron.ts +38 -158
  250. package/cloudflare/shims/events.ts +124 -0
  251. package/cloudflare/shims/fastq.ts +15 -1
  252. package/cloudflare/shims/nedb-storage.ts +16 -8
  253. package/cloudflare/shims/xss.ts +8 -0
  254. package/cloudflare/tenant-middleware.ts +36 -0
  255. package/cloudflare/tests/tenant-middleware.spec.ts +160 -0
  256. package/cloudflare/tests/worker-handler-gate.spec.ts +44 -0
  257. package/cloudflare/worker.ts +204 -433
  258. package/cloudflare/wrangler.local-e2e.jsonc +26 -0
  259. package/jest.config.js +3 -1
  260. package/package.json +33 -38
  261. package/scripts/core-env-whitelist.json +1 -0
  262. package/scripts/e2e-12b-runtime.ts +149 -0
  263. package/scripts/e2e-core-config.ts +125 -0
  264. package/scripts/e2e-d1-tenancy.ts +116 -0
  265. package/scripts/e2e-d2-cron-queue.ts +139 -0
  266. package/scripts/e2e-d3-embedded-multi.ts +171 -0
  267. package/scripts/e2e-hono-s2.ts +125 -0
  268. package/scripts/e2e-hono-s3e.ts +135 -0
  269. package/scripts/e2e-hono-s4.ts +114 -0
  270. package/scripts/e2e-migration-contract.ts +100 -0
  271. package/scripts/e2e-s0.ts +61 -0
  272. package/scripts/e2e-s1.ts +107 -0
  273. package/scripts/e2e-s2.ts +178 -0
  274. package/scripts/e2e-s3.ts +110 -0
  275. package/scripts/e2e-s4.ts +191 -0
  276. package/scripts/e2e-s5.ts +139 -0
  277. package/scripts/e2e-s6.ts +127 -0
  278. package/scripts/e2e-tenant-model.ts +119 -0
  279. package/scripts/e2e-tenant-worker.ts +199 -0
  280. package/scripts/gen-sql-migrations.js +46 -0
  281. package/scripts/phase8-codemod.js +219 -0
  282. package/scripts/phase9a-env-getters-codemod.js +82 -0
  283. package/scripts/scan-core-env.js +109 -0
  284. package/scripts/scan-tenant-queries.js +235 -0
  285. package/scripts/schema-drift-guard.ts +210 -0
  286. package/scripts/tenant-scan-whitelist.json +1 -0
  287. package/src/env.d.ts +13 -1
  288. package/tsconfig.json +1 -1
  289. package/api/src/libs/did-space.ts +0 -235
  290. package/api/src/libs/middleware.ts +0 -50
  291. package/api/src/libs/security.ts +0 -192
  292. package/api/src/queues/space.ts +0 -662
  293. package/api/src/routes/credit-tokens.ts +0 -38
  294. package/api/src/routes/exchange-rates.ts +0 -87
  295. package/api/src/routes/index.ts +0 -142
  296. package/api/src/routes/integrations/stripe.ts +0 -61
  297. package/api/src/routes/meters.ts +0 -274
  298. package/api/src/routes/passports.ts +0 -68
  299. package/api/src/routes/redirect.ts +0 -20
  300. package/api/src/routes/tool.ts +0 -65
  301. package/api/src/routes/webhook-endpoints.ts +0 -126
  302. package/api/tests/routes/credit-grants.spec.ts +0 -1261
  303. package/cloudflare/shims/did-space-js.ts +0 -17
  304. package/cloudflare/shims/did-space.ts +0 -11
  305. package/cloudflare/shims/express-compat/index.ts +0 -80
  306. package/cloudflare/shims/express-compat/types.ts +0 -41
  307. package/cloudflare/shims/lock.ts +0 -115
  308. package/cloudflare/shims/queue.ts +0 -611
  309. package/cloudflare/tests/shims/queue-delayed-persist.spec.ts +0 -87
  310. package/cloudflare/tests/shims/queue-scheduled.spec.ts +0 -186
@@ -17,20 +17,25 @@ async function main() {
17
17
  mainFields: ['module', 'main'], // Resolve ESM first, then CJS
18
18
  conditions: ['import', 'module', 'default'],
19
19
  // CF Workers runtime provides these
20
- external: [
21
- 'cloudflare:*',
22
- '__STATIC_CONTENT_MANIFEST',
23
- ],
20
+ external: ['cloudflare:*', '__STATIC_CONTENT_MANIFEST'],
24
21
  alias: {
22
+ // === Node builtins the banner require-polyfill can't cover ===
23
+ // `events` is used via CJS `require('events')` in core drivers
24
+ // (locks.ts MemoryLock, auth-storage.ts DbAuthStorage). The banner's
25
+ // globalThis.require does not list it, so require('events') -> {} and
26
+ // `class extends EventEmitter` crashes at global init. Alias both the
27
+ // bare and node:-prefixed forms to one deterministic EventEmitter shim.
28
+ events: path.resolve(cfDir, 'shims/events.ts'),
29
+ 'node:events': path.resolve(cfDir, 'shims/events.ts'),
30
+
25
31
  // === Sequelize → D1 shim ===
26
- 'sequelize': path.resolve(cfDir, 'shims/sequelize-d1/index.ts'),
27
- 'sqlite3': path.resolve(cfDir, 'shims/noop.ts'),
32
+ sequelize: path.resolve(cfDir, 'shims/sequelize-d1/index.ts'),
33
+ sqlite3: path.resolve(cfDir, 'shims/noop.ts'),
28
34
  'cls-hooked': path.resolve(cfDir, 'shims/noop.ts'),
29
35
  'express-async-errors': path.resolve(cfDir, 'shims/noop.ts'),
30
36
 
31
37
  // === Express → shim ===
32
- 'express': path.resolve(cfDir, 'shims/express-compat/index.ts'),
33
- 'cors': path.resolve(cfDir, 'shims/cors.ts'),
38
+ cors: path.resolve(cfDir, 'shims/cors.ts'),
34
39
  'cookie-parser': path.resolve(cfDir, 'shims/cookie-parser.ts'),
35
40
 
36
41
  // === @blocklet/sdk — each sub-path needs its own alias ===
@@ -46,8 +51,19 @@ async function main() {
46
51
  '@blocklet/sdk/lib/wallet-authenticator': path.resolve(cfDir, 'shims/blocklet-sdk/wallet-authenticator.ts'),
47
52
  '@blocklet/sdk/lib/wallet-handler': path.resolve(cfDir, 'shims/blocklet-sdk/wallet-handler.ts'),
48
53
  '@blocklet/sdk/lib/security': path.resolve(cfDir, 'shims/blocklet-sdk/security.ts'),
54
+ '@blocklet/sdk/lib/util/verify-session': path.resolve(cfDir, 'shims/blocklet-sdk/verify-session.ts'),
49
55
  '@blocklet/sdk/lib/util/verify-sign': path.resolve(cfDir, 'shims/blocklet-sdk/verify-sign.ts'),
50
56
  '@blocklet/sdk/lib/util/component-api': path.resolve(cfDir, 'shims/blocklet-sdk/component-api.ts'),
57
+ // Phase 4 (express→hono): the native hono resource routes pull in the forked
58
+ // app-shell middleware (session — LIVE on CF; csrf/cdn/fallback — node-shell
59
+ // only, bundled-but-unreachable on CF). Each needs a util subpath shim so the
60
+ // bare `@blocklet/sdk` catch-all below doesn't mangle the import.
61
+ '@blocklet/sdk/lib/util/login': path.resolve(cfDir, 'shims/blocklet-sdk/login.ts'),
62
+ '@blocklet/sdk/lib/util/service-api': path.resolve(cfDir, 'shims/blocklet-sdk/service-api.ts'),
63
+ '@blocklet/sdk/lib/util/csrf': path.resolve(cfDir, 'shims/blocklet-sdk/util-csrf.ts'),
64
+ '@blocklet/sdk/lib/util/wallet': path.resolve(cfDir, 'shims/blocklet-sdk/util-wallet.ts'),
65
+ '@blocklet/sdk/lib/util/asset-host-transformer': path.resolve(cfDir, 'shims/blocklet-sdk/asset-host-transformer.ts'),
66
+ '@blocklet/sdk/lib/util/constants': path.resolve(cfDir, 'shims/blocklet-sdk/util-constants.ts'),
51
67
  '@blocklet/sdk/lib/error-handler': path.resolve(cfDir, 'shims/noop.ts'),
52
68
  '@blocklet/sdk/lib/did': path.resolve(cfDir, 'shims/blocklet-sdk/did.ts'),
53
69
  '@blocklet/sdk/lib/types/notification': path.resolve(cfDir, 'shims/noop.ts'),
@@ -62,22 +78,21 @@ async function main() {
62
78
  '@blocklet/xss': path.resolve(cfDir, 'shims/xss.ts'),
63
79
  '@blocklet/error': path.resolve(cfDir, 'shims/error.ts'),
64
80
  '@blocklet/logger': path.resolve(cfDir, 'shims/blocklet-sdk/logger.ts'),
65
- '@blocklet/did-space-js': path.resolve(cfDir, 'shims/did-space-js.ts'),
66
81
  '@blocklet/payment-vendor': path.resolve(cfDir, 'shims/payment-vendor.ts'),
67
82
 
68
83
  // === ArcBlock / DID packages ===
69
84
  '@arcblock/did-connect-storage-nedb': path.resolve(cfDir, 'shims/nedb-storage.ts'),
70
85
 
71
86
  // === Other dependencies ===
72
- 'ws': path.resolve(cfDir, 'shims/ws-lite.ts'),
73
- 'pg': path.resolve(cfDir, 'shims/noop.ts'),
87
+ ws: path.resolve(cfDir, 'shims/ws-lite.ts'),
88
+ pg: path.resolve(cfDir, 'shims/noop.ts'),
74
89
  'follow-redirects': path.resolve(cfDir, 'shims/noop.ts'),
75
90
  'crypto-js': path.resolve(cfDir, 'shims/crypto-js-warn.ts'),
76
91
  'mime-types': path.resolve(cfDir, 'shims/mime-types.ts'),
77
92
  '@abtnode/cron': path.resolve(cfDir, 'shims/cron.ts'),
78
- 'querystring': path.resolve(cfDir, 'shims/querystring.ts'),
93
+ querystring: path.resolve(cfDir, 'shims/querystring.ts'),
79
94
  'dotenv-flow': path.resolve(cfDir, 'shims/noop.ts'),
80
- 'fastq': path.resolve(cfDir, 'shims/fastq.ts'),
95
+ fastq: path.resolve(cfDir, 'shims/fastq.ts'),
81
96
  },
82
97
  banner: {
83
98
  js: [
@@ -168,223 +168,6 @@ class CloudflareD1Storage extends EventEmitter {
168
168
  }
169
169
  }
170
170
 
171
- // ---------------------------------------------------------------------------
172
- // Hono adapter – wraps Express-style (req, res, next) handlers for Hono
173
- // ---------------------------------------------------------------------------
174
-
175
- function parseCookieHeader(header: string): Record<string, string> {
176
- const cookies: Record<string, string> = {};
177
- if (!header) return cookies;
178
- for (const pair of header.split(';')) {
179
- const idx = pair.indexOf('=');
180
- if (idx > 0) {
181
- const key = pair.slice(0, idx).trim();
182
- const val = pair.slice(idx + 1).trim();
183
- cookies[key] = decodeURIComponent(val);
184
- }
185
- }
186
- return cookies;
187
- }
188
-
189
- function negotiateLanguage(acceptHeader: string, ...langs: string[]): string | false {
190
- if (!acceptHeader || langs.length === 0) return langs[0] || false;
191
- const lower = acceptHeader.toLowerCase();
192
- for (const lang of langs) {
193
- const prefix = lang.toLowerCase().split('-')[0];
194
- if (lower.includes(lang.toLowerCase()) || lower.includes(prefix)) {
195
- return lang;
196
- }
197
- }
198
- return langs[0] || false;
199
- }
200
-
201
- function createHonoRequest(c: any, bodyCache: any = {}): any {
202
- const url = new URL(c.req.url);
203
- const rawHeaders: Record<string, string> = {};
204
- if (c.req.raw?.headers) {
205
- c.req.raw.headers.forEach((value: string, key: string) => {
206
- rawHeaders[key.toLowerCase()] = value;
207
- });
208
- }
209
-
210
- return {
211
- body: bodyCache,
212
- query: Object.fromEntries(url.searchParams.entries()),
213
- params: typeof c.req.param === 'function' ? c.req.param() : {},
214
- headers: rawHeaders,
215
- cookies: parseCookieHeader(rawHeaders.cookie || ''),
216
- protocol: url.protocol.replace(':', ''),
217
- originalUrl: url.pathname + url.search,
218
- get(name: string) {
219
- return rawHeaders[name.toLowerCase()];
220
- },
221
- header(name: string) {
222
- return rawHeaders[name.toLowerCase()];
223
- },
224
- acceptsLanguages(...langs: string[]) {
225
- return negotiateLanguage(rawHeaders['accept-language'] || '', ...langs);
226
- },
227
- raw: c,
228
- };
229
- }
230
-
231
- function createHonoResponse(): any {
232
- let result: { statusCode: number; body: any } | null = null;
233
-
234
- return {
235
- jsonp(data: any) {
236
- result = { statusCode: 200, body: data };
237
- },
238
- json(data: any) {
239
- result = { statusCode: 200, body: data };
240
- },
241
- status(code: number) {
242
- return {
243
- json(data: any) {
244
- result = { statusCode: code, body: data };
245
- },
246
- };
247
- },
248
- _getResult() {
249
- return result;
250
- },
251
- };
252
- }
253
-
254
- /**
255
- * Wrap a chain of Express-style middlewares + handler into a single Hono handler.
256
- */
257
- function wrapHandler(
258
- middlewares: Array<(req: any, res: any, next: () => void) => any>,
259
- handler: (req: any, res: any) => Promise<void>
260
- ) {
261
- return async (c: any) => {
262
- // Ensure __CF_ENV__ is available for D1Storage
263
- if (!(globalThis as any).__CF_ENV__) {
264
- (globalThis as any).__CF_ENV__ = c.env;
265
- }
266
- let body = {};
267
- try {
268
- const text = await c.req.text();
269
- if (text) {
270
- const contentType = c.req.header('content-type') || '';
271
- if (contentType.includes('json')) {
272
- body = JSON.parse(text);
273
- } else if (contentType.includes('urlencoded')) {
274
- body = Object.fromEntries(new URLSearchParams(text).entries());
275
- }
276
- }
277
- } catch {
278
- // Body parsing failed
279
- }
280
-
281
- const req = createHonoRequest(c, body);
282
- const res = createHonoResponse();
283
-
284
- // Run middlewares
285
- for (const mw of middlewares) {
286
- let nextCalled = false;
287
- await mw(req, res, () => {
288
- nextCalled = true;
289
- });
290
- if (!nextCalled) {
291
- const r = res._getResult();
292
- if (r) return c.json(r.body, r.statusCode);
293
- return c.json({ error: 'Unknown error' }, 500);
294
- }
295
- }
296
-
297
- // Run handler
298
- try {
299
- await handler(req, res);
300
- } catch (err: any) {
301
- console.error(
302
- '[DID Connect] handler error:',
303
- err?.message || err,
304
- err?.stack?.split('\n').slice(0, 5).join('\n')
305
- );
306
- // Write error to D1 for debugging (console.error may not be visible in tail)
307
- try {
308
- const db = (globalThis as any).__CF_ENV__?.DB;
309
- if (db) {
310
- await db
311
- .prepare('INSERT OR REPLACE INTO _locks (name, owner, expires_at) VALUES (?, ?, 0)')
312
- .bind('debug-did-error', `${err?.message || err}`.substring(0, 200))
313
- .run();
314
- }
315
- } catch {
316
- /* ignore */
317
- }
318
- return c.json({ error: err?.message || 'Internal server error' }, 500);
319
- }
320
- const r = res._getResult();
321
- return c.json(r?.body ?? {}, r?.statusCode ?? 200);
322
- };
323
- }
324
-
325
- /**
326
- * Monkey-patch the WalletHandlers.attach method to support Hono apps.
327
- *
328
- * The published @arcblock/did-connect-js expects Express. We intercept attach()
329
- * and register routes on the Hono app directly using wrapHandler().
330
- */
331
- function createHonoCompatHandlers(opts: { tokenStorage: any; authenticator: any }): WalletHandlers {
332
- const handlers = new WalletHandlers(opts);
333
- const originalAttach = handlers.attach.bind(handlers);
334
-
335
- // Override attach to use Hono-compatible route registration
336
- (handlers as any).attach = function attachHono(config: any) {
337
- const { app, action, ...rest } = config;
338
-
339
- // Create a fake Express-like app that collects routes
340
- const routes: Array<{
341
- method: string;
342
- path: string;
343
- middlewares: Function[];
344
- handler: Function;
345
- }> = [];
346
-
347
- const fakeApp: any = {
348
- get(path: string, ...fns: Function[]) {
349
- routes.push({ method: 'get', path, middlewares: fns.slice(0, -1), handler: fns[fns.length - 1] });
350
- },
351
- post(path: string, ...fns: Function[]) {
352
- routes.push({ method: 'post', path, middlewares: fns.slice(0, -1), handler: fns[fns.length - 1] });
353
- },
354
- use(_path: string, _middleware: any) {
355
- // CORS is handled by Hono's cors middleware already
356
- },
357
- };
358
-
359
- // Call original attach with fake app to collect routes
360
- originalAttach({ app: fakeApp, action, ...rest });
361
-
362
- // Now register collected routes on the real Hono app
363
- const prefix = (handlers as any).options?.prefix || '/api/did';
364
-
365
- // Add CORS for DID Connect routes
366
- app.use(`${prefix}/${action}/*`, async (c: any, next: () => Promise<void>) => {
367
- c.header('Access-Control-Allow-Origin', '*');
368
- c.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
369
- c.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
370
- if (c.req.method === 'OPTIONS') {
371
- return c.text('', 204);
372
- }
373
- await next();
374
- });
375
-
376
- for (const route of routes) {
377
- const wrapped = wrapHandler(route.middlewares as any[], route.handler as any);
378
- if (route.method === 'get') {
379
- app.get(route.path, wrapped);
380
- } else if (route.method === 'post') {
381
- app.post(route.path, wrapped);
382
- }
383
- }
384
- };
385
-
386
- return handlers;
387
- }
388
171
 
389
172
  // ---------------------------------------------------------------------------
390
173
  // Public API
@@ -0,0 +1,46 @@
1
+ -- Payment Kit: tenant columns (Phase 1, W1-1a)
2
+ -- Adds a nullable instance_did column to all 38 tenant tables.
3
+ -- Mirrors blocklets/core/api/src/store/migrations/20260610-tenant-columns.ts.
4
+ -- No constraints / indexes / backfill here — those land in 0007 (Phase 2).
5
+ --
6
+ -- NOTE: wrangler tracks applied migrations in `d1_migrations` by filename,
7
+ -- so this file is applied exactly once via `wrangler d1 migrations apply`.
8
+
9
+ ALTER TABLE customers ADD COLUMN instance_did VARCHAR(64);
10
+ ALTER TABLE products ADD COLUMN instance_did VARCHAR(64);
11
+ ALTER TABLE prices ADD COLUMN instance_did VARCHAR(64);
12
+ ALTER TABLE pricing_tables ADD COLUMN instance_did VARCHAR(64);
13
+ ALTER TABLE payment_methods ADD COLUMN instance_did VARCHAR(64);
14
+ ALTER TABLE checkout_sessions ADD COLUMN instance_did VARCHAR(64);
15
+ ALTER TABLE payment_intents ADD COLUMN instance_did VARCHAR(64);
16
+ ALTER TABLE payment_links ADD COLUMN instance_did VARCHAR(64);
17
+ ALTER TABLE setup_intents ADD COLUMN instance_did VARCHAR(64);
18
+ ALTER TABLE price_quotes ADD COLUMN instance_did VARCHAR(64);
19
+ ALTER TABLE subscriptions ADD COLUMN instance_did VARCHAR(64);
20
+ ALTER TABLE subscription_items ADD COLUMN instance_did VARCHAR(64);
21
+ ALTER TABLE subscription_schedules ADD COLUMN instance_did VARCHAR(64);
22
+ ALTER TABLE invoices ADD COLUMN instance_did VARCHAR(64);
23
+ ALTER TABLE invoice_items ADD COLUMN instance_did VARCHAR(64);
24
+ ALTER TABLE refunds ADD COLUMN instance_did VARCHAR(64);
25
+ ALTER TABLE credit_grants ADD COLUMN instance_did VARCHAR(64);
26
+ ALTER TABLE credit_transactions ADD COLUMN instance_did VARCHAR(64);
27
+ ALTER TABLE meters ADD COLUMN instance_did VARCHAR(64);
28
+ ALTER TABLE meter_events ADD COLUMN instance_did VARCHAR(64);
29
+ ALTER TABLE usage_records ADD COLUMN instance_did VARCHAR(64);
30
+ ALTER TABLE coupons ADD COLUMN instance_did VARCHAR(64);
31
+ ALTER TABLE promotion_codes ADD COLUMN instance_did VARCHAR(64);
32
+ ALTER TABLE discounts ADD COLUMN instance_did VARCHAR(64);
33
+ ALTER TABLE entitlements ADD COLUMN instance_did VARCHAR(64);
34
+ ALTER TABLE entitlement_grants ADD COLUMN instance_did VARCHAR(64);
35
+ ALTER TABLE entitlement_products ADD COLUMN instance_did VARCHAR(64);
36
+ ALTER TABLE events ADD COLUMN instance_did VARCHAR(64);
37
+ ALTER TABLE webhook_endpoints ADD COLUMN instance_did VARCHAR(64);
38
+ ALTER TABLE webhook_attempts ADD COLUMN instance_did VARCHAR(64);
39
+ ALTER TABLE payouts ADD COLUMN instance_did VARCHAR(64);
40
+ ALTER TABLE payment_stats ADD COLUMN instance_did VARCHAR(64);
41
+ ALTER TABLE revenue_snapshots ADD COLUMN instance_did VARCHAR(64);
42
+ ALTER TABLE auto_recharge_configs ADD COLUMN instance_did VARCHAR(64);
43
+ ALTER TABLE tax_rates ADD COLUMN instance_did VARCHAR(64);
44
+ ALTER TABLE settings ADD COLUMN instance_did VARCHAR(64);
45
+ ALTER TABLE payment_currencies ADD COLUMN instance_did VARCHAR(64);
46
+ ALTER TABLE product_vendors ADD COLUMN instance_did VARCHAR(64);
@@ -0,0 +1,65 @@
1
+ -- Payment Kit: tenant unique keys + indexes (Phase 2, W1-1b)
2
+ -- Mirrors the index/unique-key part of api/src/store/migrations/20260611-tenant-backfill.ts.
3
+ --
4
+ -- IMPORTANT: the instance_did BACKFILL is NOT here. Static migration SQL
5
+ -- cannot know the deployment app DID, so the backfill (and the table rebuilds
6
+ -- that drop old inline single-column UNIQUE constraints) runs through the
7
+ -- shared runtime routine `runTenantBackfill()` (api/src/store/tenant-backfill.ts),
8
+ -- invoked idempotently from the worker's scheduled() handler.
9
+ --
10
+ -- Everything below is NULL-safe before that backfill runs: SQLite treats each
11
+ -- NULL as distinct in unique indexes, and existing single-column uniqueness
12
+ -- (identifier / did / key / idempotency_key) guarantees no composite dupes.
13
+
14
+ -- composite tenant unique keys (W1 §2.1, decisions D1/D3)
15
+ CREATE UNIQUE INDEX IF NOT EXISTS `uq_customers_tenant_did` ON `customers` (`instance_did`, `did`);
16
+ CREATE UNIQUE INDEX IF NOT EXISTS `uq_meter_events_tenant_identifier` ON `meter_events` (`instance_did`, `identifier`);
17
+ CREATE UNIQUE INDEX IF NOT EXISTS `uq_meters_tenant_event_name` ON `meters` (`instance_did`, `event_name`);
18
+ CREATE UNIQUE INDEX IF NOT EXISTS `uq_entitlements_tenant_key` ON `entitlements` (`instance_did`, `key`);
19
+ CREATE UNIQUE INDEX IF NOT EXISTS `uq_price_quotes_tenant_idem` ON `price_quotes` (`instance_did`, `idempotency_key`);
20
+ CREATE UNIQUE INDEX IF NOT EXISTS `uq_promotion_codes_tenant_code` ON `promotion_codes` (`instance_did`, `livemode`, `code`);
21
+ CREATE UNIQUE INDEX IF NOT EXISTS `uq_product_vendors_tenant_key` ON `product_vendors` (`instance_did`, `vendor_key`);
22
+ CREATE UNIQUE INDEX IF NOT EXISTS `uq_revenue_snapshots_tenant` ON `revenue_snapshots` (`instance_did`, `timestamp`, `currency_id`, `livemode`, `period_type`);
23
+ DROP INDEX IF EXISTS `idx_revenue_snapshots_unique`;
24
+ DROP INDEX IF EXISTS `idx_entitlements_key`;
25
+ DROP INDEX IF EXISTS `idx_pq_idempotency`;
26
+
27
+ -- plain tenant indexes for scoped queries (Phase 3+); meter_events is served
28
+ -- by its composite unique above (high-write table, avoid a second index)
29
+ CREATE INDEX IF NOT EXISTS `idx_customers_instance_did` ON `customers` (`instance_did`);
30
+ CREATE INDEX IF NOT EXISTS `idx_products_instance_did` ON `products` (`instance_did`);
31
+ CREATE INDEX IF NOT EXISTS `idx_prices_instance_did` ON `prices` (`instance_did`);
32
+ CREATE INDEX IF NOT EXISTS `idx_pricing_tables_instance_did` ON `pricing_tables` (`instance_did`);
33
+ CREATE INDEX IF NOT EXISTS `idx_payment_methods_instance_did` ON `payment_methods` (`instance_did`);
34
+ CREATE INDEX IF NOT EXISTS `idx_checkout_sessions_instance_did` ON `checkout_sessions` (`instance_did`);
35
+ CREATE INDEX IF NOT EXISTS `idx_payment_intents_instance_did` ON `payment_intents` (`instance_did`);
36
+ CREATE INDEX IF NOT EXISTS `idx_payment_links_instance_did` ON `payment_links` (`instance_did`);
37
+ CREATE INDEX IF NOT EXISTS `idx_setup_intents_instance_did` ON `setup_intents` (`instance_did`);
38
+ CREATE INDEX IF NOT EXISTS `idx_price_quotes_instance_did` ON `price_quotes` (`instance_did`);
39
+ CREATE INDEX IF NOT EXISTS `idx_subscriptions_instance_did` ON `subscriptions` (`instance_did`);
40
+ CREATE INDEX IF NOT EXISTS `idx_subscription_items_instance_did` ON `subscription_items` (`instance_did`);
41
+ CREATE INDEX IF NOT EXISTS `idx_subscription_schedules_instance_did` ON `subscription_schedules` (`instance_did`);
42
+ CREATE INDEX IF NOT EXISTS `idx_invoices_instance_did` ON `invoices` (`instance_did`);
43
+ CREATE INDEX IF NOT EXISTS `idx_invoice_items_instance_did` ON `invoice_items` (`instance_did`);
44
+ CREATE INDEX IF NOT EXISTS `idx_refunds_instance_did` ON `refunds` (`instance_did`);
45
+ CREATE INDEX IF NOT EXISTS `idx_credit_grants_instance_did` ON `credit_grants` (`instance_did`);
46
+ CREATE INDEX IF NOT EXISTS `idx_credit_transactions_instance_did` ON `credit_transactions` (`instance_did`);
47
+ CREATE INDEX IF NOT EXISTS `idx_meters_instance_did` ON `meters` (`instance_did`);
48
+ CREATE INDEX IF NOT EXISTS `idx_usage_records_instance_did` ON `usage_records` (`instance_did`);
49
+ CREATE INDEX IF NOT EXISTS `idx_coupons_instance_did` ON `coupons` (`instance_did`);
50
+ CREATE INDEX IF NOT EXISTS `idx_promotion_codes_instance_did` ON `promotion_codes` (`instance_did`);
51
+ CREATE INDEX IF NOT EXISTS `idx_discounts_instance_did` ON `discounts` (`instance_did`);
52
+ CREATE INDEX IF NOT EXISTS `idx_entitlements_instance_did` ON `entitlements` (`instance_did`);
53
+ CREATE INDEX IF NOT EXISTS `idx_entitlement_grants_instance_did` ON `entitlement_grants` (`instance_did`);
54
+ CREATE INDEX IF NOT EXISTS `idx_entitlement_products_instance_did` ON `entitlement_products` (`instance_did`);
55
+ CREATE INDEX IF NOT EXISTS `idx_events_instance_did` ON `events` (`instance_did`);
56
+ CREATE INDEX IF NOT EXISTS `idx_webhook_endpoints_instance_did` ON `webhook_endpoints` (`instance_did`);
57
+ CREATE INDEX IF NOT EXISTS `idx_webhook_attempts_instance_did` ON `webhook_attempts` (`instance_did`);
58
+ CREATE INDEX IF NOT EXISTS `idx_payouts_instance_did` ON `payouts` (`instance_did`);
59
+ CREATE INDEX IF NOT EXISTS `idx_payment_stats_instance_did` ON `payment_stats` (`instance_did`);
60
+ CREATE INDEX IF NOT EXISTS `idx_revenue_snapshots_instance_did` ON `revenue_snapshots` (`instance_did`);
61
+ CREATE INDEX IF NOT EXISTS `idx_auto_recharge_configs_instance_did` ON `auto_recharge_configs` (`instance_did`);
62
+ CREATE INDEX IF NOT EXISTS `idx_tax_rates_instance_did` ON `tax_rates` (`instance_did`);
63
+ CREATE INDEX IF NOT EXISTS `idx_settings_instance_did` ON `settings` (`instance_did`);
64
+ CREATE INDEX IF NOT EXISTS `idx_payment_currencies_instance_did` ON `payment_currencies` (`instance_did`);
65
+ CREATE INDEX IF NOT EXISTS `idx_product_vendors_instance_did` ON `product_vendors` (`instance_did`);
@@ -0,0 +1,16 @@
1
+ -- Phase 11 (W2′) schema parity: close the D1-lineage gaps the schema-drift guard
2
+ -- (scripts/schema-drift-guard.ts) found versus the Umzug canonical schema.
3
+ -- Additive only — safe to apply on a deployed D1 (no drops, no rewrites).
4
+
5
+ -- events: the Umzug genesis creates `events` with api_version (NOT NULL) and
6
+ -- metadata (JSON); the D1 0001 events table predates both, so the worker could
7
+ -- not write them. Backfill api_version with the app constant
8
+ -- (api/src/libs/audit.ts API_VERSION = '2023-09-05'); new rows carry the same.
9
+ ALTER TABLE `events` ADD COLUMN `api_version` VARCHAR(16) NOT NULL DEFAULT '2023-09-05';
10
+ ALTER TABLE `events` ADD COLUMN `metadata` JSON;
11
+
12
+ -- Indexes present in the canonical but missing in the D1 lineage (perf parity;
13
+ -- names match the Umzug migrations 20250904-discount / 20251007-relate-tax-rate).
14
+ CREATE INDEX IF NOT EXISTS `idx_discounts_customer_id` ON `discounts` (`customer_id`);
15
+ CREATE INDEX IF NOT EXISTS `idx_invoice_items_tax_rate_id` ON `invoice_items` (`tax_rate_id`);
16
+ CREATE INDEX IF NOT EXISTS `idx_promotion_codes_verification_type_coupon_id` ON `promotion_codes` (`verification_type`, `coupon_id`);
@@ -0,0 +1,5 @@
1
+ -- The DID Space billing-mirror integration was removed (libs/did-space.ts,
2
+ -- queues/space.ts). Rows in `jobs` with queue='did-space' can never be picked
3
+ -- up again (the scheduled scan only matches registered queue names), so they
4
+ -- would sit in the table forever. Delete them. Idempotent.
5
+ DELETE FROM jobs WHERE queue = 'did-space';
@@ -0,0 +1,13 @@
1
+ // Phase 12b (W2′): pin the core queue engine to 'workerd' mode.
2
+ //
3
+ // This MUST be imported before any business queue module (api/src/queues/*),
4
+ // because createQueue runs at import time and reads the mode to decide whether
5
+ // to start the node background poll loop(). A frozen CF isolate cannot run a
6
+ // background timer, so the worker disables the loop and drives due-job
7
+ // re-dispatch from scheduled() via the core dispatchDueJobs() surface instead.
8
+ //
9
+ // ESM executes imported module bodies in source order, so importing this first
10
+ // in worker.ts guarantees the mode is set before the engine is constructed.
11
+ import { setQueueRuntimeMode } from '../api/src/libs/queue/runtime';
12
+
13
+ setQueueRuntimeMode('workerd');
@@ -3,59 +3,15 @@ const path = require("path");
3
3
  const cfDir = __dirname;
4
4
  const s = (f) => path.resolve(cfDir, f);
5
5
 
6
- // Resolve the absolute path of the original queue module so we can intercept it
7
- const queueModulePath = path.resolve(cfDir, "../api/src/libs/queue/index.ts");
8
- const queueShimPath = s("shims/queue.ts");
9
-
10
- // Resolve the absolute path of the original lock module so we can intercept it
11
- const lockModulePath = path.resolve(cfDir, "../api/src/libs/lock.ts");
12
- const lockShimPath = s("shims/lock.ts");
13
-
14
- // Plugin to redirect libs/queue to our CF Workers shim
15
- const queueNormalizedTarget = queueModulePath.replace(/\/index\.ts$/, '').replace(/\/index$/, '');
16
- const queueShimPlugin = {
17
- name: 'queue-shim',
18
- setup(build) {
19
- // Intercept any import that resolves to the original queue module
20
- build.onResolve({ filter: /libs\/queue/ }, (args) => {
21
- const resolveDir = args.resolveDir;
22
- if (!resolveDir) return undefined;
23
-
24
- // Resolve the relative import to an absolute path
25
- const resolved = path.resolve(resolveDir, args.path).replace(/\/index(\.ts)?$/, '');
26
- const match = resolved === queueNormalizedTarget;
27
- if (args.path.includes('queue')) {
28
- console.log(`[queue-shim] ${match ? 'MATCH' : 'skip'}: ${args.path} -> ${resolved} (target: ${queueNormalizedTarget})`);
29
- }
30
- if (match) {
31
- return { path: queueShimPath };
32
- }
33
- return undefined;
34
- });
35
- },
36
- };
37
-
38
- // Plugin to redirect libs/lock to our CF Workers D1-based lock shim
39
- const lockNormalizedTarget = lockModulePath.replace(/\.ts$/, '');
40
- const lockShimPlugin = {
41
- name: 'lock-shim',
42
- setup(build) {
43
- build.onResolve({ filter: /libs\/lock/ }, (args) => {
44
- const resolveDir = args.resolveDir;
45
- if (!resolveDir) return undefined;
46
-
47
- const resolved = path.resolve(resolveDir, args.path).replace(/\.ts$/, '');
48
- const match = resolved === lockNormalizedTarget;
49
- if (args.path.includes('lock')) {
50
- console.log(`[lock-shim] ${match ? 'MATCH' : 'skip'}: ${args.path} -> ${resolved} (target: ${lockNormalizedTarget})`);
51
- }
52
- if (match) {
53
- return { path: lockShimPath };
54
- }
55
- return undefined;
56
- });
57
- },
58
- };
6
+ // Phase 12b/12c: the queue-shim (libs/queue -> shims/queue.ts) and lock-shim
7
+ // (libs/lock -> shims/lock.ts) plugins were REMOVED.
8
+ // - queue-shim: option A makes api/src/libs/queue the ONE queue engine for all
9
+ // runtimes (the worker drives it via api/src/libs/queue/runtime.ts); the
10
+ // duplicate shims/queue.ts engine is deleted, so there is nothing to redirect.
11
+ // - lock-shim: lock became a Phase 8 driver (libs/drivers/locks) and the old
12
+ // shims/lock.ts no longer exists; the redirect pointed at a deleted file and
13
+ // was the only thing that broke this script.
14
+ // Everything else below is the original CF deploy-build optimization config.
59
15
 
60
16
  // Plugin to neutralize rolldown-generated `__require(import.meta.url)` helpers
61
17
  // that ship inside npm packages built with rolldown (e.g. @ocap/message,
@@ -239,7 +195,7 @@ build({
239
195
  outdir: s("dist"), minify: true, sourcemap: true, metafile: true,
240
196
  mainFields: ["module", "main"],
241
197
  plugins: [
242
- noopPackagesPlugin, queueShimPlugin, lockShimPlugin, rolldownRuntimeNoopPlugin, lodashSubpathPlugin, dropEthersWordlistsPlugin,
198
+ noopPackagesPlugin, rolldownRuntimeNoopPlugin, lodashSubpathPlugin, dropEthersWordlistsPlugin,
243
199
  ],
244
200
  external: ["cloudflare:*", "__STATIC_CONTENT_MANIFEST"],
245
201
  // Give import.meta.url a stable fallback so bundled deps that call
@@ -303,7 +259,6 @@ build({
303
259
  "sqlite3": s("shims/noop.ts"),
304
260
  "cls-hooked": s("shims/noop.ts"),
305
261
  "express-async-errors": s("shims/noop.ts"),
306
- "express": s("shims/express-compat/index.ts"),
307
262
  "cors": s("shims/cors.ts"),
308
263
  "cookie-parser": s("shims/cookie-parser.ts"),
309
264
  "@blocklet/sdk/lib/middlewares/fallback": s("shims/blocklet-sdk/fallback.ts"),
@@ -331,7 +286,6 @@ build({
331
286
  "@blocklet/xss": s("shims/xss.ts"),
332
287
  "@blocklet/error": s("shims/error.ts"),
333
288
  "@blocklet/logger": s("shims/blocklet-sdk/logger.ts"),
334
- "@blocklet/did-space-js": s("shims/did-space.ts"),
335
289
  "@blocklet/payment-vendor": s("shims/payment-vendor.ts"),
336
290
  "@arcblock/did-connect-storage-nedb": s("shims/nedb-storage.ts"),
337
291
  "@abtnode/cron": s("shims/cron.ts"),
@@ -0,0 +1,20 @@
1
+ // CF shim for @blocklet/sdk/lib/util/asset-host-transformer.
2
+ //
3
+ // DEAD on the CF path: the only importer is the cdn middleware (node-shell only —
4
+ // HTML CDN-URL rewriting on the full node app shell). The worker serves JSON API
5
+ // routes through a LITE app-shell, so cdn never executes. A pass-through stub is
6
+ // enough to resolve the bundled-but-unreachable node code.
7
+ export class AssetHostTransformer {
8
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor, no-empty-function
9
+ constructor(_assetHost?: string) {}
10
+
11
+ transform(html: string, _assetHost?: string): string {
12
+ return html;
13
+ }
14
+
15
+ transformBuffer(body: Buffer | Uint8Array, _assetHost?: string): Buffer | Uint8Array {
16
+ return body;
17
+ }
18
+ }
19
+
20
+ export default { AssetHostTransformer };
@@ -4,5 +4,12 @@ export { env };
4
4
  export const Events = {};
5
5
  export const events = { on: () => {}, emit: () => {} };
6
6
 
7
- const config = { env, Events, events };
7
+ // Phase 4 (express→hono): the fallback middleware (SPA serving) reads these. It is
8
+ // node-shell only — DEAD on the CF worker (which never serves the SPA) — so a
9
+ // resolving stub is enough. getBlockletSettings also backs sessionMiddleware's
10
+ // blacklist check, which is gated off on CF (enableBlacklist: false).
11
+ export const getBlockletSettings = (): any => ({ enableBlacklist: false, theme: {} });
12
+ export const getBlockletJs = (..._args: any[]): string => '';
13
+
14
+ const config = { env, Events, events, getBlockletSettings, getBlockletJs };
8
15
  export default config;
@@ -0,0 +1,12 @@
1
+ // CF shim for @blocklet/sdk/lib/util/login.
2
+ //
3
+ // Reached on the LIVE CF path: sessionMiddleware (used by many resource routes)
4
+ // calls isLoginToken/isAccessKey to classify the bearer token. These are pure
5
+ // string-format checks — copied verbatim from the upstream so behavior matches.
6
+ export const isLoginToken = (token: unknown): boolean =>
7
+ typeof token === 'string' && token.split('.').length === 3;
8
+
9
+ export const isAccessKey = (token: unknown): boolean =>
10
+ typeof token === 'string' && token.split('.').length === 1 && token.startsWith('blocklet-');
11
+
12
+ export default { isLoginToken, isAccessKey };
@@ -0,0 +1,14 @@
1
+ // CF shim for @blocklet/sdk/lib/util/service-api.
2
+ //
3
+ // The only caller on the CF path is sessionMiddleware's login-token blacklist
4
+ // check, which is gated by `blockletSettings.enableBlacklist` (off on the CF
5
+ // worker — the worker resolves identity via AUTH_SERVICE RPC, not the blacklist
6
+ // endpoint). The old express-compat CF path never ran this check at all, so a
7
+ // stub that reports "valid" preserves the worker's (no-blacklist) behavior and
8
+ // never wrongly blocks a token if the setting is somehow on.
9
+ const serviceApi = {
10
+ post: async (_url: string, _body?: unknown) => ({ data: { valid: true } }),
11
+ get: async (_url: string) => ({ data: {} }),
12
+ };
13
+
14
+ export default serviceApi;
@@ -1,6 +1,8 @@
1
1
  // @blocklet/sdk/lib/middlewares/session shim
2
- // Auth is resolved in Hono middleware layer via AUTH_SERVICE RPC.
3
- // req.user is already populated by mountExpressRoutes().
2
+ // Auth is resolved in the Hono middleware layer via AUTH_SERVICE RPC, then injected
3
+ // as x-user-* request headers by the worker's /api/* dispatcher (worker.ts) — the
4
+ // native authenticate()/sessionMiddleware read those. This express-middleware shim
5
+ // is inert (the old express-compat mountExpressRoutes path it served is gone).
4
6
  export default function sessionMiddleware(_options?: any) {
5
7
  return (_req: any, _res: any, next: any) => next();
6
8
  }
@@ -0,0 +1,8 @@
1
+ // CF shim for @blocklet/sdk/lib/util/constants.
2
+ //
3
+ // Reached via the fallback middleware (node-shell SPA serving — DEAD on the CF
4
+ // worker, which never serves the SPA). SERVICE_PREFIX carries its real upstream
5
+ // value so the bundled-but-unreachable node code is byte-faithful.
6
+ export const SERVICE_PREFIX = '/.well-known/service';
7
+
8
+ export default { SERVICE_PREFIX };
@@ -0,0 +1,13 @@
1
+ // CF shim for @blocklet/sdk/lib/util/csrf.
2
+ //
3
+ // DEAD on the CF path: the csrf middleware (middlewares/hono/csrf) lives only on
4
+ // the NODE app-shell pipeline (service.ts buildHonoApp / configureNativePipeline,
5
+ // reached via getHonoApp). The CF worker mounts a LITE app-shell (xss only) and
6
+ // owns its own cors, so csrf never executes on the worker. These stubs exist only
7
+ // so esbuild can resolve the import in the bundled-but-unreachable node code.
8
+ export const getCsrfSecret = (): string => '';
9
+ export const sign = (_secret: string, _value: string): string => '';
10
+ export const verify = (_secret: string, _value: string, _token: string): boolean => false;
11
+ export const hmac = (_secret: string, _value: string): string => '';
12
+
13
+ export default { getCsrfSecret, sign, verify, hmac };