expressjs / compression

Node.js compression middleware
MIT License
2.77k stars 241 forks source link

Prevent multiple injection #160

Closed developit closed 5 years ago

developit commented 5 years ago

Currently, it's possible to apply the compression middleware multiple times, which has undesirable effects. This is particularly easy to do when using Express's Router, as a given Router can install compression but have it bleed into all subsequent middleware & handlers.

This PR introduces a flag on Response objects indicating whether compression has been injected into the response methods, and uses that flag to avoid re-injecting compression.

Issues: this might break composability? I don't know if people have valid use-cases for running multiple instances of this middleware today.

developit commented 5 years ago

Ack! I went to add tests and noticed this is already being handled courtesy of the check for existing response encoding here.