hildjj / node-cbor

Encode and decode CBOR documents, with both easy mode, streaming mode, and SAX-style evented mode.
MIT License
356 stars 73 forks source link

Added encoding option to force 32bit encoding for non integer numbers #171

Open janpoltan opened 2 years ago

hildjj commented 2 years ago

I'm assuming I understand your use case, but can you explain it here so we've got it on the record, please?

hildjj commented 2 years ago

Can you please add yourself to the contributors section in package.json?

janpoltan commented 2 years ago

Added option to force encoding of non integer values to 4 byte float.

hildjj commented 1 year ago

After trying to write tests for this, I'd like to discuss what happens with large floats. They are currently encoded as a 32-bit Infinity (fa 7f 80 00 00), which is different than the 16-bit Infinity that cbor.encode(Infinity) produces (f9 7c 00). I see a couple of options:

Of those, document is the easiest of course. Error has precedent, there are several ways to cause an Error to be thrown in Encode - the only niggle is how to detect the issue, but perhaps a couple of checks in _pushFloatBE would work (check the buffer after writing for Infinity and -Infinity). I don't really like the third option.

Regardless, we need to change _pushInfinity, _pushNaN, and _pushIntNum's -0 handling to look at useFloat32bit also.