kriszyp/cbor-x

null is not an object (evaluating 'TypedArray.BYTES_PER_ELEMENT

Opened this issue ยท 5 comments

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

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. ๐Ÿ˜…

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

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.)

Thanks for the reminder, published in v1.5.2.

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. ๐Ÿ™‚