clinkx 1.2.0 → 1.4.0
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/CHANGELOG.md +60 -0
- package/README.md +17 -8
- package/clinkx-workflows/dist/architecture-arena/admission.d.ts +16 -0
- package/clinkx-workflows/dist/architecture-arena/admission.js +107 -0
- package/clinkx-workflows/dist/architecture-arena/admission.js.map +1 -0
- package/clinkx-workflows/dist/architecture-arena/contract.d.ts +1368 -0
- package/clinkx-workflows/dist/architecture-arena/contract.js +254 -0
- package/clinkx-workflows/dist/architecture-arena/contract.js.map +1 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.d.ts +53 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.js +511 -0
- package/clinkx-workflows/dist/architecture-arena/runtime.js.map +1 -0
- package/clinkx-workflows/dist/architecture-comparison/admission.d.ts +17 -1
- package/clinkx-workflows/dist/architecture-comparison/admission.js +27 -4
- package/clinkx-workflows/dist/architecture-comparison/admission.js.map +1 -1
- package/clinkx-workflows/dist/architecture-comparison/contract.d.ts +59 -62
- package/clinkx-workflows/dist/architecture-comparison/contract.js +4 -21
- package/clinkx-workflows/dist/architecture-comparison/contract.js.map +1 -1
- package/clinkx-workflows/dist/architecture-comparison/runtime.d.ts +4 -3
- package/clinkx-workflows/dist/architecture-comparison/runtime.js +22 -67
- package/clinkx-workflows/dist/architecture-comparison/runtime.js.map +1 -1
- package/clinkx-workflows/dist/artifacts.d.ts +19 -4
- package/clinkx-workflows/dist/artifacts.js +16 -7
- package/clinkx-workflows/dist/artifacts.js.map +1 -1
- package/clinkx-workflows/dist/backend.d.ts +30 -0
- package/clinkx-workflows/dist/child-env.d.ts +3 -0
- package/clinkx-workflows/dist/child-env.js +11 -0
- package/clinkx-workflows/dist/child-env.js.map +1 -1
- package/clinkx-workflows/dist/clink-client.d.ts +8 -2
- package/clinkx-workflows/dist/clink-client.js +4 -2
- package/clinkx-workflows/dist/clink-client.js.map +1 -1
- package/clinkx-workflows/dist/context-preflight.d.ts +6 -0
- package/clinkx-workflows/dist/context-preflight.js.map +1 -1
- package/clinkx-workflows/dist/council-execution-policy.d.ts +3 -1
- package/clinkx-workflows/dist/council-execution-policy.js +4 -0
- package/clinkx-workflows/dist/council-execution-policy.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/api-routes.d.ts +21 -0
- package/clinkx-workflows/dist/dashboard/api-routes.js +27 -0
- package/clinkx-workflows/dist/dashboard/api-routes.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/auth/challenge-store.d.ts +13 -0
- package/clinkx-workflows/dist/dashboard/auth/challenge-store.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/auth/controller.d.ts +27 -0
- package/clinkx-workflows/dist/dashboard/auth/controller.js +262 -23
- package/clinkx-workflows/dist/dashboard/auth/controller.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/auth/settings-step-up.d.ts +62 -0
- package/clinkx-workflows/dist/dashboard/auth/settings-step-up.js +70 -0
- package/clinkx-workflows/dist/dashboard/auth/settings-step-up.js.map +1 -0
- package/clinkx-workflows/dist/dashboard/contracts.d.ts +586 -586
- package/clinkx-workflows/dist/dashboard/progress-report-contract.d.ts +28 -28
- package/clinkx-workflows/dist/dashboard/request-admission.d.ts +6 -0
- package/clinkx-workflows/dist/dashboard/request-admission.js +9 -0
- package/clinkx-workflows/dist/dashboard/request-admission.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/server-factory.js +15 -1
- package/clinkx-workflows/dist/dashboard/server-factory.js.map +1 -1
- package/clinkx-workflows/dist/dashboard/session-log-contract.d.ts +32 -32
- package/clinkx-workflows/dist/dashboard/settings-contract.d.ts +1806 -0
- package/clinkx-workflows/dist/dashboard/settings-contract.js +630 -0
- package/clinkx-workflows/dist/dashboard/settings-contract.js.map +1 -0
- package/clinkx-workflows/dist/dashboard/settings-process-reader.d.ts +48 -0
- package/clinkx-workflows/dist/dashboard/settings-process-reader.js +195 -0
- package/clinkx-workflows/dist/dashboard/settings-process-reader.js.map +1 -0
- package/clinkx-workflows/dist/dashboard/settings-routes.d.ts +87 -0
- package/clinkx-workflows/dist/dashboard/settings-routes.js +690 -0
- package/clinkx-workflows/dist/dashboard/settings-routes.js.map +1 -0
- package/clinkx-workflows/dist/dashboard-ui/assets/index-DSOtuS0p.js +122 -0
- package/clinkx-workflows/dist/dashboard-ui/assets/style-C5guUjwN.css +1 -0
- package/clinkx-workflows/dist/dashboard-ui/index.html +2 -2
- package/clinkx-workflows/dist/delegate-signature.d.ts +4 -3
- package/clinkx-workflows/dist/delegate-signature.js +4 -3
- package/clinkx-workflows/dist/delegate-signature.js.map +1 -1
- package/clinkx-workflows/dist/durable-file-core.d.ts +76 -0
- package/clinkx-workflows/dist/durable-file-core.js +146 -0
- package/clinkx-workflows/dist/durable-file-core.js.map +1 -0
- package/clinkx-workflows/dist/durable-file.d.ts +13 -0
- package/clinkx-workflows/dist/durable-file.js +22 -0
- package/clinkx-workflows/dist/durable-file.js.map +1 -0
- package/clinkx-workflows/dist/engine.d.ts +42 -7
- package/clinkx-workflows/dist/engine.js +196 -24
- package/clinkx-workflows/dist/engine.js.map +1 -1
- package/clinkx-workflows/dist/index.js +68 -2
- package/clinkx-workflows/dist/index.js.map +1 -1
- package/clinkx-workflows/dist/ping-pong/admission.d.ts +127 -0
- package/clinkx-workflows/dist/ping-pong/admission.js +522 -0
- package/clinkx-workflows/dist/ping-pong/admission.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/contract.d.ts +2539 -0
- package/clinkx-workflows/dist/ping-pong/contract.js +819 -0
- package/clinkx-workflows/dist/ping-pong/contract.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/control.d.ts +67 -0
- package/clinkx-workflows/dist/ping-pong/control.js +12 -0
- package/clinkx-workflows/dist/ping-pong/control.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.d.ts +24 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.js +55 -0
- package/clinkx-workflows/dist/ping-pong/runtime-handle.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/runtime.d.ts +235 -0
- package/clinkx-workflows/dist/ping-pong/runtime.js +1183 -0
- package/clinkx-workflows/dist/ping-pong/runtime.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/settle.d.ts +57 -0
- package/clinkx-workflows/dist/ping-pong/settle.js +58 -0
- package/clinkx-workflows/dist/ping-pong/settle.js.map +1 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.d.ts +65 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.js +138 -0
- package/clinkx-workflows/dist/ping-pong/sidecar.js.map +1 -0
- package/clinkx-workflows/dist/plan-review/publication.d.ts +2 -1
- package/clinkx-workflows/dist/plan-review/publication.js +5 -6
- package/clinkx-workflows/dist/plan-review/publication.js.map +1 -1
- package/clinkx-workflows/dist/plan-review/snapshot.d.ts +3 -2
- package/clinkx-workflows/dist/plan-review/snapshot.js +8 -8
- package/clinkx-workflows/dist/plan-review/snapshot.js.map +1 -1
- package/clinkx-workflows/dist/recipe-host.d.ts +315 -0
- package/clinkx-workflows/dist/recipe-host.js +183 -0
- package/clinkx-workflows/dist/recipe-host.js.map +1 -0
- package/clinkx-workflows/dist/recipe-seats.d.ts +96 -0
- package/clinkx-workflows/dist/recipe-seats.js +163 -0
- package/clinkx-workflows/dist/recipe-seats.js.map +1 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.d.ts +67 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.js +111 -0
- package/clinkx-workflows/dist/recipe-shared/evidence.js.map +1 -0
- package/clinkx-workflows/dist/reducers/index.d.ts +9 -1
- package/clinkx-workflows/dist/reducers/index.js +30 -0
- package/clinkx-workflows/dist/reducers/index.js.map +1 -1
- package/clinkx-workflows/dist/run-lock-reclaim.d.ts +44 -0
- package/clinkx-workflows/dist/run-lock-reclaim.js +86 -0
- package/clinkx-workflows/dist/run-lock-reclaim.js.map +1 -0
- package/clinkx-workflows/dist/settings/audit.d.ts +101 -0
- package/clinkx-workflows/dist/settings/audit.js +318 -0
- package/clinkx-workflows/dist/settings/audit.js.map +1 -0
- package/clinkx-workflows/dist/settings/bootstrap.d.ts +35 -0
- package/clinkx-workflows/dist/settings/bootstrap.js +52 -0
- package/clinkx-workflows/dist/settings/bootstrap.js.map +1 -0
- package/clinkx-workflows/dist/settings/codec.d.ts +51 -0
- package/clinkx-workflows/dist/settings/codec.js +410 -0
- package/clinkx-workflows/dist/settings/codec.js.map +1 -0
- package/clinkx-workflows/dist/settings/durable-write.d.ts +48 -0
- package/clinkx-workflows/dist/settings/durable-write.js +31 -0
- package/clinkx-workflows/dist/settings/durable-write.js.map +1 -0
- package/clinkx-workflows/dist/settings/index.d.ts +20 -0
- package/clinkx-workflows/dist/settings/index.js +23 -0
- package/clinkx-workflows/dist/settings/index.js.map +1 -0
- package/clinkx-workflows/dist/settings/loader.d.ts +80 -0
- package/clinkx-workflows/dist/settings/loader.js +577 -0
- package/clinkx-workflows/dist/settings/loader.js.map +1 -0
- package/clinkx-workflows/dist/settings/lock.d.ts +108 -0
- package/clinkx-workflows/dist/settings/lock.js +344 -0
- package/clinkx-workflows/dist/settings/lock.js.map +1 -0
- package/clinkx-workflows/dist/settings/paths.d.ts +65 -0
- package/clinkx-workflows/dist/settings/paths.js +173 -0
- package/clinkx-workflows/dist/settings/paths.js.map +1 -0
- package/clinkx-workflows/dist/settings/process-heartbeat.d.ts +47 -0
- package/clinkx-workflows/dist/settings/process-heartbeat.js +305 -0
- package/clinkx-workflows/dist/settings/process-heartbeat.js.map +1 -0
- package/clinkx-workflows/dist/settings/registry.d.ts +178 -0
- package/clinkx-workflows/dist/settings/registry.js +1285 -0
- package/clinkx-workflows/dist/settings/registry.js.map +1 -0
- package/clinkx-workflows/dist/settings/schema.d.ts +404 -0
- package/clinkx-workflows/dist/settings/schema.js +415 -0
- package/clinkx-workflows/dist/settings/schema.js.map +1 -0
- package/clinkx-workflows/dist/settings/store.d.ts +157 -0
- package/clinkx-workflows/dist/settings/store.js +842 -0
- package/clinkx-workflows/dist/settings/store.js.map +1 -0
- package/clinkx-workflows/dist/state-schema.d.ts +11 -0
- package/clinkx-workflows/dist/state-schema.js +4 -1
- package/clinkx-workflows/dist/state-schema.js.map +1 -1
- package/clinkx-workflows/dist/state.d.ts +178 -3
- package/clinkx-workflows/dist/state.js +618 -83
- package/clinkx-workflows/dist/state.js.map +1 -1
- package/clinkx-workflows/dist/stdio-entry.d.ts +2 -0
- package/clinkx-workflows/dist/stdio-entry.js +52 -0
- package/clinkx-workflows/dist/stdio-entry.js.map +1 -0
- package/clinkx-workflows/dist/transport.d.ts +2 -1
- package/clinkx-workflows/dist/transport.js +5 -0
- package/clinkx-workflows/dist/transport.js.map +1 -1
- package/clinkx-workflows/dist/validators/architecture-arena.d.ts +6 -0
- package/clinkx-workflows/dist/validators/architecture-arena.js +16 -0
- package/clinkx-workflows/dist/validators/architecture-arena.js.map +1 -0
- package/clinkx-workflows/dist/validators/index.js +4 -0
- package/clinkx-workflows/dist/validators/index.js.map +1 -1
- package/clinkx-workflows/dist/validators/ping-pong.d.ts +6 -0
- package/clinkx-workflows/dist/validators/ping-pong.js +10 -0
- package/clinkx-workflows/dist/validators/ping-pong.js.map +1 -0
- package/clinkx-workflows/dist/workflow-tools.d.ts +51 -5
- package/clinkx-workflows/dist/workflow-tools.js +785 -34
- package/clinkx-workflows/dist/workflow-tools.js.map +1 -1
- package/clinkx-workflows/dist/workflow-verification-contract.d.ts +4 -4
- package/clinkx-workflows/templates/architecture-arena/candidate.txt +39 -0
- package/clinkx-workflows/templates/architecture-arena/judge.txt +34 -0
- package/clinkx-workflows/templates/ping-pong/implement.txt +14 -0
- package/clinkx-workflows/templates/ping-pong/remediate.txt +14 -0
- package/clinkx-workflows/templates/ping-pong/review.txt +27 -0
- package/clinkx-workflows/workflows/architecture-arena.yaml +127 -0
- package/clinkx-workflows/workflows/ping-pong.yaml +158 -0
- package/dist/context-builder/bundle.js +4 -3
- package/dist/context-builder/bundle.js.map +1 -1
- package/dist/context-builder/delegate-packet.d.ts +1 -0
- package/dist/context-builder/delegate-packet.js +66 -7
- package/dist/context-builder/delegate-packet.js.map +1 -1
- package/dist/context-builder/delegate-validation.d.ts +12 -3
- package/dist/context-builder/delegate-validation.js +23 -13
- package/dist/context-builder/delegate-validation.js.map +1 -1
- package/dist/context-builder/external-gaps.d.ts +14 -3
- package/dist/context-builder/external-gaps.js +124 -18
- package/dist/context-builder/external-gaps.js.map +1 -1
- package/dist/context-builder/handler.js +108 -191
- package/dist/context-builder/handler.js.map +1 -1
- package/dist/context-builder/review-coverage.js +27 -1
- package/dist/context-builder/review-coverage.js.map +1 -1
- package/dist/context-builder/secrets.js +1 -1
- package/dist/context-builder/secrets.js.map +1 -1
- package/dist/context-builder/versions.d.ts +10 -4
- package/dist/context-builder/versions.js +10 -4
- package/dist/context-builder/versions.js.map +1 -1
- package/dist/context-builder/workflow-runner.js +33 -24
- package/dist/context-builder/workflow-runner.js.map +1 -1
- package/dist/dashboard-cli.d.ts +2 -0
- package/dist/dashboard-cli.js +197 -58
- package/dist/dashboard-cli.js.map +1 -1
- package/dist/dashboard-companion/client.d.ts +10 -0
- package/dist/dashboard-companion/client.js +43 -1
- package/dist/dashboard-companion/client.js.map +1 -1
- package/dist/dashboard-companion/config.d.ts +19 -1
- package/dist/dashboard-companion/config.js +104 -3
- package/dist/dashboard-companion/config.js.map +1 -1
- package/dist/dashboard-companion/protocol.d.ts +16 -0
- package/dist/dashboard-companion/protocol.js +55 -1
- package/dist/dashboard-companion/protocol.js.map +1 -1
- package/dist/dashboard-companion/workstation-binding.d.ts +69 -0
- package/dist/dashboard-companion/workstation-binding.js +192 -0
- package/dist/dashboard-companion/workstation-binding.js.map +1 -0
- package/dist/dashboard-gateway/admin-cli.d.ts +10 -0
- package/dist/dashboard-gateway/admin-cli.js +266 -0
- package/dist/dashboard-gateway/admin-cli.js.map +1 -0
- package/dist/dashboard-gateway/config.d.ts +50 -0
- package/dist/dashboard-gateway/config.js +235 -1
- package/dist/dashboard-gateway/config.js.map +1 -1
- package/dist/dashboard-gateway/connection-registry.d.ts +100 -0
- package/dist/dashboard-gateway/connection-registry.js +228 -0
- package/dist/dashboard-gateway/connection-registry.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/account-auth.d.ts +78 -0
- package/dist/dashboard-gateway/control-plane/account-auth.js +198 -0
- package/dist/dashboard-gateway/control-plane/account-auth.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/billing.d.ts +264 -0
- package/dist/dashboard-gateway/control-plane/billing.js +787 -0
- package/dist/dashboard-gateway/control-plane/billing.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/contracts.d.ts +1293 -0
- package/dist/dashboard-gateway/control-plane/contracts.js +280 -0
- package/dist/dashboard-gateway/control-plane/contracts.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/cookies.d.ts +13 -0
- package/dist/dashboard-gateway/control-plane/cookies.js +46 -0
- package/dist/dashboard-gateway/control-plane/cookies.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/credentials.d.ts +63 -0
- package/dist/dashboard-gateway/control-plane/credentials.js +66 -0
- package/dist/dashboard-gateway/control-plane/credentials.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/entitlements.d.ts +118 -0
- package/dist/dashboard-gateway/control-plane/entitlements.js +329 -0
- package/dist/dashboard-gateway/control-plane/entitlements.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/errors.d.ts +17 -0
- package/dist/dashboard-gateway/control-plane/errors.js +23 -0
- package/dist/dashboard-gateway/control-plane/errors.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/hosts.d.ts +6 -0
- package/dist/dashboard-gateway/control-plane/hosts.js +16 -0
- package/dist/dashboard-gateway/control-plane/hosts.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/http.d.ts +34 -0
- package/dist/dashboard-gateway/control-plane/http.js +331 -0
- package/dist/dashboard-gateway/control-plane/http.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/schema.d.ts +9 -0
- package/dist/dashboard-gateway/control-plane/schema.js +247 -0
- package/dist/dashboard-gateway/control-plane/schema.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/secrets.d.ts +20 -0
- package/dist/dashboard-gateway/control-plane/secrets.js +39 -0
- package/dist/dashboard-gateway/control-plane/secrets.js.map +1 -0
- package/dist/dashboard-gateway/control-plane/store.d.ts +579 -0
- package/dist/dashboard-gateway/control-plane/store.js +1115 -0
- package/dist/dashboard-gateway/control-plane/store.js.map +1 -0
- package/dist/dashboard-gateway/cookie-boundary.d.ts +60 -0
- package/dist/dashboard-gateway/cookie-boundary.js +205 -0
- package/dist/dashboard-gateway/cookie-boundary.js.map +1 -0
- package/dist/dashboard-gateway/index.js +271 -494
- package/dist/dashboard-gateway/index.js.map +1 -1
- package/dist/dashboard-gateway/main.js +7 -2
- package/dist/dashboard-gateway/main.js.map +1 -1
- package/dist/dashboard-gateway/routing.d.ts +43 -0
- package/dist/dashboard-gateway/routing.js +65 -0
- package/dist/dashboard-gateway/routing.js.map +1 -0
- package/dist/dashboard-gateway/tenant-gateway.d.ts +56 -0
- package/dist/dashboard-gateway/tenant-gateway.js +989 -0
- package/dist/dashboard-gateway/tenant-gateway.js.map +1 -0
- package/dist/dashboard-gateway/tenant-main.d.ts +41 -0
- package/dist/dashboard-gateway/tenant-main.js +303 -0
- package/dist/dashboard-gateway/tenant-main.js.map +1 -0
- package/dist/dashboard-gateway/tunnel-errors.d.ts +16 -0
- package/dist/dashboard-gateway/tunnel-errors.js +22 -0
- package/dist/dashboard-gateway/tunnel-errors.js.map +1 -0
- package/dist/dashboard-gateway/tunnel-http.d.ts +21 -0
- package/dist/dashboard-gateway/tunnel-http.js +195 -0
- package/dist/dashboard-gateway/tunnel-http.js.map +1 -0
- package/dist/elaris/config.d.ts +10 -0
- package/dist/elaris/config.js +27 -1
- package/dist/elaris/config.js.map +1 -1
- package/dist/elaris/tools.js +4 -1
- package/dist/elaris/tools.js.map +1 -1
- package/dist/handler.d.ts +6 -0
- package/dist/handler.js +17 -1
- package/dist/handler.js.map +1 -1
- package/dist/index.d.ts +0 -31
- package/dist/index.js +41 -404
- package/dist/index.js.map +1 -1
- package/dist/lib.d.ts +17 -0
- package/dist/lib.js +17 -0
- package/dist/lib.js.map +1 -0
- package/dist/local-clink-backend.d.ts +2 -1
- package/dist/local-clink-backend.js +28 -4
- package/dist/local-clink-backend.js.map +1 -1
- package/dist/parsers/extract.d.ts +19 -1
- package/dist/parsers/extract.js +68 -3
- package/dist/parsers/extract.js.map +1 -1
- package/dist/pipeline.d.ts +17 -1
- package/dist/pipeline.js +34 -6
- package/dist/pipeline.js.map +1 -1
- package/dist/process-group.d.ts +65 -0
- package/dist/process-group.js +153 -0
- package/dist/process-group.js.map +1 -0
- package/dist/progress-report/lock.d.ts +17 -59
- package/dist/progress-report/lock.js +25 -329
- package/dist/progress-report/lock.js.map +1 -1
- package/dist/progress-report/schema.d.ts +23 -23
- package/dist/project-verification/files.js +1 -1
- package/dist/project-verification/files.js.map +1 -1
- package/dist/project-verification/process.js +23 -44
- package/dist/project-verification/process.js.map +1 -1
- package/dist/project-verification/service.js +9 -1
- package/dist/project-verification/service.js.map +1 -1
- package/dist/prompt.d.ts +7 -0
- package/dist/prompt.js +18 -1
- package/dist/prompt.js.map +1 -1
- package/dist/recipe-host/git-trust.d.ts +43 -0
- package/dist/recipe-host/git-trust.js +360 -0
- package/dist/recipe-host/git-trust.js.map +1 -0
- package/dist/recipe-host/git.d.ts +42 -0
- package/dist/recipe-host/git.js +207 -0
- package/dist/recipe-host/git.js.map +1 -0
- package/dist/recipe-host/native-recipe-host.d.ts +127 -0
- package/dist/recipe-host/native-recipe-host.js +880 -0
- package/dist/recipe-host/native-recipe-host.js.map +1 -0
- package/dist/recipe-host/seat-cwd.d.ts +16 -0
- package/dist/recipe-host/seat-cwd.js +170 -0
- package/dist/recipe-host/seat-cwd.js.map +1 -0
- package/dist/runner.d.ts +43 -1
- package/dist/runner.js +234 -35
- package/dist/runner.js.map +1 -1
- package/dist/server.js +84 -5
- package/dist/server.js.map +1 -1
- package/dist/settings-cli/clients.d.ts +137 -0
- package/dist/settings-cli/clients.js +1124 -0
- package/dist/settings-cli/clients.js.map +1 -0
- package/dist/settings-cli/commands.d.ts +67 -0
- package/dist/settings-cli/commands.js +726 -0
- package/dist/settings-cli/commands.js.map +1 -0
- package/dist/settings-cli/format.d.ts +34 -0
- package/dist/settings-cli/format.js +40 -0
- package/dist/settings-cli/format.js.map +1 -0
- package/dist/settings-cli/main.d.ts +18 -0
- package/dist/settings-cli/main.js +256 -0
- package/dist/settings-cli/main.js.map +1 -0
- package/dist/stdio-main.d.ts +73 -0
- package/dist/stdio-main.js +455 -0
- package/dist/stdio-main.js.map +1 -0
- package/docs/configuration.md +362 -40
- package/docs/engineering-recipes.md +291 -0
- package/docs/security.md +234 -6
- package/docs/verification/project-checks.md +24 -2
- package/package.json +6 -2
- package/clinkx-workflows/dist/dashboard-ui/assets/index-BdpmbPps.js +0 -115
- package/clinkx-workflows/dist/dashboard-ui/assets/style-Im5i1iAs.css +0 -1
|
@@ -0,0 +1,787 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import { SubscriptionStateSchema } from "./contracts.js";
|
|
3
|
+
import { MAX_TIMER_DELAY_MS } from "./entitlements.js";
|
|
4
|
+
import { ControlError, ControlStoreUnavailableError } from "./errors.js";
|
|
5
|
+
/**
|
|
6
|
+
* The Stripe API version every request is pinned to. It is the default of the
|
|
7
|
+
* pinned `stripe` 22.6.2 SDK, whose types describe only this version; the
|
|
8
|
+
* `satisfies` check fails the build if an SDK upgrade moves the default.
|
|
9
|
+
*/
|
|
10
|
+
export const STRIPE_API_VERSION = "2026-08-26.dahlia";
|
|
11
|
+
export const DEFAULT_WEBHOOK_MAX_BODY_BYTES = 512 * 1024;
|
|
12
|
+
export const DEFAULT_BILLING_RETRY_BASE_MS = 5_000;
|
|
13
|
+
export const DEFAULT_BILLING_RETRY_MAX_MS = 15 * 60_000;
|
|
14
|
+
export const DEFAULT_BILLING_MAX_ATTEMPTS = 8;
|
|
15
|
+
export const DEFAULT_BILLING_JOB_RETENTION_MS = 7 * 24 * 60 * 60_000;
|
|
16
|
+
export const DEFAULT_TRANSITION_RETRY_DELAYS_MS = [100, 500, 2_000];
|
|
17
|
+
const MAX_RECONCILE_ROUNDS = 5;
|
|
18
|
+
/** Provider lists longer than this are refused rather than applied from a truncated view. */
|
|
19
|
+
export const MAX_PROVIDER_LIST_ITEMS = 1000;
|
|
20
|
+
const MAX_CUSTOMER_BIND_ROUNDS = 3;
|
|
21
|
+
const PROVIDER_ID = /^[\x21-\x7e]{1,255}$/;
|
|
22
|
+
const EVENT_ID = /^evt_[A-Za-z0-9]{1,255}$/;
|
|
23
|
+
const RELEVANT_EVENT_PREFIXES = ["customer.subscription.", "invoice.", "checkout.session."];
|
|
24
|
+
/** A provider request failed. `kind` is a short provider error class, never a message. */
|
|
25
|
+
export class BillingProviderError extends Error {
|
|
26
|
+
kind;
|
|
27
|
+
constructor(kind, options) {
|
|
28
|
+
super(`billing provider request failed: ${kind}`, options);
|
|
29
|
+
this.name = "BillingProviderError";
|
|
30
|
+
this.kind = kind;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Stored billing state changed during every fetch-compare round; the job retries later. */
|
|
34
|
+
export class BillingContentionError extends Error {
|
|
35
|
+
constructor() {
|
|
36
|
+
super("billing state changed during every reconciliation round");
|
|
37
|
+
this.name = "BillingContentionError";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** An open invoice does not name the subscription and customer it was listed for; nothing is applied and the job retries. */
|
|
41
|
+
export class BillingInvoiceIdentityError extends Error {
|
|
42
|
+
constructor() {
|
|
43
|
+
super("an open invoice does not identify the current purchase");
|
|
44
|
+
this.name = "BillingInvoiceIdentityError";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Reconciliation cannot succeed by retrying (for example, a subscription already bound to another tenant). */
|
|
48
|
+
export class BillingPermanentError extends Error {
|
|
49
|
+
constructor(message) {
|
|
50
|
+
super(message);
|
|
51
|
+
this.name = "BillingPermanentError";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function providerErrorKind(error) {
|
|
55
|
+
if (error instanceof BillingProviderError)
|
|
56
|
+
return error.kind;
|
|
57
|
+
if (typeof error === "object" && error !== null && "type" in error) {
|
|
58
|
+
const type = error.type;
|
|
59
|
+
if (typeof type === "string" && /^[A-Za-z_]{1,64}$/.test(type))
|
|
60
|
+
return type;
|
|
61
|
+
}
|
|
62
|
+
return "request_failed";
|
|
63
|
+
}
|
|
64
|
+
async function providerCall(operation) {
|
|
65
|
+
try {
|
|
66
|
+
return await operation();
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
throw new BillingProviderError(providerErrorKind(error), { cause: error });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function positiveSecondsOrNull(value) {
|
|
73
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : null;
|
|
74
|
+
}
|
|
75
|
+
function invoiceOpenedAt(invoice) {
|
|
76
|
+
return positiveSecondsOrNull(invoice.status_transitions?.finalized_at) ?? positiveSecondsOrNull(invoice.created);
|
|
77
|
+
}
|
|
78
|
+
function idOf(value) {
|
|
79
|
+
if (typeof value === "string")
|
|
80
|
+
return PROVIDER_ID.test(value) ? value : null;
|
|
81
|
+
if (typeof value === "object" && value !== null && "id" in value)
|
|
82
|
+
return idOf(value.id);
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
/** Stripe through the pinned SDK and API version. The secret key is read from the named variable at each use. */
|
|
86
|
+
export function createStripeBillingProvider(options) {
|
|
87
|
+
const env = options.env ?? process.env;
|
|
88
|
+
const { environment, secretKeyEnv } = options.stripe;
|
|
89
|
+
let cached = null;
|
|
90
|
+
function client() {
|
|
91
|
+
const key = env[secretKeyEnv];
|
|
92
|
+
if (key === undefined || (!key.startsWith(`sk_${environment}_`) && !key.startsWith(`rk_${environment}_`))) {
|
|
93
|
+
throw new BillingProviderError("configuration");
|
|
94
|
+
}
|
|
95
|
+
if (cached?.key !== key) {
|
|
96
|
+
cached = {
|
|
97
|
+
key,
|
|
98
|
+
client: new Stripe(key, {
|
|
99
|
+
apiVersion: STRIPE_API_VERSION,
|
|
100
|
+
maxNetworkRetries: 2,
|
|
101
|
+
timeout: options.timeoutMs ?? 20_000,
|
|
102
|
+
telemetry: false,
|
|
103
|
+
...(options.httpClient === undefined ? {} : { httpClient: options.httpClient }),
|
|
104
|
+
}),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return cached.client;
|
|
108
|
+
}
|
|
109
|
+
function subscriptionOf(subscription) {
|
|
110
|
+
return {
|
|
111
|
+
id: subscription.id,
|
|
112
|
+
customerId: idOf(subscription.customer) ?? "",
|
|
113
|
+
status: subscription.status,
|
|
114
|
+
created: subscription.created,
|
|
115
|
+
livemode: subscription.livemode,
|
|
116
|
+
items: subscription.items.data.map((item) => ({
|
|
117
|
+
priceId: item.price.id,
|
|
118
|
+
currentPeriodEnd: typeof item.current_period_end === "number" ? item.current_period_end : null,
|
|
119
|
+
})),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function complete(items) {
|
|
123
|
+
if (items.length > MAX_PROVIDER_LIST_ITEMS)
|
|
124
|
+
throw new BillingProviderError("list_truncated");
|
|
125
|
+
return items;
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
createCustomer: ({ tenantId, idempotencyKey }) => providerCall(async () => {
|
|
129
|
+
const customer = await client().customers.create({ metadata: { clinkx_tenant_id: tenantId } }, { idempotencyKey });
|
|
130
|
+
return { id: customer.id, livemode: customer.livemode };
|
|
131
|
+
}),
|
|
132
|
+
createCheckoutSession: (input) => providerCall(async () => {
|
|
133
|
+
const session = await client().checkout.sessions.create({
|
|
134
|
+
mode: "subscription",
|
|
135
|
+
customer: input.customerId,
|
|
136
|
+
client_reference_id: input.tenantId,
|
|
137
|
+
line_items: [{ price: input.priceId, quantity: 1 }],
|
|
138
|
+
success_url: input.successUrl,
|
|
139
|
+
cancel_url: input.cancelUrl,
|
|
140
|
+
metadata: { clinkx_tenant_id: input.tenantId },
|
|
141
|
+
subscription_data: { metadata: { clinkx_tenant_id: input.tenantId } },
|
|
142
|
+
});
|
|
143
|
+
if (session.url === null)
|
|
144
|
+
throw new BillingProviderError("missing_url");
|
|
145
|
+
return { url: session.url };
|
|
146
|
+
}),
|
|
147
|
+
createPortalSession: (input) => providerCall(async () => {
|
|
148
|
+
const session = await client().billingPortal.sessions.create({
|
|
149
|
+
customer: input.customerId,
|
|
150
|
+
return_url: input.returnUrl,
|
|
151
|
+
});
|
|
152
|
+
return { url: session.url };
|
|
153
|
+
}),
|
|
154
|
+
listSubscriptions: (customerId) => providerCall(async () => {
|
|
155
|
+
const subscriptions = await client()
|
|
156
|
+
.subscriptions.list({ customer: customerId, status: "all", limit: 100 })
|
|
157
|
+
.autoPagingToArray({ limit: MAX_PROVIDER_LIST_ITEMS + 1 });
|
|
158
|
+
return complete(subscriptions).map(subscriptionOf);
|
|
159
|
+
}),
|
|
160
|
+
listOpenInvoices: (subscriptionId) => providerCall(async () => {
|
|
161
|
+
const invoices = await client()
|
|
162
|
+
.invoices.list({ subscription: subscriptionId, status: "open", limit: 100 })
|
|
163
|
+
.autoPagingToArray({ limit: MAX_PROVIDER_LIST_ITEMS + 1 });
|
|
164
|
+
return complete(invoices).flatMap((invoice) => invoice.id === undefined
|
|
165
|
+
? []
|
|
166
|
+
: [
|
|
167
|
+
{
|
|
168
|
+
id: invoice.id,
|
|
169
|
+
customerId: idOf(invoice.customer),
|
|
170
|
+
subscriptionId: idOf(invoice.parent?.subscription_details?.subscription),
|
|
171
|
+
status: invoice.status ?? "unknown",
|
|
172
|
+
openedAt: invoiceOpenedAt(invoice),
|
|
173
|
+
},
|
|
174
|
+
]);
|
|
175
|
+
}),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Verify a raw webhook body against the signing secret and the configured
|
|
180
|
+
* environment. The body is never parsed before its signature is checked, and
|
|
181
|
+
* nothing in it is applied as state: it only selects reconciliation work.
|
|
182
|
+
*/
|
|
183
|
+
export function verifyBillingWebhook(input) {
|
|
184
|
+
if (input.signature === undefined || input.signature.length === 0 || input.signature.length > 4096) {
|
|
185
|
+
throw new ControlError("webhook_signature_invalid");
|
|
186
|
+
}
|
|
187
|
+
let event;
|
|
188
|
+
try {
|
|
189
|
+
event = Stripe.webhooks.constructEvent(input.rawBody, input.signature, input.secret);
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (error instanceof Stripe.errors.StripeSignatureVerificationError)
|
|
193
|
+
throw new ControlError("webhook_signature_invalid");
|
|
194
|
+
throw new ControlError("invalid_request");
|
|
195
|
+
}
|
|
196
|
+
if (typeof event !== "object" || event === null)
|
|
197
|
+
throw new ControlError("invalid_request");
|
|
198
|
+
if (typeof event.id !== "string" || !EVENT_ID.test(event.id))
|
|
199
|
+
throw new ControlError("invalid_request");
|
|
200
|
+
if (typeof event.type !== "string" || event.type.length < 1 || event.type.length > 255) {
|
|
201
|
+
throw new ControlError("invalid_request");
|
|
202
|
+
}
|
|
203
|
+
if (event.livemode !== (input.environment === "live")) {
|
|
204
|
+
throw new ControlError("invalid_request", "webhook environment does not match the configured Stripe environment");
|
|
205
|
+
}
|
|
206
|
+
return { id: event.id, type: event.type, trigger: triggerOf(event.type, event.data?.object) };
|
|
207
|
+
}
|
|
208
|
+
function field(value, key) {
|
|
209
|
+
return typeof value === "object" && value !== null ? value[key] : undefined;
|
|
210
|
+
}
|
|
211
|
+
function triggerOf(type, object) {
|
|
212
|
+
if (!RELEVANT_EVENT_PREFIXES.some((prefix) => type.startsWith(prefix)))
|
|
213
|
+
return null;
|
|
214
|
+
const stripeCustomerId = idOf(field(object, "customer"));
|
|
215
|
+
if (stripeCustomerId === null)
|
|
216
|
+
return null;
|
|
217
|
+
const kind = field(object, "object");
|
|
218
|
+
let stripeSubscriptionId = null;
|
|
219
|
+
let stripeInvoiceId = null;
|
|
220
|
+
if (kind === "subscription")
|
|
221
|
+
stripeSubscriptionId = idOf(field(object, "id"));
|
|
222
|
+
if (kind === "checkout.session")
|
|
223
|
+
stripeSubscriptionId = idOf(field(object, "subscription"));
|
|
224
|
+
if (kind === "invoice") {
|
|
225
|
+
stripeInvoiceId = idOf(field(object, "id"));
|
|
226
|
+
stripeSubscriptionId = idOf(field(field(field(object, "parent"), "subscription_details"), "subscription"));
|
|
227
|
+
}
|
|
228
|
+
return { stripeCustomerId, stripeSubscriptionId, stripeInvoiceId };
|
|
229
|
+
}
|
|
230
|
+
const KNOWN_SUBSCRIPTION_STATES = new Set(SubscriptionStateSchema.exclude(["none", "unknown"]).options);
|
|
231
|
+
/** Provider status to stored state; anything unrecognized is `unknown`, which denies. */
|
|
232
|
+
export function subscriptionStateOf(status) {
|
|
233
|
+
return KNOWN_SUBSCRIPTION_STATES.has(status) ? status : "unknown";
|
|
234
|
+
}
|
|
235
|
+
const RETAINED_STATES = new Set(["active", "trialing", "past_due"]);
|
|
236
|
+
const PAYING_STATES = new Set(["active", "trialing"]);
|
|
237
|
+
function unboundRank(status) {
|
|
238
|
+
switch (status) {
|
|
239
|
+
case "past_due":
|
|
240
|
+
return 3;
|
|
241
|
+
case "unpaid":
|
|
242
|
+
case "paused":
|
|
243
|
+
return 2;
|
|
244
|
+
case "incomplete":
|
|
245
|
+
return 1;
|
|
246
|
+
default:
|
|
247
|
+
return 0;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function newestFirst(a, b) {
|
|
251
|
+
return b.created - a.created || (a.id < b.id ? 1 : a.id > b.id ? -1 : 0);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* The customer's current purchase from current provider state alone, so the
|
|
255
|
+
* order events arrived in plays no part. Among subscriptions for a configured
|
|
256
|
+
* Price: (a) the bound subscription while it is active, trialing, or past due;
|
|
257
|
+
* otherwise (b) the active or trialing one with the latest period end, then the
|
|
258
|
+
* newest; otherwise (d) the bound subscription in its terminal state. Pending
|
|
259
|
+
* (`incomplete`) and expired Checkout subscriptions therefore never displace a
|
|
260
|
+
* bound purchase and never grant. Only with no bound subscription in the
|
|
261
|
+
* provider's list does the best remaining state (past due, then unpaid or
|
|
262
|
+
* paused, then incomplete) apply. Stripe allows overlapping active
|
|
263
|
+
* subscriptions; any of them is a paying purchase.
|
|
264
|
+
*/
|
|
265
|
+
export function selectCurrentSubscription(input) {
|
|
266
|
+
const prices = new Set(input.priceIds);
|
|
267
|
+
const candidates = input.subscriptions.filter((subscription) => subscription.customerId === input.customerId &&
|
|
268
|
+
subscription.livemode === input.livemode &&
|
|
269
|
+
PROVIDER_ID.test(subscription.id) &&
|
|
270
|
+
subscription.items.some((item) => prices.has(item.priceId)));
|
|
271
|
+
const bound = candidates.find((subscription) => subscription.id === input.boundSubscriptionId) ?? null;
|
|
272
|
+
if (bound !== null && RETAINED_STATES.has(bound.status))
|
|
273
|
+
return bound;
|
|
274
|
+
const paying = candidates
|
|
275
|
+
.filter((subscription) => PAYING_STATES.has(subscription.status))
|
|
276
|
+
.sort((a, b) => (periodEndOf(b, input.priceIds) ?? Number.NEGATIVE_INFINITY) -
|
|
277
|
+
(periodEndOf(a, input.priceIds) ?? Number.NEGATIVE_INFINITY) || newestFirst(a, b));
|
|
278
|
+
if (paying[0] !== undefined)
|
|
279
|
+
return paying[0];
|
|
280
|
+
if (bound !== null)
|
|
281
|
+
return bound;
|
|
282
|
+
return [...candidates].sort((a, b) => unboundRank(b.status) - unboundRank(a.status) || newestFirst(a, b))[0] ?? null;
|
|
283
|
+
}
|
|
284
|
+
/** The access cutoff: the earliest period end among the subscription's configured-price items, in milliseconds. */
|
|
285
|
+
export function periodEndOf(subscription, priceIds) {
|
|
286
|
+
const prices = new Set(priceIds);
|
|
287
|
+
let earliest = null;
|
|
288
|
+
for (const item of subscription.items) {
|
|
289
|
+
if (!prices.has(item.priceId) || item.currentPeriodEnd === null)
|
|
290
|
+
continue;
|
|
291
|
+
if (!Number.isSafeInteger(item.currentPeriodEnd) || item.currentPeriodEnd <= 0)
|
|
292
|
+
continue;
|
|
293
|
+
if (earliest === null || item.currentPeriodEnd < earliest)
|
|
294
|
+
earliest = item.currentPeriodEnd;
|
|
295
|
+
}
|
|
296
|
+
return earliest === null ? null : earliest * 1000;
|
|
297
|
+
}
|
|
298
|
+
function bindingOf(record) {
|
|
299
|
+
return {
|
|
300
|
+
environment: record.environment,
|
|
301
|
+
stripeCustomerId: record.stripeCustomerId,
|
|
302
|
+
stripeSubscriptionId: record.stripeSubscriptionId,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
function sameBinding(a, b) {
|
|
306
|
+
return (a.environment === b.environment &&
|
|
307
|
+
a.stripeCustomerId === b.stripeCustomerId &&
|
|
308
|
+
a.stripeSubscriptionId === b.stripeSubscriptionId);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Bring one tenant's entitlement to current provider state. Provider state is
|
|
312
|
+
* fetched outside any transaction; each write then compares the stored
|
|
313
|
+
* revision and purchase binding, and any change in between restarts the
|
|
314
|
+
* fetch-compare round. Never touches suspension or device credentials.
|
|
315
|
+
*/
|
|
316
|
+
export async function reconcileTenantBilling(context, tenantId) {
|
|
317
|
+
for (let round = 0; round < MAX_RECONCILE_ROUNDS; round += 1) {
|
|
318
|
+
const stored = context.store.getEntitlement(tenantId);
|
|
319
|
+
if (stored === null)
|
|
320
|
+
return { status: "not_found" };
|
|
321
|
+
const customerId = stored.stripeCustomerId;
|
|
322
|
+
if (customerId === null || stored.environment !== context.environment)
|
|
323
|
+
return { status: "unbound" };
|
|
324
|
+
const subscriptions = await context.provider.listSubscriptions(customerId);
|
|
325
|
+
const chosen = selectCurrentSubscription({
|
|
326
|
+
subscriptions,
|
|
327
|
+
customerId,
|
|
328
|
+
priceIds: context.priceIds,
|
|
329
|
+
livemode: context.environment === "live",
|
|
330
|
+
boundSubscriptionId: stored.stripeSubscriptionId,
|
|
331
|
+
});
|
|
332
|
+
const invoices = chosen !== null && chosen.status === "past_due" ? await context.provider.listOpenInvoices(chosen.id) : [];
|
|
333
|
+
if (chosen !== null &&
|
|
334
|
+
invoices.some((invoice) => !PROVIDER_ID.test(invoice.id) || invoice.subscriptionId !== chosen.id || invoice.customerId !== customerId)) {
|
|
335
|
+
throw new BillingInvoiceIdentityError();
|
|
336
|
+
}
|
|
337
|
+
const result = applyProviderState(context, stored, customerId, chosen, invoices);
|
|
338
|
+
if (result.status === "applied")
|
|
339
|
+
return result;
|
|
340
|
+
}
|
|
341
|
+
throw new BillingContentionError();
|
|
342
|
+
}
|
|
343
|
+
function applyProviderState(context, stored, customerId, chosen, invoices) {
|
|
344
|
+
const target = {
|
|
345
|
+
environment: context.environment,
|
|
346
|
+
stripeCustomerId: customerId,
|
|
347
|
+
stripeSubscriptionId: chosen?.id ?? null,
|
|
348
|
+
};
|
|
349
|
+
const subscriptionState = chosen === null ? "none" : subscriptionStateOf(chosen.status);
|
|
350
|
+
const currentPeriodEnd = chosen === null ? null : periodEndOf(chosen, context.priceIds);
|
|
351
|
+
let revision = stored.revision;
|
|
352
|
+
let binding = bindingOf(stored);
|
|
353
|
+
let latest = stored;
|
|
354
|
+
const update = () => {
|
|
355
|
+
const result = context.store.applyEntitlementUpdate({
|
|
356
|
+
tenantId: stored.tenantId,
|
|
357
|
+
expected: { revision, binding },
|
|
358
|
+
update: { binding: target, subscriptionState, currentPeriodEnd, grace: "clear" },
|
|
359
|
+
});
|
|
360
|
+
if (result.status === "binding_conflict") {
|
|
361
|
+
throw new BillingPermanentError("the provider subscription is bound to another tenant");
|
|
362
|
+
}
|
|
363
|
+
if (result.status !== "ok")
|
|
364
|
+
return false;
|
|
365
|
+
latest = result.entitlement;
|
|
366
|
+
revision = latest.revision;
|
|
367
|
+
binding = bindingOf(latest);
|
|
368
|
+
return true;
|
|
369
|
+
};
|
|
370
|
+
const applied = () => ({ status: "applied", entitlement: latest });
|
|
371
|
+
const retry = { status: "retry" };
|
|
372
|
+
if (subscriptionState !== "past_due" || chosen === null)
|
|
373
|
+
return update() ? applied() : retry;
|
|
374
|
+
// A rebind clears grace, because grace belongs to an obligation of one
|
|
375
|
+
// purchase binding, so it is applied first.
|
|
376
|
+
if (!sameBinding(binding, target) && !update())
|
|
377
|
+
return retry;
|
|
378
|
+
const pastDueBinding = {
|
|
379
|
+
environment: context.environment,
|
|
380
|
+
stripeCustomerId: customerId,
|
|
381
|
+
stripeSubscriptionId: chosen.id,
|
|
382
|
+
};
|
|
383
|
+
const result = context.access.applyPastDue({
|
|
384
|
+
tenantId: stored.tenantId,
|
|
385
|
+
expected: { revision, binding: pastDueBinding },
|
|
386
|
+
openInvoices: invoices.map((invoice) => ({ invoiceId: invoice.id, failedSinceMs: openedAtMs(invoice) })),
|
|
387
|
+
currentPeriodEnd,
|
|
388
|
+
});
|
|
389
|
+
if (result.status !== "ok")
|
|
390
|
+
return retry;
|
|
391
|
+
latest = result.entitlement;
|
|
392
|
+
return applied();
|
|
393
|
+
}
|
|
394
|
+
function openedAtMs(invoice) {
|
|
395
|
+
const ms = invoice.openedAt === null ? null : invoice.openedAt * 1000;
|
|
396
|
+
return ms !== null && Number.isSafeInteger(ms) && ms > 0 ? ms : null;
|
|
397
|
+
}
|
|
398
|
+
/** A job failure description safe to persist: error classes only, never provider messages or secrets. */
|
|
399
|
+
export function describeBillingFailure(error) {
|
|
400
|
+
if (error instanceof BillingProviderError)
|
|
401
|
+
return `provider: ${error.kind}`;
|
|
402
|
+
if (error instanceof ControlStoreUnavailableError)
|
|
403
|
+
return "store_unavailable";
|
|
404
|
+
if (error instanceof BillingPermanentError)
|
|
405
|
+
return `permanent: ${error.message}`;
|
|
406
|
+
if (error instanceof BillingContentionError)
|
|
407
|
+
return "contention";
|
|
408
|
+
if (error instanceof BillingInvoiceIdentityError)
|
|
409
|
+
return "provider: invoice_identity";
|
|
410
|
+
return error instanceof Error ? `internal: ${error.name}` : "internal";
|
|
411
|
+
}
|
|
412
|
+
function sleep(delayMs) {
|
|
413
|
+
return new Promise((resolve) => {
|
|
414
|
+
setTimeout(resolve, delayMs).unref();
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
const NODE_TIMERS = {
|
|
418
|
+
setTimeout(callback, delayMs) {
|
|
419
|
+
const handle = setTimeout(callback, delayMs);
|
|
420
|
+
handle.unref();
|
|
421
|
+
return handle;
|
|
422
|
+
},
|
|
423
|
+
clearTimeout(handle) {
|
|
424
|
+
clearTimeout(handle);
|
|
425
|
+
},
|
|
426
|
+
};
|
|
427
|
+
function positiveMs(name, value) {
|
|
428
|
+
if (!Number.isSafeInteger(value) || value <= 0)
|
|
429
|
+
throw new Error(`${name} must be a positive integer of milliseconds`);
|
|
430
|
+
return value;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Serialized, durable reconciliation. Webhooks, the startup run, and the
|
|
434
|
+
* periodic run all become rows in `billing_job` and run through one in-process
|
|
435
|
+
* queue. A failed attempt stays pending with exponential backoff until
|
|
436
|
+
* `maxAttempts`; periodic repair still covers a purchase whose job failed.
|
|
437
|
+
*/
|
|
438
|
+
export function createBillingReconciler(options) {
|
|
439
|
+
const { store, provider } = options;
|
|
440
|
+
const environment = options.config.stripe.environment;
|
|
441
|
+
const timers = options.timers ?? NODE_TIMERS;
|
|
442
|
+
const now = options.now ?? Date.now;
|
|
443
|
+
const intervalMs = Math.min(positiveMs("reconciliationIntervalSeconds", options.config.billing.reconciliationIntervalSeconds * 1000), MAX_TIMER_DELAY_MS);
|
|
444
|
+
const retryBaseMs = positiveMs("retryBaseMs", options.retryBaseMs ?? DEFAULT_BILLING_RETRY_BASE_MS);
|
|
445
|
+
const retryMaxMs = positiveMs("retryMaxMs", options.retryMaxMs ?? DEFAULT_BILLING_RETRY_MAX_MS);
|
|
446
|
+
const maxAttempts = positiveMs("maxAttempts", options.maxAttempts ?? DEFAULT_BILLING_MAX_ATTEMPTS);
|
|
447
|
+
const jobRetentionMs = positiveMs("jobRetentionMs", options.jobRetentionMs ?? DEFAULT_BILLING_JOB_RETENTION_MS);
|
|
448
|
+
const transitionRetryDelaysMs = options.transitionRetryDelaysMs ?? DEFAULT_TRANSITION_RETRY_DELAYS_MS;
|
|
449
|
+
for (const delay of transitionRetryDelaysMs) {
|
|
450
|
+
if (!Number.isSafeInteger(delay) || delay < 0 || delay > MAX_TIMER_DELAY_MS) {
|
|
451
|
+
throw new Error("transitionRetryDelaysMs must hold non-negative timer delays");
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const context = {
|
|
455
|
+
store,
|
|
456
|
+
provider,
|
|
457
|
+
access: options.access,
|
|
458
|
+
environment,
|
|
459
|
+
priceIds: options.config.stripe.priceIds,
|
|
460
|
+
};
|
|
461
|
+
let running = false;
|
|
462
|
+
let stopped = false;
|
|
463
|
+
let active = null;
|
|
464
|
+
let jobTimer = null;
|
|
465
|
+
let periodicTimer = null;
|
|
466
|
+
let inHand = null;
|
|
467
|
+
function report(error, job) {
|
|
468
|
+
try {
|
|
469
|
+
options.onError?.(error, job === null ? null : { id: job.id, trigger: job.trigger, attempts: job.attempts });
|
|
470
|
+
}
|
|
471
|
+
catch {
|
|
472
|
+
// Diagnostics must not stop reconciliation.
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
function backoff(attempts) {
|
|
476
|
+
const exponent = Math.min(Math.max(attempts - 1, 0), 30);
|
|
477
|
+
return Math.min(retryBaseMs * 2 ** exponent, retryMaxMs);
|
|
478
|
+
}
|
|
479
|
+
function tenantOfJob(job) {
|
|
480
|
+
if (job.tenantId !== null)
|
|
481
|
+
return job.tenantId;
|
|
482
|
+
if (job.stripeCustomerId === null)
|
|
483
|
+
return null;
|
|
484
|
+
return store.findEntitlementByCustomer(job.environment, job.stripeCustomerId)?.tenantId ?? null;
|
|
485
|
+
}
|
|
486
|
+
async function recordDurably(job, write) {
|
|
487
|
+
for (let attempt = 0;; attempt += 1) {
|
|
488
|
+
try {
|
|
489
|
+
write();
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
catch (error) {
|
|
493
|
+
report(error, job);
|
|
494
|
+
const delay = transitionRetryDelaysMs[attempt];
|
|
495
|
+
if (delay === undefined)
|
|
496
|
+
return;
|
|
497
|
+
await sleep(delay);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
async function processJob(job) {
|
|
502
|
+
let failure = null;
|
|
503
|
+
try {
|
|
504
|
+
const tenantId = tenantOfJob(job);
|
|
505
|
+
if (tenantId !== null)
|
|
506
|
+
await reconcileTenantBilling(context, tenantId);
|
|
507
|
+
}
|
|
508
|
+
catch (error) {
|
|
509
|
+
report(error, job);
|
|
510
|
+
failure = { error };
|
|
511
|
+
}
|
|
512
|
+
if (failure === null) {
|
|
513
|
+
await recordDurably(job, () => store.completeBillingJob(job.id));
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
const permanent = failure.error instanceof BillingPermanentError || job.attempts >= maxAttempts;
|
|
517
|
+
const description = describeBillingFailure(failure.error);
|
|
518
|
+
await recordDurably(job, () => store.failBillingJob({ id: job.id, error: description, retryDelayMs: permanent ? null : backoff(job.attempts) }));
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* The worker is the only process and runs one job at a time, so any
|
|
522
|
+
* `running` row other than the job in hand was left by a failed durable
|
|
523
|
+
* record and is returned to `pending`.
|
|
524
|
+
*/
|
|
525
|
+
function reclaimOrphans() {
|
|
526
|
+
try {
|
|
527
|
+
store.requeueRunningBillingJobs(environment, inHand);
|
|
528
|
+
}
|
|
529
|
+
catch (error) {
|
|
530
|
+
report(error, null);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
function armJobTimer(delayOverride) {
|
|
534
|
+
if (jobTimer !== null)
|
|
535
|
+
timers.clearTimeout(jobTimer);
|
|
536
|
+
jobTimer = null;
|
|
537
|
+
if (!running)
|
|
538
|
+
return;
|
|
539
|
+
let delay = delayOverride;
|
|
540
|
+
if (delay === undefined) {
|
|
541
|
+
let next;
|
|
542
|
+
try {
|
|
543
|
+
next = store.nextBillingJobAt(environment);
|
|
544
|
+
}
|
|
545
|
+
catch (error) {
|
|
546
|
+
report(error, null);
|
|
547
|
+
next = now() + retryBaseMs;
|
|
548
|
+
}
|
|
549
|
+
if (next === null)
|
|
550
|
+
return;
|
|
551
|
+
delay = next - now();
|
|
552
|
+
}
|
|
553
|
+
jobTimer = timers.setTimeout(() => {
|
|
554
|
+
jobTimer = null;
|
|
555
|
+
void drain();
|
|
556
|
+
}, Math.min(Math.max(delay, 0), MAX_TIMER_DELAY_MS));
|
|
557
|
+
}
|
|
558
|
+
async function loop() {
|
|
559
|
+
await null;
|
|
560
|
+
try {
|
|
561
|
+
reclaimOrphans();
|
|
562
|
+
for (;;) {
|
|
563
|
+
let job;
|
|
564
|
+
try {
|
|
565
|
+
job = store.claimBillingJob(environment);
|
|
566
|
+
}
|
|
567
|
+
catch (error) {
|
|
568
|
+
report(error, null);
|
|
569
|
+
armJobTimer(retryBaseMs);
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if (job === null)
|
|
573
|
+
break;
|
|
574
|
+
inHand = job.id;
|
|
575
|
+
try {
|
|
576
|
+
await processJob(job);
|
|
577
|
+
}
|
|
578
|
+
finally {
|
|
579
|
+
inHand = null;
|
|
580
|
+
}
|
|
581
|
+
if (stopped)
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
armJobTimer();
|
|
585
|
+
}
|
|
586
|
+
finally {
|
|
587
|
+
active = null;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
function drain() {
|
|
591
|
+
active ??= loop();
|
|
592
|
+
return active;
|
|
593
|
+
}
|
|
594
|
+
async function sweep(trigger) {
|
|
595
|
+
reclaimOrphans();
|
|
596
|
+
try {
|
|
597
|
+
store.enqueueBillingSweep({ environment, trigger });
|
|
598
|
+
}
|
|
599
|
+
catch (error) {
|
|
600
|
+
report(error, null);
|
|
601
|
+
}
|
|
602
|
+
if (trigger === "periodic") {
|
|
603
|
+
try {
|
|
604
|
+
store.pruneBillingJobs(jobRetentionMs);
|
|
605
|
+
}
|
|
606
|
+
catch (error) {
|
|
607
|
+
report(error, null);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
await drain();
|
|
611
|
+
}
|
|
612
|
+
function armPeriodic() {
|
|
613
|
+
if (periodicTimer !== null)
|
|
614
|
+
timers.clearTimeout(periodicTimer);
|
|
615
|
+
periodicTimer = null;
|
|
616
|
+
if (!running)
|
|
617
|
+
return;
|
|
618
|
+
periodicTimer = timers.setTimeout(() => {
|
|
619
|
+
periodicTimer = null;
|
|
620
|
+
void sweep("periodic").finally(armPeriodic);
|
|
621
|
+
}, intervalMs);
|
|
622
|
+
}
|
|
623
|
+
return {
|
|
624
|
+
start() {
|
|
625
|
+
if (running)
|
|
626
|
+
return;
|
|
627
|
+
running = true;
|
|
628
|
+
stopped = false;
|
|
629
|
+
void sweep("startup");
|
|
630
|
+
armPeriodic();
|
|
631
|
+
},
|
|
632
|
+
async stop() {
|
|
633
|
+
running = false;
|
|
634
|
+
stopped = true;
|
|
635
|
+
if (jobTimer !== null)
|
|
636
|
+
timers.clearTimeout(jobTimer);
|
|
637
|
+
if (periodicTimer !== null)
|
|
638
|
+
timers.clearTimeout(periodicTimer);
|
|
639
|
+
jobTimer = null;
|
|
640
|
+
periodicTimer = null;
|
|
641
|
+
await active;
|
|
642
|
+
},
|
|
643
|
+
kick() {
|
|
644
|
+
void drain();
|
|
645
|
+
},
|
|
646
|
+
drain,
|
|
647
|
+
sweep,
|
|
648
|
+
get running() {
|
|
649
|
+
return running;
|
|
650
|
+
},
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
const ACTIVE_STATES = new Set(["active", "trialing", "past_due"]);
|
|
654
|
+
function isHttpsUrl(value) {
|
|
655
|
+
try {
|
|
656
|
+
const parsed = new URL(value);
|
|
657
|
+
return parsed.protocol === "https:" && parsed.username === "" && parsed.password === "" && value.length <= 4096;
|
|
658
|
+
}
|
|
659
|
+
catch {
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
export function createBillingService(options) {
|
|
664
|
+
const { store, provider, publicOrigin } = options;
|
|
665
|
+
const env = options.env ?? process.env;
|
|
666
|
+
const { environment, priceIds, webhookSecretEnv } = options.config.stripe;
|
|
667
|
+
const checkoutPriceId = priceIds[0];
|
|
668
|
+
if (checkoutPriceId === undefined)
|
|
669
|
+
throw new Error("billing needs at least one configured Price ID");
|
|
670
|
+
const webhookMaxBodyBytes = positiveMs("webhookMaxBodyBytes", options.webhookMaxBodyBytes ?? DEFAULT_WEBHOOK_MAX_BODY_BYTES);
|
|
671
|
+
function report(error) {
|
|
672
|
+
try {
|
|
673
|
+
options.onError?.(error);
|
|
674
|
+
}
|
|
675
|
+
catch {
|
|
676
|
+
// Diagnostics must not change the response.
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
async function provide(operation) {
|
|
680
|
+
try {
|
|
681
|
+
return await operation();
|
|
682
|
+
}
|
|
683
|
+
catch (error) {
|
|
684
|
+
report(error);
|
|
685
|
+
throw new ControlError("provider_unavailable", "billing provider is unavailable");
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
function account(tenantId, actor) {
|
|
689
|
+
const result = store.readBillingAccount({ tenantId, actor });
|
|
690
|
+
if (result.status === "unauthorized")
|
|
691
|
+
throw new ControlError("unauthenticated");
|
|
692
|
+
if (result.status !== "ok")
|
|
693
|
+
throw new ControlError("not_found");
|
|
694
|
+
if (result.entitlement.stripeCustomerId !== null && result.entitlement.environment !== environment) {
|
|
695
|
+
report(new BillingPermanentError("tenant billing is bound to another Stripe environment"));
|
|
696
|
+
throw new ControlError("provider_unavailable", "billing provider is unavailable");
|
|
697
|
+
}
|
|
698
|
+
return result.entitlement;
|
|
699
|
+
}
|
|
700
|
+
async function ensureCustomer(tenant, actor, initial) {
|
|
701
|
+
let current = initial;
|
|
702
|
+
for (let round = 0; round < MAX_CUSTOMER_BIND_ROUNDS; round += 1) {
|
|
703
|
+
if (current.stripeCustomerId !== null)
|
|
704
|
+
return current.stripeCustomerId;
|
|
705
|
+
const customer = await provide(() => provider.createCustomer({ tenantId: tenant.id, idempotencyKey: `clinkx-customer-${environment}-${tenant.id}` }));
|
|
706
|
+
if (!PROVIDER_ID.test(customer.id) || customer.livemode !== (environment === "live")) {
|
|
707
|
+
report(new BillingProviderError("invalid_customer"));
|
|
708
|
+
throw new ControlError("provider_unavailable", "billing provider is unavailable");
|
|
709
|
+
}
|
|
710
|
+
const bound = store.bindBillingCustomer({
|
|
711
|
+
tenantId: tenant.id,
|
|
712
|
+
actor,
|
|
713
|
+
expectedRevision: current.revision,
|
|
714
|
+
environment,
|
|
715
|
+
stripeCustomerId: customer.id,
|
|
716
|
+
});
|
|
717
|
+
if (bound.status === "ok")
|
|
718
|
+
return customer.id;
|
|
719
|
+
if (bound.status === "unauthorized")
|
|
720
|
+
throw new ControlError("unauthenticated");
|
|
721
|
+
if (bound.status === "not_found")
|
|
722
|
+
throw new ControlError("not_found");
|
|
723
|
+
if (bound.status === "binding_conflict") {
|
|
724
|
+
report(new BillingPermanentError("provider customer is bound to another tenant"));
|
|
725
|
+
throw new ControlError("provider_unavailable", "billing provider is unavailable");
|
|
726
|
+
}
|
|
727
|
+
current = account(tenant.id, actor);
|
|
728
|
+
}
|
|
729
|
+
throw new ControlError("store_unavailable");
|
|
730
|
+
}
|
|
731
|
+
function checkedUrl(url) {
|
|
732
|
+
if (!isHttpsUrl(url)) {
|
|
733
|
+
report(new BillingProviderError("invalid_url"));
|
|
734
|
+
throw new ControlError("provider_unavailable", "billing provider is unavailable");
|
|
735
|
+
}
|
|
736
|
+
return url;
|
|
737
|
+
}
|
|
738
|
+
return {
|
|
739
|
+
webhookMaxBodyBytes,
|
|
740
|
+
status(tenantId) {
|
|
741
|
+
return options.access.entitlementStatus(tenantId);
|
|
742
|
+
},
|
|
743
|
+
async createCheckout({ tenant, actor }) {
|
|
744
|
+
const current = account(tenant.id, actor);
|
|
745
|
+
if (current.stripeSubscriptionId !== null && ACTIVE_STATES.has(current.subscriptionState)) {
|
|
746
|
+
throw new ControlError("subscription_active", "a subscription is already active; manage it in the billing portal");
|
|
747
|
+
}
|
|
748
|
+
const customerId = await ensureCustomer(tenant, actor, current);
|
|
749
|
+
const session = await provide(() => provider.createCheckoutSession({
|
|
750
|
+
customerId,
|
|
751
|
+
priceId: checkoutPriceId,
|
|
752
|
+
tenantId: tenant.id,
|
|
753
|
+
successUrl: `${publicOrigin}/?billing=checkout_complete`,
|
|
754
|
+
cancelUrl: `${publicOrigin}/?billing=checkout_canceled`,
|
|
755
|
+
}));
|
|
756
|
+
account(tenant.id, actor);
|
|
757
|
+
return { url: checkedUrl(session.url) };
|
|
758
|
+
},
|
|
759
|
+
async createPortal({ tenant, actor }) {
|
|
760
|
+
const current = account(tenant.id, actor);
|
|
761
|
+
const customerId = current.stripeCustomerId;
|
|
762
|
+
if (customerId === null)
|
|
763
|
+
throw new ControlError("not_found", "no billing account exists for this tenant");
|
|
764
|
+
const session = await provide(() => provider.createPortalSession({ customerId, returnUrl: `${publicOrigin}/` }));
|
|
765
|
+
account(tenant.id, actor);
|
|
766
|
+
return { url: checkedUrl(session.url) };
|
|
767
|
+
},
|
|
768
|
+
receiveWebhook({ rawBody, signature }) {
|
|
769
|
+
const secret = env[webhookSecretEnv];
|
|
770
|
+
if (secret === undefined || !secret.startsWith("whsec_")) {
|
|
771
|
+
report(new BillingProviderError("configuration"));
|
|
772
|
+
throw new ControlError("provider_unavailable", "billing webhook is not configured");
|
|
773
|
+
}
|
|
774
|
+
const event = verifyBillingWebhook({ rawBody, signature, secret, environment });
|
|
775
|
+
const recorded = store.recordBillingEvent({
|
|
776
|
+
environment,
|
|
777
|
+
eventId: event.id,
|
|
778
|
+
eventType: event.type,
|
|
779
|
+
trigger: event.trigger,
|
|
780
|
+
});
|
|
781
|
+
if (recorded.status === "recorded" && recorded.jobId !== null)
|
|
782
|
+
options.reconciler?.kick();
|
|
783
|
+
return { eventId: event.id, duplicate: recorded.status === "duplicate" };
|
|
784
|
+
},
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
//# sourceMappingURL=billing.js.map
|