|
| 1 | +--- |
| 2 | +main_commit: 76b946025 |
| 3 | +analyzed_date: 2026-02-16 |
| 4 | +key_files: |
| 5 | + - src/publish/provider-types.ts |
| 6 | + - src/publish/provider.ts |
| 7 | + - src/publish/publish.ts |
| 8 | + - src/publish/config.ts |
| 9 | + - src/publish/common/publish.ts |
| 10 | + - src/publish/common/bundle.ts |
| 11 | + - src/publish/common/account.ts |
| 12 | + - src/publish/types.ts |
| 13 | + - src/command/publish/cmd.ts |
| 14 | + - src/publish/posit-connect-cloud/posit-connect-cloud.ts |
| 15 | + - src/publish/posit-connect-cloud/api/index.ts |
| 16 | + - src/publish/posit-connect-cloud/api/types.ts |
| 17 | +--- |
| 18 | + |
| 19 | +# Quarto Publishing Architecture |
| 20 | + |
| 21 | +How `quarto publish` works internally. Covers the provider interface, publish patterns, account management, and the end-to-end publish flow. |
| 22 | + |
| 23 | +## Provider Interface |
| 24 | + |
| 25 | +File: `src/publish/provider-types.ts` |
| 26 | + |
| 27 | +Every publish target implements `PublishProvider`: |
| 28 | + |
| 29 | +```typescript |
| 30 | +export interface PublishProvider { |
| 31 | + name: string; // e.g. "netlify", "rsconnect", "quarto-pub" |
| 32 | + description: string; // Human-readable name |
| 33 | + requiresServer: boolean; // true if user provides server URL (e.g. rsconnect) |
| 34 | + hidden?: boolean; // Hide from provider selection |
| 35 | + listOriginOnly?: boolean; // Only list in origin project |
| 36 | + |
| 37 | + accountTokens(): Promise<AccountToken[]>; |
| 38 | + removeToken(token: AccountToken): void; |
| 39 | + authorizeToken(options: PublishOptions, target?: PublishRecord): Promise<AccountToken | undefined>; |
| 40 | + resolveTarget(account: AccountToken, target: PublishRecord): Promise<PublishRecord | undefined>; |
| 41 | + publish( |
| 42 | + account: AccountToken, type: "document" | "site", input: string, |
| 43 | + title: string, slug: string, |
| 44 | + render: (flags?: RenderFlags) => Promise<PublishFiles>, |
| 45 | + options: PublishOptions, target?: PublishRecord |
| 46 | + ): Promise<[PublishRecord | undefined, URL | undefined]>; |
| 47 | + isUnauthorized(error: Error): boolean; |
| 48 | + isNotFound(error: Error): boolean; |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Key types: |
| 53 | + |
| 54 | +- `AccountToken` — stored credential with `name`, `server?`, `token` (generic string or structured object) |
| 55 | +- `AccountTokenType` — `"authorized"` (user-stored) or `"environment"` (env var) |
| 56 | +- `PublishRecord` — entry in `_publish.yml` with `id`, `url`, `code?` |
| 57 | +- `PublishFiles` — `{ baseDir: string, rootFile: string, files: string[], metafiles?: string[] }` |
| 58 | + |
| 59 | +## Provider Registration |
| 60 | + |
| 61 | +File: `src/publish/provider.ts` |
| 62 | + |
| 63 | +Providers are imported and added to `kPublishProviders`: |
| 64 | + |
| 65 | +```typescript |
| 66 | +const kPublishProviders = [ |
| 67 | + quartoPubProvider, |
| 68 | + ghpagesProvider, |
| 69 | + rsconnectProvider, |
| 70 | + netlifyProvider, |
| 71 | + positConnectCloudProvider, |
| 72 | + confluenceProvider, |
| 73 | + huggingfaceProvider, |
| 74 | +]; |
| 75 | +``` |
| 76 | + |
| 77 | +Discovery functions: `publishProviders()`, `findProvider(name)`. |
| 78 | + |
| 79 | +There is also a deprecation warning for the old `posit-cloud` provider (removed in `142a8791f`) that now suggests using `posit-connect-cloud` as an alternative. |
| 80 | + |
| 81 | +## Two Publish Patterns |
| 82 | + |
| 83 | +### Pattern A: File-by-file upload |
| 84 | + |
| 85 | +Used by: `quarto-pub`, `netlify` |
| 86 | + |
| 87 | +File: `src/publish/common/publish.ts` |
| 88 | + |
| 89 | +Uses `handlePublish()` which: |
| 90 | +1. SHA-1 hashes each file in the rendered output |
| 91 | +2. Creates a deploy with a file manifest (listing all files + checksums) |
| 92 | +3. Server responds with which files it needs (doesn't already have) |
| 93 | +4. Uploads only changed files individually |
| 94 | +5. Activates the deploy |
| 95 | + |
| 96 | +Providers using this pattern implement `PublishHandler`: |
| 97 | + |
| 98 | +```typescript |
| 99 | +export interface PublishHandler { |
| 100 | + name: string; |
| 101 | + createSite(type, title, slug, account): Promise<[string, string]>; |
| 102 | + createDeploy(siteId, account, files): Promise<PublishDeploy>; |
| 103 | + getDeploy(deployId, account): Promise<PublishDeploy>; |
| 104 | + uploadDeployFile(deployId, path, fileBody, account): Promise<void>; |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Pattern B: Bundle upload |
| 109 | + |
| 110 | +Used by: `rsconnect` (Posit Connect), `posit-connect-cloud` (Posit Connect Cloud) |
| 111 | + |
| 112 | +File: `src/publish/common/bundle.ts` |
| 113 | + |
| 114 | +Uses `createBundle()` which: |
| 115 | +1. Creates a `manifest.json` with file checksums, `appmode`, `content_category`, etc. |
| 116 | +2. Packages all files + manifest into a `.tar.gz` archive |
| 117 | +3. Returns `{ bundlePath: string, manifest: object }` |
| 118 | + |
| 119 | +The provider then uploads the entire bundle as a single blob and triggers deployment. Each provider using this pattern has its own publish logic (not via `handlePublish()`). |
| 120 | + |
| 121 | +`createBundle()` signature: |
| 122 | +```typescript |
| 123 | +function createBundle( |
| 124 | + type: "document" | "site", |
| 125 | + files: PublishFiles, |
| 126 | + tempContext: TempContext |
| 127 | +): Promise<{ bundlePath: string; manifest: Record<string, unknown> }> |
| 128 | +``` |
| 129 | + |
| 130 | +## End-to-End Publish Flow |
| 131 | + |
| 132 | +### 1. CLI Entry |
| 133 | + |
| 134 | +File: `src/command/publish/cmd.ts` |
| 135 | + |
| 136 | +`quarto publish [provider] [path]` invokes `publishAction()`. |
| 137 | + |
| 138 | +### 2. Resolve Deployment Target |
| 139 | + |
| 140 | +File: `src/publish/config.ts` |
| 141 | + |
| 142 | +Reads `_publish.yml` to find existing publish targets. Format: |
| 143 | + |
| 144 | +```yaml |
| 145 | +- source: document.qmd |
| 146 | + provider-name: |
| 147 | + - id: site-or-content-id |
| 148 | + url: https://published-url.example.com |
| 149 | +``` |
| 150 | + |
| 151 | +### 3. Select Provider |
| 152 | + |
| 153 | +If not specified on CLI, user is prompted to choose from available providers. |
| 154 | + |
| 155 | +### 4. Resolve Account |
| 156 | + |
| 157 | +File: `src/publish/common/account.ts` |
| 158 | + |
| 159 | +Account resolution order: |
| 160 | +1. Environment variable tokens (via provider's `accountTokens()`) |
| 161 | +2. Stored tokens in `~/.quarto/publish/accounts/{provider}/accounts.json` |
| 162 | +3. Interactive authorization (via provider's `authorizeToken()`) |
| 163 | + |
| 164 | +Token storage functions: |
| 165 | +- `readAccessTokens<T>(provider)` — reads stored tokens |
| 166 | +- `writeAccessToken<T>(provider, token, compareFn)` — writes/updates token |
| 167 | +- `readAccessTokenFile(provider)` — raw file path |
| 168 | + |
| 169 | +### 5. Publish |
| 170 | + |
| 171 | +File: `src/publish/publish.ts` |
| 172 | + |
| 173 | +`publishSite()` or `publishDocument()` coordinates: |
| 174 | +1. Calls provider's `publish()`, passing a `render` callback |
| 175 | +2. Provider calls `renderForPublish()` (or `render()` directly for sites) |
| 176 | +3. Provider uploads and deploys |
| 177 | +4. Returns `[PublishRecord, URL]` |
| 178 | + |
| 179 | +**Important:** Providers publishing documents should call `renderForPublish()` instead of `render()` directly. `renderForPublish()` wraps `render()` and stages the output: for HTML documents it copies `document.html` → `index.html`, for PDFs it creates a pdf.js viewer wrapper as `index.html`. Without this staging, the primary file name won't match `index.html` and bundle-based providers will fail. |
| 180 | + |
| 181 | +### 6. Update `_publish.yml` |
| 182 | + |
| 183 | +File: `src/publish/config.ts` |
| 184 | + |
| 185 | +The returned `PublishRecord` is written back to `_publish.yml` for future republishing. |
| 186 | + |
| 187 | +## Account / Token Management |
| 188 | + |
| 189 | +File: `src/publish/common/account.ts` |
| 190 | + |
| 191 | +### Storage |
| 192 | + |
| 193 | +Tokens stored at: `~/.quarto/publish/accounts/{provider}/accounts.json` |
| 194 | + |
| 195 | +Format: JSON array of `AccountToken` objects. The `token` field is provider-specific (can be a string or structured object). |
| 196 | + |
| 197 | +### Authorization Patterns |
| 198 | + |
| 199 | +**Ticket-based auth** (quarto-pub): `authorizeAccessToken()` in `src/publish/common/account.ts` |
| 200 | +- Opens browser to auth URL |
| 201 | +- Polls a ticket endpoint until user completes auth |
| 202 | +- Exchanges ticket for access token |
| 203 | + |
| 204 | +**API key auth** (rsconnect): User provides API key directly or via env var. |
| 205 | + |
| 206 | +**OAuth Device Code** (posit-connect-cloud): Uses RFC 8628 Device Code flow. See `src/publish/posit-connect-cloud/api/index.ts` for implementation. |
| 207 | + |
| 208 | +### Environment Variables |
| 209 | + |
| 210 | +Each provider can check for env vars in `accountTokens()`. Convention: |
| 211 | +- `QUARTO_PUB_AUTH_TOKEN` |
| 212 | +- `CONNECT_SERVER` + `CONNECT_API_KEY` (rsconnect) |
| 213 | +- `NETLIFY_AUTH_TOKEN` |
| 214 | +- `POSIT_CONNECT_CLOUD_ACCESS_TOKEN` + `POSIT_CONNECT_CLOUD_REFRESH_TOKEN` + `POSIT_CONNECT_CLOUD_ACCOUNT_ID` (posit-connect-cloud) |
| 215 | + |
| 216 | +## Existing Providers Summary |
| 217 | + |
| 218 | +| Provider | Pattern | Auth | `requiresServer` | |
| 219 | +|----------|---------|------|-------------------| |
| 220 | +| `quarto-pub` | A (file-by-file) | Ticket-based OAuth | false | |
| 221 | +| `netlify` | A (file-by-file) | API key / env var | false | |
| 222 | +| `rsconnect` | B (bundle) | API key (`Key <key>`) | true | |
| 223 | +| `ghpages` | Custom (git push) | Git credentials | false | |
| 224 | +| `confluence` | Custom | API token | true | |
| 225 | +| `posit-connect-cloud` | B (bundle) | OAuth Device Code (RFC 8628) | false | |
| 226 | +| `huggingface` | Custom | HF token | false | |
| 227 | + |
| 228 | +## Reusable Utilities |
| 229 | + |
| 230 | +| Utility | File | Purpose | |
| 231 | +|---------|------|---------| |
| 232 | +| `createBundle()` | `src/publish/common/bundle.ts` | tar.gz bundle with manifest.json | |
| 233 | +| `readAccessTokens<T>()` | `src/publish/common/account.ts` | Read stored tokens | |
| 234 | +| `writeAccessToken<T>()` | `src/publish/common/account.ts` | Write/update token | |
| 235 | +| `authorizeAccessToken()` | `src/publish/common/account.ts` | Ticket-based auth flow | |
| 236 | +| `renderForPublish()` | `src/publish/common/publish.ts` | Render + stage documents (HTML→index.html, PDF→pdf.js wrapper) | |
| 237 | +| `handlePublish()` | `src/publish/common/publish.ts` | File-by-file upload orchestration | |
| 238 | +| `withSpinner()` | `src/core/console.ts` | Progress spinner display | |
| 239 | +| `completeMessage()` | `src/core/console.ts` | Success/failure messages | |
| 240 | +| `createTempContext()` | `src/core/temp.ts` | Temp file management | |
| 241 | +| `openUrl()` | `src/core/shell.ts` | Open URL in browser | |
| 242 | +| `isServerSession()` | `src/core/platform.ts` | Detect headless/CI environment | |
| 243 | +| `ApiError` | `src/publish/types.ts` | HTTP error type with status code | |
0 commit comments