Skip to content

fix: prevents fastify-compress from trying to work with something it cannot#361

Open
Aeolun wants to merge 2 commits intofastify:mainfrom
Aeolun:main
Open

fix: prevents fastify-compress from trying to work with something it cannot#361
Aeolun wants to merge 2 commits intofastify:mainfrom
Aeolun:main

Conversation

@Aeolun
Copy link

@Aeolun Aeolun commented Jun 17, 2025

I'm still trying to figure out how it happens (something involving tRPC responses), but for now I'll be happy if I can stop compress from trying to compress something it can't.

I assume the differing behavior between the automatic compression (hook), and manual compression (the compress function) is because you don't want to mess with payloads (e.g. serialize them) unless people explicitly call the compress function. I've kept that behavior, and the global hook simply doesn't compress if it gets something it cannot deal with, while the compress function, well, it already serialized things, so it might cause wonky behavior if you pass it a Response object.

Tests:

  • Test that Response objects are not compressed on global hook
  • Test that objects get serialized normally when using compress
  • Test that Response objects are compressed after being serialized to {} with the compress function.

Checklist

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening a PR! Can you please add a unit test?

…cannot

Sending something other than buffers of strings in the `payload` happens, but this would break the later `Buffer.byteLength(payload)`

Signed-off-by: Bart Riepe <bart@serial-experiments.com>
@mcollina
Copy link
Member

Great work! Can you document this new option?

@Aeolun

This comment was marked as spam.

@mcollina
Copy link
Member

@Aeolun yes. There is a new option passed as the config of the plugin, and that should be mentioned in the README.md

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

Successfully merging this pull request may close these issues.

2 participants