Personal Telegram developer console for GitHub
feat/studio-g0-foundation
audit/commit-drilldown
audit/finalize-workspace-menu-security
audit/ops-actionability
audit/pr-menu-consolidation
audit/status-menu-consolidation
audit/telegram-menu-e2e
audit/telegram-menu-e2e-wave2
chore/actions-node24-runtime
cleanup/remove-diag
design/cloudflare-ops-read
docs/control-plane-2-7-design
docs/guide-v2-1-sync
docs/menu-audit-final-sync
docs/phase2-acceptance-closeout
docs/phase2-multifile-design
feat/bootstrap-worker
feat/cloudflare-ops-read
feat/cp2-multifile-final
feat/cp2-multifile-workspace
feat/cp3-github-write-final
feat/cp4-cloudflare-ops-final
feat/cp5-cicd-control-plane
feat/cp6-incident-manager
feat/cp7-automation-engine
feat/deep-file-browser
feat/final-delivery-16-20
feat/github-advanced-read
feat/in-app-guide-v2
feat/navigation-recovery-ux-v2
feat/ops-console-v2-1
feat/persistent-menu-code-input
feat/phase2-multifile-workspace
feat/phase2-multifile-workspace-v2
feat/plugin-foundation-v1
feat/safe-github-ops
feat/self-register-webhook
feat/studio-g0-foundation
feat/telegram-main-inline-menu
feat/telegram-pr-merge
feat/ux-platform-8-12
feat/v3a-readonly-copilot
feat/workspace-file-upload
feature/mobile-repo-browser
feature/multi-file-change-set
fix/approval-expiry-feedback
fix/callback-resilience-and-docs
fix/ci-actions-pagination
fix/cloudflare-fetch-binding
fix/editor-request-body-cap
fix/editor-safe-file-options
fix/issues-permission-release-empty-ux
fix/main-menu-callback-fallback
fix/phase2-file-editor-selection
fix/phase2-final-ux
fix/phase2-workspace-ref-persistence
fix/production-auto-deploy
fix/provider-write-error-feedback
fix/repository-id-navigation-fallback
fix/repository-pr-navigation
fix/telegram-callback-updates
fix/telegram-runtime-diagnostics
fix/workspace-editor-flow-v2
fix/workspace-recovery-validation
hardening/ci-zero-test-telegram-token
hardening/pr1-destructive-totp
hardening/pr2-kill-switch
main
multifile-workspace
nexa/edit-BwJEIIY3PW
nexa/edit-IPHT8irO7b
nexa/edit-b4Od3BIMYd
p2-multifile
phase2-20261005
phase2-20261005-a
phase2-20261005-b
phase2-20261005-c
phase2-20261005-d
phase2-20261005-e
phase2-20261005-f
phase2-20261005-g
phase2-20261005-h
phase2-20261005-i
phase2-20261005-j
phase2-20261005-k
phase2-20261005-l
phase2-20261005-m
phase2-20261005-n
phase2-20261005-o
phase2-20261005-p
phase2-20261005-q
phase2-20261005-r
phase2-20261005-s
phase2-20261005-t
phase2-20261005-u
phase2-20261005-v
phase2-20261005-w
phase2-20261005-x
phase2-20261005-y
phase2-20261005-z
phase2-acceptance-closeout
phase2-add-file-hotfix
phase2-change-set
phase2-design-20261005
phase2-design-final
phase2-implementation
phase2-multifile-implementation
phase2-multifile-spec
phase2-spec-final
phase2-workspace
phase2/multifile-workspace
plan/nexa-studio-v2-reviewed
security/remove-telegram-setup
spec/repository-workspace-editor
v3a/multifile
work/multifile
| 1 | const API_BASE = 'https://api.cloudflare.com/client/v4'; |
| 2 | |
| 3 | const clean = (value) => String(value ?? '').trim(); |
| 4 | const safe = (value) => encodeURIComponent(clean(value)); |
| 5 | |
| 6 | function redact(message, token) { |
| 7 | const text = String(message || 'Cloudflare request failed'); |
| 8 | return token ? text.split(token).join('[REDACTED]') : text; |
| 9 | } |
| 10 | |
| 11 | function firstError(payload) { |
| 12 | const errors = Array.isArray(payload?.errors) ? payload.errors : []; |
| 13 | return errors |
| 14 | .map((item) => item?.message) |
| 15 | .filter(Boolean) |
| 16 | .join('; '); |
| 17 | } |
| 18 | |
| 19 | export class CloudflareService { |
| 20 | constructor({ |
| 21 | token = '', |
| 22 | accountId = '', |
| 23 | workerName = '', |
| 24 | fetchImpl = globalThis.fetch |
| 25 | } = {}) { |
| 26 | this.token = clean(token); |
| 27 | this.accountId = clean(accountId); |
| 28 | this.workerName = clean(workerName); |
| 29 | this.fetchImpl = fetchImpl; |
| 30 | } |
| 31 | |
| 32 | isConfigured() { |
| 33 | return Boolean( |
| 34 | this.token && |
| 35 | this.accountId && |
| 36 | this.workerName && |
| 37 | typeof this.fetchImpl === 'function' |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | requireConfigured() { |
| 42 | if (!this.isConfigured()) |
| 43 | throw new Error('Cloudflare provider is not configured'); |
| 44 | } |
| 45 | |
| 46 | get headers() { |
| 47 | return { |
| 48 | Authorization: `Bearer ${this.token}`, |
| 49 | 'Content-Type': 'application/json' |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | endpoint(suffix) { |
| 54 | return `${API_BASE}/accounts/${safe(this.accountId)}/workers/scripts/${safe(this.workerName)}${suffix}`; |
| 55 | } |
| 56 | |
| 57 | async requestJson(suffix, label, { method = 'GET', body } = {}) { |
| 58 | this.requireConfigured(); |
| 59 | let response; |
| 60 | try { |
| 61 | const options = { method, headers: this.headers }; |
| 62 | if (body !== undefined) options.body = JSON.stringify(body); |
| 63 | response = await this.fetchImpl(this.endpoint(suffix), options); |
| 64 | } catch (error) { |
| 65 | throw new Error( |
| 66 | redact(`${label} failed: ${error?.message || error}`, this.token) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | let raw = ''; |
| 71 | try { |
| 72 | raw = await response.text(); |
| 73 | } catch { |
| 74 | raw = ''; |
| 75 | } |
| 76 | |
| 77 | let payload = null; |
| 78 | if (raw) { |
| 79 | try { |
| 80 | payload = JSON.parse(raw); |
| 81 | } catch { |
| 82 | payload = null; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (!response.ok || payload?.success === false) { |
| 87 | const detail = |
| 88 | firstError(payload) || raw.slice(0, 240) || 'request failed'; |
| 89 | throw new Error( |
| 90 | redact(`${label} failed: ${response.status} ${detail}`, this.token) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | if (payload === null) |
| 95 | throw new Error(`${label} failed: invalid JSON response`); |
| 96 | return payload?.result ?? payload; |
| 97 | } |
| 98 | |
| 99 | async listVersions() { |
| 100 | const result = await this.requestJson('/versions', 'Cloudflare versions'); |
| 101 | const items = Array.isArray(result?.items) |
| 102 | ? result.items |
| 103 | : Array.isArray(result) |
| 104 | ? result |
| 105 | : []; |
| 106 | return items |
| 107 | .slice(0, 20) |
| 108 | .map((item) => ({ |
| 109 | id: clean(item?.id), |
| 110 | createdOn: clean(item?.created_on), |
| 111 | triggeredBy: clean(item?.annotations?.['workers/triggered_by']) |
| 112 | })) |
| 113 | .filter((item) => item.id); |
| 114 | } |
| 115 | |
| 116 | async listDeployments() { |
| 117 | const result = await this.requestJson( |
| 118 | '/deployments', |
| 119 | 'Cloudflare deployments' |
| 120 | ); |
| 121 | const items = Array.isArray(result?.deployments) |
| 122 | ? result.deployments |
| 123 | : Array.isArray(result) |
| 124 | ? result |
| 125 | : []; |
| 126 | return items.slice(0, 20).map((item) => ({ |
| 127 | id: clean(item?.id), |
| 128 | createdOn: clean(item?.created_on), |
| 129 | versions: (Array.isArray(item?.versions) ? item.versions : []).map( |
| 130 | (version) => ({ |
| 131 | versionId: clean(version?.version_id), |
| 132 | percentage: Number.isFinite(Number(version?.percentage)) |
| 133 | ? Number(version.percentage) |
| 134 | : 0 |
| 135 | }) |
| 136 | ) |
| 137 | })); |
| 138 | } |
| 139 | |
| 140 | async deployVersion(versionId) { |
| 141 | const target = clean(versionId); |
| 142 | if (!/^[0-9a-f-]{36}$/i.test(target)) |
| 143 | throw new Error('Cloudflare version ID is invalid'); |
| 144 | const versions = await this.listVersions(); |
| 145 | if (!versions.some((version) => version.id === target)) |
| 146 | throw new Error('Cloudflare version is not known'); |
| 147 | await this.requestJson('/deployments', 'Cloudflare deploy version', { |
| 148 | method: 'POST', |
| 149 | body: { |
| 150 | strategy: 'percentage', |
| 151 | versions: [{ percentage: 100, version_id: target }] |
| 152 | } |
| 153 | }); |
| 154 | const snapshot = await this.getWorkerSnapshot(); |
| 155 | const active = snapshot.deployment?.versions || []; |
| 156 | if ( |
| 157 | active.length !== 1 || |
| 158 | active[0].versionId !== target || |
| 159 | Number(active[0].percentage) !== 100 |
| 160 | ) { |
| 161 | throw new Error('Cloudflare deployment verification failed'); |
| 162 | } |
| 163 | return { |
| 164 | versionId: target, |
| 165 | deploymentId: snapshot.deployment?.id || '', |
| 166 | verified: true |
| 167 | }; |
| 168 | } |
| 169 | |
| 170 | async getWorkerSnapshot() { |
| 171 | this.requireConfigured(); |
| 172 | const [deploymentsResult, versionsResult, settingsResult] = |
| 173 | await Promise.all([ |
| 174 | this.requestJson('/deployments', 'Cloudflare deployments'), |
| 175 | this.requestJson('/versions', 'Cloudflare versions'), |
| 176 | this.requestJson('/settings', 'Cloudflare settings') |
| 177 | ]); |
| 178 | |
| 179 | const deployments = Array.isArray(deploymentsResult?.deployments) |
| 180 | ? deploymentsResult.deployments |
| 181 | : Array.isArray(deploymentsResult) |
| 182 | ? deploymentsResult |
| 183 | : []; |
| 184 | const versions = Array.isArray(versionsResult?.items) |
| 185 | ? versionsResult.items |
| 186 | : Array.isArray(versionsResult) |
| 187 | ? versionsResult |
| 188 | : []; |
| 189 | |
| 190 | const latestDeployment = deployments[0] || null; |
| 191 | const traffic = Array.isArray(latestDeployment?.versions) |
| 192 | ? latestDeployment.versions |
| 193 | : []; |
| 194 | const activeVersionId = |
| 195 | traffic.length === 1 && Number(traffic[0]?.percentage) === 100 |
| 196 | ? clean(traffic[0]?.version_id) |
| 197 | : ''; |
| 198 | let activeVersion = activeVersionId |
| 199 | ? versions.find((version) => clean(version?.id) === activeVersionId) |
| 200 | : null; |
| 201 | // The versions list can omit annotations or the deployed version itself. |
| 202 | // Read that exact version's detail; an unrelated latest upload is no proof. |
| 203 | if ( |
| 204 | activeVersionId && |
| 205 | activeVersion?.annotations?.['workers/commit_sha'] === undefined && |
| 206 | activeVersion?.annotations?.['workers/tag'] === undefined |
| 207 | ) { |
| 208 | const detail = await this.requestJson( |
| 209 | `/versions/${safe(activeVersionId)}`, |
| 210 | 'Cloudflare active version' |
| 211 | ); |
| 212 | activeVersion = clean(detail?.id) === activeVersionId ? detail : null; |
| 213 | } |
| 214 | const bindings = Array.isArray(settingsResult?.bindings) |
| 215 | ? settingsResult.bindings |
| 216 | .map((binding) => ({ |
| 217 | name: clean(binding?.name), |
| 218 | type: clean(binding?.type) |
| 219 | })) |
| 220 | .filter((binding) => binding.name || binding.type) |
| 221 | : []; |
| 222 | const observability = settingsResult?.observability || {}; |
| 223 | |
| 224 | return { |
| 225 | workerName: this.workerName, |
| 226 | deployment: latestDeployment |
| 227 | ? { |
| 228 | id: clean(latestDeployment.id), |
| 229 | createdOn: clean(latestDeployment.created_on), |
| 230 | versions: (Array.isArray(latestDeployment.versions) |
| 231 | ? latestDeployment.versions |
| 232 | : [] |
| 233 | ).map((item) => ({ |
| 234 | versionId: clean(item?.version_id), |
| 235 | percentage: Number.isFinite(Number(item?.percentage)) |
| 236 | ? Number(item.percentage) |
| 237 | : 0 |
| 238 | })) |
| 239 | } |
| 240 | : null, |
| 241 | version: activeVersion |
| 242 | ? { |
| 243 | id: clean(activeVersion.id), |
| 244 | createdOn: clean( |
| 245 | activeVersion.metadata?.created_on || activeVersion.created_on |
| 246 | ), |
| 247 | triggeredBy: clean( |
| 248 | activeVersion.annotations?.['workers/triggered_by'] |
| 249 | ), |
| 250 | annotations: Object.fromEntries( |
| 251 | ['workers/commit_sha', 'workers/tag'] |
| 252 | .filter( |
| 253 | (key) => typeof activeVersion.annotations?.[key] === 'string' |
| 254 | ) |
| 255 | .map((key) => [key, activeVersion.annotations[key]]) |
| 256 | ) |
| 257 | } |
| 258 | : null, |
| 259 | bindings, |
| 260 | observability: { |
| 261 | enabled: observability.enabled === true, |
| 262 | logsEnabled: observability.logs?.enabled === true, |
| 263 | invocationLogs: observability.logs?.invocation_logs === true, |
| 264 | tracesEnabled: observability.traces?.enabled === true |
| 265 | } |
| 266 | }; |
| 267 | } |
| 268 | } |