Cotton code
184 lines · 12.5 KB · TypeScriptRaw
import type { Business } from "./business.ts";import type { CallRoom } from "./calling/room.ts";import type { Directory } from "./directory.ts";import type { MailJobMessage } from "./jobs/mail_job.ts";import type { StepJobMessage } from "./jobs/step_job.ts";import type { Published } from "./published.ts";import type { Space } from "./space/space.ts";export interface Env {  BUSINESS: DurableObjectNamespace<Business>;  /** The Worker version answering (wrangler `version_metadata`): its id, tag and upload time. Absent in a test pool that binds none. */  CF_VERSION_METADATA?: { id: string; tag?: string; timestamp?: string };  /** The one directory: which database each phone number owns (src/directory.ts). */  DIRECTORY: DurableObjectNamespace<Directory>;  /** Cotton Calling's signalling rooms, one per call (src/calling/room.ts). */  CALLROOM: DurableObjectNamespace<CallRoom>;  /** The published databases, one object per `<publisher>/<name>` (src/published.ts). */  PUBLISHED: DurableObjectNamespace<Published>;  /** Reads of published databases a signed-in database may make in a minute (src/published.ts `LIMITS`). */  PUBLISHED_READS: RateLimit;  /** The shared spaces, one object per space (src/space/space.ts): sealed records, members, links; its bytes under `spaces/<id>/` in MEDIA. */  SPACES: DurableObjectNamespace<Space>;  /** Secret. A JSON object of publisher → that publisher's write token (src/published.ts): `{"cotton": "<token>"}`. Never a var, never logged. */  PUBLISHER_TOKENS?: string;  PHOTOS: R2Bucket;  /** The personal database's media (cotton-personal-media-apac): `<slug>/<kind>/<name>`, the owner's alone. */  MEDIA: R2Bucket;  /** Cloudflare Email Sending (`send_email`), for mail from the person's own domain (src/mail/send.ts); absent where sending is not set up. */  EMAIL?: SendEmail;  /** The Worker's static assets (wrangler.jsonc `assets`): the kits' fan tile pictures, `/kit-pictures/<sha256>.png` (scripts/kit.ts, src/media.ts `kitPicture`). */  ASSETS: Fetcher;  /** Cloudflare Images: draws a stored image at the size the phone asks. */  IMAGES: ImagesBinding;  /** Secret. Onboarding, import and export: `wrangler secret put ADMIN_TOKEN`. */  ADMIN_TOKEN?: string;  /** Secret. Signs a film's stream URL (src/media.ts `streamToken`): `wrangler secret put STREAM_SIGNING_KEY`. */  STREAM_SIGNING_KEY?: string;  /** `dev` | `test` | `production`. */  ENV_LABEL?: string;  /** `today` | `narrow`: what a new key or connection may do when nothing names it (keyDefault). */  KEY_DEFAULT?: string;  /** `on` lets a person delete their account (src/account_delete.ts `switchedOn`); off by default. */  ACCOUNT_DELETE?: string;  /** Space for photos and files (assets/design/shared/SPACE.md): off (today's cap), count (days and friends counted), on (the rules hold). Absent is off. */  SPACE_RULES?: string;  /** "on": a Circle row written once its sealed copy is held keeps no clear number or name (PEOPLE-DESIGN b.4 step B). Off unless set. */  CIRCLE_SEALED_ONLY?: string;  /** The fake payment provider's webhook secret (src/plans.ts): bound in the tests only, so `/v1/plans/fake` answers nowhere else. */  PLANS_FAKE_SECRET?: string;  /** The directory's per-IP hour limits, raised on the development Worker (src/directory.ts `limitsFor`); production keeps its constants. */  SIGN_IN_ATTEMPTS_PER_HOUR?: string;  /** The commit deployed (the Worker deploy passes it); GET /health says it. */  BUILD_SHA?: string;  /** Sign-in calls from every network together in an hour (src/directory.ts GLOBAL_ATTEMPTS_PER_HOUR); the dev Worker raises it. */  SIGN_IN_ATTEMPTS_GLOBAL_PER_HOUR?: string;  SIGN_UPS_PER_HOUR?: string;  /** Codes one number may ask for in an hour, raised on the development Worker (src/auth.ts `codesPerHour`). */  CODES_PER_HOUR?: string;  /** Country calling codes an SMS code may go to, comma separated (src/auth.ts SMS_COUNTRIES is the default). */  SMS_COUNTRIES?: string;  /** SMS codes sent in a day, every number together, before sending stops (src/directory.ts SMS_PER_DAY is the default). */  SMS_PER_DAY?: string;  /** SMS codes one number may be sent in a day (src/directory.ts SMS_PER_NUMBER_PER_DAY is the default); the test pool raises it. */  SMS_PER_NUMBER_PER_DAY?: string;  /** The one address for every person (`cottondb.com`): the bearer names the object, or the sign-in finds it by number. */  OPEN_HOST?: string;  /** The site Worker (cotton-site), over a service binding: the open host hands it every request that is not the door's own (src/open.ts `forSite`). */  SITE?: Fetcher;  /** `twilio` sends through Twilio Verify (live); `fixed` answers every code with the OTP_FIXED_CODE secret, refused unless ENV_LABEL is `dev` or `test` (src/auth.ts `otpMode`). */  OTP_MODE?: string;  /** Android app origins a Cotton passkey may be used from: `android:apk-key-hash:<b64url sha256 of the signing cert>`, comma-separated (src/passkey.ts). */  PASSKEY_ANDROID_ORIGINS?: string;  /** The open host's /.well-known/assetlinks.json, verbatim: the Cotton apps and their signing certs, so a passkey for this host works in them. */  ANDROID_ASSET_LINKS?: string;  /** The open host's /.well-known/apple-app-site-association, verbatim: `{"webcredentials":{"apps":["<TEAM>.<bundle id>", …]}}` for every Cotton iPhone app, so a passkey for this host works in them. */  APPLE_APP_SITE_ASSOCIATION?: string;  OTP_FIXED_CODE?: string;  /** Secret. The named test numbers, a JSON object of E.164 number → six-digit code: each proves its own code and never reaches Twilio (src/auth.ts `testCodes`). Never a var, never logged. */  OTP_TEST_CODES?: string;  TWILIO_ACCOUNT_SID?: string;  TWILIO_AUTH_TOKEN?: string;  TWILIO_VERIFY_SID?: string;  /** Twilio Messaging Service sid for a plain text with our own words (TRUSTED RECOVERY BUILD 1: the Stop link). Unset: no such text goes, and the request says so. */  TWILIO_MESSAGING_SID?: string;  /** Secret. Seals a saved secret's value at rest, k1 in src/vault.ts (never a service's token: SERVICE_TOKEN_KEY): 32 random bytes, base64url (`wrangler secret put VAULT_KEY`). */  VAULT_KEY?: string;  /** Secret. Seals the refresh tokens the Worker uses itself (Google Calendar, Spotify) at rest, t1 in src/vault.ts: 32 random bytes, base64url (`wrangler secret put SERVICE_TOKEN_KEY`). Without it no service connects or pulls. */  SERVICE_TOKEN_KEY?: string;  /** Secret. Seals an unlock window's keys and its requests' one-use private halves at rest (src/window.ts): 32 random bytes, base64url (`wrangler secret put WINDOW_KEY`). Without it no window opens, and nothing is answered in the clear. */  WINDOW_KEY?: string;  /** `on`: Your Claude's standing grant (src/agent.ts) answers beside the unlock windows. Set on the dev Worker only until proven there; absent, every assistant asks for windows as before. */  AGENT_GRANT?: string;  /** "on": a new personal database is born with the memory lock and the open tables lock on, sealed from its first row (src/business.ts provision). */  LOCKS_AT_BIRTH?: string;  /** SNAPSHOTS 1: "on" takes sealed snapshots of every account not switched off by name (src/snapshots.ts). Unset: off. */  SNAPSHOTS_ALL?: string;  /** The key mailbox passwords given to Cotton are encrypted under (src/mail/fetch.ts): base64url, 32 bytes, a Worker secret used for nothing else. */  MAIL_FETCH_KEY?: string;  /** The background jobs' queue (JOBS OUT 1, src/jobs/): a run of Cotton's fetcher for one mailbox, scheduled by the account object alone. */  JOBS?: Queue<MailJobMessage | StepJobMessage>;  /** Secret. Signs a job's credential (src/jobs/token.ts): only the account object mints one; the job never holds this. */  JOB_TOKEN_KEY?: string;  /** Secret. Google's WEB OAuth client, whose one redirect is `https://<OPEN_HOST>/v1/google/callback` (src/google.ts). */  /**   * Dev and test only: Google's place for every OAuth and Calendar call (the fake Google, apps/fake-google), instead   * of accounts.google.com, oauth2.googleapis.com and www.googleapis.com. Ignored on any other Worker (src/google.ts   * `googleUrls`). With it unset, the real Google: the day the real OAuth client exists it is GOOGLE_CLIENT_ID and   * GOOGLE_CLIENT_SECRET, and this var taken away.   */  GOOGLE_BASE?: string;  GOOGLE_CLIENT_ID?: string;  GOOGLE_CLIENT_SECRET?: string;  /** The owner's Spotify developer app (src/spotify.ts): SPOTIFY_CLIENT_ID a var, SPOTIFY_CLIENT_SECRET a secret; its one redirect is `https://<OPEN_HOST>/v1/spotify/callback`. */  SPOTIFY_CLIENT_ID?: string;  SPOTIFY_CLIENT_SECRET?: string;  /** A development Worker's fake Spotify (`wrangler dev` walks only; refused in production): the accounts host and the API base. */  SPOTIFY_ACCOUNTS_URL?: string;  SPOTIFY_API_URL?: string;  /** `http` on a local `wrangler dev` walk, whose open host serves no TLS; refused in production. */  SPOTIFY_REDIRECT_SCHEME?: string;  /** Secret. Cotton Cinemas' Find a film (src/films.ts): a TMDB v3 API key, `wrangler secret put TMDB_API_KEY`; never logged, never said in an answer. A Worker without it answers find_film not_connected. */  TMDB_API_KEY?: string;  /** Secret. Cotton Calling's APNs auth key, the .p8 file's PKCS#8 PEM (src/calling/apns.ts): `wrangler secret put APNS_KEY`. Without it no phone is rung. */  APNS_KEY?: string;  /** The APNs key's id (10 characters) and the Apple team id: vars beside APNS_KEY. */  APNS_KEY_ID?: string;  APNS_TEAM_ID?: string;  /** A test Worker's fake APNs, standing for both Apple hosts; refused in production. */  APNS_URL?: string;  /** `cloudflare` (the default) or `coturn`: which TURN relay mints a call's credentials (src/calling/turn.ts). */  TURN_PROVIDER?: string;  /** Cloudflare Realtime TURN: the TURN key's id (a var) and its API token (a secret, `wrangler secret put CF_TURN_API_TOKEN`). */  CF_TURN_KEY_ID?: string;  CF_TURN_API_TOKEN?: string;  /** A test Worker's fake Cloudflare TURN; refused in production. */  CF_TURN_URL?: string;  /** Secret. A self-hosted coturn's static-auth-secret (`wrangler secret put COTURN_SECRET`), and its `turn:`/`turns:` URLs, comma-separated (a var). */  COTURN_SECRET?: string;  COTURN_URLS?: string;  /** Cotton Calling's group calls: the Cloudflare Realtime SFU app's id (a var) and secret (`wrangler secret put CF_SFU_APP_SECRET`), src/calling/sfu.ts. Without them a group call carries signalling and no media. */  CF_SFU_APP_ID?: string;  CF_SFU_APP_SECRET?: string;  /** A test Worker's fake SFU; refused in production. */  CF_SFU_URL?: string;  /** Secret. Cotton Calling's Android rings: the Firebase service account's JSON key (`wrangler secret put FCM_SERVICE_ACCOUNT`), src/calling/fcm.ts. */  FCM_SERVICE_ACCOUNT?: string;  /** The Firebase project; the service account's own when unset. */  FCM_PROJECT_ID?: string;  /** A test Worker's fake FCM send host and token endpoint; refused in production. */  FCM_URL?: string;  FCM_TOKEN_URL?: string;  /** STUN URLs, comma-separated; `stun:stun.cloudflare.com:3478` when unset. */  STUN_URLS?: string;  /** Workers AI: the calendar engine embeds an event's words to judge its kind (src/calendar_engine.ts). */  AI?: Ai;  /** `off`: the artwork engine (src/artwork.ts) fetches nothing — the test pool, which has no internet, hands a pass its own fetch. */  ARTWORK?: string;  /** A development object's embedder in AI's place (`wrangler dev` only; refused in production): a local server of the same model. */  KIND_EMBEDDINGS_URL?: string;}export function isProduction(env: Env): boolean {  return env.ENV_LABEL === "production";}/** * The dev Worker or the test pool: the one place that says so (ADMIN KEYS 1b). Live is "production"; a Worker with no * label, or any other, is treated as live: never dev by default. */export function devWorker(env: { ENV_LABEL?: string }): boolean {  return env.ENV_LABEL === "dev" || env.ENV_LABEL === "test";}/** * ADMIN KEYS 3, the one server switch for what a NEW key or OAuth connection may do when nothing names it (the * `KEY_DEFAULT` var): "today" gives it no allow list, every key as it was before allows; "narrow" gives it * key_allow's DEFAULT_ALLOW. Live says "today" until the owner flips it once the phones have the Assistants screen; * dev says "narrow". Unset or anything else: "narrow" on dev and in tests, "today" anywhere else (never narrowed by * accident on live). */export function keyDefault(env: { ENV_LABEL?: string; KEY_DEFAULT?: string }): "today" | "narrow" {  if (env.KEY_DEFAULT === "today" || env.KEY_DEFAULT === "narrow") return env.KEY_DEFAULT;  return devWorker(env) ? "narrow" : "today";}