Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Buffer is not defined in browser #209

Closed
HexaField opened this issue Dec 10, 2020 · 6 comments
Closed

Buffer is not defined in browser #209

HexaField opened this issue Dec 10, 2020 · 6 comments

Comments

@HexaField
Copy link
Contributor

Trying to put a key into a DHT from the browser fails with the following error, despite all simulated aegir tests passing.

Seems to be an issue with either the webpack process or a dependency that is out of date. This may be the reason why this package was not pushed to the browser CDNs. I've tried tracking down the culprate to no avail.

Uncaught (in promise) ReferenceError: Buffer is not defined
    at n (index.min.js:2)
    at c.enqueue (index.min.js:2)
    at async e.exports.addPeerToQuery (index.min.js:2)
    at async Promise.all (:8080/index 0)
    at async e.exports.execute (index.min.js:2)
    at async Promise.all (:8080/index 0)
    at async e.exports.executePaths (index.min.js:2)
    at async e.exports.execute (index.min.js:2)
    at async Object.getClosestPeers (index.min.js:2)
    at async e.exports.getClosestPeers (index.min.js:2)
@HexaField
Copy link
Contributor Author

Further diagnosis shows this seems to be a problem with libp2p@0.29.0 (likely passing a bad function), updating to 0.29.4 seems to have fixed the issue.

@HexaField HexaField reopened this Dec 11, 2020
@HexaField
Copy link
Contributor Author

HexaField commented Dec 11, 2020

The issue has returned. Not sure what to make of this. It happens only when there is another peer connected with the same DHT protocol, which makes sense considering the action.

@jacobheun
Copy link
Contributor

This is likely due to xor-distance using Buffer https://github.com/mafintosh/xor-distance/blob/v2.0.0/index.js#L5. Having another DHT peer connected would result in a query happening which would cause the peer-list logic to kick in,

distance: distance(this.from, key)
.

Not sure why this wasn't caught during the switch over to Uint8Arrays, #202. @achingbrain any thoughts here on resolving this? Ideally we should keep the allocUnsafe usage in xor-distance in Nodejs as it's significantly more performant than Uint8Array usage. Perhaps we can find a workaround until we spend more time on DHT updates in the coming months.

@HexaField
Copy link
Contributor Author

I have forked, upgraded and tested xor-distance to use typed arrays https://github.com/HexaField/xor-distance-typedarray

Will now implement, test & make a PR.

@achingbrain
Copy link
Member

I've PR'd xor-distance which should solve the issue.

In the interim we can add a webpack key to .aegir.js and tell it to include Buffer in the bundle.

I guess this is hitting an untested branch in the code?

@HexaField
Copy link
Contributor Author

Closing this as I have found a solution that works. See #210 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants