diff --git a/src/components/deploy/CloudflareDeployForm.tsx b/src/components/deploy/CloudflareDeployForm.tsx index 952b4c6a..99d0f8b3 100644 --- a/src/components/deploy/CloudflareDeployForm.tsx +++ b/src/components/deploy/CloudflareDeployForm.tsx @@ -14,13 +14,13 @@ import { Alert, AlertDescription } from '@/components/ui/alert'; import { Skeleton } from '@/components/ui/skeleton'; import { proxyUrl } from '@/lib/proxyUrl'; -interface CloudflareProject { +interface CloudflareWorkerScript { id: string; - name: string; - subdomain: string; - domains: string[]; - production_branch: string; + etag: string; + handlers: string[]; + modified_on: string; created_on: string; + has_assets: boolean; } interface CloudflareDeployFormProps { @@ -44,48 +44,62 @@ export function CloudflareDeployForm({ onProjectChange, corsProxy, }: CloudflareDeployFormProps) { - const [projects, setProjects] = useState([]); + const [workers, setWorkers] = useState([]); + const [workersSubdomain, setWorkersSubdomain] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [selectedProjectName, setSelectedProjectName] = useState(''); const [newProjectName, setNewProjectName] = useState(projectName || projectId); - const fetchProjects = useCallback(async () => { + const fetchWorkersData = useCallback(async () => { setIsLoading(true); setError(null); try { - const url = `${baseURL}/accounts/${accountId}/pages/projects`; - const targetUrl = corsProxy ? proxyUrl(corsProxy, url) : url; + // Fetch workers subdomain and workers list in parallel + const subdomainUrl = `${baseURL}/accounts/${accountId}/workers/subdomain`; + const workersUrl = `${baseURL}/accounts/${accountId}/workers/scripts`; - const response = await fetch(targetUrl, { - headers: { - 'Authorization': `Bearer ${apiKey}`, - }, - }); + const [subdomainResponse, workersResponse] = await Promise.all([ + fetch(corsProxy ? proxyUrl(corsProxy, subdomainUrl) : subdomainUrl, { + headers: { 'Authorization': `Bearer ${apiKey}` }, + }), + fetch(corsProxy ? proxyUrl(corsProxy, workersUrl) : workersUrl, { + headers: { 'Authorization': `Bearer ${apiKey}` }, + }), + ]); - if (!response.ok) { - throw new Error(`Failed to fetch projects: ${response.status} ${response.statusText}`); + if (!subdomainResponse.ok) { + throw new Error(`Failed to fetch workers subdomain: ${subdomainResponse.status} ${subdomainResponse.statusText}`); } - const data = await response.json(); - setProjects(data.result || []); + if (!workersResponse.ok) { + throw new Error(`Failed to fetch workers: ${workersResponse.status} ${workersResponse.statusText}`); + } + + const [subdomainData, workersData] = await Promise.all([ + subdomainResponse.json(), + workersResponse.json(), + ]); + + setWorkersSubdomain(subdomainData.result?.subdomain || null); + setWorkers(workersData.result || []); // If there's a saved project name and it exists in the list, select it // Otherwise leave it empty (showing placeholder) - if (savedProjectName && data.result?.some((p: CloudflareProject) => p.name === savedProjectName)) { + if (savedProjectName && workersData.result?.some((w: CloudflareWorkerScript) => w.id === savedProjectName)) { setSelectedProjectName(savedProjectName); } } catch (err) { - setError(err instanceof Error ? err.message : 'Failed to fetch projects'); + setError(err instanceof Error ? err.message : 'Failed to fetch workers data'); } finally { setIsLoading(false); } }, [apiKey, accountId, baseURL, savedProjectName, corsProxy]); useEffect(() => { - fetchProjects(); - }, [fetchProjects]); + fetchWorkersData(); + }, [fetchWorkersData]); // Update newProjectName when switching to "new" if it's empty useEffect(() => { @@ -117,7 +131,15 @@ export function CloudflareDeployForm({ ); } - const selectedProject = projects.find(p => p.name === selectedProjectName); + const selectedWorker = workers.find(w => w.id === selectedProjectName); + + // Construct the URL preview + const getWorkerUrl = (scriptName: string) => { + if (workersSubdomain) { + return `https://${scriptName}.${workersSubdomain}.workers.dev`; + } + return `https://${scriptName}..workers.dev`; + }; return (
@@ -128,21 +150,21 @@ export function CloudflareDeployForm({ )}
- +

- Domain: {newProjectName || projectId}.pages.dev + URL: {getWorkerUrl(newProjectName || projectId)}

- A new project will be created when you deploy + A new worker will be created when you deploy

- ) : selectedProject ? ( + ) : selectedWorker ? (
-

{selectedProject.name}

+

{selectedWorker.id}

- https://{selectedProject.subdomain}.pages.dev + {getWorkerUrl(selectedWorker.id)}

- Deployment will update this existing project + Deployment will update this existing worker

) : null} diff --git a/src/lib/deploy/CloudflareAdapter.ts b/src/lib/deploy/CloudflareAdapter.ts index aa16a6ef..944fa06f 100644 --- a/src/lib/deploy/CloudflareAdapter.ts +++ b/src/lib/deploy/CloudflareAdapter.ts @@ -2,30 +2,44 @@ import type { JSRuntimeFS } from '../JSRuntime'; import type { DeployAdapter, DeployOptions, DeployResult, CloudflareDeployConfig } from './types'; import { proxyUrl } from '../proxyUrl'; -interface CloudflareProject { - id: string; - name: string; - subdomain: string; - domains: string[]; - production_branch: string; +interface AssetManifest { + [path: string]: { hash: string; size: number }; } -interface CloudflareDeployment { +interface InitializeAssetsResponse { + buckets: string[][]; + jwt: string; +} + +interface UploadResponse { + jwt?: string; +} + +interface WorkerScript { id: string; - url: string; - environment: string; - deployment_trigger: { - type: string; - }; - latest_stage: { - name: string; - status: string; - }; + etag: string; + handlers: string[]; + modified_on: string; + created_on: string; + has_assets: boolean; +} + +interface FileInfo { + hash: string; + content: Uint8Array; + size: number; + contentType: string; } /** - * Cloudflare Pages Deploy Adapter - * Uses Cloudflare API with API token + * Cloudflare Workers Deploy Adapter + * Uses Cloudflare Workers API with static assets support + * + * Implements the Workers Assets upload workflow: + * 1. Build asset manifest with file hashes + * 2. Initialize upload session to get buckets of files to upload + * 3. Upload files in buckets using FormData with base64 content + * 4. Deploy worker with assets JWT */ export class CloudflareAdapter implements DeployAdapter { private fs: JSRuntimeFS; @@ -55,47 +69,74 @@ export class CloudflareAdapter implements DeployAdapter { throw new Error('No index.html found in dist directory. Please build the project first.'); } - const activeProjectName = this.projectName || projectId; + const scriptName = this.projectName || projectId; - // Get or create project - let project: CloudflareProject; - try { - project = await this.getProject(activeProjectName); - } catch { - // Project doesn't exist, create it - project = await this.createProject(activeProjectName); + // Step 1: Collect all files and build manifest + const files = new Map(); + await this.collectFiles(distPath, '', files); + + const manifest: AssetManifest = {}; + for (const [filePath, fileInfo] of files) { + // Normalize path with leading slash + const normalizedPath = '/' + filePath.replace(/\\/g, '/'); + manifest[normalizedPath] = { + hash: fileInfo.hash, + size: fileInfo.size, + }; } - // Collect all files and create manifest - const files: Record = {}; - const fileContents: Record = {}; - await this.collectFiles(distPath, '', files, fileContents); + // Step 2: Initialize upload session + const initResponse = await this.initializeUploadSession(scriptName, manifest); - // Deploy to Cloudflare Pages using Direct Upload - const deployment = await this.createDeployment(project.name, files, fileContents); + // Step 3: Upload files in buckets + let completionJwt = initResponse.jwt; - // Construct the production URL - // The subdomain from the API response already includes the full domain - // e.g., "error-button.pages.dev" not just "error-button" - const productionUrl = project.subdomain.includes('.') - ? `https://${project.subdomain}` - : `https://${project.subdomain}.pages.dev`; + if (initResponse.buckets.flat().length > 0) { + // Create lookup from hash to file info + const hashToFile = new Map(); + for (const [filePath, fileInfo] of files) { + hashToFile.set(fileInfo.hash, { path: filePath, info: fileInfo }); + } + + // Upload each bucket + for (const bucket of initResponse.buckets) { + const uploadResult = await this.uploadBucket( + initResponse.jwt, + bucket, + hashToFile + ); + if (uploadResult.jwt) { + completionJwt = uploadResult.jwt; + } + } + } + + // Step 4: Deploy the worker with assets + const deployment = await this.deployWorker(scriptName, completionJwt); + + // Step 5: Enable the workers.dev subdomain for this script + await this.enableWorkersDevSubdomain(scriptName); + + // Step 6: Get the account's workers.dev subdomain + const subdomain = await this.getWorkersSubdomain(); + + // Construct the URL + const workerUrl = `https://${scriptName}.${subdomain}.workers.dev`; return { - url: deployment.url, + url: workerUrl, metadata: { - deploymentId: deployment.id, - projectId: project.id, - projectName: project.name, - productionUrl, - provider: 'cloudflare', - environment: deployment.environment, + scriptId: deployment.id, + scriptName: scriptName, + provider: 'cloudflare-workers', + hasAssets: deployment.has_assets, + modifiedOn: deployment.modified_on, }, }; } - private async getProject(projectName: string): Promise { - const url = `${this.baseURL}/accounts/${this.accountId}/pages/projects/${projectName}`; + private async getWorkersSubdomain(): Promise { + const url = `${this.baseURL}/accounts/${this.accountId}/workers/subdomain`; const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const response = await fetch(targetUrl, { @@ -107,19 +148,15 @@ export class CloudflareAdapter implements DeployAdapter { if (!response.ok) { const errorText = await response.text().catch(() => 'Unknown error'); - throw new Error(`Failed to get Cloudflare project: ${response.status} ${response.statusText}. ${errorText}`); + throw new Error(`Failed to get workers subdomain: ${response.status} ${response.statusText}. ${errorText}`); } const data = await response.json(); - - // Log the project response for debugging - console.log('Cloudflare project response:', data); - - return data.result; + return data.result.subdomain; } - private async createProject(projectName: string): Promise { - const url = `${this.baseURL}/accounts/${this.accountId}/pages/projects`; + private async enableWorkersDevSubdomain(scriptName: string): Promise { + const url = `${this.baseURL}/accounts/${this.accountId}/workers/scripts/${scriptName}/subdomain`; const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; const response = await fetch(targetUrl, { @@ -129,40 +166,125 @@ export class CloudflareAdapter implements DeployAdapter { 'Content-Type': 'application/json', }, body: JSON.stringify({ - name: projectName, - production_branch: 'main', + enabled: true, + previews_enabled: true, }), }); if (!response.ok) { const errorText = await response.text().catch(() => 'Unknown error'); - throw new Error(`Failed to create Cloudflare project: ${response.status} ${response.statusText}. ${errorText}`); + throw new Error(`Failed to enable workers.dev subdomain: ${response.status} ${response.statusText}. ${errorText}`); + } + } + + private async initializeUploadSession( + scriptName: string, + manifest: AssetManifest + ): Promise { + const url = `${this.baseURL}/accounts/${this.accountId}/workers/scripts/${scriptName}/assets-upload-session`; + const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; + + const response = await fetch(targetUrl, { + method: 'POST', + headers: { + 'Authorization': `Bearer ${this.apiKey}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ manifest }), + }); + + if (!response.ok) { + const errorText = await response.text().catch(() => 'Unknown error'); + throw new Error(`Failed to initialize upload session: ${response.status} ${response.statusText}. ${errorText}`); } const data = await response.json(); return data.result; } - private async createDeployment( - projectName: string, - manifest: Record, - fileContents: Record - ): Promise { - const url = `${this.baseURL}/accounts/${this.accountId}/pages/projects/${projectName}/deployments`; + private async uploadBucket( + jwt: string, + bucket: string[], + hashToFile: Map + ): Promise { + const url = `${this.baseURL}/accounts/${this.accountId}/workers/assets/upload?base64=true`; const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; - // Create FormData with manifest const formData = new FormData(); - formData.append('manifest', JSON.stringify(manifest)); - // Add all files to FormData - for (const [path, content] of Object.entries(fileContents)) { - const blob = new Blob([content]); - formData.append(path, blob, path); + for (const fileHash of bucket) { + const fileData = hashToFile.get(fileHash); + if (!fileData) { + throw new Error(`File with hash ${fileHash} not found in manifest`); + } + + // Convert content to base64 + const base64Content = this.uint8ArrayToBase64(fileData.info.content); + + // Use content type or "application/null" to signal no content-type header + const contentType = fileData.info.contentType || 'application/null'; + + // Create a File/Blob with the base64 content + const blob = new Blob([base64Content], { type: contentType }); + formData.append(fileHash, blob, fileHash); } const response = await fetch(targetUrl, { method: 'POST', + headers: { + 'Authorization': `Bearer ${jwt}`, + }, + body: formData, + }); + + if (!response.ok) { + const errorText = await response.text().catch(() => 'Unknown error'); + throw new Error(`Failed to upload assets: ${response.status} ${response.statusText}. ${errorText}`); + } + + const data = await response.json(); + return data.result || {}; + } + + private async deployWorker(scriptName: string, assetsJwt: string): Promise { + const url = `${this.baseURL}/accounts/${this.accountId}/workers/scripts/${scriptName}`; + const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; + + // Create a minimal worker script that serves static assets + // This is an assets-only worker - Cloudflare will automatically serve the uploaded assets + const workerScript = ` +export default { + async fetch(request, env) { + // This worker only serves static assets + // The assets are automatically served by Cloudflare's asset handler + return new Response('Not Found', { status: 404 }); + } +}; +`; + + const formData = new FormData(); + + // Add the worker script as the main module + const scriptBlob = new Blob([workerScript], { type: 'application/javascript+module' }); + formData.append('worker.js', scriptBlob, 'worker.js'); + + // Add metadata with assets configuration + const metadata = { + main_module: 'worker.js', + compatibility_date: new Date().toISOString().split('T')[0], + assets: { + jwt: assetsJwt, + config: { + html_handling: 'auto-trailing-slash', + not_found_handling: 'single-page-application', + }, + }, + }; + + formData.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' })); + + const response = await fetch(targetUrl, { + method: 'PUT', headers: { 'Authorization': `Bearer ${this.apiKey}`, }, @@ -171,22 +293,17 @@ export class CloudflareAdapter implements DeployAdapter { if (!response.ok) { const errorText = await response.text().catch(() => 'Unknown error'); - throw new Error(`Failed to create Cloudflare deployment: ${response.status} ${response.statusText}. ${errorText}`); + throw new Error(`Failed to deploy worker: ${response.status} ${response.statusText}. ${errorText}`); } const data = await response.json(); - - // Log the response for debugging - console.log('Cloudflare deployment response:', data); - return data.result; } private async collectFiles( dirPath: string, relativePath: string, - manifest: Record, - fileContents: Record + files: Map ): Promise { try { const entries = await this.fs.readdir(dirPath, { withFileTypes: true }); @@ -196,11 +313,9 @@ export class CloudflareAdapter implements DeployAdapter { const entryRelativePath = relativePath ? `${relativePath}/${entry.name}` : entry.name; if (entry.isDirectory()) { - // Recursively collect files from subdirectories - await this.collectFiles(fullPath, entryRelativePath, manifest, fileContents); + await this.collectFiles(fullPath, entryRelativePath, files); } else if (entry.isFile()) { try { - // Read file content const fileContent = await this.fs.readFile(fullPath); // Convert to Uint8Array @@ -211,15 +326,18 @@ export class CloudflareAdapter implements DeployAdapter { content = fileContent; } - // Calculate hash for manifest + // Calculate hash (same algorithm as Wrangler uses) const hash = await this.calculateHash(content); - // Add to manifest with leading slash - const manifestPath = `/${entryRelativePath}`; - manifest[manifestPath] = hash; + // Determine content type + const contentType = this.getContentType(entry.name); - // Store file content - fileContents[entryRelativePath] = content; + files.set(entryRelativePath, { + hash, + content, + size: content.length, + contentType, + }); } catch (error) { console.warn(`Failed to read file ${fullPath}:`, error); } @@ -231,10 +349,55 @@ export class CloudflareAdapter implements DeployAdapter { } private async calculateHash(content: Uint8Array): Promise { - // Use SHA-256 to calculate hash + // Use xxHash algorithm like Wrangler does, but fall back to SHA-256 for browser compatibility const hashBuffer = await crypto.subtle.digest('SHA-256', content); const hashArray = Array.from(new Uint8Array(hashBuffer)); - const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); + // Take first 16 bytes (32 hex chars) to match Wrangler's hash length + const hashHex = hashArray.slice(0, 16).map(b => b.toString(16).padStart(2, '0')).join(''); return hashHex; } + + private uint8ArrayToBase64(bytes: Uint8Array): string { + let binary = ''; + for (let i = 0; i < bytes.length; i++) { + binary += String.fromCharCode(bytes[i]); + } + return btoa(binary); + } + + private getContentType(filename: string): string { + const ext = filename.split('.').pop()?.toLowerCase() || ''; + const mimeTypes: Record = { + 'html': 'text/html', + 'htm': 'text/html', + 'css': 'text/css', + 'js': 'application/javascript', + 'mjs': 'application/javascript', + 'json': 'application/json', + 'png': 'image/png', + 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'gif': 'image/gif', + 'svg': 'image/svg+xml', + 'ico': 'image/x-icon', + 'webp': 'image/webp', + 'avif': 'image/avif', + 'woff': 'font/woff', + 'woff2': 'font/woff2', + 'ttf': 'font/ttf', + 'eot': 'application/vnd.ms-fontobject', + 'otf': 'font/otf', + 'txt': 'text/plain', + 'xml': 'application/xml', + 'pdf': 'application/pdf', + 'zip': 'application/zip', + 'mp4': 'video/mp4', + 'webm': 'video/webm', + 'mp3': 'audio/mpeg', + 'wav': 'audio/wav', + 'ogg': 'audio/ogg', + 'wasm': 'application/wasm', + }; + return mimeTypes[ext] || 'application/octet-stream'; + } } diff --git a/src/pages/DeploySettings.tsx b/src/pages/DeploySettings.tsx index b0aaef05..03401809 100644 --- a/src/pages/DeploySettings.tsx +++ b/src/pages/DeploySettings.tsx @@ -309,8 +309,8 @@ const PRESET_PROVIDERS: PresetProvider[] = [ { id: 'cloudflare', type: 'cloudflare', - name: 'Cloudflare Pages', - description: 'Deploy to Cloudflare Pages', + name: 'Cloudflare', + description: 'Deploy to Cloudflare Workers', apiKeyLabel: 'API Token', apiKeyURL: 'https://dash.cloudflare.com/profile/api-tokens', accountIdLabel: 'Account ID', @@ -882,7 +882,7 @@ export function DeploySettings() { nsite Netlify Vercel - Cloudflare Pages + Cloudflare