context item seems not to persist from middleware to middleware and from middleware to function

I found this behaviour trying to set up a session object and attach it to the context in the root middleware, expecting to see it bubble up the middleware chain and eventually into the "leaf" function. Tagging @HardlyWorkin' after asking consent. In the root middleware I attach a function to context and add a field: import { HCContextSetup } from "./libs/HorseCollar"; async function setupHorsecollarSession(context){ console.log("setting up HC in the root middleware"); context.hc = "saddled"; console.log("we are still in the root middleware: is the context saddled? [printing context.hc]", context.hc); const res = await context.next(); return res; } // export const onRequest = [setupHorsecollarSession,errorHandling]; export const onRequest = [setupHorsecollarSession]; Then, in the /api/ middleware: async function authentication(context) { console.log("we are in the /api/ middleware: is the context saddled? [printing context.hc]", context.hc); return context.next(); } export const onRequest = [authentication]; Then in the /api/user/token function: export async function onRequestGet(context) { console.log("we are in the /api/user/token onRequestGet function: is the context saddled? [printing context.hc]", context.hc); return new Response("foo ["+context.hc+"]", { status: 200 }); } output in next message
26 Replies
Paolo of Lost Pages
wrangler 3.53.1 console output: ✨ Compiled Worker successfully ⎔ Reloading local server... setting up HC in the root middleware we are still in the root middleware: is the context saddled? [printing context.hc] saddled
we are in the /api/ middleware: is the context saddled? [printing context.hc] undefined
we are in the /api/user/token onRequestGet function: is the context saddled? [printing context.hc] undefined
[wrangler:inf] GET /api/user/token 200 OK (23ms)
browser output: foo [undefined] $ npx -v 9.6.7 Running on git bash on windows 11
Paolo of Lost Pages
yep and it worked until last week I have no idea what's going on tbh
Hello, I’m Allie!
GitHub
GitHub - helloimalastair/paolo-middleware
Contribute to helloimalastair/paolo-middleware development by creating an account on GitHub.
Hello, I’m Allie!
TL;DR: context.data.hc, instead of context.hc
Paolo of Lost Pages
let me try The outcome is to add a function so I can bring up a session handler thank you, it's working now const user = await context.data.horsecollar().user(); Not pretty but works If we had total isolation I could have a singleton and just call await Horsecollar().user() I think I'll get by calling await Horsecollar(context).user()
Hello, I’m Allie!
A bit cleaner:
export const onRequestGet: PagesFunction<Environment, "", Data> = ({ data: { horsecollar }}) => {
const user = await horsecollar().user();
};
export const onRequestGet: PagesFunction<Environment, "", Data> = ({ data: { horsecollar }}) => {
const user = await horsecollar().user();
};
Maybe
Paolo of Lost Pages
I think I'll just await Horsecollar(context).user() Horsecollar() can get the field from context.data and wrangle stuff out. I'm not sure I want to change the boilerplate for all invocations
Hello, I’m Allie!
You could also use AsyncLocalStorage?
Cloudflare Docs
AsyncLocalStorage · Cloudflare Workers docs
To use Node.js APIs in your Worker, add the nodejs_compat compatibility flag to your wrangler.toml file.
Hello, I’m Allie!
Looks like a global, but isn't Sort of
Paolo of Lost Pages
I had a look but it looks expensive at scale
Hello, I’m Allie!
? It's not billed
Paolo of Lost Pages
is it not? this changes quite a lot of unrelated things
Hello, I’m Allie!
ALS isn't, no
Paolo of Lost Pages
oh damn I reimplemented sessions from scratch WELL every day is a school day! 😄 @HardlyWorkin' thank you kindly :MeowHeartCloudflare:
Hello, I’m Allie!
Happy to help! :blobhaj:
Paolo of Lost Pages
I have a turbo-stupid idea
Paolo of Lost Pages
if the runtime has zero-cost stacktrace I can call HC() and recover the topmost onRequest* and get the context out of it
Hello, I’m Allie!
I don't think they are zero-cost?
Paolo of Lost Pages
it's runtime dependant
Hello, I’m Allie!
I meant workerd specifically
Paolo of Lost Pages
what's workerd/functions based on? V8?
Hello, I’m Allie!
Yeah, but decently modified Some of the runtime, workerd is open-source. But all of the machinery around it isn't I would just go with ALS to be safe
Paolo of Lost Pages
ALS?
Hello, I’m Allie!
AsyncLocalStorage
Want results from more Discord servers?
Add your server