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

Buffer is not a browser method #288

Open
sartaj-devoted opened this issue Sep 7, 2022 · 1 comment
Open

Buffer is not a browser method #288

sartaj-devoted opened this issue Sep 7, 2022 · 1 comment

Comments

@sartaj-devoted
Copy link

sartaj-devoted commented Sep 7, 2022

PubNub client has been failing for us since we upgraded to the latest Webpack, which no longer polyfills nodejs methods.

Screen Shot 2022-09-06 at 9 40 43 PM

The issue here is that this library uses a non standard global Buffer method. There may be a case where this is failing for others who have upgraded to the latest compilers too.

Some thoughts:

  1. Convert to using browser standards for buffers.
  2. If that is not in the timelines, perhaps make the error more graceful, with the library explicitly checking for if Buffer exists. I wouldn't be surprised if other users of PubNub are not aware their client started failing due to a webpack upgrade.

As an aside, are there docs on where/how to handle errors like this? We weren't aware of the issue for months as nothing obvious was erroring within the addListener({message method, and logVerbosity:true did not surface any errors.

@sartaj-devoted
Copy link
Author

sartaj-devoted commented Sep 7, 2022

As a solution to at least fix the base64=>buffer problem, I was able to replace `Buffer.from(b64str, 'base64') with the following:

if (!window.Buffer) {
  window.Buffer = {
    from: (base64_string: string, encoding: BufferEncoding): Buffer => {
      if (encoding !== 'base64') {
        throw new Error('Buffer polyfill only supports base64 encoding');
      }
      return Uint8Array.from(window.atob(base64_string), c =>
        c.charCodeAt(0),
      ) as any;
    },
  } as any;
}

I think this project could find other uses of Buffer, and replace them with built in browser methods.

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

No branches or pull requests

1 participant