solana-web3.js icon indicating copy to clipboard operation
solana-web3.js copied to clipboard

web3.js: Remove use of Buffer

Open macalinao opened this issue 1 year ago • 8 comments

Problem

Buffer isn't built for the web. Uint8Array is and saves 10kb gzipped.

Proposed Solution

Begin migrating code to use Uint8Array instead. Code that doesn't use Borsh should get optimized automatically.

macalinao avatar Sep 04 '22 05:09 macalinao

100%. This is another change that we should negotiate with near/borsh-js to eliminate this dependency from the web bundle entirely, everywhere. cc/ @ailisp.

steveluscher avatar Sep 06 '22 23:09 steveluscher

Your treeshaking proposal is impressive! We also have plan to drop Buffer-usage in borsh. Uint8Array is available for all, <10-year browsers. The only challenge part is to borsh serialize a string, it uses Buffer.from(string, 'utf-8'). Standard web api for doing that is TextEncoder, but that need polyfills.

ailisp avatar Sep 08 '22 03:09 ailisp

Your treeshaking proposal is impressive! We also have plan to drop Buffer-usage in borsh. Uint8Array is available for all, <10-year browsers. The only challenge part is to borsh serialize a string, it uses Buffer.from(string, 'utf-8'). Standard web api for doing that is TextEncoder, but that need polyfills.

Yeah Uint8Array has good polyfills so I think it's fine-- virtually all frontend packaging tools have some form of browserslist/polyfilling in their build configs.

10kb gzipped if you guys accomplish this. And no more browserify! 🙂

macalinao avatar Sep 08 '22 07:09 macalinao

Glad to hear this! node buffer shim took me almost 3 hours to make it to work for both dev and prod via vite. 🤷‍♂️

katopz avatar Feb 03 '23 04:02 katopz

Is this already work in progress? I might want to have a look at this. Does anyone know how big/complex this migration gonna be?

mindrunner avatar Jul 05 '23 21:07 mindrunner

My focus is on replacing web3.js altogether. Follow along in #1111 and in all of the new packages in the packages/ directory.

Deprecating Buffer in the current API would probably take just as long as rewriting the whole thing. Backward compat is the hardest thing, because Uint8Array and Buffer behave slightly differently (particularly toString()) and it’s impossible to know who depends on which of the peculiar behaviours.

steveluscher avatar Jul 06 '23 06:07 steveluscher

Sorry for being that guy but any update on this? This prevents me to use vite before.

val-samonte avatar Mar 19 '24 14:03 val-samonte

Sorry for being that guy but any update on this? This prevents me to use vite before.

Unfortunately we can't save the legacy library from Buffer, but the new (2.0) release is going to be all Uint8Array. You can see preview release candidates here: https://github.com/solana-labs/solana-web3.js/releases

buffalojoec avatar Mar 19 '24 18:03 buffalojoec