-
Notifications
You must be signed in to change notification settings - Fork 18
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
refactor: use ALS for process.env
object
#98
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 9f13e52 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
packages/cloudflare/src/cli/templates/utils/create-als-proxy.ts
Outdated
Show resolved
Hide resolved
packages/cloudflare/src/cli/templates/utils/create-als-proxy.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Dario Piotrowicz <[email protected]>
0d86245
to
e7f9206
Compare
process
object
This reverts commit 4be38fa.
a3a98a8
to
9ecefdf
Compare
process
objectprocess.env
object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks for the improvement 🙂
@james-elicx if you would could we wait for @petebacondarwin to maybe have a quick look tomorrow? Since @vicb seemed against the change I think it'd be great to have also Pete's point of view on this one 🙂
(globalThis as any)[Symbol.for("__cloudflare-context__")] = createALSProxy(cloudflareContextALS); | ||
|
||
// @ts-expect-error - populated when we run inside the ALS context | ||
globalThis.process.env = createALSProxy(processEnvALS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I agree with @vicb to some extent here.
Before any request, there might already be values on process.env
, which are being lost by assigning it here.
How about we capture the original process.env
somehow and then include that in the initialization of the store below.
E.g.
const originalEnv = globalThis.process.env;
globalThis.process.env = createALSProxy(processEnvALS);
...
export default {
async fetch(request, env, ctx) {
return processEnvALS.run({ NODE_ENV: "production", ...originalEnv, ...env }, () => {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to add tests.
(I think we should requires them in the PR template, as well as repro on issue templat - well we have to add those templates first, I can take care of that next week)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nit - but could still do with a test or two...
@@ -20,6 +20,7 @@ const cloudflareContextALS = new AsyncLocalStorage<CloudflareContext>(); | |||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |||
(globalThis as any)[Symbol.for("__cloudflare-context__")] = createALSProxy(cloudflareContextALS); | |||
|
|||
const originalEnv: Partial<typeof process.env> = { ...globalThis.process.env }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to clone this object here? We make a copy of the originalEnv
when setting up the ALS.
Context
process.env is currently assigned via spreading env when there is no request handler. The env values available should be scoped to a request, and dealt with on a per-request basis.
Changes
I recommend hiding whitespace changes when looking at the diff in this pr.