Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug?]: Error when using Fastify Compress Plugin due to stream being closed prematurely #6765

Closed
1 task done
dthyresson opened this issue Oct 31, 2022 · 0 comments · Fixed by #6766
Closed
1 task done
Assignees
Labels
bug/confirmed We have confirmed this is a bug

Comments

@dthyresson
Copy link
Contributor

What's not working?

When registering the fastify/compress plugin

    await fastify.register(import('@fastify/compress'), {
      global: true,
      threshold: 1_024,
      encodings: ['deflate'],
    })

and making a request, there is an error in the compress plugin and fustily due to the stream closing prematurely:

api | 06:43:01 🐛 graphql-server GraphQL execution completed: BlogPostsQuery
api | 06:43:01 🐛 Execution end
api | 06:43:01 🌲 stream closed prematurely
api | 06:43:01 🚨 premature close 
api | 
api | 🚨 Error Info
api | 
api | {}
api |  
api | 🥞 Error Stack
api | 
api | Error: premature close
api |     at onclosenexttick (/Users/dthyresson/Dropbox/Code/throwaways/blog-compress/node_modules/end-of-stream/index.js:54:86)
api |     at processTicksAndRejections (node:internal/process/task_queues:78:11)
api | 
a

How do we reproduce the bug?

See config above.

There is discussion on Fastify about such behavior:

fastify/fastify-compress#215
fastify/fastify#3651

Fix

A fix seems to be return the reply

const fastifyResponseForLambdaResult = (
  reply: FastifyReply,
  lambdaResult: APIGatewayProxyResult
) => {
  const {
    statusCode = 200,
    headers,
    body = '',
    multiValueHeaders,
  } = lambdaResult
  const h = mergeMultiValueHeaders(headers, multiValueHeaders)
  reply.headers(h)
  reply.status(statusCode)

  if (lambdaResult.isBase64Encoded) {
    // Correctly handle base 64 encoded binary data. See
    // https://aws.amazon.com/blogs/compute/handling-binary-data-using-amazon-api-gateway-http-apis
    return reply.send(Buffer.from(body, 'base64')) // <---- add return
  } else {
    return reply.send(body) // <---- add return
  }
}

and now can see that deflate is applied as the compression:

image

What's your environment? (If it applies)

No response

Are you interested in working on this?

  • I'm interested in working on this
@dthyresson dthyresson added bug/needs-info More information is needed for reproduction bug/confirmed We have confirmed this is a bug labels Oct 31, 2022
@dthyresson dthyresson self-assigned this Oct 31, 2022
@dthyresson dthyresson changed the title [Bug?]: [Bug?]: Error when using Fastify Compress Plugin due to stream being closed prematurely Oct 31, 2022
@dac09 dac09 removed the bug/needs-info More information is needed for reproduction label Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/confirmed We have confirmed this is a bug
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants