Guard AsyncLocalStorage#enterWith on Cloudflare
nitro/cloudflare/no-unguarded-als-enterwithsrc/rule-packs/nitro/rules/no-unguarded-als-enter-with.ts Upstream docs Run this rule
pnpm vite-doctor . --framework nitro --rules nitro/cloudflare/no-unguarded-als-enterwith
Why it matters
workerd implements AsyncLocalStorage run(), exit(), getStore(), bind(), and snapshot(), but enterWith() throws asyncLocalStorage.enterWith() is not implemented, with nodejs_compat or nodejs_als enabled. Code that works in Node.js then fails the request on Cloudflare. The rule runs when Project Inventory shows a Cloudflare target for the workspace package: a wrangler config, a cloudflare Nitro preset, Cloudflare tooling dependencies such as wrangler or @cloudflare/workers-types, a cloudflare package keyword, or cloudflare: imports in the file.
Recommended fix
Run the work inside als.run(store, callback) so the store is scoped to the callback. If the call has to stay for Node.js runtimes, wrap it in try/catch and keep a fallback that does not depend on enterWith().
Useful links
Example
Scope the store with run()
Before
const eventStorage = new AsyncLocalStorage<H3Event>()
export function enterEvent(event: H3Event) {
eventStorage.enterWith(event)
}
After
const eventStorage = new AsyncLocalStorage<H3Event>()
export function runWithEvent<T>(event: H3Event, callback: () => T): T {
return eventStorage.run(event, callback)
}