dahlia / fedify

ActivityPub server framework in TypeScript
https://fedify.dev/
MIT License
547 stars 20 forks source link

Question about cloud edge functions support #161

Open elct9620 opened 2 hours ago

elct9620 commented 2 hours ago

Hi,

I find the microblog document has some notes about Recently, cloud edge functions like Cloudflare Workers have also gained popularity as JavaScript runtimes

Does Fedify expect to support it natively?

If I choose to use Cloudflare Workers, I need to use the Bindings feature to get the key-value store.

interface Bindings {
  Cache: KVNamespace;
}

const app = new Hono<{ Bindings: Bindings }>()

// ...
app.use(federation(fedi, (ctx) => {
   // the "ctx.env.Cache" is here
  return // ...
}));  

However, the Federation object is required kv and initialized before handling the request.

const fedi = createFederation<string>({
  // ctx.env.Cache is not available yet
});

const app = new Hono();
app.use(federation(fedi, (ctx) => "context data"));  

Or the expected usage is like this

interface Bindings {
  Cache: KVNamespace;
}

const app = new Hono<{ Bindings: Bindings }>()

app.use(async (ctx, next) => {
  const fedi = createFederation<string>({
    kv: new MyKVStore(ctx.env.Cache)
  });

  ctx.set('fedi', fedi) // use this later

  await next()
});  
dahlia commented 2 hours ago

Creating a Federation instance for every request technically makes no problem, but I doubt that Fedify currently work well on Cloudflare Workers, as it's never tested on that. 🤔