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

Add environment to Cloudflare adapter #2844

Closed
piperswe opened this issue Nov 20, 2021 · 7 comments
Closed

Add environment to Cloudflare adapter #2844

piperswe opened this issue Nov 20, 2021 · 7 comments

Comments

@piperswe
Copy link

Describe the problem

Cloudflare Workers provide an environment object containing environment variables and bindings to other Cloudflare services, such as Workers KV and Durable Objects. Currently, the Cloudflare adapter doesn't seem pass this object to endpoints, so those endpoints can't access those services.

Describe the proposed solution

The Cloudflare adapter should pass the environment object through to the endpoint somehow, potentially through the request's locals. In addition, it would be great to have a way to export Durable Object classes.

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

@piperswe
Copy link
Author

Related to #2807

@tv42
Copy link

tv42 commented Dec 6, 2021

Not being able to access Cloudflare KV makes implementing sessions way too hard :-(

@samuba
Copy link

samuba commented Dec 6, 2021

@tv42 yeah it is a pity. For now I'm using KV via API in my project. It works but is cumbersome.

@denizkenan
Copy link

denizkenan commented Dec 6, 2021

I had to fork existing adapter and pass env to request object. It is a pity that this adapter even exists(it is pre-alpha IMHO). Without providing access to ENV & Durable Objects, using workers is almost pointless.

@eric-naguras
Copy link

We need this, I hope it will be fixed soon.

@bryanwood
Copy link

bryanwood commented Jan 8, 2022

I had to fork existing adapter and pass env to request object. It is a pity that this adapter even exists(it is pre-alpha IMHO). Without providing access to ENV & Durable Objects, using workers is almost pointless.

This no longer works since the commit that added a check for the keys being passed in from the adapter:

// TODO remove this for 1.0
if (Object.keys(request).sort().join() !== 'headers,method,rawBody,url') {
throw new Error('Adapters should call app.render({ url, method, headers, rawBody })');
}

As a workaround I'm adding env as a property/symbol so it'll be available as in hooks.js.

In the forked adapter:

 const rendered = await app.render({
        url,
        rawBody: new Uint8Array(await req.arrayBuffer()),
        headers: Object.fromEntries(req.headers),
        method: req.method,
        [Symbol.for("cf-env")]: env,
      });

I imagine passing extra keys on the request object would begin working again once that TODO to remove the check is resolved.

Not sure if passing extra properties to the request object is going to be a reliable way of passing platform features along to hooks.js and endpoints, guessing it's not specifically part of the app.render api contract?

Edit: Changed to passing env via symbol property so it doesn't appear in Object.keys().

@benmccann
Copy link
Member

This should be implemented now

https://github.com/sveltejs/kit/tree/master/packages/adapter-cloudflare#environment-variables

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

No branches or pull requests

7 participants