fastify-reply-from icon indicating copy to clipboard operation
fastify-reply-from copied to clipboard

onResponse hook TypeScript definition issue with response.stream.

Open gwicks opened this issue 5 months ago • 2 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.0.0

Plugin version

11.0.1

Node.js version

20.x

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

14.5

Description

(I did check this issue as well, but doesn't seem immediately related: https://github.com/fastify/fastify-reply-from/issues/334 )

Hello,

I am using the onResponse hook to properly implement some custom tracing with OpenTelemetry and need the hook to otherwise behave normally, but trigger some extra logic to clean up a OpenTelemetry Span stored in the request. Note that I am using this inside of @fastify/http-proxy but the underlying issue is coming from this package since that package simply wraps this one.

There seems to be a moderate issue when using TypeScript and overriding the onResponse hook. Specifically, there's seemingly no good way to access the stream on RawReplyDefaultExpression<RawServerBase> that's assigned to the res in that hook. I see that it's defined in Http2ServerResponse type in Fastify, and that RawReplyDefaultExpression is supposed to be a union of all the possible response types (HTTP and HTTP2). I have my proxy configured as HTTP/1.

Previously, I was able to get away with

onResponse: async (
        request,
        reply,
        res: RawReplyDefaultExpression<RawServerBase>
      ) => {
        if (request.currentSubspan) {
          request.currentSubspan.setAttribute(
            "reply.elapsedTime",
            reply.elapsedTime
          );
          request.currentSubspan.end();
          request.currentSubspan = null;
        }
        reply.send(res);
    }
),
http2: false,

But upon upgrading to Fastify 5.x and the latest version of this plugin, this no longer functions, despite res itself being defined as a stream. Using this approach currently results in a blank response where it previously.

If you try to use res.stream, you will get the following error in the TypeScript compiler.

Property 'stream' does not exist on type 'RawReplyDefaultExpression<RawServerBase>'.
  Property 'stream' does not exist on type 'ServerResponse<IncomingMessage>'.

Right now, my workaround is.

      onResponse: async (
        request,
        reply,
        res: RawReplyDefaultExpression<RawServerBase>
      ) => {
        if (request.currentSubspan) {
          request.currentSubspan.setAttribute(
            "reply.elapsedTime",
            reply.elapsedTime
          );
          request.currentSubspan.end();
          request.currentSubspan = null;
        }
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        reply.send((res as any)?.stream);
      },
      http2: false,

This is really suboptimal for pretty obvious reasons. The actual structure of response is not reflected in the TypeScript definition, so as a result, it's difficult to use properly. I suppose the "fix" here would be to ensure there's a definition of stream in the type definition.

Link to code that reproduces the bug

No response

Expected Behavior

I would expect the response.stream property to be defined in some fashion in the exported TypeScript definitions for the onResponse hook.

gwicks avatar Sep 23 '24 17:09 gwicks