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

Reply with a long string will encounter "Premature close #215" #286

Closed
2 tasks done
stanleyxu2005 opened this issue Mar 27, 2024 · 2 comments
Closed
2 tasks done

Comments

@stanleyxu2005
Copy link
Contributor

stanleyxu2005 commented Mar 27, 2024

Prerequisites

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

Fastify version

4.26.2

Plugin version

7.0.1

Node.js version

18.17.1

Operating system

Windows

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

Windows 11

Description

Similar issue to #215

I'm sending a long string as response, with fastify-compress enabled, it will fail with error Premature close.

Steps to Reproduce

  const fastify = new Fastify({})

  fastify.register(fastifyCompress) // #1 disable this line will work

  fastify.get('/subscription', async (req, reply) => {

    const longString = new Array(1024).fill('a').join('') // <--- build a string, whose length is >= 1024 (compress.threshold)

    // return reply.send(longString) // <--- ok, string will be sent

    reply.send(longString) // <--- not ok, if `return ` is forgotten to be added at block end
  })

Expected Behavior

Regardless return is added or not, it should always respond with expected content.

Question

Do you think, there is something can be improved in @fastify/compress

@stanleyxu2005 stanleyxu2005 changed the title Similar issue of "Premature close #215" Reply with a string length is 1024 will encounter "Premature close #215" Mar 27, 2024
@stanleyxu2005 stanleyxu2005 changed the title Reply with a string length is 1024 will encounter "Premature close #215" Reply with a long string will encounter "Premature close #215" Mar 27, 2024
@gurgunday
Copy link
Member

If you have an async handler, you should always return the body instead of using reply.send:

  const fastify = new Fastify({})

  fastify.register(fastifyCompress) // #1 disable this line will work

  fastify.get('/subscription', async (req, reply) => {

    const longString = new Array(1024).fill('a').join('') // <--- build a string, whose length is >= 1024 (compress.threshold)

    return longString
  })

Or:

  const fastify = new Fastify({})

  fastify.register(fastifyCompress) // #1 disable this line will work

  fastify.get('/subscription', (req, reply) => {

    const longString = new Array(1024).fill('a').join('') // <--- build a string, whose length is >= 1024 (compress.threshold)

    reply.send(longString)
  })

You can find more information here: https://fastify.dev/docs/latest/Reference/Routes/#promise-resolution

@gurgunday gurgunday closed this as not planned Won't fix, can't repro, duplicate, stale Mar 27, 2024
@stanleyxu2005
Copy link
Contributor Author

@gurgunday Thanks for sharing info.

It's a bit tricky for a new user. I'd now strictly call return reply.send(...). Syntax sugar is painful when migrating between different frameworks.

I was not aware this when migrating from Koa of promise based routes.

// in koa
async someGetRoute(ctx) {
  ctx.body = await getMyDataAsync()
}

// in fastify
async someGetRoute(request, reply) {
  return reply.send(await getMyDataAsync())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants