mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Deploy to Cloudflare Workers instead of Cloudflare Pages
This commit is contained in:
3 files changed
+313
-128
No files matched your search
@@ -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<CloudflareProject[]>([]);
|
||||
const [workers, setWorkers] = useState<CloudflareWorkerScript[]>([]);
|
||||
const [workersSubdomain, setWorkersSubdomain] = useState<string | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [selectedProjectName, setSelectedProjectName] = useState<string>('');
|
||||
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}.<subdomain>.workers.dev`;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
@@ -128,21 +150,21 @@ export function CloudflareDeployForm({
|
||||
)}
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="cloudflare-project">Select Project</Label>
|
||||
<Label htmlFor="cloudflare-project">Select Worker</Label>
|
||||
<Select value={selectedProjectName} onValueChange={setSelectedProjectName}>
|
||||
<SelectTrigger id="cloudflare-project">
|
||||
<SelectValue placeholder="Select a project or create new..." />
|
||||
<SelectValue placeholder="Select a worker or create new..." />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="new">
|
||||
<div className="flex items-center gap-2">
|
||||
<Plus className="h-4 w-4" />
|
||||
Create new project
|
||||
Create new worker
|
||||
</div>
|
||||
</SelectItem>
|
||||
{projects.map((project) => (
|
||||
<SelectItem key={project.id} value={project.name}>
|
||||
{project.name}
|
||||
{workers.map((worker) => (
|
||||
<SelectItem key={worker.id} value={worker.id}>
|
||||
{worker.id}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
@@ -151,7 +173,7 @@ export function CloudflareDeployForm({
|
||||
|
||||
{selectedProjectName === 'new' ? (
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="new-project-name">New Project Name</Label>
|
||||
<Label htmlFor="new-project-name">New Worker Name</Label>
|
||||
<Input
|
||||
id="new-project-name"
|
||||
value={newProjectName}
|
||||
@@ -159,34 +181,34 @@ export function CloudflareDeployForm({
|
||||
placeholder={projectName || projectId}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Domain: <span className="font-mono">{newProjectName || projectId}.pages.dev</span>
|
||||
URL: <span className="font-mono">{getWorkerUrl(newProjectName || projectId)}</span>
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
A new project will be created when you deploy
|
||||
A new worker will be created when you deploy
|
||||
</p>
|
||||
</div>
|
||||
) : selectedProject ? (
|
||||
) : selectedWorker ? (
|
||||
<div className="space-y-2">
|
||||
<div className="rounded-lg border p-3 space-y-2">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<p className="text-sm font-medium">{selectedProject.name}</p>
|
||||
<p className="text-sm font-medium">{selectedWorker.id}</p>
|
||||
<p className="text-xs text-muted-foreground font-mono">
|
||||
https://{selectedProject.subdomain}.pages.dev
|
||||
{getWorkerUrl(selectedWorker.id)}
|
||||
</p>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => window.open(`https://dash.cloudflare.com/pages/${selectedProject.id}`, '_blank')}
|
||||
onClick={() => window.open(`https://dash.cloudflare.com/${accountId}/workers/services/view/${selectedWorker.id}`, '_blank')}
|
||||
>
|
||||
<ExternalLink className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Deployment will update this existing project
|
||||
Deployment will update this existing worker
|
||||
</p>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
@@ -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<string, FileInfo>();
|
||||
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<string, string> = {};
|
||||
const fileContents: Record<string, Uint8Array> = {};
|
||||
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<string, { path: string; info: FileInfo }>();
|
||||
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<CloudflareProject> {
|
||||
const url = `${this.baseURL}/accounts/${this.accountId}/pages/projects/${projectName}`;
|
||||
private async getWorkersSubdomain(): Promise<string> {
|
||||
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<CloudflareProject> {
|
||||
const url = `${this.baseURL}/accounts/${this.accountId}/pages/projects`;
|
||||
private async enableWorkersDevSubdomain(scriptName: string): Promise<void> {
|
||||
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<InitializeAssetsResponse> {
|
||||
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<string, string>,
|
||||
fileContents: Record<string, Uint8Array>
|
||||
): Promise<CloudflareDeployment> {
|
||||
const url = `${this.baseURL}/accounts/${this.accountId}/pages/projects/${projectName}/deployments`;
|
||||
private async uploadBucket(
|
||||
jwt: string,
|
||||
bucket: string[],
|
||||
hashToFile: Map<string, { path: string; info: FileInfo }>
|
||||
): Promise<UploadResponse> {
|
||||
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<WorkerScript> {
|
||||
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<string, string>,
|
||||
fileContents: Record<string, Uint8Array>
|
||||
files: Map<string, FileInfo>
|
||||
): Promise<void> {
|
||||
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<string> {
|
||||
// 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<string, string> = {
|
||||
'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';
|
||||
}
|
||||
}
|
||||
@@ -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() {
|
||||
<SelectItem value="nsite">nsite</SelectItem>
|
||||
<SelectItem value="netlify">Netlify</SelectItem>
|
||||
<SelectItem value="vercel">Vercel</SelectItem>
|
||||
<SelectItem value="cloudflare">Cloudflare Pages</SelectItem>
|
||||
<SelectItem value="cloudflare">Cloudflare</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user