Line data Source code
1 : import type { HostedBillingDestination } from '@comfyorg/account-core/billing'
2 :
3 : import { remoteConfig } from '@/platform/remoteConfig/remoteConfig'
4 :
5 : const LOCAL_HOSTNAMES = new Set(['localhost', '127.0.0.1', '[::1]'])
6 :
7 : /** The SDK owns the vocabulary; the server resolves it per user through the `hosted_billing_destination` flag. */
8 : export type { HostedBillingDestination }
9 :
10 : /**
11 : * The single narrowing boundary for the raw wire value. Anything but the
12 : * exact `billing_web` variant (absent, a typo, a boolean coerced by an
13 : * override) stays on the provider page, so a bad read can never send a
14 : * customer to an origin that is not serving yet.
15 : */
16 0 : export function normalizeHostedBillingDestination(
17 : value: unknown
18 : ): HostedBillingDestination {
19 0 : return value === 'billing_web' ? 'billing_web' : 'stripe'
20 : }
21 :
22 0 : function validateBillingWebUrl(value: string): URL | null {
23 0 : try {
24 0 : const url = new URL(value)
25 0 : if (url.username || url.password) return null
26 0 : if (url.protocol === 'https:') return url
27 :
28 : if (
29 : import.meta.env.DEV &&
30 : url.protocol === 'http:' &&
31 : LOCAL_HOSTNAMES.has(url.hostname)
32 : ) {
33 : return url
34 : }
35 : } catch {
36 0 : return null
37 : }
38 :
39 0 : return null
40 : }
41 :
42 : /**
43 : * The server-provided origin (`/api/features`' `billing_web_url`, one per
44 : * environment) takes precedence; `VITE_BILLING_WEB_URL` only fills in when
45 : * the server sends nothing, which is how local dev points itself at a
46 : * `pnpm dev:cloud:billing-web` instance.
47 : *
48 : * A server value that fails validation does *not* fall through to the env
49 : * var: the env fallback exists for local dev against a backend that has no
50 : * billing-web origin configured at all, not for a real deployment that sent
51 : * a broken one. Silently substituting a build-time default for a live
52 : * backend's malformed answer would route a real customer to whatever origin
53 : * happens to be baked into that build. Failing closed to the provider page
54 : * is the safer read, and it matches the fail-closed posture the rest of the
55 : * hosted-billing rollout already takes for an unresolved destination.
56 : */
57 0 : export function getBillingWebUrl(): URL | null {
58 0 : const fromServer = remoteConfig.value.billing_web_url
59 0 : if (typeof fromServer === 'string' && fromServer !== '') {
60 0 : return validateBillingWebUrl(fromServer)
61 : }
62 :
63 : const fromEnv = import.meta.env.VITE_BILLING_WEB_URL
64 0 : return fromEnv ? validateBillingWebUrl(fromEnv) : null
65 : }
|