undici icon indicating copy to clipboard operation
undici copied to clipboard

request dont decompress response body

Open PandaWorker opened this issue 2 years ago • 15 comments

Bug Description

Why doesn't request support response body decompression?

import { request } from "undici";

const resp = await request('https://jsonplaceholder.typicode.com/todos/1', {
    headers: {
        'accept-encoding': 'gzip'
    }
});

const content = await resp.body.text();
console.log(`status:`, resp.statusCode);
console.log(`encoding:`, resp.headers['content-encoding']);
console.log(`body:`, content); // compressed

Logs & Screenshots

using fetch image

using request image

Environment

[email protected]

PandaWorker avatar Sep 09 '23 09:09 PandaWorker

That’s because there’s no hard need for request to decompress the body. By spec fetch requires to do so.

This is not a bug in Undici but rather an expected outcome. Decompress is up to the responde handler and not to the client itself per se

metcoder95 avatar Sep 09 '23 10:09 metcoder95

Maybe you need to add some parameter to unpack body ?

PandaWorker avatar Sep 09 '23 10:09 PandaWorker

I'm not 100% convinced but not against it. I have the feeling that it can cause several regressions, as well as hide possible optimizations that can be made on the implementer's side. Like custom backpressure and whatnot.

@ronag @mcollina @KhafraDev any thoughts? I think Khafra is already +1 😄 ?

metcoder95 avatar Sep 10 '23 08:09 metcoder95

I'm -1 in adding it to request, it would slow us down even if it's not enabled.

I'm ok to add it under an option, if you really think it's necessary.

mcollina avatar Sep 10 '23 09:09 mcollina

I think we need to add the decompress: boolean parameter in requestOptions

PandaWorker avatar Sep 10 '23 09:09 PandaWorker

Hello ! Maybe something automatic, like if the response contains gzip encoding header we should decompress it ?

Ealenn avatar Oct 25 '23 09:10 Ealenn

I'm more into the possibility of creating a DecompressHandler that infers the content-encoding header and applies decompression, similar to what we are doing for retries on #2264, but -1 on adding it as part of the request options or within core at all

metcoder95 avatar Oct 25 '23 09:10 metcoder95

https://github.com/nodejs/undici/discussions/1155#discussioncomment-4644321

I think that if we are going to recommend request over fetch, it should be just as easy to use. I'm fine with it not being enabled by default, but it wouldn't be that hard to reuse the logic from fetch.

KhafraDev avatar Oct 25 '23 13:10 KhafraDev

I'm fine with an option. An alternative is to add it through a handler, similar to what https://github.com/nxtedition/nxt-undici does.

Not sure if I like our current "interceptor" pattern but it could go through something like that.

ronag avatar Oct 25 '23 14:10 ronag

I'm +1 to an option or a documentated interceptor.

mcollina avatar Oct 25 '23 17:10 mcollina

Marking as good-first-issue

metcoder95 avatar Oct 25 '23 19:10 metcoder95

I Don't know how "good first issue" this is, but here are the files redirect interceptor follows...

https://github.com/nodejs/undici/blob/6a04edcd5867c4de6bd53d10f6b44eaecf02755e/lib/interceptor/redirectInterceptor.js https://github.com/nodejs/undici/blob/6a04edcd5867c4de6bd53d10f6b44eaecf02755e/lib/handler/RedirectHandler.js https://github.com/nodejs/undici/blob/main/test/types/interceptor.test-d.ts#L5 https://github.com/nodejs/undici/blob/main/lib/client.js#L267-269 https://github.com/nodejs/undici/blob/main/types/index.d.ts#L41 https://github.com/nodejs/undici/blob/main/lib/agent.js#L47 https://github.com/nodejs/undici/blob/main/index.js#L44

It might also help (although the redirect interceptor doesn't seem to have); to provide unit tests for the various parts prior to connecting through the whole of undici.

Lewiscowles1986 avatar Dec 27 '23 20:12 Lewiscowles1986

See #951 as well RE: last comment.

Lewiscowles1986 avatar Dec 27 '23 20:12 Lewiscowles1986

See RetryHandler to get a sense of how an interceptor looks like.

metcoder95 avatar Dec 27 '23 20:12 metcoder95

https://github.com/nodejs/undici/blob/6a04edcd5867c4de6bd53d10f6b44eaecf02755e/lib/fetch/index.js#L2171-L2210 for the actual decompressing

KhafraDev avatar Dec 27 '23 21:12 KhafraDev