Skip to content

Use AsyncLocalStorage - #40

Open
jplhomer wants to merge 11 commits into
mainfrom
use-als
Open

Use AsyncLocalStorage#40
jplhomer wants to merge 11 commits into
mainfrom
use-als

Conversation

@jplhomer

@jplhomer jplhomer commented Mar 23, 2023

Copy link
Copy Markdown
Owner

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 🙃

/**
* 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
Copy Markdown
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
Copy Markdown
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.

Comment thread packages/superflare/src/context.ts Outdated
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
Copy Markdown
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

blittle commented Mar 28, 2024

Copy link
Copy Markdown
Contributor

👀

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