-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
react native not support fetch data from provider(v5.0.8) #993
Comments
Looks like in this file packages/web/src.ts/geturl.ts, http and https module only works in node |
@282931 that means your bundler is picking up the nodejs version, not the browser version. Do you have a rollup config or webpack? Make sure you add I am planning to put together a specific react/expo version at some point too, but that will be a while yet I think... |
not work
still get the same problom |
I have no issue using ethers.js in expo(other than the slow decrypt geth wallet issue). In fact, ethers.js fulfill full some other dependencies I need in my app. |
@garyng2000 what ethers version you using? I try expo the same error |
5.0.5, though I am not using etherscan and only standard JSONRPC provider(my own). via this import { ethers } from 'ethers'; |
check the snack I just created on expo : https://snack.expo.io/@3dtheo/ethers-provider-test |
@garyng2000 you switch to expo-web it works fine, IOS and Android shows the problem |
@282931 |
I m facing the same issue in the v5 the error was |
Same problem here with expo
13. Aug. 2020, 11:01 von notifications@github.com:
…
I m facing the same issue in the v5 the error was
> Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.0.5
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, > view it on GitHub <#993 (comment)>> , or > unsubscribe <https://github.com/notifications/unsubscribe-auth/AAAKI7J5XKXC53ID65E37X3SAOTWHANCNFSM4PZWA33Q>> .
|
Hi, sorry, I was away for a bit. I'm back. Are you still experiencing this problem? The noNetwork error indicates that your node did not respond meaningfully to either |
@ricmoo I found the problem, react-native not support Body.arrayBuffer() this method
|
Hmmmmm... Do you have any documentation for what it does support? Is there an alternative? Or someway to shim it? |
I find a way to use from readAsDataURL: const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const atob = (input = '') => {
let str = input.replace(/=+$/, '');
let output = '';
if (str.length % 4 == 1) {
throw new Error("'atob' failed: The string to be decoded is not correctly encoded.");
}
for (let bc = 0, bs = 0, buffer, i = 0;
buffer = str.charAt(i++);
~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
) {
buffer = chars.indexOf(buffer);
}
return output;
}
FileReader.prototype.readAsArrayBuffer = function (blob) {
if (this.readyState === this.LOADING) throw new Error("InvalidStateError");
this._setReadyState(this.LOADING);
this._result = null;
this._error = null;
const fr = new FileReader();
fr.onloadend = () => {
const content = atob(fr.result.substr("data:application/octet-stream".length));
const buffer = new ArrayBuffer(content.length);
const view = new Uint8Array(buffer);
view.set(Array.from(content).map(c => c.charCodeAt(0)));
this._result = buffer;
this._setReadyState(this.DONE);
};
fr.readAsDataURL(blob);
} |
now I am confused. I have no problem calling my own JSONRPC endpoint which should all be using the same routine to handle the return which is some form of RLE in the form of byte array(thus the body.arrayBuffer) ? It build and run fine on my iPhone. |
@garyng2000 because v5.0.5 is using response.text(); You get this problem if you update to V5.0.8
|
ah, I see. thanks. I only knew RN is bad in this area for binary upload when I was working on ipfs, didn't know even fetch(and I assume xmlhttprequest as well) has this issue. and yes, that is the only work around I know unless one goes to the react-native link route. @ricmoo |
I think those versions used I'm hoping to get an actual |
@ricmoo |
The change was made to handle working with binary data though, so the effort put into the web package could be used by other projects. It’s partly for other projects and partly a step towards v6, which will have a better API. I think in the case of bad UTF-8 data, the I have an idea though, I need to experiment with a simple react app though to see if it will work. :) I’ll try a few things out today... |
beacuse of RN issues: ethers-io/ethers.js#993
Note that there is also https://github.com/joltup/rn-fetch-blob for handling the binary data issue. |
beacuse of RN issues: ethers-io/ethers.js#993
@mnzaki |
(Random aside; is there a reason RN and Expo did not implement these? Would it be worth my while to add to these projects and do they generally accept pull requests?) |
Expo depends on RN for the base implementations like fetch() or Blob() or FormData(), sometimes they then amend their package say there was no randombyte() equivalent in RN and Expo add extra(their own 'native' extension which is similar to rn-fetch-blob but packaged as part of Expo so we don't need to do that). I would not raise my hope of having them do the PR. For me I would rather do what @282931 is doing which is replace the body.arrayBuffer() with body.blob() then turn that into arrayBuffer. Though that would mean some form of detection of whether this is RN. That is what I do for my ipfs implementation. so I can still use the same 'helper' calls be it RN or browser(but I don't care about node.js backend nor service worker for me, so I just test existence of window object, probably harder for this as it is supposed to cover broader usages) |
Sorry... I have lots of questions. :) If it works (or fails) on one react-native, will it work (or fail) on all? Like if I set up the CI to create a demo app on a macOS instance will it adequately test whether it would work on iOS or on Android? Or is it possible the |
my experience(limited though) in Expo is still need to test separately on iPhone and Android phone(I don't know about emulator and found it to be more troublesome to setup), there can be subtle difference which don't show up until it is run on the device. That is why I opt for re-implementation using the common denominator :-( |
arrayBuffer not work on all platform. react-native has not implemented this method yet: const result=await fetch('https://testtesttest.com')
const body=result.arrayBuffer(); it just throw the |
@282931 I've fixed this by adding this shim: facebook/react-native#21209 I'm working on a test suite right now that will validate all features work in React Native (with the shim). I just need to make an App that embeds my test suite. I'm close... |
(oh an macOS seems to adequately fail where needed, so I can test ;)) |
Can you try updating to 5.0.10 and using the shim provided? They should make RN happier. The tests also currently build and run entire test suite inside an iOS simulator during the CI, so these issues should be caught before being published in the future. :) |
How to use shims in v5? shims are not bundled with ethers package right? I am using this way, import '@ethersproject/shims/dist'. Btw, fetch is working after updating to 5.0.10 and after using this shim |
@manoj398, |
I don’t know much about expo. I just spent enough time learning React Native And it’s source code enough to get tests running and all the internal libraries working properly in React Native. Is Expo the same as RN, based on JavaScriptCore with many (but not all) common web API’s? The main change was that ethers now uses the binary fetch API features, which RN does not support. So the main goal of the shim is to add |
Expo is a wrapper of RN, it is the 'where to load the shim' that I is a bit different. and also, where is the shim if I just do 'npm install ethers.js' |
For partial environments (like React Native, and I'm guessing Expo), you also need to do:
And in your code to import ethers, would should import the shims before it: import "@ethersproject/shims";
import { ethers } from "ethers"; This will shim missing features (only if not present or a broken implementation is detected), such as |
It works,i add the library (@ethersproject/shims) as the first import in index.js file |
error: Error: While trying to resolve module
I get this problem if i just use My env is not expo, but react native 0.63.2 |
@manoj398 guess you are using the old version @ethersproject/shims which the package.json file not include browser field. |
beacuse of RN issues: ethers-io/ethers.js#993
@manoj398 Yes, can you try the suggestion by @282931 ? Updating to the most recent version of the shim should fix your import issue. That error can be safely ignored. There are many work-arounds lying around though. :) |
Seems like the OP issue is fixed, so I'm. going to close this now. If you still have problems though, please feel free to re-open. Thanks! :) |
Worked for EtherscanProvider as well. Thanks! |
It shows the error:
Error: missing response (requestBody=null, requestMethod="GET", serverError={}, url="https://api-kovan.etherscan.io/api?module=account&action=balance&address=0xD0FB2864a573F4a828cdc3971450Ae164182965d&tag=latest&apikey=46PPUBQYXPJBCX4I3E4JEVU9WYJN9F5XEM", code=SERVER_ERROR, version=web/5.0.3)
I tried to use 4.0.47 the response looks fine
The text was updated successfully, but these errors were encountered: