algorand / js-algorand-sdk

The official JavaScript SDK for Algorand.
https://algorand.github.io/js-algorand-sdk/
MIT License
285 stars 204 forks source link

Polyfill node libraries within the sdk #645

Open Blackglade opened 2 years ago

Blackglade commented 2 years ago

Problem

According to the FAQ page:

Webpack 5 no longer auto-polyfills some of the node modules used by this sdk. You will have to polyfill them to fix any errors regarding missing modules.

It seems that most sdk maintainers in the industry have opted for doing package level polyfills and automatically including checks to see crypto, buffer, and other relevant libraries are available instead of asking for the user to polyfill on their end.

It seems the direction that the Algorand SDK has chosen will cause more problems for its users not experienced with webpack or are simply looking for a hassle free way to start building with Algorand. I believe this is net detrimental to the developer experience.

Solution

Including all relevant polyfill libraries and do a sanity check to see if a native version exists given the execution environment instead of asking users to do polyfills.

SilentRhetoric commented 2 years ago

Strongly support this recommendation. This polyfill issue has generated a ton of questions and frustrations in the dev community.

jasonpaulos commented 2 years ago

We do include polyfills for all necessary libraries in our browser build (currently this is just Buffer). See our README, which has a snippet of HTML that you can drop into any webpage and have the algosdk package available without any further configuration.

Problems tend to arise when using 3rd party frameworks to build for the browser, e.g. React or Vite, which have their own build processes we cannot control. We attempt to make it possible for these frameworks to identify and use our browser build (with its added polyfills), by declaring it in our package.json, but we cannot control if a framework decides to instead consume the code declared under "module" or "main" (which are meant for Node.js).

We unfortunately do not test with every browser framework and do not know their build processes. If you have suggestions for improvements we can make to better support a framework, please let us know, I think we'd be more than happy to make this process easier for everyone.

Note my reply to your other comment here about the crypto module, which is not an issue of a missing polyfill, but rather an issue of the React webpack build being overly cautious: https://github.com/algorand/js-algorand-sdk/issues/643#issuecomment-1241107542

jasonpaulos commented 1 year ago

I've discovered a way of fixing the missing crypto module error without any additional configuration by users, and it's in #707.

I have some ideas about how to fix the Buffer issue as well, which I've described in #708.