Middleware in `HttpServer.serve` Unable to Manipulate Response Headers

tl;dr Should middleware used in HttpServer.serve be able to manipulate the response like middleware used elsewhere? It seems that they can't.

We've tried to create a middleware that manipulates the response (in our case setting a new cookie: https://github.com/PREreview/prereview.org/blob/4f4612df836221bff09bfb5f3f7fbcd3f7126e29/src/index.ts#L240). Similar to platform when using it in HttpServer.serve it didn't work: the response was being manipulated after it's been served. We could see it being executed successfully using logging, but the response our client received didn't contain the change. Using NodeHttpServerRequest.toServerResponse we could see that the headers had already been sent.
Moving it outside HttpServer.serve (so composing the middleware: https://github.com/PREreview/prereview.org/commit/4f4612df836221bff09bfb5f3f7fbcd3f7126e29#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80L239-R259) sees it work as expected: our client receives the manipulated response.
Was this page helpful?