crypto-browserify
crypto-browserify copied to clipboard
Usage without polyfills
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
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.
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?
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.
webpack 4
used to provide polyfills for node core modules by default which is not the case with webpack5
.
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 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.