paralleldrive / cuid2

Next generation guids. Secure, collision-resistant ids optimized for horizontal scaling and performance.
MIT License
2.65k stars 54 forks source link

SyntaxError: Invalid or unexpected token at bufToBigInt on UC Browser only #39

Open baptisteArno opened 1 year ago

baptisteArno commented 1 year ago

My users have this error when using UC browser:

SyntaxError: Invalid or unexpected token
  at bufToBigInt(../../node_modules/.pnpm/@paralleldrive+cuid2@2.2.0/node_modules/@paralleldrive/cuid2/src/index.js:23:1)
CleanShot 2023-02-25 at 19 09 06@2x

For now, I don't have much more info because I don't have the right device to debug this. But I'll continue to investigate.

ericelliott commented 1 year ago

Probably an old browser that doesn't support BigInt literals. You can probably compile them away in your build step. What compiler are you using?

ericelliott commented 1 year ago

Actually, it looks like it may support BigInt literals (otherwise, it should have stopped on line 21), but maybe there's a bug where it fails to look ahead to see that 0n is a BigInt literal, and not a hex literal (which is denoted with a leading 0x)? Can you try replacing that line with BigInt(0) and see if that solves the error?

baptisteArno commented 1 year ago

I decided to remove it from the bundle, indeed! So I can't really do more testing!

Thank you very much for the help. 🙏

ericelliott commented 1 year ago

I think we still probably need to get this fixed. Thanks for reporting!

luizlopescom commented 2 weeks ago

I received the same error. Since the buf is a string within 0-9 and a-f, I converted it into an array splitting each 2 characters. I really don't know if it is secure. So I am sharing my code:

function bufToBigInt(buf) {

  //Convert buf to Uint8Array
  const length = buf.length / 2;
  const uint8Array = new Uint8Array(length);
  for (let i = 0; i < length; i++) {
    uint8Array[i] = parseInt(buf.substr(i * 2, 2), 16);
  }
  let bits = 8n;
  let value = 0n;
  //for (const i of buf.values()) {
  for (const i of uint8Array) {
    const bi = BigInt(i);
    value = (value << bits) + bi;
  }
  return value;
}