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

Use AsyncLocalStorage #40

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

Use AsyncLocalStorage #40

wants to merge 11 commits into from

Conversation

jplhomer
Copy link
Owner

@jplhomer jplhomer commented Mar 23, 2023

Now that Cloudflare supports ALS using the nodejs_compat compatibility flag, this allows us to attach Superflare config to a request-local context. This is way better than assigning a bunch of variables to a global singleton because:

  • It no longer bleeds between requests
  • We can assign per-request information like Session and Request and Auth
  • We no longer require the use of Remix loader context (which obvs isn't available in other frameworks anyway)

This means we can start providing helpers like session() and auth(), and use them from within Superflare internals as well 🎉

We've been waiting on a patch to Remix which just landed in dev remix-run/remix#5773

TODO:

  • Fix tests to be able to run with ALS. This is a thing which is difficult apparently across other Node.js projects and testing frameworks.

const articles = await Article.with("user").orderBy("createdAt", "desc");
const articles = await Article.with("user")
.orderBy("createdAt", "desc")
.get();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A limitation with ALS in CF Workers today is that custom thenables don't properly retain context: https://twitter.com/jasnell/status/1634764772121145344

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -1,4 +1,5 @@
import { marked } from "marked";
// TODO: Find a lighter solution for syntax highlighting
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to ALS per se, but my CF Workers bundle is like 4MB because of this 🙃

@@ -51,8 +51,7 @@ export async function handleFetch<Env extends { APP_KEY: string }>(
},
async () => {
/**
* We inject env and session into the Remix load context.
* Someday, we could replace this with AsyncLocalStorage.
* TODO: REMOVE THIS since we're using AsyncLocalStorage
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just deprecate it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, I think I'll still put some separate things in the context for now. But a lot of people probably never should need to care about context.

return await asyncLocalStorage.run({}, async () => {
/**
* I'm being extra cautious to not set the object value until we're _inside_ the asyncLocalStorage.run
* closure. This is to prevent accidental leaks between requests. I don't know if it's necessary.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Verify whether this is necessary or if I can just populate an object outside of the run.

@blittle
Copy link
Contributor

blittle commented Mar 28, 2024

👀

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.

2 participants