Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about cloud edge functions support #161

Open
elct9620 opened this issue Nov 1, 2024 · 8 comments
Open

Question about cloud edge functions support #161

elct9620 opened this issue Nov 1, 2024 · 8 comments

Comments

@elct9620
Copy link

elct9620 commented Nov 1, 2024

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
Copy link
Owner

dahlia commented Nov 1, 2024

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. 🤔

@elct9620
Copy link
Author

elct9620 commented Nov 2, 2024

I will test it before I start my project. If I encounter any problems, I will report them or make a PR.

@elct9620
Copy link
Author

elct9620 commented Nov 2, 2024

Tested with Cloudflare Workers (local dev mode) but the shim-deno broke it.

✘ [ERROR] service core:user:poc-fedify: Uncaught ReferenceError: __dirname is not defined

    at null.<anonymous> (index.js:4178:186) in
  node_modules/@deno/shim-deno/dist/deno/stable/variables/mainModule.js
    at null.<anonymous> (index.js:32:50) in __require2
    at null.<anonymous> (index.js:6211:27) in
  node_modules/@deno/shim-deno/dist/deno/stable/variables.js
    at null.<anonymous> (index.js:32:50) in __require2
    at null.<anonymous> (index.js:11061:26) in
  node_modules/@deno/shim-deno/dist/deno/stable/functions.js
    at null.<anonymous> (index.js:32:50) in __require2
    at null.<anonymous> (index.js:11503:18) in node_modules/@deno/shim-deno/dist/deno/stable/main.js
    at null.<anonymous> (index.js:32:50) in __require2
    at null.<anonymous> (index.js:11541:18) in node_modules/@deno/shim-deno/dist/deno.js
    at null.<anonymous> (index.js:32:50) in __require2

It seems some packages or features are not supported by Cloudflare Worker.
https://developers.cloudflare.com/workers/runtime-apis/nodejs/

Is it possible to avoid using shim in Fedify for better capabilities?

@dahlia
Copy link
Owner

dahlia commented Nov 3, 2024

We're willing to support edge functions including Cloudflare Workers in a long term, but I guess it wouldn't be trivial in a short term… 😭 One cloud edge function I've tested with Fedify is Deno Deploy at the moment.

@dahlia
Copy link
Owner

dahlia commented Nov 3, 2024

Ah, Fedify on JSR would work better with edge functions.

@elct9620
Copy link
Author

elct9620 commented Nov 3, 2024

Switching to npx jsr add @fedify/fedify can resolve the shim issue. However, it throws a Temporal is not defined error.

I use new SimpleKvStore() here, but not relate it because I created a mock store that still does not work.

✘ [ERROR] ReferenceError: Temporal is not defined

      at kvCache
  (file:///Users/elct9620/Desktop/poc-fedify/node_modules/@fedify/fedify/runtime/docloader.ts:356:26)
      at new FederationImpl
  (file:///Users/elct9620/Desktop/poc-fedify/node_modules/@fedify/fedify/federation/middleware.ts:354:53)
      at createFederation
  (file:///Users/elct9620/Desktop/poc-fedify/node_modules/@fedify/fedify/federation/middleware.ts:245:10)
      at Array.<anonymous> (file:///Users/elct9620/Desktop/poc-fedify/src/index.ts:7:16)
      at Object.dispatch
  (file:///Users/elct9620/Desktop/poc-fedify/node_modules/hono/dist/hono-base.js:187:37)
      at Object.fetch
  (file:///Users/elct9620/Desktop/poc-fedify/node_modules/hono/dist/hono-base.js:213:17)
      at fetchDispatcher
  (file:///Users/elct9620/Desktop/poc-fedify/.wrangler/tmp/bundle-ZHjIs9/middleware-loader.entry.ts:54:17)
      at __facade_invokeChain__
  (file:///Users/elct9620/Desktop/poc-fedify/node_modules/wrangler/templates/middleware/common.ts:53:9)
      at Object.next
  (file:///Users/elct9620/Desktop/poc-fedify/node_modules/wrangler/templates/middleware/common.ts:50:11)
      at jsonError
  (file:///Users/elct9620/Desktop/poc-fedify/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:30)


@elct9620
Copy link
Author

elct9620 commented Nov 3, 2024

Updated:

Fixed, the typo caused the web finger not to return anything.


I can run it correctly after adding polyfill.

According to the tutorial - https://fedify.dev/tutorial/basics#actor-dispatcher

I can call curl -H "Accept: application/activity+json" http://localhost:8787/users/me to get the correct response, but Web Finger (curl "http://localhost:8787/.well-known/webfinger?resource=acct:me@localhost:887") returns 404.

@dahlia
Copy link
Owner

dahlia commented Nov 3, 2024

Could you try a polyfill for Temporal API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants