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

null is not an object (evaluating 'TypedArray.BYTES_PER_ELEMENT #75

Open
jribeiro opened this issue Mar 7, 2023 · 5 comments
Open

null is not an object (evaluating 'TypedArray.BYTES_PER_ELEMENT #75

jribeiro opened this issue Mar 7, 2023 · 5 comments

Comments

@jribeiro
Copy link

jribeiro commented Mar 7, 2023

On browsers not supporting BigUint64Array or BigInt64Array, cbor-x is throwing an exception null is not an object (evaluating 'TypedArray.BYTES_PER_ELEMENT. This is due to this code: https://github.com/kriszyp/cbor-x/blob/master/decode.js#L1064-L1065

You can reproduce this on Safari 13 or IE 11 for instance.

I applied the following patch locally. If I understand correctly, It will still throw if those types are used which might not be ideal.

diff --git a/node_modules/cbor-x/decode.js b/node_modules/cbor-x/decode.js
index 9185b63..8888329 100644
--- a/node_modules/cbor-x/decode.js
+++ b/node_modules/cbor-x/decode.js
@@ -1042,7 +1042,7 @@ function registerTypedArray(TypedArray, tag) {
 	let dvMethod = 'get' + TypedArray.name.slice(0, -5)
 	if (typeof TypedArray !== 'function')
 		TypedArray = null;
-	let bytesPerElement = TypedArray.BYTES_PER_ELEMENT
+	let bytesPerElement = TypedArray != null ? TypedArray.BYTES_PER_ELEMENT : null;
 	for (let littleEndian = 0; littleEndian < 2; littleEndian++) {
 		if (!littleEndian && bytesPerElement == 1)
 			continue
@rosston
Copy link

rosston commented Mar 21, 2023

Yes, I'm seeing this on React Native too (though it depends on which JS engine is being used). The suggested fix above should indeed work. Another option would be to move the bytesPerElement definition above the typeof TypedArray !== 'function' check.

If need be, could even put BYTES_PER_ELEMENT on the fake typed array constructors. (Per MDN, the value of that property would be 8 for each of those.)

I'd be happy to submit a PR to fix this. My only concern is how this can be tested without running on a JS engine with no big int support. 😅

@rosston
Copy link

rosston commented Mar 24, 2023

I just came here to open a PR, but I see that this was fixed in a commit to master. Thanks, @kriszyp! 🎉

@rosston
Copy link

rosston commented Mar 30, 2023

Hi @kriszyp! Just a friendly bump: would you mind pushing a new version to npm to get this fix out? (I had initially assumed this would be an easy patch version bump, but I see that the diff since the last release is larger than this one commit.)

@kriszyp
Copy link
Owner

kriszyp commented Mar 30, 2023

Thanks for the reminder, published in v1.5.2.

@rosston
Copy link

rosston commented Mar 30, 2023

Confirmed this fixed my React Native app startup (with Hermes < 0.12). I'm not the issue opener, but I'm pretty sure you can close this. 🙂

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