grantila / fetch-h2

HTTP/1+2 Fetch API client for Node.js
MIT License
336 stars 16 forks source link

response.body.getReader(), as ReadableStream #95

Open pkieltyka opened 4 years ago

pkieltyka commented 4 years ago

Hi there, first I want to thank you for making this! finally a quality fetch implementation for nodejs :) Please let me know if you are accepting sponsorship, I'm happy to support!

secondly, what's your opinion on getting to api parity with the fetch spec? my personal use case is to having javascript clients that can be used in both nodejs and in browsers, I've achieved this in github.com/webrpc/webrpc example: https://github.com/webrpc/webrpc/blob/master/_examples/hello-webrpc-ts/webapp/src/client.gen.ts .. but now I am adding http streaming support to webrpc and I'd like the JS client to use identical code for node and browsers for simplicity.

Sadly, every fetch implementation for node fails to implement the readable streams with a consistent api as browsers.

example:

const consume = responseReader => {
    return responseReader.read().then(result => {
        if (result.done) { return; }

        // do something with the current chunk
        const chunk = result.value;

        return consume(responseReader);
    });
}

// Perform the request and consume response stream
fetch(url).then(response => {
    return consume(response.body.getReader());
})
.catch(console.log.bind(console));
pkieltyka commented 4 years ago

I spent some more time reading the README and source, and seems like you made a conscious decision to use the node readablestream api over whatwg. Two questions..

  1. Why use node's api for readablestreams over whatwg?
  2. Why not make fetch-h2's api and use of readablestreams consistent with whatwg, while creating an interop layer to node ecosystem?
  3. If you don't like the second idea, what about creating a "whatwg" mode in this library which when enabled will provide the compatibility?
chris-kruining commented 2 years ago

Hi there, first I want to thank you for making this! finally a quality fetch implementation for nodejs :) Please let me know if you are accepting sponsorship, I'm happy to support!

secondly, what's your opinion on getting to api parity with the fetch spec? my personal use case is to having javascript clients that can be used in both nodejs and in browsers, I've achieved this in github.com/webrpc/webrpc example: https://github.com/webrpc/webrpc/blob/master/_examples/hello-webrpc-ts/webapp/src/client.gen.ts .. but now I am adding http streaming support to webrpc and I'd like the JS client to use identical code for node and browsers for simplicity.

Sadly, every fetch implementation for node fails to implement the readable streams with a consistent api as browsers.

example:

const consume = responseReader => {
    return responseReader.read().then(result => {
        if (result.done) { return; }

        // do something with the current chunk
        const chunk = result.value;

        return consume(responseReader);
    });
}

// Perform the request and consume response stream
fetch(url).then(response => {
    return consume(response.body.getReader());
})
.catch(console.log.bind(console));

dit you manage to figure this out? I am working on a streaming grpc lib and I am also running into the issue of the API's not being compatible