rvagg / cborg

fast CBOR with a focus on strictness
Other
47 stars 13 forks source link

Disallow certain types when decoding #110

Closed achingbrain closed 7 months ago

achingbrain commented 8 months ago

I want to decode a buffer, but supporting only a subset of CBOR value types - only those that will round-trip to JSON and error out if any non-compliant values are found.

I can control some of this with existing options allowBigInt, useMaps, etc but not everything (Uint8Arrays, for example).

Is it possible to override decoding of certain types to throw?

I saw the tokenizer option to decode but looking at the source it seems a bit all-or-nothing, and the default Tokenizer needs access to jump and quick tables so it's not easily used from outside of cborg.

I think what I really want is something similar to the tags option that lets me supply a decoder for individual data types, or it might be as simple as exporting the default Tokenizer for extension and throwing based on the output of super.next()?

rvagg commented 8 months ago

Been thinking about this, it's a shame that we don't easily expose the Tokenizer because being able to wrap the existing one in a new one that gates types would be the way to go I think. I think the answer here is to expose more innards to let you do this. Let me experiment.