js-stellar-sdk
js-stellar-sdk copied to clipboard
Replace Axios with Fetch
Axios browserified doesn't support setting 'keep-alive' (the httpsAgent
needed is just a stub), so every new server connection will do SSL negotiation, adding quite some time (~0.25s perhaps) to the response time.
isomorphic-fetch
comes with node and browser support, with polyfills for older browsers.
I can do the PR, just want a go/no-go
After building with webpack
the result package shows that axios
static size is 36.2 KB, or 11.6 KB gzipped.
@johansten Go for it!
I'd suggest avoiding pulling in any polyfills if possible. sokra sums up my thoughts on it here, it leads to redundant code for library consumers, and fetch
is common enough that I think it's reasonable to ask apps to provide it if the environment doesn't.
@vcarl I think we can pull it out in a later, breaking change update, but for now I'd like to preserve compatibility between releases as much as possible.
For sure, that makes sense.
I ended up removing an eventsource
polyfill and leaving it up to consumers to include, so @johansten I wouldn't include a cross-browser polyfill for it anymore! I used the isNode
library to apply that polyfill for node:
https://github.com/stellar/js-stellar-sdk/pull/239/files#diff-41147c007709fcf3e884eb6268314e81R4
Just so you know, I have a PR in that might make it to next week's release that uses Axios infrastructure:
https://github.com/stellar/js-stellar-sdk/pull/249
If you're still planning to pursue a fix to this, you'll need to take into account this feature.
@morleyzhi I don't it it will cause too much problem. I will most likely have to create some helper functions to make things compatible with axios anyway, as Fetch doesn't have support straight out of the box for the timeouts or content-length checks that are being used in some places.
Edit: Yeah, maybe not. I'll put this on hold. Keep-alive would have been awesome though ¯\_(ツ)_/¯
@johansten ah shoot, your idea seemed like a great change! Is adding replacements for those axios features on top of fetch
pretty difficult?
@tomquisel
It doesn't look very complicated.. I'll just replace HorizonAxiosClient with the same functionality implemented using Fetch instead. One issue might be that proper timeouts w/ Fetch needs something called an AbortController
which came into full support later than Fetch itself.