Add CORS Proxy config for deploy providers

This commit is contained in:
Alex Gleason committed 2025-10-18 19:59:24 -05:00
1 parent 6eb6bde5bb
commit 0d7e616753
9 files changed
+117 -13

No files matched your search

+15 -3
View File
@@ -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}`,
+25 -4
View File
@@ -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}`);
+7 -1
View File
@@ -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}`,
+3
View File
@@ -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;
}