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

decompressResponse changes the type of the response object #2389

Open
barjin opened this issue Nov 21, 2024 · 0 comments
Open

decompressResponse changes the type of the response object #2389

barjin opened this issue Nov 21, 2024 · 0 comments

Comments

@barjin
Copy link

barjin commented Nov 21, 2024

A response with enabled decompression is a prototype-child of a PassThrough stream, but without the decompression, it's Node's IncomingMessage.

This happens here in got:

response = decompressResponse(response);

Why is it a problem

  1. We get different response objects based on the server's decision (compression/no compression). This is not very clear to anyone who needs to debug this.
  2. The PassThrough stream has some own properties hanging from it (e.g. the headers getter), but the IncomingMessage response only inherits those from the IncomingMessage prototype. This means that { ...passThroughResponse }.headers exists, but { ...incomingMessageResponse }.headers is undefined. See 1. - this only depends on the server's decision, so it gets really confusing.

Minimal reproduction scenario:

// compresedResponse is a PassThrough stream (because of the decompression)
const compressedResponse = await got('http://httpbin.org/gzip');
const { headers: gzipHeaders } = { ...compressedResponse };

// uncompressedResponse is an IncomingMessage (native Node.js response object)
const uncompressedResponse = await got('http://httpbin.org/html');
const { headers: htmlHeaders } = { ...uncompressedResponse };

console.log(`gzip response headers:`, gzipHeaders);
console.log(`html response headers:`, htmlHeaders);

results in

gzip response headers: {
  date: 'Thu, 21 Nov 2024 13:14:39 GMT',
  'content-type': 'application/json',
  'content-length': '236',
  connection: 'keep-alive',
  server: 'gunicorn/19.9.0',
  'content-encoding': 'gzip',
  'access-control-allow-origin': '*',
  'access-control-allow-credentials': 'true'
}
html response headers: undefined
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

1 participant