File size: 3,375 Bytes
cd3f86a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig(({ mode }) => {
  // Load env file based on `mode` in the current working directory.
  const env = loadEnv(mode, '.', '');
  
  return {
    plugins: [react()],
    define: {
      // Expose the API_KEY from the environment to the client-side code.
      'process.env.API_KEY': JSON.stringify(env.API_KEY),
      'process.env': {} 
    },
    server: {
      port: 7860,
      host: true,
      proxy: {
        // Secure Bridge Proxy Implementation
        '/comfy-bridge': {
          target: 'http://localhost:8188', // Fallback target
          changeOrigin: true,
          secure: false,
          ws: true,
          router: (req: any) => { // Added explicit 'any' type
            // Dynamically determine target from header or query param
            const target = req.headers['x-bridge-target'] as string;
            if (target) return target;
            
            // For GET requests like images, target might be in query
            const url = new URL(req.url!, 'http://localhost');
            const queryTarget = url.searchParams.get('target_base');
            if (queryTarget) return queryTarget;

            return 'http://localhost:8188';
          },
          rewrite: (path: string) => path.replace(/^\/comfy-bridge/, ''),
          onProxyReq: (proxyReq: any) => { // Fixed implicit any and removed unused params
            // CRITICAL: Strip security headers that cause 403 on ComfyUI
            proxyReq.removeHeader('origin');
            proxyReq.removeHeader('referer');
            proxyReq.removeHeader('x-bridge-target'); // Clean up internal header
            
            // Also clean up query params used for routing
            if (proxyReq.path.includes('target_base=')) {
                proxyReq.path = proxyReq.path.replace(/[&?]target_base=[^&]*/, '');
                if (proxyReq.path.endsWith('?') || proxyReq.path.endsWith('&')) {
                    proxyReq.path = proxyReq.path.slice(0, -1);
                }
            }
          },
          onProxyRes: (proxyRes: any) => { // Fixed implicit any and removed unused params
            // Ensure CORS is handled by the proxy
            proxyRes.headers['Access-Control-Allow-Origin'] = '*';
          }
        }
      }
    },
    preview: {
      port: 7860,
      host: true,
      allowedHosts: true,
      proxy: {
        // Implementation duplicated for preview mode (HF Spaces production)
        '/comfy-bridge': {
          target: 'http://localhost:8188',
          changeOrigin: true,
          secure: false,
          ws: true,
          router: (req: any) => { // Added explicit 'any' type
            const target = req.headers['x-bridge-target'] as string;
            if (target) return target;
            const url = new URL(req.url!, 'http://localhost');
            const queryTarget = url.searchParams.get('target_base');
            return queryTarget || 'http://localhost:8188';
          },
          rewrite: (path: string) => path.replace(/^\/comfy-bridge/, ''),
          onProxyReq: (proxyReq: any) => { // Fixed implicit any
            proxyReq.removeHeader('origin');
            proxyReq.removeHeader('referer');
            proxyReq.removeHeader('x-bridge-target');
          }
        }
      }
    }
  };
});