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

feat(cloudflare): expose event.context.cf #927

Merged
merged 3 commits into from
Feb 8, 2023
Merged

feat(cloudflare): expose event.context.cf #927

merged 3 commits into from
Feb 8, 2023

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Feb 8, 2023

๐Ÿ”— Linked issue

resolves #391

closes #677

โ“ Type of change

  • ๐Ÿ“– Documentation (updates to the documentation or readme)
  • ๐Ÿž Bug fix (a non-breaking change that fixes an issue)
  • ๐Ÿ‘Œ Enhancement (improving an existing functionality like performance)
  • โœจ New feature (a non-breaking change that adds functionality)
  • โš ๏ธ Breaking change (fix or feature that would cause existing functionality to change)

๐Ÿ“š Description

This PR exposes cloudflare request context as event.context.cf.

Implementation is based on new unenv support. Any other platform based on localFetch, can expose the same context by passing { context: {} } to the request init.

A new generic handler is added to nitro. It will be used later for other purposes as well.

Note: For nested $fetch, we should explicitly pass the cf to be proxied:

  await $fetch("/test", { context: { cf: event.context.cf } });

๐Ÿ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@codecov
Copy link

codecov bot commented Feb 8, 2023

Codecov Report

Merging #927 (6f6d0e0) into main (247f312) will not change coverage.
The diff coverage is n/a.

โ— Current head 6f6d0e0 differs from pull request most recent head 14318bd. Consider uploading reports for the commit 14318bd to get more accurate results

@@           Coverage Diff           @@
##             main     #927   +/-   ##
=======================================
  Coverage   67.77%   67.77%           
=======================================
  Files          59       59           
  Lines        5992     5992           
  Branches      679      679           
=======================================
  Hits         4061     4061           
  Misses       1922     1922           
  Partials        9        9           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@altryne
Copy link

altryne commented Feb 19, 2023

Is there a way to use this to get bindings from .env?

I saw a screenshot from Atinux about storing state in cloudflare KV workers, so it seems that conceptually that's possible?

But the above exposed request.cf does not include the .env (nor the secrets stored in cloudflare) nor the service, KV, queue bindings.

Is there a way that I'm just missing?

@pi0
Copy link
Member Author

pi0 commented Feb 20, 2023

@dario-piotrowicz
Copy link
Contributor

@altryne you're right you cannot access KVs or other Cloudflare services from what it's exposed, we need to pass the env and ctx to the user as I am doing here for the cloudflare-pages adaptor: #997

After I (hopefully) get the cloudflare-pages adaptor PR merged I can look into adding access to the env and ctx to the cloudflare worker adaptor as well, although in order to do so we'd fist need to convert the adaptor to the ESM syntax as now it uses the old service worker one in which the various variables are just put in the global scope (and some like DO aren't even available).

@altryne
Copy link

altryne commented Feb 25, 2023

I actually found a way! it's possible via self.

https://discord.com/channels/473401852243869706/1002541842409193563/1077016381662433372

@dario-piotrowicz
Copy link
Contributor

ah wow, that's interesting! ๐Ÿ˜ฎ

are you deploying your Nuxt app via the worker adaptor? (not the pages one)
because if you are yeah I do imagine that the bindings are just added to the global scope by Cloudflare so they end up being accessible by self ๐Ÿค”

@altryne
Copy link

altryne commented Feb 26, 2023

Yeah not via the pages one.

@dario-piotrowicz
Copy link
Contributor

ah ok I see, that makes sense, thanks ๐Ÿ™‚๐Ÿ‘

@altryne
Copy link

altryne commented Feb 26, 2023

You can use storage layer to store data in KV, Filesystem, memory and anywhere else.

@pi0 I'm so sorry I missed your comment on this, however, this isn't answering the question of accessing cloudflare bindings on .env.

The storage layer adds storage, however, on the .env, cloudflare is storing the following things that are not storage:

All those things are needed when developing a server side application on cloudflare, all their examples aways require to take this information from the request context and I think it would be great to have them, and not have to do hacks like this one:
https://discord.com/channels/473401852243869706/1002541842409193563/1077016381662433372

I think for many worker devs, this would be more important than the context of the IP and country (which is also important)

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

Successfully merging this pull request may close these issues.

Cloudflare request.cf
3 participants