crypto-browserify icon indicating copy to clipboard operation
crypto-browserify copied to clipboard

Usage without polyfills

Open mendeljacks opened this issue 2 years ago • 6 comments

the library cipher-base from browserify-sign is using node js internal module 'stream' which is not available in the browser.

I have come up with a fix for now which is to patch require('stream') to be require('readable-stream'), but it's not very elegant.

To reproduce:

  • start a vite project with rollup bundler for react
  • import crypto-browserify and try to sign using createSign

Expected:

  • It should work

Actual:

  • Cannot read property call of undefined at require('stream').Transform

mendeljacks avatar Apr 24 '22 17:04 mendeljacks

The bug is in vite; a non-broken node module bundler provides browser polyfills for node core modules that can work in the browser, including stream.

ljharb avatar Apr 24 '22 20:04 ljharb

I think you are right because I just checked and it works with create react apps webpack config.

If the target audience is browser users who don't have access to node crypto wouldn't it make sense to make the whole package browser compatible out of the box?

mendeljacks avatar Apr 24 '22 23:04 mendeljacks

It’s already browser compatible - when used with a non-broken bundler. This is how the node ecosystem has worked for over a decade - it’s just that a few bundlers have recently decided to break this tradition and hurt users.

ljharb avatar Apr 25 '22 01:04 ljharb

webpack 4 used to provide polyfills for node core modules by default which is not the case with webpack5.

aanand07 avatar Oct 06 '22 08:10 aanand07

For anyone running into this issue I found this package: https://github.com/davidmyersdev/vite-plugin-node-polyfills. I tested it out using createHash from crypto-browserify and it seems to fix the Vite related issues. Maybe the maintainer @ljharb could give his opinion on this package? It looks pretty good from what I can tell but I haven't audited the code, it has over 33K weekly downloads though which is pretty good for a niche package like this one.

secondl1ght avatar Jul 28 '23 06:07 secondl1ght

@secondl1ght i haven't audited that package either, but if it can fix vite's broken omission of node polyfills, then that seems like the appropriate solution.

ljharb avatar Jul 28 '23 17:07 ljharb