Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
This repository was 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

@NaimKabir

Description

@NaimKabir

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions