caelum0x 90f6ba3b78 feat(ship): custom domains for shipped apps on the customer's own Cloudflare account (QY1.5, I6)
- packages/cli/src/domains.mjs: a dependency-free client of Cloudflare's
  public Workers Custom Domains API (zones lookup, PUT/GET/DELETE
  /accounts/{id}/workers/domains, certificate pack state). Cloudflare
  issues the TLS certificate; a domain reads active once it is. The token
  goes in the Authorization header only and is scrubbed from every error.
  Byte copy at apps/console/lib/ship/cf-domains.mjs (drift test).
- railyard domains [add|remove HOSTNAME] [--app]: the console attaches the
  hostname to the app's production Worker with the org's Cloudflare grant
  (the QY1.1 OAuth step; the access token is opened and used inside the
  console only). Without a grant (409 no_cloudflare_grant) the CLI does the
  same with the local login (CLOUDFLARE_API_TOKEN or the wrangler login
  file after `wrangler whoami`) and the console records the outcome. Adding
  an attached hostname keeps it; one on another Worker is refused; remove
  is idempotent; a hostname outside the account's zones is refused plainly.
- Console: migration 0010 railyard.app_domains (one org per hostname,
  lower case, cascade, RLS, no anon); GET/POST/DELETE /api/v1/domains and
  POST /api/v1/domains/record (CLI bearer); states refreshed from
  Cloudflare when the org has a grant. Cloudflare OAuth default scopes add
  zone:read and ssl_certs:write (orgs linked before need to run
  railyard link once more for console-side domains).
- Scribase bridge: new signed route domains {scribase_org_id, app?} ->
  {available, domains:[{app, hostname, state, checked_at, created_at}],
  next} for the Deploy tab; contract fixture v2 adds it (additive).

Tests: CLI 23/23 (new domains.test.mjs 5: grant path with the real console
handlers and a fake Cloudflare, local-login path, wrangler config file,
refusals, idempotent remove, no token printed or sent to the console);
console 147/147 (new lib/ship/domains.test.ts 4: drift, worker naming
equal to the CLI's, attach/detach and cross-org refusal, record and
refresh, bridge listing; 0010 migration on a scratch PG); contract 6/6;
console tsc clean.

Owner-only: one real-domain proof (railyard domains add on a domain in the
owner's Cloudflare account) is not run here; record it at the deploy gate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:45:57 +03:00

667 lines
16 KiB
JSON

{
"contract": "scribase-bridge",
"version": 2,
"package": "Wave P PY1.0 (RYP-E6)",
"note": "Scribase control-api (CA/adapters/railyard/bridge_client.rs) sends these requests; Railyard console (apps/console/app/api/v1/bridge/scribase) answers. Both sides test against this file. Every request is a POST with Standard Webhooks headers (webhook-id, webhook-timestamp, webhook-signature: v1,<base64 HMAC-SHA256 of id.timestamp.body>) under the shared secret RAILYARD_SCRIBASE_BRIDGE_SECRET (whsec_...). Version 2 (P-Y1): webhook-id is msg_<route>_<24 lowercase hex>, so the route is inside the signed content and a signature made for one route is refused on another (wrong_route); every orgs request carries addon.as_of, Scribase's time of that add-on state in epoch milliseconds, and an orgs request older than the stored state changes nothing and answers stale: true. Wave Q QY1.2 adds, without changing anything above: the product_analytics route; analytics_host and analytics_key on the sites answer; first_product_event_at on the insights answer; the proof kind first_product_event; and the analytics_host section (contract 5.3) that Scribase SDKs and the Purchases forwarders (QU1.4) build against. Wave Q QY1.3 adds targeting (null, or {rules:[...]}) to every flags item, and the flags set may carry targeting. Wave Q QY1.5 adds the domains route ({scribase_org_id, app?} -> {available, domains:[{app, hostname, state: pending|active|error, checked_at, created_at}], next}) for the Deploy tab.",
"base_path": "/api/v1/bridge/scribase",
"cases": [
{
"name": "first use creates the org",
"route": "orgs",
"request": {
"scribase_org_id": "org_fixture",
"name": "Fixture Co",
"addon": {
"active": true,
"slots": [
"analytics",
"deploy",
"ai"
],
"renews_at": "2026-10-29T12:00:00Z",
"caps": {
"apps": 5,
"analytics_events_month": 3000000
},
"as_of": 1790683140000
}
},
"status": 200,
"response_keys": [
"addon",
"created",
"org_id"
],
"response_match": {
"created": true,
"addon": {
"active": true,
"slots": [
"ai",
"analytics",
"deploy"
]
}
}
},
{
"name": "second call returns the same org",
"route": "orgs",
"request": {
"scribase_org_id": "org_fixture",
"name": "Fixture Co",
"addon": {
"active": true,
"slots": [
"analytics",
"deploy",
"ai"
],
"renews_at": "2026-10-29T12:00:00Z",
"caps": {
"apps": 5,
"analytics_events_month": 3000000
},
"as_of": 1790683170000
}
},
"status": 200,
"response_keys": [
"addon",
"created",
"org_id"
],
"response_match": {
"created": false
}
},
{
"name": "claim for an entitled slot",
"route": "claims",
"request": {
"scribase_org_id": "org_fixture",
"slot": "deploy"
},
"status": 200,
"response_keys": [
"claim",
"expires_in",
"token_endpoint"
],
"response_match": {
"expires_in": 120
}
},
{
"name": "claim for a slot outside the add-on",
"route": "claims",
"request": {
"scribase_org_id": "org_fixture",
"slot": "flags"
},
"status": 402,
"response_keys": [
"error"
]
},
{
"name": "claim for an unknown org",
"route": "claims",
"request": {
"scribase_org_id": "org_unknown",
"slot": "deploy"
},
"status": 404,
"response_keys": [
"error"
]
},
{
"name": "proof for a slot",
"route": "proof",
"request": {
"scribase_org_id": "org_fixture",
"slot": "analytics"
},
"status": 200,
"response_keys": [
"live",
"next",
"proof",
"slot",
"state"
],
"response_match": {
"slot": "analytics"
}
},
{
"name": "deploys for the Scribase Deploy tab",
"route": "deploys",
"request": {
"scribase_org_id": "org_fixture"
},
"status": 200,
"response_keys": [
"deploys"
]
},
{
"name": "custom domains for the Scribase Deploy tab before they are set up",
"route": "domains",
"request": {
"scribase_org_id": "org_fixture"
},
"status": 200,
"response_keys": [
"available",
"domains",
"next"
],
"response_match": {
"available": false,
"domains": []
}
},
{
"name": "deploy proof before the first deploy",
"route": "proof",
"request": {
"scribase_org_id": "org_fixture",
"slot": "deploy"
},
"status": 200,
"response_keys": [
"live",
"next",
"proof",
"slot",
"state"
],
"response_match": {
"slot": "deploy",
"state": "no_proof_yet",
"live": true
}
},
{
"name": "analytics site for a project environment",
"route": "sites",
"request": {
"scribase_org_id": "org_fixture",
"app": "shop-prod"
},
"status": 200,
"response_keys": [
"analytics_host",
"analytics_key",
"app",
"app_id",
"collector_url",
"script_url",
"site_key"
],
"response_match": {
"app": "shop-prod"
}
},
{
"name": "analytics tab before Insights is configured",
"route": "insights",
"request": {
"scribase_org_id": "org_fixture",
"app": "shop-prod"
},
"status": 200,
"response_keys": [
"available",
"first_product_event_at",
"next"
],
"response_match": {
"available": false
}
},
{
"name": "product analytics tab before the service is connected",
"route": "product_analytics",
"request": {
"scribase_org_id": "org_fixture",
"app": "shop-prod"
},
"status": 200,
"response_keys": [
"available",
"next"
],
"response_match": {
"available": false
}
},
{
"name": "ai gateway for a project environment (the key goes to the control plane only)",
"route": "ai",
"request": {
"scribase_org_id": "org_fixture",
"project": "shop-prod"
},
"status": 200,
"response_keys": [
"connect_url",
"gateway_url",
"key",
"providers"
]
},
{
"name": "ai tab usage",
"route": "ai_usage",
"request": {
"scribase_org_id": "org_fixture",
"project": "shop-prod"
},
"status": 200,
"response_keys": [
"keys",
"providers",
"traces"
]
},
{
"name": "the add-on lists flags",
"route": "orgs",
"request": {
"scribase_org_id": "org_fixture",
"name": "Fixture Co",
"addon": {
"active": true,
"slots": [
"analytics",
"deploy",
"ai",
"flags",
"uptime"
],
"renews_at": "2026-10-29T12:00:00Z",
"caps": {
"apps": 5,
"analytics_events_month": 3000000
},
"as_of": 1790683175000
}
},
"status": 200,
"response_keys": [
"addon",
"created",
"org_id"
],
"response_match": {
"addon": {
"slots": [
"ai",
"analytics",
"deploy",
"flags",
"uptime"
]
}
}
},
{
"name": "flags for a project environment (created on first use)",
"route": "flags",
"request": {
"scribase_org_id": "org_fixture",
"app": "shop",
"env": "production"
},
"status": 200,
"response_keys": [
"app",
"client_key",
"created",
"env",
"first_read_at",
"flags",
"flags_url"
],
"response_match": {
"app": "shop",
"env": "production",
"created": true,
"flags": []
}
},
{
"name": "a flag change for a branch that starts from production",
"route": "flags",
"request": {
"scribase_org_id": "org_fixture",
"app": "shop",
"env": "feature-x",
"copy_from": "production",
"set": {
"key": "new_checkout",
"enabled": true,
"value": "blue"
}
},
"status": 200,
"response_keys": [
"app",
"client_key",
"created",
"env",
"first_read_at",
"flags",
"flags_url"
],
"response_match": {
"env": "feature-x",
"created": true
},
"flags_match": [
{
"key": "new_checkout",
"enabled": true,
"value": "blue"
}
]
},
{
"name": "uptime check for a project's address",
"route": "uptime",
"request": {
"scribase_org_id": "org_fixture",
"app": "shop",
"url": "https://shop.example.com",
"name": "Shop",
"owner_email": "owner@example.com"
},
"status": 200,
"response_keys": [
"app",
"checks",
"created"
],
"response_match": {
"app": "shop",
"created": true
}
},
{
"name": "lapsed add-on",
"route": "orgs",
"request": {
"scribase_org_id": "org_fixture",
"name": "Fixture Co",
"addon": {
"active": false,
"slots": [
"analytics",
"deploy",
"ai"
],
"renews_at": null,
"caps": {
"apps": 5,
"analytics_events_month": 3000000
},
"as_of": 1790683190000
}
},
"status": 200,
"response_keys": [
"addon",
"created",
"org_id"
],
"response_match": {
"created": false,
"addon": {
"active": false
}
}
},
{
"name": "a late activation older than the lapse changes nothing",
"route": "orgs",
"request": {
"scribase_org_id": "org_fixture",
"name": "Fixture Co",
"addon": {
"active": true,
"slots": [
"analytics",
"deploy",
"ai"
],
"renews_at": "2026-10-29T12:00:00Z",
"caps": {
"apps": 5,
"analytics_events_month": 3000000
},
"as_of": 1790683180000
}
},
"status": 200,
"response_keys": [
"addon",
"created",
"org_id",
"stale"
],
"response_match": {
"stale": true,
"addon": {
"active": false
}
}
},
{
"name": "claim after the add-on lapsed",
"route": "claims",
"request": {
"scribase_org_id": "org_fixture",
"slot": "deploy"
},
"status": 402,
"response_keys": [
"error"
]
},
{
"name": "flags after the add-on lapsed",
"route": "flags",
"request": {
"scribase_org_id": "org_fixture",
"app": "shop",
"env": "production"
},
"status": 402,
"response_keys": [
"error"
]
},
{
"name": "uptime after the add-on lapsed",
"route": "uptime",
"request": {
"scribase_org_id": "org_fixture",
"app": "shop"
},
"status": 402,
"response_keys": [
"error"
]
}
],
"unsigned": {
"status": 401,
"response_keys": [
"error"
]
},
"replayed_id": {
"status": 409,
"response_keys": [
"error"
]
},
"slot_state": {
"states": [
"live",
"arriving",
"not_entitled",
"no_proof_yet"
],
"proof": {
"kind": "string",
"id": "string",
"at": "RFC 3339 string"
}
},
"signature_vector": {
"secret": "whsec_Y29udHJhY3QtZml4dHVyZS1zZWNyZXQtMzItYnl0ZXM=",
"id": "msg_proof_0123456789abcdef01234567",
"timestamp": "1790683200",
"body": "{\"scribase_org_id\":\"org_fixture\",\"slot\":\"deploy\"}",
"signature": "v1,6uDtRSMzCTvrYX+R1jcSoP6UWaxQ3cljO9tF0cIFoS8="
},
"insights_answer_keys": [
"app",
"available",
"cap_note",
"events",
"events_limit",
"first_event_at",
"first_product_event_at",
"funnel",
"month_events",
"visitors"
],
"id_format": "^msg_(orgs|claims|proof|deploys|sites|insights|ai|ai_usage|flags|uptime|product_analytics|domains)_[0-9a-f]{24}$",
"wrong_route": {
"status": 401,
"response_keys": [
"error"
],
"note": "a request signed with an id for proof, delivered to claims"
},
"flags_answer_keys": [
"app",
"client_key",
"created",
"env",
"first_read_at",
"flags",
"flags_url"
],
"flags_item_keys": [
"description",
"enabled",
"key",
"targeting",
"updated_at",
"value"
],
"uptime_check_keys": [
"app",
"first_result_at",
"id",
"last_checked_at",
"name",
"paused",
"state",
"status_url",
"url"
],
"product_analytics_answer_keys": [
"app",
"available",
"first_event",
"host",
"key",
"web"
],
"product_analytics_server_key": {
"request_field": "with_server_key",
"answer_field": "server_key",
"format": "^ry_pak_[0-9a-f]{64}$",
"note": "Only when the request sets with_server_key: true; only the Scribase control plane receives it and keeps it as the environment secret ANALYTICS_SERVER_KEY. Never written to an env file, printed or logged."
},
"web_keys": [
"range",
"totals",
"series",
"pages",
"referrers",
"utm",
"vitals"
],
"analytics_host": {
"package": "Wave Q QY1.2 (contract 5.3)",
"env": {
"host": [
"NEXT_PUBLIC_ANALYTICS_HOST",
"EXPO_PUBLIC_ANALYTICS_HOST",
"VITE_ANALYTICS_HOST"
],
"key": [
"NEXT_PUBLIC_ANALYTICS_KEY",
"EXPO_PUBLIC_ANALYTICS_KEY",
"VITE_ANALYTICS_KEY"
],
"site": [
"NEXT_PUBLIC_ANALYTICS_SITE",
"EXPO_PUBLIC_ANALYTICS_SITE",
"VITE_ANALYTICS_SITE"
],
"server_secret": "ANALYTICS_SERVER_KEY"
},
"sites_fields": {
"analytics_host": "public base URL of product analytics, or null while it is not connected",
"analytics_key": "the project's public key; equal to site_key"
},
"capture": {
"public": [
"/capture/",
"/batch/",
"/e/",
"/i/v0/e/"
],
"protocol": "the open product analytics capture protocol: {api_key, event, distinct_id, properties, uuid?, timestamp?} or {api_key, batch:[...]}",
"key_field": "api_key"
},
"server_capture": {
"path": "/v1/server/{app_id}/batch",
"auth": "Authorization: Bearer ry_pak_<64 hex>",
"body": "{batch:[{event, distinct_id, properties, uuid?, timestamp?}]} without api_key",
"answers": {
"200": "{status:1, received}",
"401": "wrong key for this app",
"400": "invalid batch or a payload naming another project's key"
}
},
"proof": {
"kind": "first_product_event",
"id": "the app id",
"at": "RFC 3339"
},
"app_id_of_scribase_project": "the sites answer's app_id for app <project>-<environment>"
},
"domains_answer_keys": [
"available",
"domains",
"next"
],
"domain_item_keys": [
"app",
"checked_at",
"created_at",
"hostname",
"state"
]
}