How to pass reference to supabase instead object in Cloudflare worker

I created supabase client and pass reference to supbase into fetch function, this is worker:

 addEventListener(“fetch”, (event) => {
   const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
   event.respondWith(handleRequest(event.request, {supabase:supabase}));
 });

 async function handleRequest(request, obj) {
 ...
 else if (request.url.includes("script.js")) {
 return new Response(js(request, obj.supabase), {
   headers: { "content-type": "text/javascript" },
 });
 ...

inside my script I realize my algorithm, supabase operation need to my algorithm, something like this

const js = (request, supabase) => {
  return ` 
  send = document.getElementById('send') 
  send.supabase=${supabase} 
  .... 
  }});`
}
export default js;

without supabase all working fine, but I watch my script in browser now and see

const send = document.getElementById('send')
send.supabase=[object Object]

How to receive reference in this place instead object?

I resolve this issue by injection supabase client to worker:

const js = (request, supabase) =>  {
    return `
import { createClient } from 'https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm'
const supabase = createClient('${supabase.URL}', '${supabase.KEY}')
...
`
I have injected Supabase Key and Url in boilerplate level, and than create supabase client in worker level. Of course, this is less secure solution, anybody can see you Key and Url, therefore Row-Level Security for Supabase must be added and configured.
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.