export const als = new AsyncLocalStorage();
router.get("/", async () => {
return als.run("foo", async () => {
const resp = foo.bar();
return new Response(resp);
});
});import { als } from ".";
export default {
bar: () => {
return als.getStore();
},
};alsglobalThis.als = new AsyncLocalStorage would workglobalThis in the first place and bypass ALS?)import reference modules/files.globalThis.als = new AsyncLocalStorage(); seems to work too

d7d6d2f4 isn't overwritten by 987f4e92 like it would be if you just stored them on globalThis)globalThis runs the risk of multi-request leakage, how come I avoid that problem by hooking the (same) ALS onto globalThis? How does that avoid the same issue, since each request will be running that code (making a store, hooking the same property name onto it?) In other words, what guarantees that request 1 hooking foo=1 and request 2 hooking foo=2 will be preserved within the scope of their respective requests, using ALS?

globalThis.foo set in one file and read in another file risks mutation/leakage from other requestsglobalThis.als, where als is a store with {foo: <something>} set on it, avoids that problem
fetch handler which is it's own "asynchronous duration" - it's an async function, basically called by the runtime.als.run(..., async () => { ... }); inside your fetch handler, als keeps track of what context it was called in (this specific call of the fetch handler).als.getStore(), those modules were called within that run callback - it's tied to that invocation of the fetch handler.als.getStore() within the fetch handler but outside of the run callback, it'd be undefined. run stores what you pass to it in a context that's only available to the callback, and other calls to run by other requests doesn't mutate our store since als knows that is a separate invocation of the fetch handler.wrangler dev --remoteals aren't susceptible to mutation from other requests, but other things e.g. on globalThis.myObj are. I never really "got" that until now. Appreciate the explanation!node:async_hooks?platform is not a documented config property that I can seecompatibility_flags = [ "nodejs_als" ] in the TOML but that raises the error _"No such compatibility flag: nodejsals"fs. Filesystem doesn't really make sense as a concept when using Workers. I haven't had any other issues using node modules.alsalsalsalsalsglobalThis.als = new AsyncLocalStorageglobalThisglobalThisglobalThisglobalThisimport { als } from ".";globalThis.als = new AsyncLocalStorage();d7d6d2f4987f4e92foo=1foo=2globalThis.fooglobalThis.als{foo: <something>}als.run(..., async () => { ... });als.getStore()als.getStore()runrunrunrunwrangler dev --remoteglobalThis.myObjnode:async_hooksplatformcompatibility_flags = [ "nodejs_als" ]fsfs