Skip to content

Buffer variable overwrites global Buffer #259

@kyranjamie

Description

@kyranjamie

Edit: Duplicate of #255


BN throws buffer undefined assertion errors in some browser-like environments, despite having Buffer defined as a global. I'm using bn in an electron app with strict security settings: nodeIntegration: false, contextIsolation: true, enabledRemoteModule: false etc.

With these settings require is disabled in the renderer thread, where bn.js is invoked. Because of this, Buffer needs to be polyfilled as a global.

The following code declares a variable that redeclares the globally defined Buffer type to undefined in the local scope. Then silently fails when it tries to tries to require in the try block.

bn.js/lib/bn.js

Lines 51 to 55 in 6ef485d

var Buffer;
try {
Buffer = require('buffer').Buffer;
} catch (e) {
}

Not sure on the most appropriate fix, but something like this should work around the issue.

Buffer = window.Buffer || global.Buffer || require('buffer').Buffer;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions