Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

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

Open
NaimKabir opened this issue May 17, 2024 · 0 comments

Comments

@NaimKabir
Copy link

NaimKabir commented May 17, 2024

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!

@NaimKabir NaimKabir changed the title Fastify adapter fails when used together with fastify-compression Fastify adapter fails when used together with fastify-compress May 17, 2024
@NaimKabir NaimKabir changed the title Fastify adapter fails when used together with fastify-compress Fastify adapter fails with 'premature close' error when used together with fastify-compress May 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant