Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: switch randombytes for iso-random-stream for browser compatibility #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

achingbrain
Copy link
Contributor

@achingbrain achingbrain commented Apr 28, 2023

The randombytes package depends on safe-buffer which uses the built-in buffer module from node core without declaring the polyfill as a dep.

If you have this installed accidentally as a dependency of another dep you will be fine but if not you'll see errors like this in the browser:

dex.js:12 Uncaught TypeError: Cannot read properties of undefined (reading 'from')
    at node_modules/safe-buffer/index.js (index.js:12:12)
    at __require2 (chunk-LFBQMW2U.js?v=930a8016:19:50)
    at node_modules/randombytes/browser.js (browser.js:15:14)
    at __require2 (chunk-LFBQMW2U.js?v=930a8016:19:50)
    at node_modules/k-bucket/index.js (index.js:31:21)
    at __require2 (chunk-LFBQMW2U.js?v=930a8016:19:50)
    at index.ts:2:19

This PR switches randombytes for iso-random-stream which does not have this problem and uses the native random bytes functionality in both node and browsers.

It also adds a dependency on the events polyfill in case you don't have that as a transitive dependency either.

The `randombytes` package depends on `safe-buffer` which uses the
built-in `buffer` module from node core without declaring the polyfill
as a dep.

If you have this installed as a transitive dependency you will be fine
but if not you'll see errors like:

```
dex.js:12 Uncaught TypeError: Cannot read properties of undefined (reading 'from')
    at node_modules/safe-buffer/index.js (index.js:12:12)
    at __require2 (chunk-LFBQMW2U.js?v=930a8016:19:50)
    at node_modules/randombytes/browser.js (browser.js:15:14)
    at __require2 (chunk-LFBQMW2U.js?v=930a8016:19:50)
    at node_modules/k-bucket/index.js (index.js:31:21)
    at __require2 (chunk-LFBQMW2U.js?v=930a8016:19:50)
    at index.ts:2:19
```

This PR switches `randombytes` for `iso-random-stream` which does not
have this problem and uses the native random bytes functionality in
both node and browsers.

It also adds a dependency on the `events` polyfill in case you don't
have that as a transitive dependency either.
achingbrain added a commit to libp2p/js-libp2p-kad-dht that referenced this pull request May 9, 2023
Removes the k-bucket dep and imports the code here since the random
bytes dep it uses doesn't work in browsers.

This PR can be reverted if tristanls/k-bucket#53
is ever merged.
achingbrain added a commit to libp2p/js-libp2p-kad-dht that referenced this pull request May 9, 2023
Removes the k-bucket dep and imports the code here since the random
bytes dep it uses doesn't work in browsers.

This PR can be reverted if tristanls/k-bucket#53
is ever merged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant