js-stellar-sdk icon indicating copy to clipboard operation
js-stellar-sdk copied to clipboard

Replace Axios with Fetch

Open johansten opened this issue 5 years ago • 10 comments

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

johansten avatar Mar 05 '19 05:03 johansten

After building with webpack the result package shows that axios static size is 36.2 KB, or 11.6 KB gzipped.

orbitlens avatar Mar 05 '19 12:03 orbitlens

@johansten Go for it!

morleyzhi avatar Mar 05 '19 15:03 morleyzhi

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 avatar Mar 05 '19 16:03 vcarl

@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.

morleyzhi avatar Mar 05 '19 16:03 morleyzhi

For sure, that makes sense.

vcarl avatar Mar 05 '19 16:03 vcarl

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

morleyzhi avatar Mar 05 '19 23:03 morleyzhi

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 avatar Mar 13 '19 23:03 morleyzhi

@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 avatar Mar 14 '19 01:03 johansten

@johansten ah shoot, your idea seemed like a great change! Is adding replacements for those axios features on top of fetch pretty difficult?

tomquisel avatar Mar 18 '19 16:03 tomquisel

@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.

johansten avatar Mar 30 '19 13:03 johansten