Skip to content

Read a Streamed response in a middleware #2397

Closed Answered by unsync
unsync asked this question in Q&A
Discussion options

You must be logged in to vote

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 :

c.executionCtx.waitUntil(myAsyncPromise());

The middleware:

export function myMiddleware() {
    return async function handle(c: Context, next: Next) {
        // do what you want here
        await next()
        
        co…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by unsync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant