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
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
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
yep
and it worked until last week
I have no idea what's going on tbh
GitHub
GitHub - helloimalastair/paolo-middleware
Contribute to helloimalastair/paolo-middleware development by creating an account on GitHub.
TL;DR:
context.data.hc
, instead of context.hc
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()
A bit cleaner:
Maybe
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
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.
Looks like a global, but isn't
Sort of
I had a look but it looks expensive at scale
? It's not billed
is it not?
this changes quite a lot of unrelated things
ALS isn't, no
oh damn I reimplemented sessions from scratch
WELL
every day is a school day! 😄
@HardlyWorkin' thank you kindly :MeowHeartCloudflare:
Happy to help!
:blobhaj:
I have a turbo-stupid idea
?
if the runtime has zero-cost stacktrace I can call HC() and recover the topmost onRequest* and get the context out of it
I don't think they are zero-cost?
it's runtime dependant
I meant
workerd
specificallywhat's workerd/functions based on? V8?
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 safeALS?
AsyncLocalStorage