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

Runtime TypeError getBigUint64 is not a function using Safari 14 #24

Closed
pedelman opened this issue Nov 12, 2020 · 9 comments
Closed

Runtime TypeError getBigUint64 is not a function using Safari 14 #24

pedelman opened this issue Nov 12, 2020 · 9 comments

Comments

@pedelman
Copy link
Contributor

Hi @timostamm, I have recently updated to Safari 14 and noticed the following error in my application which appears to be coming from the runtime.

return VIEW64.getBigUint64(0, true);

Unhandled Promise Rejection: TypeError: O.getBigUint64 is not a function. (In 'O.getBigUint64(0,!0)', 'O.getBigUint64' is undefined)

Surprisingly, I didnt see this error in Safari 13, despite this method apparently not being supported. Im guessing the error was silently ignored?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView#Browser_compatibility

I did find some discussion on this topic already and it looks like it wouldnt be too bad to write a polyfill using JSBI.

System information

  • macOS: 10.15.7
  • Safari: Version 14.0 (15610.1.28.1.9, 15610)

Let me know if there is any other information I can provide! Thanks again for putting together this project!

@timostamm
Copy link
Owner

Hi Patrick,

unfortunately, BigInt can't be used in browsers because of Safari :-/

protobuf-ts supports two alternatives to the BigInt representation: JavaScript number and string. Instead of BigInt, your fields will have the number or string type. The string representation is the most reliable and versatile. The easiest way to enable it is to set the plugin parameter long_type_string. See the manual for details.

For arithmetics across browsers, you need a 3rd party lib. JSBI, or long.js.

// using long.js:
let a = Long.fromString(myMessage.int64Field)
let b = a.add(123);
myMessage.int64Field = b.toString();

// JSBI, very similar:
let a = JSBI.BigInt(myMessage.int64Field)
let b = a.add(123);
myMessage.int64Field = b.toString();

It thought about supporting JSBI as a representation and decided against, mostly because it would add a dependency to the runtime (at the moment, it has no deps at all).

Anyways, the error message in Safari should be improved! What about:

BigInt unavailable. See https://bit.ly/3lpAaUM

Would this have helped you to find / fix the problem?

Not sure about Safari 13... my Version 13.1 (14609.1.20.111.8) shows the same type error when trying to call getBigUint64. To reproduce, I'd need some more info.

@pzeinlinger
Copy link

pzeinlinger commented Nov 12, 2020

Hey @timostamm,
first thanks for your amazing work!!! :)

Using your suggested workaround long_type_string does not seem to work. The proto compiler does indeed replace all BigInts with string types but the error persists. Apparently it is thown by runtime-rpc:68/grpc-web-transport:142.

Using long_type_number seems to work.....

@pedelman
Copy link
Contributor Author

Hmm I have the long_type_string enabled and seeing this still.

Here are the options I a using from my build script.

npx protoc --ts_out some_dir/src --ts_opt long_type_string --ts_opt disable_service_client --ts_opt optimize_code_size --ts_opt generate_dependencies ...

Appreciate the prompt reply!

@pzeinlinger
Copy link

Using long_type_string I still have references to getBigUint64 left in the compiled code.

@timostamm
Copy link
Owner

Ok, this sounds like there is some code path that calls PbUlong.toBigInt(), even though code is generated for string representation.

A stack trace would be super helpful.

@pzeinlinger, are you on Safari 14 as well?

I'll do an update to Safari 14 and try to get this reproduced...

@pzeinlinger
Copy link

pzeinlinger commented Nov 12, 2020 via email

@pedelman
Copy link
Contributor Author

pedelman commented Nov 12, 2020

Screen Shot 2020-11-12 at 2 45 37 PM

return this.toBigInt().toString()


Checking in console:

> globalThis.BigInt
< function BigInt() {
      [native code]
  }

It looks like BigInt was added into Safari 14 https://developer.apple.com/documentation/safari-release-notes/safari-14-release-notes

Confirmed that globalThis.BigInt is coming back undefined in Safari 13.1.1.

@timostamm
Copy link
Owner

Thank you both for the details! This is fixed in release v1.0.8.

Safari 14 adds the BigInt constructor function, but it does not implement the DataView BigInt methods.

The fixed code if all necessary BigInt features are present.

When running bigint code in Safari, the following error is raised: image

@pedelman
Copy link
Contributor Author

Thanks @timostamm!! Appreciate it!

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

3 participants