indutny / bn.js

BigNum in pure javascript
MIT License
1.19k stars 150 forks source link

Buffer being clobbered in capacitor and other non-node environments #255

Closed schancel closed 3 years ago

schancel commented 3 years ago

https://github.com/indutny/bn.js/blob/master/lib/bn.js#L51-L55

Buffer will already be defined, and require() simply returns an empty object in Capacitor. This results in Buffer being reset to undefined and triggering asserts later on. Not sure what the best course of action is here due to variable lifetimes.

Maybe:

const tryPolyfillBuffer = () => {
  if(typeof Buffer !== 'function') {
    try {
      return require('buffer').Buffer;
    } catch (err) {
      return Buffer;
    }
  }
  return Buffer;
}

let Buffer = tryPolyfillBuffer();
fanatid commented 3 years ago

Closed in https://github.com/indutny/bn.js/pull/260