jlalmes / trpc-openapi

OpenAPI support for tRPC 🧩
https://www.npmjs.com/package/trpc-openapi
MIT License
2.12k stars 136 forks source link

Fastify adapter fails with 'premature close' error when used together with fastify-compress #449

Open NaimKabir opened 2 months ago

NaimKabir commented 2 months ago

If you modify the simple starter example with compression:

import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
import Fastify from 'fastify';
import { fastifyTRPCOpenApiPlugin } from 'trpc-openapi';

import { appRouter } from './router';

const fastify = Fastify();

async function main() {
  await fastify.register(fastifyTRPCPlugin, { router: appRouter });
  await fastify.register(fastifyTRPCOpenApiPlugin, { router: appRouter });
  await server.register(import("@fastify/compress"), encodings: ["gzip", "deflate"]})  /* 👈 */

  await fastify.listen({ port: 3000 });
}

main();

This will fail for responses that include very long strings (more than a kilobyte).

Logging verbosely with Fastify in these cases reveals this error:

ERROR (88453): premature close
     reqId: "req-1"    
     err: {             
       "type": "Error",    
       "message": "premature close",
       "stack":      
           Error: premature close

This is related to issues filed but closed as caller-errors in the fastify-compress repo:

Not entirely sure if their recommendations apply here since you do in fact use reply.send(body) in your Fastify adapter—but this does error out in a way that's eerily close, still.

Appreciate any insight you can get me—love the library otherwise, thanks!