You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Deno集成Supabase时的代理配置可行方案咨询

在Deno中为Supabase配置代理的可行方案

问题描述

我在Deno里用createHttpClient配置代理很方便,但这个方法没法和Supabase配合使用。试了好几种在fetch里配置代理的方案都没成功,想问问这种场景下怎么配置代理。

当前我的实现代码:

const proxyUrl = Deno.env.get("PROXY_URL");
const proxyUsername = Deno.env.get("PROXY_USERNAME"); 
const proxyPassword = Deno.env.get("PROXY_PASSWORD");

if (!proxyUrl || !proxyUsername || !proxyPassword) {
  throw new Error("Proxy configuration is missing");
}

const proxyAuthUrl = `https://${proxyUsername}:${proxyPassword}@${proxyUrl}`;

const testUrl = ``;
const response = await this.httpClient.fetch(testUrl, {
  method: 'GET',
  proxy: proxyAuthUrl
});

可行解决方案

方案1:自定义Supabase客户端的fetch实现

Supabase客户端允许传入自定义fetch函数,你可以基于Deno的createHttpClient创建带代理的客户端,再封装成标准fetch函数传给Supabase:

import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
import { createHttpClient } from "https://deno.land/std@0.200.0/http/client.ts";

// 初始化带代理的HTTP客户端
const proxyUrl = Deno.env.get("PROXY_URL");
const proxyUsername = Deno.env.get("PROXY_USERNAME");
const proxyPassword = Deno.env.get("PROXY_PASSWORD");

if (!proxyUrl || !proxyUsername || !proxyPassword) {
  throw new Error("Proxy configuration is missing");
}

const proxyAuthUrl = `https://${proxyUsername}:${proxyPassword}@${proxyUrl}`;
const httpClient = createHttpClient({ proxy: proxyAuthUrl });

// 封装成符合标准的fetch函数
const proxyFetch = async (url: string | URL, init?: RequestInit) => {
  const response = await httpClient.fetch(url, init);
  return new Response(response.body, {
    status: response.status,
    statusText: response.statusText,
    headers: response.headers,
  });
};

// 创建Supabase客户端时传入自定义fetch
const supabase = createClient(
  Deno.env.get("SUPABASE_URL")!,
  Deno.env.get("SUPABASE_ANON_KEY")!,
  {
    global: {
      fetch: proxyFetch,
    },
  }
);

// 测试使用
const { data, error } = await supabase.from("your_table").select("*");

方案2:系统级环境变量配置代理(限部分场景)

如果你的代理支持HTTP/HTTPS环境变量,可直接在启动Deno前设置:

export HTTP_PROXY=https://${proxyUsername}:${proxyPassword}@${proxyUrl}
export HTTPS_PROXY=https://${proxyUsername}:${proxyPassword}@${proxyUrl}
deno run --allow-env --allow-net your_script.ts

注意:这种方式依赖Deno的fetch是否读取系统代理变量,部分版本可能需额外配置,不适用于动态切换代理的场景。

方案3:搭建本地代理中间层

如果前两种方法无效,可以用Deno写一个简单的转发中间层,让Supabase请求先到中间层,再由中间层通过代理转发到Supabase:

// proxy-middleware.ts
import { serve } from "https://deno.land/std@0.200.0/http/server.ts";
import { createHttpClient } from "https://deno.land/std@0.200.0/http/client.ts";

const proxyUrl = Deno.env.get("PROXY_URL");
const proxyUsername = Deno.env.get("PROXY_USERNAME");
const proxyPassword = Deno.env.get("PROXY_PASSWORD");
const targetBaseUrl = Deno.env.get("SUPABASE_URL")!;

if (!proxyUrl || !proxyUsername || !proxyPassword) {
  throw new Error("Proxy configuration is missing");
}

const proxyAuthUrl = `https://${proxyUsername}:${proxyPassword}@${proxyUrl}`;
const httpClient = createHttpClient({ proxy: proxyAuthUrl });

serve(async (req) => {
  // 构造目标Supabase URL
  const targetUrl = new URL(req.url.replace("/proxy", targetBaseUrl));
  // 转发请求
  const response = await httpClient.fetch(targetUrl, {
    method: req.method,
    headers: req.headers,
    body: req.body,
  });
  return new Response(response.body, {
    status: response.status,
    headers: response.headers,
  });
}, { port: 8080 });

启动中间层后,修改Supabase客户端的请求地址指向中间层:

const supabase = createClient(
  "http://localhost:8080/proxy",
  Deno.env.get("SUPABASE_ANON_KEY")!
);

内容的提问来源于stack exchange,提问作者Joran Dob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 02:22:42