Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci-oss-assets-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ jobs:
if pnpx license-checker-rseidelsohn@4 \
--production \
--summary \
--excludePackages '@comfyorg/comfyui-frontend;@comfyorg/account;@comfyorg/account-ui;@comfyorg/design-system;@comfyorg/ingest-types;@comfyorg/registry-types;@comfyorg/shared-frontend-utils;@comfyorg/tailwind-utils;@comfyorg/comfyui-electron-types;@comfyorg/comfy-multi-player' \
--excludePackages '@comfyorg/comfyui-frontend;@comfyorg/account;@comfyorg/account-ui;@comfyorg/billing-contract;@comfyorg/design-system;@comfyorg/ingest-types;@comfyorg/registry-types;@comfyorg/shared-frontend-utils;@comfyorg/tailwind-utils;@comfyorg/comfyui-electron-types;@comfyorg/comfy-multi-player' \
--clarificationsFile .github/license-clarifications.json \
--onlyAllow 'MIT;MIT*;Apache-2.0;BSD-2-Clause;BSD-3-Clause;ISC;0BSD;BlueOak-1.0.0;Python-2.0;CC0-1.0;Unlicense;(MIT OR Apache-2.0);(MIT OR GPL-3.0);(Apache-2.0 OR MIT);(MPL-2.0 OR Apache-2.0);MPL-2.0;CC-BY-4.0;CC-BY-3.0;GPL-3.0-only'; then
echo ''
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci-tests-unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Run package unit tests with coverage
run: |
rc=0
for pkg in account account-ui object-info-parser shared-frontend-utils design-system; do
for pkg in account account-ui billing-contract object-info-parser shared-frontend-utils design-system; do
pnpm --filter "@comfyorg/$pkg" exec vitest run --coverage || rc=1
done
exit $rc
Expand All @@ -60,7 +60,7 @@ jobs:
if: always()
uses: codecov/codecov-action@fb8b3582c8e4def4969c97caa2f19720cb33a72f # v7.0.0
with:
files: coverage/lcov.info,packages/account/coverage/lcov.info,packages/account-ui/coverage/lcov.info,packages/object-info-parser/coverage/lcov.info,packages/shared-frontend-utils/coverage/lcov.info,packages/design-system/coverage/lcov.info
files: coverage/lcov.info,packages/account/coverage/lcov.info,packages/account-ui/coverage/lcov.info,packages/billing-contract/coverage/lcov.info,packages/object-info-parser/coverage/lcov.info,packages/shared-frontend-utils/coverage/lcov.info,packages/design-system/coverage/lcov.info
flags: unit
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
1 change: 1 addition & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export default defineConfig([
allowDefaultProject: [
'packages/account/vitest.config.ts',
'packages/account-ui/vitest.config.ts',
'packages/billing-contract/vitest.config.ts',
'packages/design-system/vitest.config.ts',
'packages/object-info-parser/vitest.config.ts',
'packages/shared-frontend-utils/vitest.config.ts',
Expand Down
3 changes: 3 additions & 0 deletions knip.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const config: KnipConfig = {
'packages/account-ui': {
project: ['src/**/*.{js,ts,vue}']
},
'packages/billing-contract': {
project: ['src/**/*.ts']
},
'packages/design-system': {
project: ['src/**/*.{css,js,ts}']
},
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
"knip": "knip --cache",
"lint:fix:no-cache": "pnpm oxlint:main --fix && pnpm oxlint:audit && eslint src --fix && pnpm lint:website:fix",
"lint:fix": "pnpm oxlint:main --fix && pnpm oxlint:audit && eslint src --cache --fix && pnpm lint:website:fix",
"lint:no-cache": "pnpm exec stylelint '{apps,packages,src}/**/*.{css,vue}' && pnpm oxlint && eslint src && pnpm lint:website && pnpm --filter @comfyorg/account lint && pnpm --filter @comfyorg/billing-web lint && pnpm --filter @comfyorg/account-ui lint",
"lint:no-cache": "pnpm exec stylelint '{apps,packages,src}/**/*.{css,vue}' && pnpm oxlint && eslint src && pnpm lint:website && pnpm --filter @comfyorg/account lint && pnpm --filter @comfyorg/billing-web lint && pnpm --filter @comfyorg/account-ui lint && pnpm --filter @comfyorg/billing-contract lint",
"lint:unstaged:fix": "git diff --name-only --diff-filter=d -z HEAD -- '*.astro' '*.js' '*.ts' '*.vue' '*.mts' | xargs -0 -r eslint --cache --fix",
"lint:unstaged": "git diff --name-only --diff-filter=d -z HEAD -- '*.astro' '*.js' '*.ts' '*.vue' '*.mts' | xargs -0 -r eslint --cache",
"lint": "pnpm stylelint && pnpm oxlint && eslint src --cache && pnpm lint:website && pnpm --filter @comfyorg/account lint && pnpm --filter @comfyorg/billing-web lint && pnpm --filter @comfyorg/account-ui lint",
"lint": "pnpm stylelint && pnpm oxlint && eslint src --cache && pnpm lint:website && pnpm --filter @comfyorg/account lint && pnpm --filter @comfyorg/billing-web lint && pnpm --filter @comfyorg/account-ui lint && pnpm --filter @comfyorg/billing-contract lint",
"lint:website": "eslint \"apps/website/**/*.{astro,js,mjs,ts,tsx,mts,vue}\"",
"lint:website:fix": "pnpm lint:website --fix",
"locale": "tsx scripts/i18n/update-locales.ts",
Expand Down Expand Up @@ -74,9 +74,10 @@
"test:custom-nodes:self-check": "pnpm test:custom-nodes:watch -g \"self-check\"",
"test:coverage": "vitest run --coverage",
"test:unit": "vitest run",
"typecheck": "vue-tsc --noEmit && pnpm typecheck:account && pnpm typecheck:billing-web && pnpm typecheck:account-ui",
"typecheck": "vue-tsc --noEmit && pnpm typecheck:account && pnpm typecheck:billing-web && pnpm typecheck:account-ui && pnpm typecheck:billing-contract",
"typecheck:account": "pnpm --filter @comfyorg/account typecheck",
"typecheck:account-ui": "pnpm --filter @comfyorg/account-ui typecheck",
"typecheck:billing-contract": "pnpm --filter @comfyorg/billing-contract typecheck",
"typecheck:browser": "vue-tsc --project browser_tests/tsconfig.json",
"typecheck:build": "vue-tsc --project build/tsconfig.json",
"typecheck:billing-web": "pnpm --filter @comfyorg/billing-web run typecheck",
Expand Down
23 changes: 23 additions & 0 deletions packages/billing-contract/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@comfyorg/billing-contract",
"version": "1.0.0-alpha.0",
"private": true,
"description": "The URL contract every Comfy product shares with the hosted billing app: the versioned route builder, the entry parser billing-web reads on arrival, and the closed return_to registry that keeps the trip back off open redirects",
"keywords": [],
"license": "MIT",
"type": "module",
"exports": {
".": "./src/index.ts"
},
"scripts": {
"lint": "oxlint src --type-aware && eslint src",
"test": "vitest run",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@types/node": "catalog:",
"@vitest/coverage-v8": "catalog:",
"typescript": "catalog:",
"vitest": "catalog:"
}
}
167 changes: 167 additions & 0 deletions packages/billing-contract/src/billingEntry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { describe, expect, it } from 'vitest'

import { BILLING_INTENTS, BILLING_PRODUCTS } from './contract'
import { parseBillingEntry } from './entryParser'
import type { BillingEntryInput, BillingEntryUrlErrorCode } from './entryUrl'
import { buildBillingEntryUrl } from './entryUrl'

const BASE_INPUT: BillingEntryInput = {
billingOrigin: 'https://billing.comfy.org',
intent: 'checkout',
product: 'platform',
returnTo: 'platform_account'
}

function entryUrl(input: BillingEntryInput): URL {
const result = buildBillingEntryUrl(input)
if (result.status === 'error')
throw new Error(`expected an entry URL, got ${result.code}`)
return result.url
}

function errorCode(input: BillingEntryInput): BillingEntryUrlErrorCode | 'ok' {
const result = buildBillingEntryUrl(input)
return result.status === 'error' ? result.code : 'ok'
}

describe('buildBillingEntryUrl', () => {
it('routes an intent under the contract version', () => {
expect(entryUrl({ ...BASE_INPUT, intent: 'invoices' }).pathname).toBe(
'/v1/invoices'
)
})

it('keeps only the origin of a billing URL that carries a path', () => {
expect(
entryUrl({
...BASE_INPUT,
billingOrigin: 'https://billing.comfy.org/app'
}).href
).toBe(
'https://billing.comfy.org/v1/checkout?product=platform&return_to=platform_account'
)
})

it('omits an absent optional identifier rather than writing it empty', () => {
expect(entryUrl(BASE_INPUT).search).toBe(
'?product=platform&return_to=platform_account'
)
})

it.for([
['https://billing.comfy.org', 'ok'],
['http://localhost:5174', 'ok'],
['http://127.0.0.1:5174', 'ok'],
['http://billing.comfy.org', 'INVALID_ORIGIN'],
['https://user:secret@billing.comfy.org', 'INVALID_ORIGIN'],
['ftp://billing.comfy.org', 'INVALID_ORIGIN'],
['not a url', 'INVALID_ORIGIN']
] as const)('reports %s as %s', ([billingOrigin, expected]) => {
expect(errorCode({ ...BASE_INPUT, billingOrigin })).toBe(expected)
})

it.for([
[{ returnTo: 'attacker_site' }, 'UNKNOWN_RETURN_TARGET'],
[{ plan: 'pro plan' }, 'INVALID_PLAN'],
[{ correlationId: '../escape' }, 'INVALID_CORRELATION_ID'],
[{ workspaceId: '' }, 'INVALID_WORKSPACE_ID']
] as const)('refuses %o with %s', ([overrides, expected]) => {
expect(errorCode({ ...BASE_INPUT, ...overrides })).toBe(expected)
})
})

describe('parseBillingEntry', () => {
it.for(BILLING_INTENTS)('round-trips the %s intent', (intent) => {
const url = entryUrl({
...BASE_INPUT,
intent,
plan: 'pro_monthly',
correlationId: 'corr-1',
workspaceId: 'ws_1'
})

expect(parseBillingEntry(url)).toEqual({
status: 'ok',
entry: {
version: 'v1',
intent,
product: 'platform',
returnTo: 'platform_account',
plan: 'pro_monthly',
correlationId: 'corr-1',
workspaceId: 'ws_1'
}
})
})

it.for(BILLING_PRODUCTS)('round-trips the %s product', (product) => {
const url = entryUrl({ ...BASE_INPUT, product })

expect(parseBillingEntry(url)).toEqual({
status: 'ok',
entry: {
version: 'v1',
intent: 'checkout',
product,
returnTo: 'platform_account'
}
})
})

it('ignores a query parameter the contract does not name', () => {
expect(
parseBillingEntry(
'/v1/pricing?product=workshop&return_to=comfyui_credits&utm_source=email'
)
).toEqual({
status: 'ok',
entry: {
version: 'v1',
intent: 'pricing',
product: 'workshop',
returnTo: 'comfyui_credits'
}
})
})

it.for([
[
'/v2/checkout?product=platform&return_to=platform_account',
'UNSUPPORTED_VERSION'
],
['nonsense', 'UNSUPPORTED_VERSION'],
['http://[', 'UNSUPPORTED_VERSION'],
[
'/v1/refunds?product=platform&return_to=platform_account',
'UNKNOWN_INTENT'
],
[
'/v1/checkout/extra?product=platform&return_to=platform_account',
'UNKNOWN_INTENT'
],
['/v1/checkout?return_to=platform_account', 'UNKNOWN_PRODUCT'],
[
'/v1/checkout?product=desktop&return_to=platform_account',
'UNKNOWN_PRODUCT'
],
['/v1/checkout?product=platform', 'UNKNOWN_RETURN_TARGET'],
[
'/v1/checkout?product=platform&return_to=https://attacker.example',
'UNKNOWN_RETURN_TARGET'
],
[
'/v1/checkout?product=platform&return_to=platform_account&plan=pro%20plan',
'INVALID_PLAN'
],
[
'/v1/checkout?product=platform&return_to=platform_account&correlation_id=a/b',
'INVALID_CORRELATION_ID'
],
[
'/v1/checkout?product=platform&return_to=platform_account&workspace_id=',
'INVALID_WORKSPACE_ID'
]
] as const)('refuses %s with %s', ([url, expected]) => {
expect(parseBillingEntry(url)).toEqual({ status: 'error', code: expected })
})
})
73 changes: 73 additions & 0 deletions packages/billing-contract/src/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* The versioned URL contract every Comfy product uses to hand a customer to
* the hosted billing app, and that the app reads back on arrival.
*
* The entry URL is routing input and nothing more. It never carries a token,
* a JWT, a payment-provider secret, an authoritative price, or a caller-chosen
* return URL: a product states which billing screen it wants, which product is
* asking, and which registered destination the customer should land on
* afterwards. Billing resolves everything else against its own environment and
* the signed-in session, so a workspace id here is a requested scope and never
* an authorization.
*
* Route and parameter names are provisional while the hosted app is behind a
* flag. `BILLING_CONTRACT_VERSION` is what lets them change without stranding
* a product that is already deployed against the older shape.
*
* Whether a payment renders embedded or hosted is not decided here — that is
* the SDK core's `hostedDestination` port. This package stays dependency-free
* and framework-free so a product, the billing app, and a future non-Vue host
* can share exactly one definition of the route surface.
*/
export const BILLING_CONTRACT_VERSION = 'v1'

export const BILLING_INTENTS = [
'pricing',
'checkout',
'subscription',
'payment-methods',
'invoices',
'result'
] as const

export type BillingIntent = (typeof BILLING_INTENTS)[number]

export function isBillingIntent(
value: string | null | undefined
): value is BillingIntent {
return BILLING_INTENTS.some((intent) => intent === value)
}

/** The route an intent occupies, e.g. `/v1/checkout`. */
export function billingIntentPath(intent: BillingIntent): string {
return `/${BILLING_CONTRACT_VERSION}/${intent}`
}

/** The surfaces allowed to originate a billing trip. */
export const BILLING_PRODUCTS = [
'comfyui',
'platform',
'workshop',
'models'
] as const

export type BillingProduct = (typeof BILLING_PRODUCTS)[number]

export function isBillingProduct(
value: string | null | undefined
): value is BillingProduct {
return BILLING_PRODUCTS.some((product) => product === value)
}

/**
* Which deployment family a return destination belongs to. The backends run
* three of them, not two, and pair each origin with exactly one family, so a
* product resolves its return target in the same family its session was
* minted against.
*
* A host picks its family by origin, never by the label on its own build: the
* cloud app's "staging" distribution points at `testcloud.comfy.org`
* (`STAGING_CLOUD_BASE_URL` in `src/config/comfyApi.ts`) and therefore maps to
* `test` here, while `staging` means `stagingcloud.comfy.org`.
*/
export type BillingEnvironment = 'production' | 'staging' | 'test'
36 changes: 36 additions & 0 deletions packages/billing-contract/src/entryFields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* The query shape of an entry URL, in one table so the builder and the parser
* cannot drift apart: a field is written and read under the same name, and a
* value that fails the shared charset reports the same code on both sides.
*/

/** Names are provisional; see `contract.ts`. */
export const ENTRY_PARAM_PRODUCT = 'product'
export const ENTRY_PARAM_RETURN_TO = 'return_to'

export type OptionalEntryKey = 'plan' | 'correlationId' | 'workspaceId'

export type InvalidIdentifierCode =
| 'INVALID_PLAN'
| 'INVALID_CORRELATION_ID'
| 'INVALID_WORKSPACE_ID'

interface OptionalEntryField {
readonly key: OptionalEntryKey
readonly param: string
readonly code: InvalidIdentifierCode
}

export const OPTIONAL_ENTRY_FIELDS: readonly OptionalEntryField[] = [
{ key: 'plan', param: 'plan', code: 'INVALID_PLAN' },
{
key: 'correlationId',
param: 'correlation_id',
code: 'INVALID_CORRELATION_ID'
},
{ key: 'workspaceId', param: 'workspace_id', code: 'INVALID_WORKSPACE_ID' }
]

export type OptionalEntryValues = {
readonly [K in OptionalEntryKey]?: string
}
Loading
Loading