Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
vovkman committed May 10, 2024
1 parent 4a4a73c commit 64b73c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
28 changes: 13 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ interface Env {

export default {
async fetch(request: Request, env: Env) {

// If the request is an OPTIONS request, return a 200 response with permissive CORS headers
// This is required for the Helius RPC Proxy to work from the browser and arbitrary origins
// If you wish to restrict the origins that can access your Helius RPC Proxy, you can do so by
Expand All @@ -15,47 +14,46 @@ export default {
// originated from one of the domains in the `CORS_ALLOW_ORIGIN` environment variable.
const supportedDomains = env.CORS_ALLOW_ORIGIN ? env.CORS_ALLOW_ORIGIN.split(',') : undefined;
const corsHeaders: Record<string, string> = {
"Access-Control-Allow-Methods": "GET, HEAD, POST, PUT, OPTIONS",
"Access-Control-Allow-Headers": "*",
}
'Access-Control-Allow-Methods': 'GET, HEAD, POST, PUT, OPTIONS',
'Access-Control-Allow-Headers': '*',
};
if (supportedDomains) {
const origin = request.headers.get('Origin')
const origin = request.headers.get('Origin');
if (origin && supportedDomains.includes(origin)) {
corsHeaders['Access-Control-Allow-Origin'] = origin
corsHeaders['Access-Control-Allow-Origin'] = origin;
}
} else {
corsHeaders['Access-Control-Allow-Origin'] = '*'
corsHeaders['Access-Control-Allow-Origin'] = '*';
}

if (request.method === "OPTIONS") {
if (request.method === 'OPTIONS') {
return new Response(null, {
status: 200,
headers: corsHeaders,
});
}

const upgradeHeader = request.headers.get('Upgrade')
const upgradeHeader = request.headers.get('Upgrade');

if (upgradeHeader || upgradeHeader === 'websocket') {
return await fetch(`https://mainnet.helius-rpc.com/?api-key=${env.HELIUS_API_KEY}`, request)
return await fetch(`http://mainnet.dev.lb.helius-internal.com`, request);
}


const { pathname, search } = new URL(request.url)
const { pathname, search } = new URL(request.url);
const payload = await request.text();
const proxyRequest = new Request(`https://${pathname === '/' ? 'mainnet.helius-rpc.com' : 'api.helius.xyz'}${pathname}?api-key=${env.HELIUS_API_KEY}${search ? `&${search.slice(1)}` : ''}`, {
const proxyRequest = new Request(`http://mainnet.dev.lb.helius-internal.com`, {
method: request.method,
body: payload || null,
headers: {
'Content-Type': 'application/json',
'X-Helius-Cloudflare-Proxy': 'true',
}
},
});

return await fetch(proxyRequest).then(res => {
return new Response(res.body, {
status: res.status,
headers: corsHeaders
headers: corsHeaders,
});
});
},
Expand Down
13 changes: 12 additions & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
name = "rpc-proxy" # todo
name = "cf-debugging-worker" # todo
main = "./src/index.ts"
compatibility_date = "2022-05-03"
account_id = "677751811ae9772122dc6f1d03848974"
logpush = true
usage_model = "unbound"
node_compat = true

routes = [
{ pattern = "cf-debugging.helius-internal.com", custom_domain = true },
]

[placement]
mode = "smart"

0 comments on commit 64b73c5

Please sign in to comment.