browserify / crypto-browserify

partial implementation of node's `crypto` for the browser
MIT License
653 stars 199 forks source link

Usage without polyfills #212

Open mendeljacks opened 2 years ago

mendeljacks commented 2 years ago

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:

Expected:

Actual:

ljharb commented 2 years ago

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.

mendeljacks commented 2 years ago

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?

ljharb commented 2 years ago

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.

aanand07 commented 1 year ago

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

secondl1ght commented 1 year ago

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.

ljharb commented 1 year ago

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