mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add CORS Proxy config for deploy providers
This commit is contained in:
9 files changed
+117
-13
No files matched your search
@@ -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<string>('');
|
||||
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}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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<NetlifySite[]>([]);
|
||||
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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<DeployResult> {
|
||||
@@ -82,7 +85,10 @@ export class NetlifyAdapter implements DeployAdapter {
|
||||
}
|
||||
|
||||
private async createSite(name: string): Promise<NetlifySite> {
|
||||
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<NetlifySite> {
|
||||
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<NetlifyBuildResponse> {
|
||||
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}`,
|
||||
|
||||
@@ -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<DeployResult> {
|
||||
@@ -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}`);
|
||||
|
||||
@@ -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<DeployResult> {
|
||||
@@ -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}`,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -49,13 +49,13 @@ export class DeployProjectTool implements Tool<DeployProjectParams> {
|
||||
|
||||
// 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,
|
||||
});
|
||||
|
||||
|
||||
@@ -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
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<div className="flex items-center space-x-2">
|
||||
<Checkbox
|
||||
id={`provider-${index}-proxy`}
|
||||
checked={provider.proxy || false}
|
||||
onCheckedChange={(checked) => onUpdate(index, { ...provider, proxy: checked === true })}
|
||||
/>
|
||||
<Label
|
||||
htmlFor={`provider-${index}-proxy`}
|
||||
className="text-sm font-normal cursor-pointer"
|
||||
>
|
||||
Use CORS Proxy
|
||||
</Label>
|
||||
</div>
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
@@ -183,6 +198,7 @@ const PRESET_PROVIDERS: PresetProvider[] = [
|
||||
description: 'Deploy to Netlify with personal access token',
|
||||
apiKeyLabel: 'Personal Access Token',
|
||||
apiKeyURL: 'https://app.netlify.com/user/applications#personal-access-tokens',
|
||||
proxy: true,
|
||||
},
|
||||
{
|
||||
id: 'vercel',
|
||||
@@ -191,6 +207,7 @@ const PRESET_PROVIDERS: PresetProvider[] = [
|
||||
description: 'Deploy to Vercel with access token',
|
||||
apiKeyLabel: 'Access Token',
|
||||
apiKeyURL: 'https://vercel.com/account/tokens',
|
||||
proxy: true,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -204,6 +221,14 @@ export function DeploySettings() {
|
||||
const [presetApiKeys, setPresetApiKeys] = useState<Record<string, string>>({});
|
||||
const [presetBaseURLs, setPresetBaseURLs] = useState<Record<string, string>>({});
|
||||
const [presetHosts, setPresetHosts] = useState<Record<string, string>>({});
|
||||
const [presetProxies, setPresetProxies] = useState<Record<string, boolean>>(() => {
|
||||
// Initialize with preset proxy defaults
|
||||
const initialProxies: Record<string, boolean> = {};
|
||||
PRESET_PROVIDERS.forEach(preset => {
|
||||
initialProxies[preset.id] = preset.proxy || false;
|
||||
});
|
||||
return initialProxies;
|
||||
});
|
||||
const [presetNames, setPresetNames] = useState<Record<string, string>>(() => {
|
||||
// Initialize with preset names
|
||||
const initialNames: Record<string, string> = {};
|
||||
@@ -241,6 +266,7 @@ export function DeploySettings() {
|
||||
const apiKey = presetApiKeys[preset.id];
|
||||
const baseURL = presetBaseURLs[preset.id];
|
||||
const host = presetHosts[preset.id];
|
||||
const proxy = presetProxies[preset.id];
|
||||
const customName = presetNames[preset.id];
|
||||
|
||||
// For non-Shakespeare providers, require API key
|
||||
@@ -257,6 +283,7 @@ export function DeploySettings() {
|
||||
name: finalName,
|
||||
type: 'shakespeare',
|
||||
...(host?.trim() && { host: host.trim() }),
|
||||
...(proxy !== undefined && { proxy }),
|
||||
};
|
||||
} else if (preset.type === 'netlify') {
|
||||
newProvider = {
|
||||
@@ -264,6 +291,7 @@ export function DeploySettings() {
|
||||
type: 'netlify',
|
||||
apiKey: apiKey.trim(),
|
||||
...(baseURL?.trim() && { baseURL: baseURL.trim() }),
|
||||
...(proxy !== undefined && { proxy }),
|
||||
};
|
||||
} else {
|
||||
newProvider = {
|
||||
@@ -271,15 +299,17 @@ export function DeploySettings() {
|
||||
type: 'vercel',
|
||||
apiKey: apiKey.trim(),
|
||||
...(baseURL?.trim() && { baseURL: baseURL.trim() }),
|
||||
...(proxy !== undefined && { proxy }),
|
||||
};
|
||||
}
|
||||
|
||||
setProviders([...settings.providers, newProvider]);
|
||||
|
||||
// Clear inputs and reset name to preset default
|
||||
// Clear inputs and reset to preset defaults
|
||||
setPresetApiKeys(prev => ({ ...prev, [preset.id]: '' }));
|
||||
setPresetBaseURLs(prev => ({ ...prev, [preset.id]: '' }));
|
||||
setPresetHosts(prev => ({ ...prev, [preset.id]: '' }));
|
||||
setPresetProxies(prev => ({ ...prev, [preset.id]: preset.proxy || false }));
|
||||
setPresetNames(prev => ({ ...prev, [preset.id]: preset.name }));
|
||||
};
|
||||
|
||||
@@ -480,6 +510,23 @@ export function DeploySettings() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center space-x-2">
|
||||
<Checkbox
|
||||
id={`preset-${preset.id}-proxy`}
|
||||
checked={presetProxies[preset.id] || false}
|
||||
onCheckedChange={(checked) => setPresetProxies(prev => ({
|
||||
...prev,
|
||||
[preset.id]: checked === true,
|
||||
}))}
|
||||
/>
|
||||
<Label
|
||||
htmlFor={`preset-${preset.id}-proxy`}
|
||||
className="text-sm font-normal cursor-pointer"
|
||||
>
|
||||
Use CORS Proxy
|
||||
</Label>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={() => handleAddPresetProvider(preset)}
|
||||
disabled={
|
||||
|
||||
Reference in new issue
Block a user