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

refactor: use ALS for process.env object #98

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

james-elicx
Copy link
Collaborator

@james-elicx james-elicx commented Oct 12, 2024

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

  • Pulls out ALS proxy creation into a utility.
  • Assigns a proxy for an ALS to process.
  • Runs the worker inside the ALS with the worker env object spread on the ALS.

I recommend hiding whitespace changes when looking at the diff in this pr.

Copy link

changeset-bot bot commented Oct 12, 2024

🦋 Changeset detected

Latest commit: 9f13e52

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare Patch

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

@james-elicx james-elicx changed the title refactor: extract proxy creation into utility refactor: use ALS for process.env Oct 12, 2024
Copy link

pkg-pr-new bot commented Oct 12, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/@opennextjs/cloudflare@98

commit: 9f13e52

@james-elicx james-elicx marked this pull request as ready for review October 12, 2024 23:53
@james-elicx james-elicx force-pushed the james/process-env-als branch from 0d86245 to e7f9206 Compare October 18, 2024 13:16
@james-elicx james-elicx changed the title refactor: use ALS for process.env refactor: use ALS for process object Oct 23, 2024
@james-elicx james-elicx force-pushed the james/process-env-als branch from a3a98a8 to 9ecefdf Compare October 23, 2024 19:51
@james-elicx james-elicx changed the title refactor: use ALS for process object refactor: use ALS for process.env object Oct 23, 2024
Copy link
Contributor

@dario-piotrowicz dario-piotrowicz left a 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);
Copy link
Contributor

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 }, () => {

Copy link
Contributor

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)

Copy link
Contributor

@petebacondarwin petebacondarwin left a 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 };
Copy link
Contributor

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.

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.

4 participants