hildjj / node-cbor

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

Big Integers not being collapsed in canonical mode #137

Closed refi93 closed 3 years ago

refi93 commented 3 years ago

Not sure if this is intended but looking at the source it seems to me that in canonical mode, big integers should not have the "2" tag prepended:

http://hildjj.github.io/node-cbor/encoder.js.html#line124

When I do:

const cbor = require("cbor")

console.log(
    new cbor.Encoder({
      collapseBigIntegers: true
    })._encodeAll([BigInt(47)]).toString('hex')
)

I get:

182f

However, when I do:

const cbor = require("cbor")

console.log(
    new cbor.Encoder({
      canonical: true
    })._encodeAll([BigInt(47)]).toString('hex')
)

I get:

c2412f

The same also happens when I call cbor.encodeCanonical()

But in canonical mode I would generally expect the "2" being appended only for actual big numbers (i.e. beyond MAX_INT64), otherwise that isn't really a canonical encoding of numbers, but maybe I'm missing something

Could you clarify if the behavior described above is expected and if so, why?

refi93 commented 3 years ago

Ok, I was testing that on https://npm.runkit.com/cbor and for some reason it loads v5.2.0 of the library, on the newest 7.0.4 version it doesn't happen, sorry for the false alarm