Giter VIP home page Giter VIP logo

Comments (5)

kgoguevgoget avatar kgoguevgoget commented on May 23, 2024 1

Hi there, just wanted to give an update and a bit of a guide on how I managed to get it to work:

As per your notes: React Native uses whatwg-fetch which DOES NOT support ReadableStream and the Body property. There is however the following: https://www.npmjs.com/package/react-native-polyfill-globals

From the react-native-pollyfill globals package we can actually patch the whatwg-fetch to support the body and FileReader.readAsArrayBuffer.

npm i @stardazed/streams-polyfill
npm install react-native-polyfill-globals
npm install -D patch-package
npx patch-package --patch-dir node_modules/react-native-polyfill-globals/patches

In index.js at the top add

import "@stardazed/streams-polyfill";

Once ReactNative is patched to support those mentioned above we can actually just run the GrpcWebFetchTransport as I did in the first post.

So yes actually React Native works with your library but you'll need to polyfill + use the patches in the linked package

from protobuf-ts.

timostamm avatar timostamm commented on May 23, 2024

Hey @kgoguevgoget,

the RpcError seems to originate here:

if (!fetchResponse.body)
throw new RpcError('premature end of response', GrpcStatusCode[GrpcStatusCode.DATA_LOSS]);

So the body property of the fetch response is null. It should be a ReadableStream<Uint8Array>, see MDN.

We need the ReadableStream for server streaming methods. We cannot just read the entire data using arrayBuffer() (see MDN), because then the client would only know about a response message when the entire response has been read.

The question is, why is body === null?

Apparently, react native uses the fetch API polyfill whatwg-fetch. See setUpXHR.js and fetch.js.

Unfortunately, this polyfill does not implement the body property, see JakeChampion/fetch#746

from protobuf-ts.

timostamm avatar timostamm commented on May 23, 2024

(sorry, my fat fingers hit enter by accident)

I see the following options:

  1. determine whether the fetch polyfill added by react native is actually necessary, try to turn it off
  2. implement a GrpcWebXhrTransport that uses a similar hack like grpc-web-react-native-transport

Could you have a look at option 1?

I guess this could look like this:

if (__DEV__) {
	(globalThis as any).fetch = (globalThis as any).originalFetch;
}

See https://medium.com/@kureevalexey/hi-birkir-gudjonsson-fb6299cd7e33 and https://github.com/facebook/react-native/blob/6e6443afd04a847ef23fb6254a84e48c70b45896/Libraries/Utilities/PolyfillFunctions.js

from protobuf-ts.

kgoguevgoget avatar kgoguevgoget commented on May 23, 2024

Ah Ok I will try to see if option 1 is a viable solution here.

As for option 2, I am afraid that my knowledge in typescript is not as adept as Go so I wouldn't be able to help out with that one but lets see if at least option 1 gets us further

Thanks for the quick response!

from protobuf-ts.

timostamm avatar timostamm commented on May 23, 2024

That's great news, thanks for the info. I have added a note in the manual with a link to this issue.

from protobuf-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.