-
Hi ! I'm trying, in a middleware, to spy/read/follow or whatever naming is best, a streamed response. Context:
I've tried many, many, many things i could find on the web (clone the response, etc), most of the time i either end up reading the stream and i block the response, or i manage to somehow follow the stream but the client gets the response in one time. The why:The idea is to log some of the data returned into the stream. My workaround:While i want to avoid touching my handler code, i added a context variable that contains a function, called when the stream is done :
Forgive me for this partial and somehow pseudo-code, if someone get the idea and needs more, i'm listening :) My middleware :
Thanks a lot for any tips towards a solution 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Turns out i found the solution, i guess i needed another look with a cleared head. There is some obvious null checks to add depending on your use-case and code, but if are trying to achieve something similar, it should work :) Disclamer: this has only been tested locally with wrangler, and on Cloudflare Also, if you don't know this yet, remember to use this whenever you have a promise in your callback :
The middleware:
|
Beta Was this translation helpful? Give feedback.
Turns out i found the solution, i guess i needed another look with a cleared head.
Here is my code for the posterity, feel free to challenge it :)
There is some obvious null checks to add depending on your use-case and code, but if are trying to achieve something similar, it should work :)
Disclamer: this has only been tested locally with wrangler, and on Cloudflare
Also, if you don't know this yet, remember to use this whenever you have a promise in your callback :
The middleware: