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

NodeJS Buffer returns different result for same operation #307

Open
NunoSav opened this issue Jan 7, 2022 · 1 comment
Open

NodeJS Buffer returns different result for same operation #307

NunoSav opened this issue Jan 7, 2022 · 1 comment

Comments

@NunoSav
Copy link

NunoSav commented Jan 7, 2022

I've recently switched to this library on a @ionic/vue app and since then some of my unit tests fail since they run under node environment. The following is an example that returns a different value depending on where it runs on.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Buffer</title>
  </head>
  <body>
    <script src="https://bundle.run/buffer@6.0.3"></script>
    <script defer>
      const { Buffer } = window.buffer;
      const intInBase64 = "/w==";

      function decodeBase64ToInt(value) {
        const buf = Buffer.from(value, 'base64');
        const bytes = new Int8Array(buf.buffer);

        return bytes[0];
      }

      console.log(decodeBase64ToInt(intInBase64))
    </script>
  </body>
</html>
const intInBase64 = "/w==";

function decodeBase64ToInt(value) {
  const buf = Buffer.from(value, 'base64');
  const bytes = new Int8Array(buf.buffer);

  return bytes[0];
}

console.log(decodeBase64ToInt(intInBase64))

Running the HTML snippet on the browser will return -1 and running the second snippet with node will return 47.
How come?

@0x520x650x78
Copy link

Anybody has any idea why since I'm having the same issue.
I'm having the same issue with browerify version, Buffer.from value is different between NodeJS and browserify version that is plugged into browser.

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

2 participants