From 0d7e6167537b7a4eccf75a7347d5b59ac91500fa Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 18 Oct 2025 19:52:49 -0500 Subject: [PATCH] Add CORS Proxy config for deploy providers --- src/components/DeployDialog.tsx | 6 +++ src/components/deploy/NetlifyDeployForm.tsx | 10 ++++- src/contexts/DeploySettingsContext.ts | 3 ++ src/lib/deploy/NetlifyAdapter.ts | 18 ++++++-- src/lib/deploy/ShakespeareAdapter.ts | 29 ++++++++++-- src/lib/deploy/VercelAdapter.ts | 8 +++- src/lib/deploy/types.ts | 3 ++ src/lib/tools/DeployProjectTool.ts | 4 +- src/pages/DeploySettings.tsx | 49 ++++++++++++++++++++- 9 files changed, 117 insertions(+), 13 deletions(-) diff --git a/src/components/DeployDialog.tsx b/src/components/DeployDialog.tsx index 72a6ba01..875e6148 100644 --- a/src/components/DeployDialog.tsx +++ b/src/components/DeployDialog.tsx @@ -23,6 +23,7 @@ import { useDeploySettings } from '@/hooks/useDeploySettings'; import { useProjectDeploySettings } from '@/hooks/useProjectDeploySettings'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useFS } from '@/hooks/useFS'; +import { useAppContext } from '@/hooks/useAppContext'; import { ShakespeareAdapter, NetlifyAdapter, VercelAdapter, DeployAdapter } from '@/lib/deploy'; import { Link } from 'react-router-dom'; import type { ShakespeareDeployProvider, NetlifyProvider, VercelProvider } from '@/contexts/DeploySettingsContext'; @@ -57,6 +58,7 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep const { settings: projectSettings, updateSettings: updateProjectSettings } = useProjectDeploySettings(projectId); const { user } = useCurrentUser(); const { fs } = useFS(); + const { config } = useAppContext(); const [selectedProviderName, setSelectedProviderName] = useState(''); const [isDeploying, setIsDeploying] = useState(false); @@ -108,6 +110,7 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep signer: user.signer, host: shakespeareProvider.host, subdomain: shakespeareForm.subdomain || undefined, + corsProxy: shakespeareProvider.proxy ? config.corsProxy : undefined, }); } else if (selectedProvider.type === 'netlify') { const netlifyProvider = selectedProvider as NetlifyProvider; @@ -123,6 +126,7 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep baseURL: netlifyProvider.baseURL, siteName: netlifyForm.siteId ? undefined : (netlifyForm.siteName || undefined), siteId: netlifyForm.siteId || undefined, + corsProxy: netlifyProvider.proxy ? config.corsProxy : undefined, }); } else if (selectedProvider.type === 'vercel') { const vercelProvider = selectedProvider as VercelProvider; @@ -136,6 +140,7 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep baseURL: vercelProvider.baseURL, teamId: vercelForm.teamId || undefined, projectName: vercelForm.projectName || undefined, + corsProxy: vercelProvider.proxy ? config.corsProxy : undefined, }); } else { throw new Error('Unknown provider type'); @@ -223,6 +228,7 @@ export function DeployDialog({ projectId, projectName, open, onOpenChange }: Dep projectName={projectName} savedSiteId={projectSettings.netlify?.siteId} onSiteChange={handleNetlifySiteChange} + corsProxy={netlifyProvider.proxy ? config.corsProxy : undefined} /> ); } diff --git a/src/components/deploy/NetlifyDeployForm.tsx b/src/components/deploy/NetlifyDeployForm.tsx index 6a32af12..e26eea38 100644 --- a/src/components/deploy/NetlifyDeployForm.tsx +++ b/src/components/deploy/NetlifyDeployForm.tsx @@ -12,6 +12,7 @@ import { } from '@/components/ui/select'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Skeleton } from '@/components/ui/skeleton'; +import { proxyUrl } from '@/lib/proxyUrl'; interface NetlifySite { id: string; @@ -28,6 +29,7 @@ interface NetlifyDeployFormProps { projectName?: string; savedSiteId?: string; onSiteChange: (siteId: string, siteName: string) => void; + corsProxy?: string; } export function NetlifyDeployForm({ @@ -37,6 +39,7 @@ export function NetlifyDeployForm({ projectName, savedSiteId, onSiteChange, + corsProxy, }: NetlifyDeployFormProps) { const [sites, setSites] = useState([]); const [isLoading, setIsLoading] = useState(true); @@ -49,7 +52,10 @@ export function NetlifyDeployForm({ setError(null); try { - const response = await fetch(`${baseURL}/sites`, { + const url = `${baseURL}/sites`; + const targetUrl = corsProxy ? proxyUrl(corsProxy, url) : url; + + const response = await fetch(targetUrl, { headers: { 'Authorization': `Bearer ${apiKey}`, }, @@ -74,7 +80,7 @@ export function NetlifyDeployForm({ } finally { setIsLoading(false); } - }, [apiKey, baseURL, savedSiteId]); + }, [apiKey, baseURL, savedSiteId, corsProxy]); useEffect(() => { fetchSites(); diff --git a/src/contexts/DeploySettingsContext.ts b/src/contexts/DeploySettingsContext.ts index f0c2df8b..af6b9709 100644 --- a/src/contexts/DeploySettingsContext.ts +++ b/src/contexts/DeploySettingsContext.ts @@ -4,6 +4,7 @@ export interface ShakespeareDeployProvider { name: string; type: 'shakespeare'; host?: string; + proxy?: boolean; } export interface NetlifyProvider { @@ -11,6 +12,7 @@ export interface NetlifyProvider { type: 'netlify'; apiKey: string; baseURL?: string; + proxy?: boolean; } export interface VercelProvider { @@ -18,6 +20,7 @@ export interface VercelProvider { type: 'vercel'; apiKey: string; baseURL?: string; + proxy?: boolean; } export type DeployProvider = ShakespeareDeployProvider | NetlifyProvider | VercelProvider; diff --git a/src/lib/deploy/NetlifyAdapter.ts b/src/lib/deploy/NetlifyAdapter.ts index b6f9e131..32ceb6b5 100644 --- a/src/lib/deploy/NetlifyAdapter.ts +++ b/src/lib/deploy/NetlifyAdapter.ts @@ -1,6 +1,7 @@ import JSZip from 'jszip'; import type { JSRuntimeFS } from '../JSRuntime'; import type { DeployAdapter, DeployOptions, DeployResult, NetlifyDeployConfig } from './types'; +import { proxyUrl } from '../proxyUrl'; interface NetlifySite { id: string; @@ -27,6 +28,7 @@ export class NetlifyAdapter implements DeployAdapter { private baseURL: string; private siteName?: string; private siteId?: string; + private corsProxy?: string; constructor(config: NetlifyDeployConfig) { this.fs = config.fs; @@ -34,6 +36,7 @@ export class NetlifyAdapter implements DeployAdapter { this.baseURL = config.baseURL || 'https://api.netlify.com/api/v1'; this.siteName = config.siteName; this.siteId = config.siteId; + this.corsProxy = config.corsProxy; } async deploy(options: DeployOptions): Promise { @@ -82,7 +85,10 @@ export class NetlifyAdapter implements DeployAdapter { } private async createSite(name: string): Promise { - const response = await fetch(`${this.baseURL}/sites`, { + const url = `${this.baseURL}/sites`; + const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; + + const response = await fetch(targetUrl, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, @@ -103,7 +109,10 @@ export class NetlifyAdapter implements DeployAdapter { } private async getSite(siteId: string): Promise { - const response = await fetch(`${this.baseURL}/sites/${siteId}`, { + const url = `${this.baseURL}/sites/${siteId}`; + const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; + + const response = await fetch(targetUrl, { method: 'GET', headers: { 'Authorization': `Bearer ${this.apiKey}`, @@ -119,7 +128,10 @@ export class NetlifyAdapter implements DeployAdapter { } private async triggerDeploy(siteId: string, zipBlob: Blob): Promise { - const response = await fetch(`${this.baseURL}/sites/${siteId}/deploys`, { + const url = `${this.baseURL}/sites/${siteId}/deploys`; + const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, url) : url; + + const response = await fetch(targetUrl, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, diff --git a/src/lib/deploy/ShakespeareAdapter.ts b/src/lib/deploy/ShakespeareAdapter.ts index 490048d5..25561c7d 100644 --- a/src/lib/deploy/ShakespeareAdapter.ts +++ b/src/lib/deploy/ShakespeareAdapter.ts @@ -1,7 +1,9 @@ import JSZip from 'jszip'; -import { NIP98Client } from '@nostrify/nostrify'; +import { NIP98 } from '@nostrify/nostrify'; +import { N64 } from '@nostrify/nostrify/utils'; import type { JSRuntimeFS } from '../JSRuntime'; import type { DeployAdapter, DeployOptions, DeployResult, ShakespeareDeployConfig } from './types'; +import { proxyUrl } from '../proxyUrl'; /** * Shakespeare Deploy Adapter @@ -12,12 +14,14 @@ export class ShakespeareAdapter implements DeployAdapter { private signer: ShakespeareDeployConfig['signer']; private host: string; private subdomain?: string; + private corsProxy?: string; constructor(config: ShakespeareDeployConfig) { this.fs = config.fs; this.signer = config.signer; this.host = config.host || 'shakespeare.wtf'; this.subdomain = config.subdomain; + this.corsProxy = config.corsProxy; } async deploy(options: DeployOptions): Promise { @@ -50,13 +54,30 @@ export class ShakespeareAdapter implements DeployAdapter { formData.append('hostname', hostname); formData.append('file', zipBlob, `${projectId}.zip`); - // Create NIP-98 authenticated client and deploy - const nip98Client = new NIP98Client({ signer: this.signer }); - const response = await nip98Client.fetch(deployUrl, { + // Create the request + let request = new Request(deployUrl, { method: 'POST', body: formData, }); + // Create NIP-98 token for authentication + const template = await NIP98.template(request); + const event = await this.signer.signEvent(template); + const token = N64.encodeEvent(event); + + // Add the Authorization header + const headers = new Headers(request.headers); + headers.set('Authorization', `Nostr ${token}`); + request = new Request(request, { headers }); + + // Apply proxy if configured + if (this.corsProxy) { + request = new Request(proxyUrl(this.corsProxy, request.url), request); + } + + // Deploy + const response = await fetch(request); + if (!response.ok) { const errorText = await response.text().catch(() => 'Unknown error'); throw new Error(`Deployment failed: ${response.status} ${response.statusText}. ${errorText}`); diff --git a/src/lib/deploy/VercelAdapter.ts b/src/lib/deploy/VercelAdapter.ts index 33df08da..5f9a3a6f 100644 --- a/src/lib/deploy/VercelAdapter.ts +++ b/src/lib/deploy/VercelAdapter.ts @@ -1,5 +1,6 @@ import type { JSRuntimeFS } from '../JSRuntime'; import type { DeployAdapter, DeployOptions, DeployResult, VercelDeployConfig } from './types'; +import { proxyUrl } from '../proxyUrl'; interface VercelFile { file: string; @@ -23,6 +24,7 @@ export class VercelAdapter implements DeployAdapter { private baseURL: string; private teamId?: string; private projectName?: string; + private corsProxy?: string; constructor(config: VercelDeployConfig) { this.fs = config.fs; @@ -30,6 +32,7 @@ export class VercelAdapter implements DeployAdapter { this.baseURL = config.baseURL || 'https://api.vercel.com'; this.teamId = config.teamId; this.projectName = config.projectName; + this.corsProxy = config.corsProxy; } async deploy(options: DeployOptions): Promise { @@ -63,8 +66,11 @@ export class VercelAdapter implements DeployAdapter { apiUrl += `?teamId=${this.teamId}`; } + // Apply proxy if configured + const targetUrl = this.corsProxy ? proxyUrl(this.corsProxy, apiUrl) : apiUrl; + // Deploy to Vercel - const response = await fetch(apiUrl, { + const response = await fetch(targetUrl, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, diff --git a/src/lib/deploy/types.ts b/src/lib/deploy/types.ts index e108523f..fb63e231 100644 --- a/src/lib/deploy/types.ts +++ b/src/lib/deploy/types.ts @@ -25,6 +25,7 @@ export interface ShakespeareDeployConfig { signer: NostrSigner; host?: string; subdomain?: string; + corsProxy?: string; } export interface NetlifyDeployConfig { @@ -33,6 +34,7 @@ export interface NetlifyDeployConfig { baseURL?: string; siteName?: string; siteId?: string; + corsProxy?: string; } export interface VercelDeployConfig { @@ -41,4 +43,5 @@ export interface VercelDeployConfig { baseURL?: string; teamId?: string; projectName?: string; + corsProxy?: string; } diff --git a/src/lib/tools/DeployProjectTool.ts b/src/lib/tools/DeployProjectTool.ts index a5537738..01c063bb 100644 --- a/src/lib/tools/DeployProjectTool.ts +++ b/src/lib/tools/DeployProjectTool.ts @@ -49,13 +49,13 @@ export class DeployProjectTool implements Tool { // Deploy the project using ShakespeareAdapter const adapter = new ShakespeareAdapter({ + fs: this.fs, signer: this.signer, - deployServer, + host: deployServer, }); const result = await adapter.deploy({ projectId: this.projectId, - fs: this.fs, projectPath: this.cwd, }); diff --git a/src/pages/DeploySettings.tsx b/src/pages/DeploySettings.tsx index 5bf1f78b..c00d2614 100644 --- a/src/pages/DeploySettings.tsx +++ b/src/pages/DeploySettings.tsx @@ -1,6 +1,7 @@ import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Rocket, ArrowLeft, Trash2, Check, GripVertical } from 'lucide-react'; +import { Checkbox } from '@/components/ui/checkbox'; import { DndContext, closestCenter, @@ -39,6 +40,7 @@ interface PresetProvider { requiresNostr?: boolean; apiKeyLabel?: string; apiKeyURL?: string; + proxy?: boolean; } interface SortableProviderItemProps { @@ -154,6 +156,19 @@ function SortableProviderItem({ provider, index, preset, onRemove, onUpdate, sho )} +
+ onUpdate(index, { ...provider, proxy: checked === true })} + /> + +