@tokenoftrust/storefront-runner 7.3.2 → 7.5.1
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.
- package/agency-kit/tools/lib/catalog-model/files.mjs +11 -4
- package/agency-kit/tools/lib/catalog-model/model.mjs +5 -1
- package/agency-kit/tools/lib/redirect-shadows.mjs +261 -0
- package/apps/storefront/astro.config.mjs +8 -2
- package/apps/storefront/package.json +1 -2
- package/apps/storefront/src/components/catalog/storefront/ProductCard.astro +20 -6
- package/apps/storefront/src/components/catalog/storefront/islands/ImageGallery.tsx +17 -6
- package/apps/storefront/src/components/compliance/storefront/islands/CheckoutComplianceGate.tsx +1 -32
- package/apps/storefront/src/config/compliance/rulesets.ts +1 -1
- package/apps/storefront/src/layouts/Layout.astro +17 -0
- package/apps/storefront/src/lib/access/auth/agentRoles.ts +20 -0
- package/apps/storefront/src/lib/access/auth/agentSessionGate.ts +32 -8
- package/apps/storefront/src/lib/access/auth/brokerAssertion.ts +16 -2
- package/apps/storefront/src/lib/access/auth/contactReaderAuthority.ts +39 -0
- package/apps/storefront/src/lib/access/auth/loginGate.ts +15 -17
- package/apps/storefront/src/lib/access/dashboard/staffAdmission.ts +5 -0
- package/apps/storefront/src/lib/access/dashboard/tenantSelection.ts +2 -0
- package/apps/storefront/src/lib/access/shared/capability.ts +14 -6
- package/apps/storefront/src/lib/access/shared/generated/capabilityCatalog.generated.ts +161 -0
- package/apps/storefront/src/lib/activity/activityScope.ts +36 -0
- package/apps/storefront/src/lib/activity/recordActivity.ts +23 -1
- package/apps/storefront/src/lib/activity/serverFanOut.ts +31 -0
- package/apps/storefront/src/lib/activity/store.ts +9 -3
- package/apps/storefront/src/lib/checkout/agent/checkoutProtocol.ts +196 -0
- package/apps/storefront/src/lib/checkout/agent/checkoutSession.ts +296 -0
- package/apps/storefront/src/lib/checkout/agent/webBotAuth.ts +347 -0
- package/apps/storefront/src/lib/checkout/commerce/cardCartForm.ts +2 -0
- package/apps/storefront/src/lib/checkout/commerce/cartSession.ts +109 -12
- package/apps/storefront/src/lib/checkout/commerce/checkoutHandoffRoute.ts +53 -25
- package/apps/storefront/src/lib/checkout/commerce/finalCartAuthorization.ts +48 -6
- package/apps/storefront/src/lib/checkout/proposal/cartProposal.ts +388 -0
- package/apps/storefront/src/lib/checkout/proposal/listMatcher.ts +149 -0
- package/apps/storefront/src/lib/checkout/proposal/proposalRoutes.ts +522 -0
- package/apps/storefront/src/lib/checkout/proposal/reviewHtml.ts +100 -0
- package/apps/storefront/src/lib/compliance/browser/checkoutVerificationFlow.ts +26 -2
- package/apps/storefront/src/lib/content/shared/redirects.ts +21 -4
- package/apps/storefront/src/lib/publication/shared/edgeCache.ts +5 -2
- package/apps/storefront/src/lib/publication/shared/previewAnnotations/renderDecorator.ts +15 -8
- package/apps/storefront/src/lib/publication/shared/runtimeEnv.ts +1 -1
- package/apps/storefront/src/middleware/controlPlane.ts +15 -1
- package/apps/storefront/src/middleware/index.ts +7 -3
- package/apps/storefront/src/middleware/renderChain.ts +46 -33
- package/apps/storefront/src/pages/api/cart-proposal/confirm.ts +12 -0
- package/apps/storefront/src/pages/api/cart-proposal/review.ts +26 -0
- package/apps/storefront/src/pages/api/commerce/checkout-sessions/[id].ts +9 -0
- package/apps/storefront/src/pages/api/commerce/checkout-sessions.ts +8 -0
- package/apps/storefront/src/pages/api/commerce/final-cart-authorization.ts +29 -6
- package/apps/storefront/src/pages/api/compliance/preflight.ts +25 -1
- package/apps/storefront/src/pages/products/[handle].astro +2 -15
- package/apps/storefront/tsconfig.astro.json +11 -0
- package/package.json +1 -1
- package/packages/public-runtime/src/activity/README.md +12 -0
- package/packages/public-runtime/src/activity/catalog.ts +7 -0
- package/packages/public-runtime/src/activity/event.ts +19 -0
- package/packages/public-runtime/src/activity/redaction.ts +31 -2
- package/packages/public-runtime/src/catalog-files.ts +3 -2
- package/packages/public-runtime/src/checkout-orders.ts +18 -19
- package/packages/public-runtime/src/checkout.ts +70 -0
- package/packages/public-runtime/src/compliance/evaluate.ts +60 -1
- package/packages/public-runtime/src/compliance/index.ts +1 -1
- package/packages/public-runtime/src/compliance/pact-report.ts +184 -3
- package/packages/public-runtime/src/compliance/profile.ts +268 -20
- package/packages/public-runtime/src/compliance/ruleset.ts +42 -0
- package/packages/public-runtime/src/customization-runtime.ts +1 -1
- package/packages/public-runtime/src/product.ts +9 -1
- package/packages/public-runtime/src/publication-content-redirects.ts +16 -3
- package/packages/public-runtime/tests/fixtures/compliance-decisions.golden.json +252 -52
- package/pnpm-lock.runner.yaml +0 -15
- package/scripts/tenant/materialize-tenant-content.mjs +3 -0
- package/scripts/tenant/validate.mjs +128 -0
- package/apps/storefront/dev-plugins/dev-publish.mjs +0 -55
- package/apps/storefront/drizzle.config.apps.ts +0 -13
- package/apps/storefront/integrations/preview-annotations-client-bundle.mjs +0 -80
- package/apps/storefront/migrations-apps/0000_fast_millenium_guard.sql +0 -129
- package/apps/storefront/migrations-apps/0001_woozy_lyja.sql +0 -22
- package/apps/storefront/migrations-apps/0002_good_switch.sql +0 -26
- package/apps/storefront/migrations-apps/0003_mute_marauders.sql +0 -12
- package/apps/storefront/migrations-apps/0004_deep_morph.sql +0 -36
- package/apps/storefront/migrations-apps/0005_common_mystique.sql +0 -23
- package/apps/storefront/migrations-apps/0006_broad_microchip.sql +0 -16
- package/apps/storefront/migrations-apps/0007_brave_outlaw_kid.sql +0 -15
- package/apps/storefront/migrations-apps/0008_blue_dazzler.sql +0 -11
- package/apps/storefront/migrations-apps/0009_bored_shinobi_shaw.sql +0 -15
- package/apps/storefront/migrations-apps/0010_ws4_verified_provider_measurement.sql +0 -35
- package/apps/storefront/migrations-apps/0011_dt_tracking_tenant_databases.sql +0 -23
- package/apps/storefront/migrations-apps/0012_test_destinations.sql +0 -29
- package/apps/storefront/migrations-apps/0013_publication_space_editorial.sql +0 -64
- package/apps/storefront/migrations-apps/0014_retire_candidate_ledger.sql +0 -12
- package/apps/storefront/migrations-apps/0015_merchant_calculation_settings.sql +0 -31
- package/apps/storefront/migrations-apps/0016_scrappy_commerce_contract.sql +0 -166
- package/apps/storefront/migrations-apps/0017_commerce_durable_projection.sql +0 -28
- package/apps/storefront/migrations-apps/0018_commerce_provider_cart_identity.sql +0 -13
- package/apps/storefront/migrations-apps/0019_commerce_source_authority.sql +0 -23
- package/apps/storefront/migrations-apps/0020_contact_messages.sql +0 -18
- package/apps/storefront/migrations-apps/0021_contact_vault.sql +0 -166
- package/apps/storefront/migrations-apps/0022_contact_vault_fields.sql +0 -342
- package/apps/storefront/migrations-apps/0023_principal_grant_contacts.sql +0 -15
- package/apps/storefront/migrations-apps/0024_order_contact_erasure_state.sql +0 -22
- package/apps/storefront/migrations-apps/0025_shipment_audit_order_lookup.sql +0 -2
- package/apps/storefront/migrations-apps/0026_fulfillment_assignment.sql +0 -44
- package/apps/storefront/migrations-apps/0027_app_contact_authority.sql +0 -70
- package/apps/storefront/migrations-apps/0028_app_jwk_and_candidates.sql +0 -133
- package/apps/storefront/migrations-apps/0029_inventory_locations_wms.sql +0 -38
- package/apps/storefront/migrations-apps/0030_assignment_split_shipments.sql +0 -149
- package/apps/storefront/migrations-apps/meta/0000_snapshot.json +0 -843
- package/apps/storefront/migrations-apps/meta/0001_snapshot.json +0 -990
- package/apps/storefront/migrations-apps/meta/0002_snapshot.json +0 -1160
- package/apps/storefront/migrations-apps/meta/0003_snapshot.json +0 -1235
- package/apps/storefront/migrations-apps/meta/0004_snapshot.json +0 -1474
- package/apps/storefront/migrations-apps/meta/0005_snapshot.json +0 -1617
- package/apps/storefront/migrations-apps/meta/0006_snapshot.json +0 -1716
- package/apps/storefront/migrations-apps/meta/0007_snapshot.json +0 -1812
- package/apps/storefront/migrations-apps/meta/0008_snapshot.json +0 -1881
- package/apps/storefront/migrations-apps/meta/0009_snapshot.json +0 -1979
- package/apps/storefront/migrations-apps/meta/_journal.json +0 -76
- package/apps/storefront/perf/README.md +0 -64
- package/apps/storefront/perf/assert-budgets.ts +0 -159
- package/apps/storefront/platform-reconcile-tenants.json +0 -3
- package/apps/storefront/playwright.config.ts +0 -23
- package/apps/storefront/public/js/dashboard-access-banner.js +0 -53
- package/apps/storefront/public/js/dashboard-apps.js +0 -183
- package/apps/storefront/public/js/dashboard-contact-capture.js +0 -52
- package/apps/storefront/public/js/dashboard-delegate.js +0 -167
- package/apps/storefront/public/js/dashboard-team.js +0 -356
- package/apps/storefront/public/js/dashboard-vendor-search.js +0 -115
- package/apps/storefront/src/components/content/storefront/AsyncStylesheet.astro +0 -26
- package/apps/storefront/src/components/integrations/storefront/WidgetFrame.astro +0 -34
- package/apps/storefront/src/config/adminTenantLookup.ts +0 -35
- package/apps/storefront/src/config/evidenceTenantRoster.ts +0 -25
- package/apps/storefront/src/config/staffProvisionProjection.ts +0 -24
- package/apps/storefront/src/config/storeName.ts +0 -34
- package/apps/storefront/src/lib/access/admin/README.md +0 -122
- package/apps/storefront/src/lib/access/admin/adminShell.ts +0 -281
- package/apps/storefront/src/lib/access/admin/envelope.ts +0 -9
- package/apps/storefront/src/lib/access/admin/viewmodels/settings.ts +0 -64
- package/apps/storefront/src/lib/access/auth/adminApiGuard.ts +0 -66
- package/apps/storefront/src/lib/access/auth/adminEntry.ts +0 -126
- package/apps/storefront/src/lib/access/auth/mcpClientAssertion.ts +0 -217
- package/apps/storefront/src/lib/access/auth/operatorSession.ts +0 -120
- package/apps/storefront/src/lib/access/dashboard/agentStoreEntry.ts +0 -253
- package/apps/storefront/src/lib/access/dashboard/delegateGate.ts +0 -130
- package/apps/storefront/src/lib/access/dashboard/staffStoreEntry.ts +0 -153
- package/apps/storefront/src/lib/access/dashboard/staffVendorAccess.ts +0 -474
- package/apps/storefront/src/lib/access/dashboard/vendorQuery.ts +0 -61
- package/apps/storefront/src/lib/access/fixtures/settings.ts +0 -67
- package/apps/storefront/src/lib/access/fixtures/tenants.ts +0 -29
- package/apps/storefront/src/lib/access/notifications/magicLinkInviteEmail.ts +0 -62
- package/apps/storefront/src/lib/access/shared/grantContactLookup.ts +0 -97
- package/apps/storefront/src/lib/access/shared/principalGrants.ts +0 -775
- package/apps/storefront/src/lib/activity/alerts.ts +0 -426
- package/apps/storefront/src/lib/activity/candidateSubmitLease.ts +0 -87
- package/apps/storefront/src/lib/activity/changeActorAttribution.ts +0 -181
- package/apps/storefront/src/lib/activity/evidence/evidenceReference.ts +0 -32
- package/apps/storefront/src/lib/activity/fixtures/evidence.ts +0 -99
- package/apps/storefront/src/lib/activity/query.ts +0 -403
- package/apps/storefront/src/lib/activity/uiActor.ts +0 -152
- package/apps/storefront/src/lib/admin/services/mock/index.ts +0 -12
- package/apps/storefront/src/lib/assets/addons/catalog.ts +0 -60
- package/apps/storefront/src/lib/assets/addons/storeEntitlements.ts +0 -260
- package/apps/storefront/src/lib/assets/durableAssetsRoute.ts +0 -65
- package/apps/storefront/src/lib/assets/durableR2.ts +0 -473
- package/apps/storefront/src/lib/assets/durableR2Bindings.ts +0 -33
- package/apps/storefront/src/lib/automation/admin/viewmodels/aiWorkflows.ts +0 -65
- package/apps/storefront/src/lib/automation/ai/approvalMode.ts +0 -20
- package/apps/storefront/src/lib/automation/ai/askEnvelope.ts +0 -47
- package/apps/storefront/src/lib/automation/ai/audit.ts +0 -12
- package/apps/storefront/src/lib/automation/ai/operationalRuns.ts +0 -155
- package/apps/storefront/src/lib/automation/ai/recommendation.ts +0 -20
- package/apps/storefront/src/lib/automation/fixtures/aiWorkflows.ts +0 -59
- package/apps/storefront/src/lib/catalog/admin/catalogSettingsClient.ts +0 -250
- package/apps/storefront/src/lib/catalog/admin/catalogSettingsFields.ts +0 -134
- package/apps/storefront/src/lib/catalog/admin/catalogSettingsPreview.ts +0 -37
- package/apps/storefront/src/lib/catalog/admin/catalogSettingsPricingView.ts +0 -39
- package/apps/storefront/src/lib/catalog/admin/catalogSettingsRuleView.ts +0 -44
- package/apps/storefront/src/lib/catalog/admin/catalogSettingsSkuView.ts +0 -21
- package/apps/storefront/src/lib/catalog/admin/catalogSettingsState.ts +0 -132
- package/apps/storefront/src/lib/catalog/admin/productSetupPhoto.ts +0 -23
- package/apps/storefront/src/lib/catalog/admin/productsTabClient.ts +0 -193
- package/apps/storefront/src/lib/catalog/admin/services/catalogProducts.ts +0 -32
- package/apps/storefront/src/lib/catalog/admin/viewmodels/catalogProducts.ts +0 -164
- package/apps/storefront/src/lib/catalog/admin/viewmodels/products.ts +0 -267
- package/apps/storefront/src/lib/catalog/fixtures/products.ts +0 -105
- package/apps/storefront/src/lib/catalog/tot/syncState.ts +0 -111
- package/apps/storefront/src/lib/checkout/admin/checkoutStyleInput.ts +0 -172
- package/apps/storefront/src/lib/checkout/admin/checkoutStyleTarget.ts +0 -65
- package/apps/storefront/src/lib/checkout/browser/checkoutExciseForm.ts +0 -152
- package/apps/storefront/src/lib/checkout/browser/checkoutRequirementsForm.ts +0 -72
- package/apps/storefront/src/lib/checkout/browser/checkoutStyleForm.ts +0 -110
- package/apps/storefront/src/lib/checkout/browser/settingDraftActions.ts +0 -367
- package/apps/storefront/src/lib/checkout/commerce/checkoutSettings.ts +0 -246
- package/apps/storefront/src/lib/checkout/fixtures/checkoutSettings.ts +0 -77
- package/apps/storefront/src/lib/content/blog/markdown.ts +0 -174
- package/apps/storefront/src/lib/content/chrome/examples/tokenoftrust.chrome-assignments.example.json +0 -6
- package/apps/storefront/src/lib/content/chrome/examples/tokenoftrust.chrome.example.json +0 -79
- package/apps/storefront/src/lib/content/index.ts +0 -27
- package/apps/storefront/src/lib/content/the-build/readingTime.ts +0 -35
- package/apps/storefront/src/lib/content/the-build/toc.ts +0 -95
- package/apps/storefront/src/lib/customers/admin/viewmodels/customers.ts +0 -56
- package/apps/storefront/src/lib/customers/contactVault/blindIndex.ts +0 -28
- package/apps/storefront/src/lib/customers/contactVault/contactAudit.ts +0 -149
- package/apps/storefront/src/lib/customers/contactVault/dataKeys.ts +0 -283
- package/apps/storefront/src/lib/customers/contactVault/fieldEnvelope.ts +0 -88
- package/apps/storefront/src/lib/customers/contactVault/isolateMetrics.ts +0 -37
- package/apps/storefront/src/lib/customers/contactVault/kms.ts +0 -292
- package/apps/storefront/src/lib/customers/contactVault/retention.ts +0 -302
- package/apps/storefront/src/lib/customers/contactVault/retentionPolicy.ts +0 -290
- package/apps/storefront/src/lib/customers/contactVault/zip3.ts +0 -24
- package/apps/storefront/src/lib/customers/fixtures/customers.ts +0 -86
- package/apps/storefront/src/lib/developer-experience/admin/memoizeByEnvelope.ts +0 -27
- package/apps/storefront/src/lib/developer-experience/admin/seed.ts +0 -54
- package/apps/storefront/src/lib/developer-experience/dev/activityIngestToken.ts +0 -85
- package/apps/storefront/src/lib/developer-experience/dev/activityStore.ts +0 -82
- package/apps/storefront/src/lib/developer-experience/dev/agentGrantAttestation.ts +0 -74
- package/apps/storefront/src/lib/developer-experience/dev/ai-widget-client.js +0 -308
- package/apps/storefront/src/lib/developer-experience/dev/apiBase.ts +0 -17
- package/apps/storefront/src/lib/developer-experience/dev/cliSignInCode.ts +0 -124
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/ask.ts +0 -49
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/clickToSource.ts +0 -71
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/copyCommand.ts +0 -227
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/files.ts +0 -249
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/hostedActivity.ts +0 -500
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/infoPopover.ts +0 -12
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/mcpCopy.ts +0 -34
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/monitor.ts +0 -107
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/stepper.ts +0 -116
- package/apps/storefront/src/lib/developer-experience/dev/cockpit/themeToggle.ts +0 -23
- package/apps/storefront/src/lib/developer-experience/dev/livePreview.ts +0 -77
- package/apps/storefront/src/lib/developer-experience/dev/obstacleStore.ts +0 -214
- package/apps/storefront/src/lib/developer-experience/dev/previewStatus.ts +0 -87
- package/apps/storefront/src/lib/developer-experience/dev/rendezvousBroker.ts +0 -226
- package/apps/storefront/src/lib/developer-experience/dev/runtimeStore.ts +0 -291
- package/apps/storefront/src/lib/developer-experience/dev/storeStatus.ts +0 -142
- package/apps/storefront/src/lib/developer-experience/dev/subjectReissue.ts +0 -67
- package/apps/storefront/src/lib/developer-experience/fixtures/regulated-commerce-truth/index.ts +0 -540
- package/apps/storefront/src/lib/developer-experience/fixtures/ws3-readonly-clone-preflight/index.ts +0 -495
- package/apps/storefront/src/lib/engagement/contact/contactMessages.ts +0 -671
- package/apps/storefront/src/lib/engagement/contact/messagesTabClient.ts +0 -175
- package/apps/storefront/src/lib/engagement/contact/submission.ts +0 -112
- package/apps/storefront/src/lib/engagement/messaging/messagesClient.ts +0 -149
- package/apps/storefront/src/lib/engagement/newsletter/digest.ts +0 -117
- package/apps/storefront/src/lib/engagement/newsletter/resendClient.ts +0 -79
- package/apps/storefront/src/lib/engagement/newsletter/send.ts +0 -265
- package/apps/storefront/src/lib/engagement/social/golive.ts +0 -212
- package/apps/storefront/src/lib/engagement/social/notify.ts +0 -32
- package/apps/storefront/src/lib/engagement/social/tokenStore.ts +0 -171
- package/apps/storefront/src/lib/fulfillment/admin/commerceDesk.ts +0 -180
- package/apps/storefront/src/lib/fulfillment/admin/commerceDeskRouting.ts +0 -45
- package/apps/storefront/src/lib/fulfillment/admin/viewmodels/fulfillment.ts +0 -1071
- package/apps/storefront/src/lib/fulfillment/appShipmentEvidence.ts +0 -271
- package/apps/storefront/src/lib/fulfillment/carrierEvidence.ts +0 -105
- package/apps/storefront/src/lib/fulfillment/commerceProjection.ts +0 -157
- package/apps/storefront/src/lib/fulfillment/commerceReadback.ts +0 -60
- package/apps/storefront/src/lib/fulfillment/commerceRecovery.ts +0 -84
- package/apps/storefront/src/lib/fulfillment/commerceRuntime.ts +0 -41
- package/apps/storefront/src/lib/fulfillment/fulfillmentService.ts +0 -2842
- package/apps/storefront/src/lib/fulfillment/readiness.ts +0 -217
- package/apps/storefront/src/lib/fulfillment/splitShipments.ts +0 -388
- package/apps/storefront/src/lib/fulfillment/syntheticPreviewOrders.ts +0 -292
- package/apps/storefront/src/lib/integrations/apps/appContactAuthority.ts +0 -27
- package/apps/storefront/src/lib/integrations/apps/appContactConsents.ts +0 -118
- package/apps/storefront/src/lib/integrations/apps/appInstallEnv.ts +0 -28
- package/apps/storefront/src/lib/integrations/apps/gtmMigrationDraft.ts +0 -103
- package/apps/storefront/src/lib/integrations/apps/inventoryWrite.ts +0 -906
- package/apps/storefront/src/lib/integrations/apps/orders/customerHash.ts +0 -28
- package/apps/storefront/src/lib/integrations/apps/orders/idempotency.ts +0 -100
- package/apps/storefront/src/lib/integrations/apps/reports/salesVelocityReport.ts +0 -96
- package/apps/storefront/src/lib/integrations/apps/routeParams.ts +0 -45
- package/apps/storefront/src/lib/integrations/webhooks/cloudflareQueueDispatcher.ts +0 -82
- package/apps/storefront/src/lib/integrations/webhooks/commercePayloads.ts +0 -159
- package/apps/storefront/src/lib/integrations/webhooks/deliveryEngine.ts +0 -379
- package/apps/storefront/src/lib/integrations/webhooks/deliveryMapper.ts +0 -34
- package/apps/storefront/src/lib/integrations/webhooks/deliveryStore.ts +0 -903
- package/apps/storefront/src/lib/integrations/webhooks/dispatcher.ts +0 -168
- package/apps/storefront/src/lib/integrations/webhooks/emit.ts +0 -245
- package/apps/storefront/src/lib/integrations/webhooks/events.ts +0 -105
- package/apps/storefront/src/lib/integrations/webhooks/getDispatcher.ts +0 -77
- package/apps/storefront/src/lib/integrations/webhooks/orderFulfilledClaim.ts +0 -75
- package/apps/storefront/src/lib/measurement/admin/analyticsRegionPolicy.ts +0 -151
- package/apps/storefront/src/lib/measurement/admin/analyticsRegionsClient.ts +0 -84
- package/apps/storefront/src/lib/measurement/admin/consentSettingsClient.ts +0 -113
- package/apps/storefront/src/lib/measurement/analytics/index.ts +0 -44
- package/apps/storefront/src/lib/measurement/attribution/attributionService.ts +0 -206
- package/apps/storefront/src/lib/measurement/attribution/orderConversionD1.ts +0 -150
- package/apps/storefront/src/lib/measurement/attribution/verifiedProviderMeasurement.ts +0 -118
- package/apps/storefront/src/lib/measurement/tracking/interactionCollector.ts +0 -205
- package/apps/storefront/src/lib/measurement/tracking/pruneRollup.ts +0 -260
- package/apps/storefront/src/lib/measurement/tracking/tenantReadAccess.ts +0 -138
- package/apps/storefront/src/lib/measurement/tracking-writeback/rewriteTrackingIds.ts +0 -175
- package/apps/storefront/src/lib/measurement/tracking-writeback/writebackCandidate.ts +0 -110
- package/apps/storefront/src/lib/observability/diag/capture.ts +0 -162
- package/apps/storefront/src/lib/observability/diag/consoleLine.ts +0 -76
- package/apps/storefront/src/lib/observability/diag/debugConsole.ts +0 -118
- package/apps/storefront/src/lib/observability/diag/debugMode.ts +0 -163
- package/apps/storefront/src/lib/observability/diag/debugWindow.ts +0 -111
- package/apps/storefront/src/lib/observability/diag/redact.ts +0 -17
- package/apps/storefront/src/lib/observability/diag/reference.ts +0 -148
- package/apps/storefront/src/lib/observability/diag/referenceStore.ts +0 -113
- package/apps/storefront/src/lib/observability/diag/report.ts +0 -110
- package/apps/storefront/src/lib/observability/diag/serverResponseTime.ts +0 -91
- package/apps/storefront/src/lib/observability/diag/session.ts +0 -93
- package/apps/storefront/src/lib/observability/monitoring/manifest.ts +0 -301
- package/apps/storefront/src/lib/observability/performance/lighthouseReport.ts +0 -109
- package/apps/storefront/src/lib/observability/performance/rumAlert.ts +0 -179
- package/apps/storefront/src/lib/orders/admin/orderActions.ts +0 -556
- package/apps/storefront/src/lib/orders/admin/orderClient.ts +0 -248
- package/apps/storefront/src/lib/orders/admin/orderSource.ts +0 -410
- package/apps/storefront/src/lib/orders/admin/services/orders.ts +0 -922
- package/apps/storefront/src/lib/orders/admin/viewmodels/orders.ts +0 -135
- package/apps/storefront/src/lib/orders/data/commerceCallerConfig.ts +0 -37
- package/apps/storefront/src/lib/orders/data/ledgerContractOrders.ts +0 -148
- package/apps/storefront/src/lib/orders/data/orderForwardReceiver.ts +0 -532
- package/apps/storefront/src/lib/orders/data/orderMapper.ts +0 -454
- package/apps/storefront/src/lib/orders/data/ordersStore.ts +0 -623
- package/apps/storefront/src/lib/orders/data/sourceAuthorityContract.ts +0 -380
- package/apps/storefront/src/lib/orders/data/sourceAuthorityStore.ts +0 -830
- package/apps/storefront/src/lib/orders/fixtures/orders.ts +0 -186
- package/apps/storefront/src/lib/orders/providers/foxyOrderClient.ts +0 -207
- package/apps/storefront/src/lib/orders/vault/appRecipientRead.ts +0 -222
- package/apps/storefront/src/lib/orders/vault/contactIntake.ts +0 -63
- package/apps/storefront/src/lib/orders/vault/contactRoutes.ts +0 -110
- package/apps/storefront/src/lib/orders/vault/kmsEnv.ts +0 -11
- package/apps/storefront/src/lib/orders/vault/orderContact.ts +0 -96
- package/apps/storefront/src/lib/orders/vault/orderContactVault.ts +0 -1427
- package/apps/storefront/src/lib/orders/vault/vault-test-support.ts +0 -258
- package/apps/storefront/src/lib/publication/d1/schema-apps.ts +0 -808
- package/apps/storefront/src/lib/publication/notifications/candidateConflictEmail.ts +0 -80
- package/apps/storefront/src/lib/publication/servingMode.ts +0 -52
- package/apps/storefront/src/lib/publication/shared/previewAnnotations/moduleAsset.ts +0 -65
- package/apps/storefront/src/lib/publication/shared/routedApiParam.ts +0 -38
- package/apps/storefront/src/lib/reporting/admin/viewmodels/reporting.ts +0 -34
- package/apps/storefront/src/lib/reporting/fixtures/reporting.ts +0 -51
- package/apps/storefront/src/lib/subscriptions/admin/viewmodels/subscriptions.ts +0 -106
- package/apps/storefront/src/lib/subscriptions/fixtures/subscriptions.ts +0 -398
- package/apps/storefront/src/pages/api/admin/activity-alerts.ts +0 -58
- package/apps/storefront/src/pages/api/admin/commerce-readiness.ts +0 -172
- package/apps/storefront/src/pages/api/admin/fulfillment/batches.ts +0 -59
- package/apps/storefront/src/pages/api/admin/fulfillment/orders/[id]/address.ts +0 -66
- package/apps/storefront/src/pages/api/admin/fulfillment/orders/[id].ts +0 -118
- package/apps/storefront/src/pages/api/admin/fulfillment/orders/contact-search.ts +0 -26
- package/apps/storefront/src/pages/api/admin/fulfillment/orders/contacts.ts +0 -22
- package/apps/storefront/src/pages/api/admin/interaction-insights.ts +0 -101
- package/apps/storefront/src/pages/api/admin/orders/[id].ts +0 -63
- package/apps/storefront/src/pages/api/admin/orders.ts +0 -65
- package/apps/storefront/src/pages/api/contact.ts +0 -144
- package/apps/storefront/src/scheduled/contactVaultSweep.ts +0 -113
- package/apps/storefront/src/styles/admin.css +0 -1161
- package/apps/storefront/test/pipeline/fake-preview-services.ts +0 -288
- package/apps/storefront/test/pipeline/harness.ts +0 -209
- package/apps/storefront/test/pipeline/intake-worker.mjs +0 -14
- package/apps/storefront/test/pipeline/matrix-faults.ts +0 -366
- package/apps/storefront/test/pipeline/pipeline-host.ts +0 -418
- package/apps/storefront/test/pipeline/pool-config.cjs +0 -85
- package/apps/storefront/test/pipeline/publish-kinds.ts +0 -279
- package/apps/storefront/test/pipeline/synthetic-job.ts +0 -303
- package/apps/storefront/test/pipeline-faults/matrix-lib.ts +0 -696
- package/apps/storefront/vitest.config.cts +0 -44
- package/apps/storefront/vitest.faults.config.cts +0 -13
- package/apps/storefront/vitest.workers.config.cts +0 -7
- package/packages/feedback/fixtures/v1/expected-findings.json +0 -16
- package/packages/feedback/fixtures/v1/feedback-action/invalid/carries-transport-credential.json +0 -14
- package/packages/feedback/fixtures/v1/feedback-action/invalid/control-char-in-description.json +0 -13
- package/packages/feedback/fixtures/v1/feedback-action/invalid/legacy-schema-version.json +0 -13
- package/packages/feedback/fixtures/v1/feedback-action/invalid/page-path-with-query.json +0 -14
- package/packages/feedback/fixtures/v1/feedback-action/invalid/title-too-short.json +0 -13
- package/packages/feedback/fixtures/v1/feedback-action/valid/element-problem.json +0 -33
- package/packages/feedback/fixtures/v1/feedback-action/valid/page-comment.json +0 -20
- package/packages/feedback/fixtures/v1/feedback-action/valid/region-improvement.json +0 -24
- package/packages/feedback/fixtures/v1/feedback-action/valid/text-selection-feature.json +0 -30
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/accepted-at-not-utc.json +0 -9
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/bad-feedback-id.json +0 -9
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/legacy-contract-version.json +0 -9
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/missing-replayed.json +0 -8
- package/packages/feedback/fixtures/v1/submission-receipt/valid/accepted.json +0 -9
- package/packages/feedback/fixtures/v1/submission-receipt/valid/fractional-seconds.json +0 -9
- package/packages/feedback/fixtures/v1/submission-receipt/valid/replayed.json +0 -9
- package/packages/feedback/fixtures/v1/target-evidence/invalid/page-kind-claims-structure.json +0 -6
- package/packages/feedback/fixtures/v1/target-evidence/invalid/path-too-deep.json +0 -17
- package/packages/feedback/fixtures/v1/target-evidence/invalid/relative-region-out-of-range.json +0 -11
- package/packages/feedback/fixtures/v1/target-evidence/invalid/selector-uses-class.json +0 -7
- package/packages/feedback/fixtures/v1/target-evidence/invalid/source-precision-without-anchor.json +0 -6
- package/packages/feedback/fixtures/v1/target-evidence/valid/element-structure.json +0 -21
- package/packages/feedback/fixtures/v1/target-evidence/valid/host-iframe.json +0 -14
- package/packages/feedback/fixtures/v1/target-evidence/valid/source-anchor.json +0 -12
- package/packages/feedback/fixtures/v1/target-evidence/valid/text-selection.json +0 -26
- package/packages/feedback/package.json +0 -35
- package/packages/feedback/scripts/build-player.mjs +0 -28
- package/packages/feedback/src/index.ts +0 -1
- package/packages/feedback/src/primitives/composer.ts +0 -639
- package/packages/feedback/src/primitives/dialog.ts +0 -263
- package/packages/feedback/src/primitives/hostAdapter.ts +0 -116
- package/packages/feedback/src/primitives/index.ts +0 -10
- package/packages/feedback/src/primitives/launcher.ts +0 -200
- package/packages/feedback/src/primitives/picker.ts +0 -663
- package/packages/feedback/src/primitives/redaction.ts +0 -204
- package/packages/feedback/src/primitives/selection.ts +0 -49
- package/packages/feedback/src/primitives/shiftAccelerator.ts +0 -160
- package/packages/feedback/src/primitives/styles.ts +0 -77
- package/packages/feedback/src/primitives/surface.ts +0 -124
- package/packages/feedback/src/primitives/target.ts +0 -293
- package/packages/feedback/src/tour/canonical.ts +0 -27
- package/packages/feedback/src/tour/index.ts +0 -3
- package/packages/feedback/src/tour/player/browser.ts +0 -5
- package/packages/feedback/src/tour/player/host.ts +0 -12
- package/packages/feedback/src/tour/player/index.ts +0 -205
- package/packages/feedback/src/tour/player/manifest.ts +0 -76
- package/packages/feedback/src/tour/player/player.css +0 -130
- package/packages/feedback/src/tour/player/resolver.ts +0 -12
- package/packages/feedback/src/tour/player/surface.ts +0 -295
- package/packages/feedback/src/tour/player/types.ts +0 -8
- package/packages/feedback/src/tour/resolve/core.ts +0 -267
- package/packages/feedback/src/tour/resolve/dom.ts +0 -115
- package/packages/feedback/src/tour/resolve/index.ts +0 -29
- package/packages/feedback/src/tour/sanitize.ts +0 -76
- package/packages/feedback/src/tour/schema.ts +0 -362
- package/packages/feedback/src/v1/guards.ts +0 -335
- package/packages/feedback/src/v1/index.ts +0 -3
- package/packages/feedback/src/v1/protectedSurfaces.ts +0 -32
- package/packages/feedback/src/v1/types.ts +0 -177
- package/packages/feedback/tsconfig.json +0 -9
- package/packages/feedback/vitest.config.cts +0 -21
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Retry/backoff dispatcher — PrivateApps epic, D4 (Chunk C: Dispatcher +
|
|
3
|
-
* Delivery Engine). Owns WHEN a delivery gets attempted: the `WebhookDispatcher`
|
|
4
|
-
* transport seam that Chunk D (emit hooks) and Chunk E (routes — e.g. manual
|
|
5
|
-
* replay) enqueue work through, the pure backoff policy every dispatcher
|
|
6
|
-
* implementation shares, and `InMemoryDispatcher`, the V1 implementation that
|
|
7
|
-
* per `d4-design-resolutions` IS the production path.
|
|
8
|
-
*
|
|
9
|
-
* `cloudflareQueueDispatcher.ts` builds a Cloudflare Queues adapter behind the
|
|
10
|
-
* SAME seam, but it is not wired into the Worker entry yet — per the design
|
|
11
|
-
* resolution, V1 does not provision a real Queue. Investigation into whether
|
|
12
|
-
* that's even possible with the current adapter: `@astrojs/cloudflare` v14
|
|
13
|
-
* generates the Worker entry as `{ fetch: handle }` only (see
|
|
14
|
-
* `node_modules/@astrojs/cloudflare/dist/entrypoints/server.js`) — there is no
|
|
15
|
-
* hook to also export a `queue()` consumer alongside the generated `fetch()`.
|
|
16
|
-
* `dist/index.js` DOES thread `queues.producers` through to the underlying
|
|
17
|
-
* `@cloudflare/vite-plugin` config (for `env.WEBHOOK_DELIVERY_QUEUE.send()` to
|
|
18
|
-
* work as a producer binding), but nothing analogous exists for consumers.
|
|
19
|
-
* TODO(infra decision — wire the Queue consumer entry): once a real Queue is
|
|
20
|
-
* provisioned, either (a) upgrade `@astrojs/cloudflare` in case a later
|
|
21
|
-
* version adds consumer support, or (b) hand-write a small wrapper worker
|
|
22
|
-
* module that re-exports the generated `fetch` and ALSO exports a `queue()`
|
|
23
|
-
* that calls `cloudflareQueueDispatcher.ts`'s `queue()` — replacing
|
|
24
|
-
* `wrangler.toml`'s implicit "main is generated" note with an explicit `main`
|
|
25
|
-
* pointing at that wrapper. Neither is done here; this file and
|
|
26
|
-
* `cloudflareQueueDispatcher.ts` only build the seam + adapter, gated behind
|
|
27
|
-
* `getDispatcher.ts`'s binding-presence check (no `[[queues]]` binding is
|
|
28
|
-
* declared in `wrangler.toml`, so that branch is dead code until infra adds one).
|
|
29
|
-
*
|
|
30
|
-
* Deliberately decoupled from `deliveryEngine.ts`: the dispatcher's job is
|
|
31
|
-
* scheduling ("run this deliveryId now / at this time"), not HTTP delivery.
|
|
32
|
-
* `InMemoryDispatcher` takes the engine's `attemptDelivery` as an injected
|
|
33
|
-
* `AttemptDeliveryFn` rather than importing `deliveryEngine.ts` directly, so
|
|
34
|
-
* tests can drive it with a fake attempt function with no store/registry/
|
|
35
|
-
* signing/fetch involved at all.
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
/** One unit of dispatcher work: attempt (or retry) delivering one delivery. */
|
|
39
|
-
export interface DispatchJob {
|
|
40
|
-
deliveryId: string;
|
|
41
|
-
/** ISO timestamp; the dispatcher must not run the job before this. Omitted = run ASAP. */
|
|
42
|
-
notBefore?: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** The transport seam `deliveryEngine.ts`'s callers (Chunk D/E) enqueue work through. */
|
|
46
|
-
export interface WebhookDispatcher {
|
|
47
|
-
enqueue(job: DispatchJob): Promise<void>;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* `attemptDelivery`'s outcome, as classified by `deliveryEngine.ts` — this is
|
|
52
|
-
* the type the dispatcher branches on to decide whether to reschedule.
|
|
53
|
-
* `"retrying"`/`"dead-lettered"` mirror `DeliveryState` names on purpose (see
|
|
54
|
-
* `deliveryStore.ts`); `"delivered"`/`"dead-lettered"` are terminal — only
|
|
55
|
-
* `"retrying"` ever reschedules.
|
|
56
|
-
*/
|
|
57
|
-
export type AttemptOutcome =
|
|
58
|
-
| { kind: "delivered" }
|
|
59
|
-
| { kind: "retrying"; nextAttemptAt: string }
|
|
60
|
-
| { kind: "dead-lettered"; reason: "install_unavailable" | "max_attempts" | "endpoint_blocked" | "payload_refused" };
|
|
61
|
-
|
|
62
|
-
/** One attempt of one delivery. Implemented by `deliveryEngine.ts`'s `attemptDelivery`, partially applied over its deps. */
|
|
63
|
-
export type AttemptDeliveryFn = (deliveryId: string) => Promise<AttemptOutcome>;
|
|
64
|
-
|
|
65
|
-
/** Deliveries stop retrying and are dead-lettered after this many attempts. */
|
|
66
|
-
export const MAX_DELIVERY_ATTEMPTS = 6;
|
|
67
|
-
|
|
68
|
-
const BASE_DELAY_SECONDS = 30;
|
|
69
|
-
const MAX_DELAY_SECONDS = 60 * 60; // 1h cap
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Exponential backoff with full jitter, capped at 1h:
|
|
73
|
-
* `min(BASE * 2^(attempt-1), cap)`, then a uniform pick in `(0, cap]`.
|
|
74
|
-
* `attempt` is 1-indexed — the attempt number that just failed (so attempt 1
|
|
75
|
-
* ⇒ up to 30s, attempt 2 ⇒ up to 60s, … capping out well before attempt 6 /
|
|
76
|
-
* {@link MAX_DELIVERY_ATTEMPTS}).
|
|
77
|
-
*/
|
|
78
|
-
export function computeBackoffDelaySeconds(attempt: number): number {
|
|
79
|
-
const exp = BASE_DELAY_SECONDS * 2 ** Math.max(0, attempt - 1);
|
|
80
|
-
const cap = Math.min(exp, MAX_DELAY_SECONDS);
|
|
81
|
-
return Math.floor(Math.random() * cap) + 1; // never 0 — always some delay before retrying
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
interface PendingJob {
|
|
85
|
-
deliveryId: string;
|
|
86
|
-
notBefore: number;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface InMemoryDispatcherOptions {
|
|
90
|
-
/** Injected clock (epoch ms). Defaults to `Date.now`. */
|
|
91
|
-
now?: () => number;
|
|
92
|
-
/**
|
|
93
|
-
* Default `true` (production behavior): an immediate job runs on a
|
|
94
|
-
* microtask and a delayed retry schedules a real `setTimeout`. Set `false`
|
|
95
|
-
* in tests — jobs queue internally instead of touching real timers; drain
|
|
96
|
-
* them deterministically with {@link InMemoryDispatcher.runDueNow}.
|
|
97
|
-
*/
|
|
98
|
-
useRealTimers?: boolean;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* V1's WORKING production dispatcher (per `d4-design-resolutions`) — an
|
|
103
|
-
* in-process queue with no external infra. `enqueue()` never awaits the
|
|
104
|
-
* actual HTTP attempt; it schedules it and returns.
|
|
105
|
-
*/
|
|
106
|
-
export class InMemoryDispatcher implements WebhookDispatcher {
|
|
107
|
-
private readonly now: () => number;
|
|
108
|
-
private readonly useRealTimers: boolean;
|
|
109
|
-
private readonly pending: PendingJob[] = [];
|
|
110
|
-
private readonly timers = new Set<ReturnType<typeof setTimeout>>();
|
|
111
|
-
|
|
112
|
-
constructor(
|
|
113
|
-
private readonly attempt: AttemptDeliveryFn,
|
|
114
|
-
options: InMemoryDispatcherOptions = {},
|
|
115
|
-
) {
|
|
116
|
-
this.now = options.now ?? (() => Date.now());
|
|
117
|
-
this.useRealTimers = options.useRealTimers ?? true;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async enqueue(job: DispatchJob): Promise<void> {
|
|
121
|
-
const notBefore = job.notBefore ? Date.parse(job.notBefore) : this.now();
|
|
122
|
-
this.scheduleOrRun(job.deliveryId, notBefore);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Test-only deterministic drain: repeatedly runs every job whose
|
|
127
|
-
* `notBefore` is `<= now()` — including retries a just-run attempt itself
|
|
128
|
-
* schedules, if those are also due — until nothing is due. Only meaningful
|
|
129
|
-
* when constructed with `useRealTimers: false`; with real timers, jobs never
|
|
130
|
-
* sit in `pending` (they're already scheduled via microtask/setTimeout).
|
|
131
|
-
*/
|
|
132
|
-
async runDueNow(): Promise<void> {
|
|
133
|
-
for (;;) {
|
|
134
|
-
const now = this.now();
|
|
135
|
-
const dueIndex = this.pending.findIndex((j) => j.notBefore <= now);
|
|
136
|
-
if (dueIndex === -1) return;
|
|
137
|
-
const [job] = this.pending.splice(dueIndex, 1);
|
|
138
|
-
await this.run(job!.deliveryId);
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private scheduleOrRun(deliveryId: string, notBefore: number): void {
|
|
143
|
-
if (!this.useRealTimers) {
|
|
144
|
-
this.pending.push({ deliveryId, notBefore });
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
const delayMs = notBefore - this.now();
|
|
148
|
-
if (delayMs <= 0) {
|
|
149
|
-
queueMicrotask(() => {
|
|
150
|
-
void this.run(deliveryId);
|
|
151
|
-
});
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
const timer = setTimeout(() => {
|
|
155
|
-
this.timers.delete(timer);
|
|
156
|
-
void this.run(deliveryId);
|
|
157
|
-
}, delayMs);
|
|
158
|
-
if (typeof timer.unref === "function") timer.unref(); // don't hold the process open for a pending retry
|
|
159
|
-
this.timers.add(timer);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
private async run(deliveryId: string): Promise<void> {
|
|
163
|
-
const outcome = await this.attempt(deliveryId);
|
|
164
|
-
if (outcome.kind === "retrying") {
|
|
165
|
-
this.scheduleOrRun(deliveryId, Date.parse(outcome.nextAttemptAt));
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Webhook event emitter — PrivateApps epic, D4 (Chunk D: Emit hooks). The one
|
|
3
|
-
* place a caller (today: the two D2 admin routes; a future catalog/order/etc.
|
|
4
|
-
* trigger later) turns "something happened" into a persisted event + one
|
|
5
|
-
* delivery per eligible install + an enqueued dispatcher job per delivery.
|
|
6
|
-
* Targeting is resolved HERE so callers never hand-roll it: an install is
|
|
7
|
-
* eligible for a topic when it's `active` (`app.uninstalled` is the one
|
|
8
|
-
* exception — see {@link isEligibleTarget}, and `deliveryEngine.ts`'s
|
|
9
|
-
* matching `isDeadInstall` exemption), has a `webhookEndpoint`, and its
|
|
10
|
-
* `scopes` include `TOPIC_REQUIRED_SCOPE[topic]` when that topic requires one
|
|
11
|
-
* (`events.ts`) — an app only receives events it's scoped for.
|
|
12
|
-
*
|
|
13
|
-
* Two targeting modes, chosen by whether `opts.installId` is given:
|
|
14
|
-
* - Given: scoped to exactly that one install (still subject to
|
|
15
|
-
* {@link isEligibleTarget}). This is REQUIRED for the two lifecycle
|
|
16
|
-
* topics (`app.installed`/`app.uninstalled` — {@link LIFECYCLE_TOPICS}):
|
|
17
|
-
* each is inherently about ONE subject install, never a tenant-wide
|
|
18
|
-
* broadcast — otherwise every other app on the tenant would learn about
|
|
19
|
-
* (and, for `app.uninstalled`, keep getting redelivered) a DIFFERENT
|
|
20
|
-
* app's lifecycle transitions, a cross-app isolation leak. Omitting it
|
|
21
|
-
* for a lifecycle topic is refused (logged, `null`), not silently
|
|
22
|
-
* broadened.
|
|
23
|
-
* - Omitted: tenant-wide fan-out to every eligible, topic-subscribed
|
|
24
|
-
* install — correct for data topics (`order.created`, etc.), where
|
|
25
|
-
* multiple installed apps legitimately subscribe to the same data.
|
|
26
|
-
*
|
|
27
|
-
* Stage wall: every data topic names the commerce stage it happened in
|
|
28
|
-
* (`opts.stage`, required — a data topic without a valid stage is refused,
|
|
29
|
-
* logged, `null`). Only installs whose `env` is the one that stage admits
|
|
30
|
-
* (`appInstallEnvForStage`) are targeted, so a `test` install never receives
|
|
31
|
-
* a `publication` event and a `production` install never a `preview` one.
|
|
32
|
-
* Lifecycle topics are about one subject install and stay env-agnostic.
|
|
33
|
-
*
|
|
34
|
-
* Mirrors `adminService.ts`'s `getRegistryService()` composition-root shape —
|
|
35
|
-
* reuses it outright for the registry, and builds its own
|
|
36
|
-
* `WebhookDeliveryStore` over the same `STOREFRONT_APPS_DB` binding (the D1
|
|
37
|
-
* binding is one binding shared by every composition root; there is no
|
|
38
|
-
* separate "events DB").
|
|
39
|
-
*
|
|
40
|
-
* Best-effort, not authoritative: an unconfigured runtime, or an unconfigured
|
|
41
|
-
* dispatcher (`getDispatcher()` returning its 503), is logged and swallowed
|
|
42
|
-
* here rather than thrown — a webhook-plumbing outage must never fail the
|
|
43
|
-
* business mutation (install/uninstall) that triggered the emit. Callers
|
|
44
|
-
* should still wrap their own call in try/catch as a second layer (belt +
|
|
45
|
-
* suspenders), since an unexpected store/registry error is NOT swallowed —
|
|
46
|
-
* see the two admin routes this wires into.
|
|
47
|
-
*
|
|
48
|
-
* Payload minimization is enforced at the call site, not here: `data` is
|
|
49
|
-
* passed through to the CloudEvents envelope verbatim, so never pass secrets
|
|
50
|
-
* or raw PII. Per the contract, `app.installed`/`app.uninstalled` carry no
|
|
51
|
-
* customer data at all (envelope identity only — `{}`); any future topic
|
|
52
|
-
* needing customer identity must use the per-(tenant, app-install) opaque
|
|
53
|
-
* hash, never a raw identifier.
|
|
54
|
-
*/
|
|
55
|
-
import {
|
|
56
|
-
COMMERCE_DELIVERY_GRACE_MS,
|
|
57
|
-
CommercePayloadRefused,
|
|
58
|
-
WebhookDeliveryStore,
|
|
59
|
-
type WebhookTopic,
|
|
60
|
-
} from "./deliveryStore.js";
|
|
61
|
-
import { commercePayloadAdmissible, isCommerceTopic } from "./commercePayloads.js";
|
|
62
|
-
import { InMemoryDispatcher } from "./dispatcher.js";
|
|
63
|
-
import { TOPIC_REQUIRED_SCOPE } from "./events.js";
|
|
64
|
-
import { getDispatcher } from "./getDispatcher.js";
|
|
65
|
-
import { getRegistryService } from "../apps/registryAccess.js";
|
|
66
|
-
import type { AppInstall } from "../apps/registryService.js";
|
|
67
|
-
import { fromD1 } from "../../catalog/d1/catalog.js";
|
|
68
|
-
import { setInventoryChangedNotify } from "../../inventory/mirrorStock.js";
|
|
69
|
-
import { getWorkerEnv, readD1, readEnv } from "../../publication/shared/env.js";
|
|
70
|
-
import type { CommerceStage } from "@tot/public-runtime";
|
|
71
|
-
import { appInstallEnvForStage } from "../apps/appInstallEnv.js";
|
|
72
|
-
|
|
73
|
-
/** Contract-scoped payload schema version — `asyncapi.yaml`'s `dataschemaversion` is `const: "1"` for V1. */
|
|
74
|
-
const DATASCHEMA_VERSION = "1";
|
|
75
|
-
const WEBHOOK_SIGNING_PRIVATE_KEY_ENV = "WEBHOOK_SIGNING_PRIVATE_KEY_B64";
|
|
76
|
-
|
|
77
|
-
/** Topics about one subject install, env-agnostic. */
|
|
78
|
-
export type LifecycleWebhookTopic = "app.installed" | "app.uninstalled";
|
|
79
|
-
/** Topics carrying data from one commerce stage. */
|
|
80
|
-
export type DataWebhookTopic = Exclude<WebhookTopic, LifecycleWebhookTopic>;
|
|
81
|
-
|
|
82
|
-
interface EmitOptionsBase {
|
|
83
|
-
/** Override for a future contract version bump. Defaults to {@link DATASCHEMA_VERSION}. */
|
|
84
|
-
dataschemaVersion?: string;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface LifecycleEmitOptions extends EmitOptionsBase {
|
|
88
|
-
/** The event's subject install — delivery is restricted to exactly this one install. */
|
|
89
|
-
installId: string;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export interface DataEmitOptions extends EmitOptionsBase {
|
|
93
|
-
/** The commerce stage the data belongs to; only installs in the env it admits are targeted. */
|
|
94
|
-
stage: CommerceStage;
|
|
95
|
-
/** Restrict delivery to this one install; omit for a tenant-wide fan-out. */
|
|
96
|
-
installId?: string;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export type EmitWebhookEventOptions<T extends WebhookTopic = WebhookTopic> = T extends LifecycleWebhookTopic
|
|
100
|
-
? LifecycleEmitOptions
|
|
101
|
-
: DataEmitOptions;
|
|
102
|
-
|
|
103
|
-
/** `null` when no install on the tenant was eligible — not an error; no event/delivery rows are written in that case. */
|
|
104
|
-
export type EmitWebhookEventResult = { eventId: string; deliveryCount: number } | null;
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Topics that are inherently about ONE subject install, never a tenant-wide
|
|
108
|
-
* broadcast — `emitWebhookEvent` refuses to run one of these without
|
|
109
|
-
* `opts.installId` (see the header + the guard below).
|
|
110
|
-
*/
|
|
111
|
-
const LIFECYCLE_TOPICS: ReadonlySet<WebhookTopic> = new Set(["app.installed", "app.uninstalled"]);
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* An install is eligible for `topic` when it has a webhook endpoint and
|
|
115
|
-
* carries the topic's required scope, if any, and — with one exception — is
|
|
116
|
-
* `active`. `app.uninstalled` waives the active check: it's the defined
|
|
117
|
-
* farewell event, fired AFTER `registry.uninstall()` has already flipped the
|
|
118
|
-
* install to `uninstalled`, so requiring `active` here would mean it never
|
|
119
|
-
* fires at all. It does NOT waive the endpoint requirement.
|
|
120
|
-
*/
|
|
121
|
-
function isEligibleTarget(install: AppInstall, topic: WebhookTopic): boolean {
|
|
122
|
-
if (!install.webhookEndpoint) return false;
|
|
123
|
-
if (install.status !== "active" && topic !== "app.uninstalled") return false;
|
|
124
|
-
const requiredScope = TOPIC_REQUIRED_SCOPE[topic];
|
|
125
|
-
return requiredScope == null || install.scopes.includes(requiredScope);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Swallow-and-log a soft failure — logging itself must never throw. Same `[diag:...]` shape as `grants/developerInvites.ts`. */
|
|
129
|
-
function logDiag(fields: Record<string, unknown>): void {
|
|
130
|
-
try {
|
|
131
|
-
console.warn("[diag:webhooks:emit]", JSON.stringify(fields));
|
|
132
|
-
} catch {
|
|
133
|
-
/* diagnostics must never break the caller */
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Emit one webhook event for `tenantId`: resolve eligible installs, persist
|
|
139
|
-
* the event + one `pending` delivery per eligible install, then enqueue each
|
|
140
|
-
* delivery on the dispatcher. Returns `null` (not an error) when nothing on
|
|
141
|
-
* the tenant was eligible, when the runtime isn't configured for webhooks at
|
|
142
|
-
* all, or when a lifecycle topic was called without the required
|
|
143
|
-
* `opts.installId` (a caller bug — logged, never silently broadened to a
|
|
144
|
-
* tenant-wide broadcast).
|
|
145
|
-
*/
|
|
146
|
-
export async function emitWebhookEvent<T extends WebhookTopic>(
|
|
147
|
-
topic: T,
|
|
148
|
-
tenantId: string,
|
|
149
|
-
data: unknown,
|
|
150
|
-
opts: EmitWebhookEventOptions<T>,
|
|
151
|
-
): Promise<EmitWebhookEventResult> {
|
|
152
|
-
const options: { installId?: string; stage?: unknown; dataschemaVersion?: string } = opts;
|
|
153
|
-
const lifecycle = LIFECYCLE_TOPICS.has(topic);
|
|
154
|
-
if (lifecycle && !options.installId) {
|
|
155
|
-
logDiag({ topic, tenantId, reason: "lifecycle_topic_requires_installId" });
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
const stage = options.stage;
|
|
159
|
-
if (!lifecycle && stage !== "preview" && stage !== "publication") {
|
|
160
|
-
logDiag({ topic, tenantId, reason: "data_topic_requires_stage" });
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
163
|
-
const expectedEnv = lifecycle ? null : appInstallEnvForStage(stage as CommerceStage);
|
|
164
|
-
if (isCommerceTopic(topic) && !commercePayloadAdmissible(topic, data)) {
|
|
165
|
-
throw new CommercePayloadRefused(topic);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const d1 = await readD1("STOREFRONT_APPS_DB");
|
|
169
|
-
if (!d1) {
|
|
170
|
-
logDiag({ topic, tenantId, reason: "storefront_apps_db_unconfigured" });
|
|
171
|
-
return null;
|
|
172
|
-
}
|
|
173
|
-
const store = new WebhookDeliveryStore(fromD1(d1));
|
|
174
|
-
|
|
175
|
-
const registry = await getRegistryService();
|
|
176
|
-
if (registry instanceof Response) {
|
|
177
|
-
// Shouldn't happen given the STOREFRONT_APPS_DB check above (same
|
|
178
|
-
// binding), but stay fail-closed rather than assume.
|
|
179
|
-
logDiag({ topic, tenantId, reason: "registry_unconfigured" });
|
|
180
|
-
return null;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// `listByTenant` is the tenant-ownership check for a given `installId`, for
|
|
184
|
-
// free: an install belonging to a different tenant simply never appears in
|
|
185
|
-
// `candidates`, so a cross-tenant `installId` yields zero eligible targets
|
|
186
|
-
// rather than leaking across tenants.
|
|
187
|
-
const candidates = await registry.listByTenant(tenantId);
|
|
188
|
-
const scoped = options.installId ? candidates.filter((install) => install.installId === options.installId) : candidates;
|
|
189
|
-
const eligible = scoped.filter(
|
|
190
|
-
(install) => isEligibleTarget(install, topic) && (expectedEnv === null || install.env === expectedEnv),
|
|
191
|
-
);
|
|
192
|
-
if (eligible.length === 0) return null;
|
|
193
|
-
|
|
194
|
-
const { event, deliveries } = await store.createEvent(
|
|
195
|
-
{ tenantId, topic, dataschemaVersion: options.dataschemaVersion ?? DATASCHEMA_VERSION, payload: data },
|
|
196
|
-
eligible.map((install) => ({ installId: install.installId })),
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
const dispatcher = await getDispatcher();
|
|
200
|
-
if (dispatcher instanceof Response) {
|
|
201
|
-
// The event + delivery rows are already persisted `pending` — nothing is
|
|
202
|
-
// lost, just not yet scheduled. A future scan/replay path (Chunk E) can
|
|
203
|
-
// still pick them up once the dispatcher IS configured.
|
|
204
|
-
logDiag({ topic, tenantId, eventId: event.id, deliveryCount: deliveries.length, reason: "dispatcher_unconfigured" });
|
|
205
|
-
return { eventId: event.id, deliveryCount: deliveries.length };
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
for (const delivery of deliveries) {
|
|
209
|
-
await dispatcher.enqueue({ deliveryId: delivery.deliveryId });
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return { eventId: event.id, deliveryCount: deliveries.length };
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Best-effort dispatch after committing `inventory.changed` delivery rows: arms the tenant
|
|
217
|
-
* commerce alarm and enqueues each delivery on the configured dispatcher.
|
|
218
|
-
*/
|
|
219
|
-
export async function dispatchInventoryDeliveries(
|
|
220
|
-
tenantId: string,
|
|
221
|
-
deliveryIds: readonly string[],
|
|
222
|
-
now: number = Date.now(),
|
|
223
|
-
): Promise<void> {
|
|
224
|
-
if (deliveryIds.length === 0) return;
|
|
225
|
-
try {
|
|
226
|
-
const namespace = (await getWorkerEnv())?.TENANT_PIPELINE as
|
|
227
|
-
| { idFromName(name: string): unknown; get(id: unknown): { scheduleCommerce(t: string, at: number): Promise<void> } }
|
|
228
|
-
| undefined;
|
|
229
|
-
if (namespace && typeof namespace.idFromName === "function") {
|
|
230
|
-
await namespace.get(namespace.idFromName(tenantId)).scheduleCommerce(tenantId, now + COMMERCE_DELIVERY_GRACE_MS + 1000);
|
|
231
|
-
}
|
|
232
|
-
} catch {
|
|
233
|
-
// The committed delivery rows remain pending for the next alarm sweep.
|
|
234
|
-
}
|
|
235
|
-
try {
|
|
236
|
-
const dispatcher = await getDispatcher();
|
|
237
|
-
if (dispatcher instanceof Response) return;
|
|
238
|
-
if (dispatcher instanceof InMemoryDispatcher && !(await readEnv(WEBHOOK_SIGNING_PRIVATE_KEY_ENV))) return;
|
|
239
|
-
for (const deliveryId of deliveryIds) await dispatcher.enqueue({ deliveryId });
|
|
240
|
-
} catch {
|
|
241
|
-
// Same: the pending rows remain the durable record.
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
setInventoryChangedNotify(dispatchInventoryDeliveries);
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CloudEvents envelope builder — PrivateApps epic, D4 (Chunk C). Builds the
|
|
3
|
-
* EXACT wire shape `docs/private-apps/contract/asyncapi.yaml`'s
|
|
4
|
-
* `CloudEventEnvelope` schema requires (field-for-field, including the
|
|
5
|
-
* CloudEvents extensions), plus `TOPIC_REQUIRED_SCOPE` — the topic→required-
|
|
6
|
-
* scope map from `docs/private-apps/api-event-reference.md` §"Scope →
|
|
7
|
-
* endpoint / topic matrix", which `events.test.ts` checks against that table
|
|
8
|
-
* directly (no machine-readable topic-scope JSON exists, unlike
|
|
9
|
-
* `scopes.json`) — same drift-proofing discipline `scopes.test.ts` uses for
|
|
10
|
-
* `V1_SCOPES` vs. `contract/scopes.json`.
|
|
11
|
-
*/
|
|
12
|
-
import type { WebhookTopic } from "./deliveryStore.js";
|
|
13
|
-
|
|
14
|
-
export const CLOUDEVENTS_SPECVERSION = "1.0";
|
|
15
|
-
export const CLOUDEVENTS_DATACONTENTTYPE = "application/json";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Required scope per topic (asyncapi.yaml channel descriptions +
|
|
19
|
-
* api-event-reference.md's topic matrix). `null` = no event scope gates it —
|
|
20
|
-
* `app.installed`/`app.uninstalled` deliver to any install with a webhook
|
|
21
|
-
* endpoint, per "(any valid token)" in the matrix.
|
|
22
|
-
*/
|
|
23
|
-
export const TOPIC_REQUIRED_SCOPE: Record<WebhookTopic, string | null> = {
|
|
24
|
-
"app.installed": null,
|
|
25
|
-
"app.uninstalled": null,
|
|
26
|
-
"catalog.product.updated": "catalog:read",
|
|
27
|
-
"inventory.changed": "inventory:read",
|
|
28
|
-
"order.created": "orders:webhook",
|
|
29
|
-
"order.paid": "orders:webhook",
|
|
30
|
-
"order.fulfilled": "orders:webhook",
|
|
31
|
-
"order.partially_fulfilled": "orders:webhook",
|
|
32
|
-
"order.cancelled": "orders:webhook",
|
|
33
|
-
"order.updated": "orders:webhook",
|
|
34
|
-
"customer.marketing_consent.updated": "customers:read:minimal",
|
|
35
|
-
"attribution.finalized": "attribution:write",
|
|
36
|
-
"fulfillment.assignment.created": "fulfillment:process",
|
|
37
|
-
"fulfillment.assignment.updated": "fulfillment:process",
|
|
38
|
-
"fulfillment.label.voided": "fulfillment:process",
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/** `type`'s reverse-DNS prefix (asyncapi.yaml `CloudEventEnvelope.type`). */
|
|
42
|
-
export function eventType(topic: WebhookTopic): string {
|
|
43
|
-
return `com.tokenoftrust.storefront.${topic}`;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** `source` — the emitting gateway + tenant (asyncapi.yaml: e.g. `/tenants/acme/storefront`). */
|
|
47
|
-
export function envelopeSource(tenantId: string): string {
|
|
48
|
-
return `/tenants/${tenantId}/storefront`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface BuildEnvelopeInput {
|
|
52
|
-
/** The wire `id` — this delivery's id, stable across its own retries (never the eventId). */
|
|
53
|
-
deliveryId: string;
|
|
54
|
-
tenantId: string;
|
|
55
|
-
topic: WebhookTopic;
|
|
56
|
-
dataschemaVersion: string;
|
|
57
|
-
/** The event-specific payload — opaque here, shaped by `topic` (OrderData, etc. in the contract). */
|
|
58
|
-
data: unknown;
|
|
59
|
-
appInstallId: string;
|
|
60
|
-
/** Delivery attempt count so far, 0 on the first attempt (CloudEvents `retrycount` extension). */
|
|
61
|
-
retryCount: number;
|
|
62
|
-
/** Correlation extensions — omitted from the envelope entirely when not supplied. */
|
|
63
|
-
totRequestId?: string;
|
|
64
|
-
traceparent?: string;
|
|
65
|
-
/** Injected clock; defaults to `Date`. Override in tests for a deterministic `time`. */
|
|
66
|
-
now?: () => Date;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/** Mirrors `asyncapi.yaml`'s `CloudEventEnvelope` schema exactly — required + optional extension fields. */
|
|
70
|
-
export interface CloudEventEnvelope {
|
|
71
|
-
specversion: "1.0";
|
|
72
|
-
id: string;
|
|
73
|
-
source: string;
|
|
74
|
-
type: string;
|
|
75
|
-
time: string;
|
|
76
|
-
datacontenttype: "application/json";
|
|
77
|
-
dataschemaversion: string;
|
|
78
|
-
tenantid: string;
|
|
79
|
-
appinstallid: string;
|
|
80
|
-
retrycount: number;
|
|
81
|
-
totrequestid?: string;
|
|
82
|
-
traceparent?: string;
|
|
83
|
-
data: unknown;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** Build one delivery's CloudEvents envelope. Never adds anything beyond `data` — payload minimization is the emitter's (Chunk D's) job. */
|
|
87
|
-
export function buildCloudEventEnvelope(input: BuildEnvelopeInput): CloudEventEnvelope {
|
|
88
|
-
const now = input.now ?? (() => new Date());
|
|
89
|
-
const envelope: CloudEventEnvelope = {
|
|
90
|
-
specversion: CLOUDEVENTS_SPECVERSION,
|
|
91
|
-
id: input.deliveryId,
|
|
92
|
-
source: envelopeSource(input.tenantId),
|
|
93
|
-
type: eventType(input.topic),
|
|
94
|
-
time: now().toISOString(),
|
|
95
|
-
datacontenttype: CLOUDEVENTS_DATACONTENTTYPE,
|
|
96
|
-
dataschemaversion: input.dataschemaVersion,
|
|
97
|
-
tenantid: input.tenantId,
|
|
98
|
-
appinstallid: input.appInstallId,
|
|
99
|
-
retrycount: input.retryCount,
|
|
100
|
-
data: input.data,
|
|
101
|
-
};
|
|
102
|
-
if (input.totRequestId) envelope.totrequestid = input.totRequestId;
|
|
103
|
-
if (input.traceparent) envelope.traceparent = input.traceparent;
|
|
104
|
-
return envelope;
|
|
105
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Composition root for the webhook dispatcher — PrivateApps epic, D4 (Chunk
|
|
3
|
-
* C). Mirrors `../apps/adminService.ts`'s `getRegistryService()`: builds the
|
|
4
|
-
* real `WebhookDeliveryStore` + `AppRegistryService` over the
|
|
5
|
-
* `STOREFRONT_APPS_DB` binding, wires them into `attemptDelivery` as the `AttemptDeliveryFn` every
|
|
6
|
-
* dispatcher implementation runs per job, and returns the SAME 503-when-
|
|
7
|
-
* unconfigured `Response` shape `getRegistryService()` does when that binding
|
|
8
|
-
* is missing.
|
|
9
|
-
*
|
|
10
|
-
* Selects the dispatcher implementation by binding presence:
|
|
11
|
-
* `env.WEBHOOK_DELIVERY_QUEUE` present → `CloudflareQueueDispatcher`, else
|
|
12
|
-
* `InMemoryDispatcher` — V1's actual production path (no `[[queues]]` binding
|
|
13
|
-
* is declared in `wrangler.toml` yet, so the Cloudflare branch is unreachable
|
|
14
|
-
* until infra provisions one; see `dispatcher.ts`'s header for the full
|
|
15
|
-
* design-resolution + entry-wiring context).
|
|
16
|
-
*/
|
|
17
|
-
import { WebhookDeliveryStore } from "./deliveryStore.js";
|
|
18
|
-
import { attemptDelivery, sweepDurableCommerce, type CommerceDeliver, type CommerceSweepResult } from "./deliveryEngine.js";
|
|
19
|
-
import { InMemoryDispatcher, type AttemptDeliveryFn, type WebhookDispatcher } from "./dispatcher.js";
|
|
20
|
-
import { CloudflareQueueDispatcher, type CloudflareQueueSender } from "./cloudflareQueueDispatcher.js";
|
|
21
|
-
import { AppRegistryService } from "../apps/registryService.js";
|
|
22
|
-
import { D1CredentialStore } from "../apps/credentials.js";
|
|
23
|
-
import { DEFAULT_APP_GATEWAY_KID, APP_GATEWAY_KID_ENV } from "../apps/gatewayKeys.js";
|
|
24
|
-
import { json } from "../apps/registryAccess.js";
|
|
25
|
-
import { fromD1, type Queryable } from "../../catalog/d1/catalog.js";
|
|
26
|
-
import { readD1, readEnv, readQueue } from "../../publication/shared/env.js";
|
|
27
|
-
import { COMMERCE_SCHEDULER } from "@tot/public-runtime";
|
|
28
|
-
|
|
29
|
-
/** Binding name `getDispatcher()` checks to pick the Cloudflare Queues adapter. Not yet declared in `wrangler.toml`. */
|
|
30
|
-
export const WEBHOOK_QUEUE_BINDING = "WEBHOOK_DELIVERY_QUEUE";
|
|
31
|
-
|
|
32
|
-
/** The `attemptDelivery` composition every dispatcher and the commerce alarm run per delivery id. */
|
|
33
|
-
export function deliveryAttemptFor(db: Queryable, activeKid: string = DEFAULT_APP_GATEWAY_KID): AttemptDeliveryFn {
|
|
34
|
-
const store = new WebhookDeliveryStore(db);
|
|
35
|
-
const registry = new AppRegistryService(db, new D1CredentialStore(db, activeKid));
|
|
36
|
-
return (deliveryId) => attemptDelivery(deliveryId, { store, registry });
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Build a `WebhookDispatcher` over the real `STOREFRONT_APPS_DB` binding, or
|
|
41
|
-
* return a 503 `Response` when that binding isn't configured in this
|
|
42
|
-
* runtime — same fail-closed shape as `getRegistryService()`.
|
|
43
|
-
*/
|
|
44
|
-
export async function getDispatcher(): Promise<WebhookDispatcher | Response> {
|
|
45
|
-
const d1 = await readD1("STOREFRONT_APPS_DB");
|
|
46
|
-
if (!d1) return json({ error: "webhook dispatcher not configured" }, 503);
|
|
47
|
-
const db = fromD1(d1);
|
|
48
|
-
|
|
49
|
-
const activeKid = (await readEnv(APP_GATEWAY_KID_ENV)) ?? DEFAULT_APP_GATEWAY_KID;
|
|
50
|
-
const attempt = deliveryAttemptFor(db, activeKid);
|
|
51
|
-
|
|
52
|
-
const queueSender = (await readQueue(WEBHOOK_QUEUE_BINDING)) as CloudflareQueueSender | undefined;
|
|
53
|
-
if (queueSender) return new CloudflareQueueDispatcher(queueSender);
|
|
54
|
-
return new InMemoryDispatcher(attempt);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Commerce work is already durable when it is saved. This dispatcher only
|
|
59
|
-
* sweeps that outbox, and only when the declared scheduler is configured.
|
|
60
|
-
* It never falls back to an in-memory queue.
|
|
61
|
-
*/
|
|
62
|
-
export class CommerceAlarmDispatcher {
|
|
63
|
-
constructor(
|
|
64
|
-
private readonly db: Queryable,
|
|
65
|
-
private readonly scheduler: string,
|
|
66
|
-
) {}
|
|
67
|
-
|
|
68
|
-
sweep(now: string, deliver?: CommerceDeliver): Promise<CommerceSweepResult> {
|
|
69
|
-
return sweepDurableCommerce({ db: this.db, scheduler: this.scheduler, now, deliver });
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export async function getReliableDispatcher(): Promise<CommerceAlarmDispatcher | Response> {
|
|
74
|
-
const d1 = await readD1("STOREFRONT_APPS_DB");
|
|
75
|
-
if (!d1) return json({ error: "commerce scheduler not configured" }, 503);
|
|
76
|
-
return new CommerceAlarmDispatcher(fromD1(d1), COMMERCE_SCHEDULER);
|
|
77
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `order.fulfilled` is claimed once per order across its two producers: the fulfillment handoff
|
|
3
|
-
* and the order-forward receiver. The claim is keyed (tenant, stage, source, source order id,
|
|
4
|
-
* topic); each producer writes its claim and its gated event rows in one batch, and only the
|
|
5
|
-
* producer whose claim carries its event id writes the event.
|
|
6
|
-
*/
|
|
7
|
-
import type { CommerceStage } from "@tot/public-runtime";
|
|
8
|
-
import type { CommerceEventStatement } from "./deliveryStore.js";
|
|
9
|
-
|
|
10
|
-
export interface OrderFulfilledClaimKey {
|
|
11
|
-
tenantId: string;
|
|
12
|
-
stage: CommerceStage;
|
|
13
|
-
source: string;
|
|
14
|
-
sourceOrderId: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** The forward contract's `env` mapped to a commerce stage; any other value claims and emits nothing. */
|
|
18
|
-
export function forwardStage(env: string): CommerceStage | null {
|
|
19
|
-
if (env === "production") return "publication";
|
|
20
|
-
if (env === "sandbox") return "preview";
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/** The handoff's claim: written only when no claim exists and `gate` holds (the handoff's CAS committed). */
|
|
25
|
-
export function handoffClaimStatement(
|
|
26
|
-
key: OrderFulfilledClaimKey,
|
|
27
|
-
eventId: string,
|
|
28
|
-
now: string,
|
|
29
|
-
gate: CommerceEventStatement,
|
|
30
|
-
): CommerceEventStatement {
|
|
31
|
-
return {
|
|
32
|
-
sql: `INSERT INTO commerce_event_claims
|
|
33
|
-
(tenant_id, stage, source, source_order_id, topic, event_id, claimed_by, claimed_at)
|
|
34
|
-
SELECT ?, ?, ?, ?, 'order.fulfilled', ?, 'handoff', ?
|
|
35
|
-
WHERE NOT EXISTS (
|
|
36
|
-
SELECT 1 FROM commerce_event_claims
|
|
37
|
-
WHERE tenant_id = ? AND stage = ? AND source = ? AND source_order_id = ? AND topic = 'order.fulfilled'
|
|
38
|
-
) AND (${gate.sql})`,
|
|
39
|
-
params: [
|
|
40
|
-
key.tenantId,
|
|
41
|
-
key.stage,
|
|
42
|
-
key.source,
|
|
43
|
-
key.sourceOrderId,
|
|
44
|
-
eventId,
|
|
45
|
-
now,
|
|
46
|
-
key.tenantId,
|
|
47
|
-
key.stage,
|
|
48
|
-
key.source,
|
|
49
|
-
key.sourceOrderId,
|
|
50
|
-
...gate.params,
|
|
51
|
-
],
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** The forward receiver's claim. */
|
|
56
|
-
export function forwardClaimStatement(key: OrderFulfilledClaimKey, eventId: string, now: string): CommerceEventStatement {
|
|
57
|
-
return {
|
|
58
|
-
sql: `INSERT OR IGNORE INTO commerce_event_claims
|
|
59
|
-
(tenant_id, stage, source, source_order_id, topic, event_id, claimed_by, claimed_at)
|
|
60
|
-
VALUES (?, ?, ?, ?, 'order.fulfilled', ?, 'forward', ?)`,
|
|
61
|
-
params: [key.tenantId, key.stage, key.source, key.sourceOrderId, eventId, now],
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Event gate: the claim for `key` carries `eventId`. */
|
|
66
|
-
export function claimedByEventGate(key: OrderFulfilledClaimKey, eventId: string): CommerceEventStatement {
|
|
67
|
-
return {
|
|
68
|
-
sql: `EXISTS (
|
|
69
|
-
SELECT 1 FROM commerce_event_claims
|
|
70
|
-
WHERE tenant_id = ? AND stage = ? AND source = ? AND source_order_id = ? AND topic = 'order.fulfilled'
|
|
71
|
-
AND event_id = ?
|
|
72
|
-
)`,
|
|
73
|
-
params: [key.tenantId, key.stage, key.source, key.sourceOrderId, eventId],
|
|
74
|
-
};
|
|
75
|
-
}
|