yes because i will need multiple workers to run with this parent workers on different urls. There wo
yes because i will need multiple workers to run with this parent workers on different urls. There would be too many combinations to handle
global.js, containing a {}, which multiple scripts import and hook things onto, will this leak/bleed into multiple requests to my worker given Node's import caching, or will it be isolated to just the current request?foo=1, and I want to hook that onto a global object so other files can use it, rather than laboriously having to pass it around to all functions. But the next request may send QS foo=2. Sounds like I'll be OK based on what you say. (But presumably in a non-serverless/Worker environment this would be problematic?)
globalObject.localStorage wouldn't work? If so, using AsyncLocalStorage, am I right that it would look something like this, to store a global property for later reading downstream?run takes the object you want to store, and then a callbackrun requires the callbackmystore.run({foo: bar}) is insufficient to write to the storage? If so, what would any callback I passed actually "do", if all I want to do is write for later reading?runrun and return my Response thereasyncLocalStorage.getStore(); to access req, env, ctxrun()'s callback?global.js{}foo=1foo=2globalObject.localStorageimport { AsyncLocalStorage } from 'node:async_hooks';
const fooStore = new AsyncLocalStorage();
fooStore.run('bar'); //<-- running in some async middlewareimport { AsyncLocalStorage } from 'node:async_hooks';
const fooStore = new AsyncLocalStorage();
const fooVal = fooStore.getStore(); //"bar"runrunrunrunmystore.run({foo: bar})asyncLocalStorage.getStore();run()import { AsyncLocalStorage } from "node:async_hooks";
const asyncLocalStorage = new AsyncLocalStorage();
async function doStuff() {
const { req, env, ctx } = asyncLocalStorage.getStore();
const obj = await env.KV.get("foo");
// ...
}
export default {
async fetch(req, env, ctx) {
return asyncLocalStorage.run({ req, env, ctx }, async () => {
await doStuff();
return new Response("Hello World!");
});
},
};