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

Type array tags CBOR #174

Open Ottunger opened 1 year ago

Ottunger commented 1 year ago

When we know that resulting Buffer (or Uint8Array in case of cbor-web) is to be used as part of a larger CBOR payload, it should be encoded using tag 24.

Is there any way to pass to encodeOne an overwrite for the tag to apply on typed array, say bufferTagType much like there already exists dateType? This would be used on the top-level encode operation.

Ottunger commented 1 year ago

We could solve our issue using genTypes, specifically:

const EmbeddedCBORGenTypes = {
    Uint8Array: (gen: Encoder, obj: Uint8Array) => {
        if(!gen._pushTag(0b00011000)) {
            return false;
        }
        return CBOR.Encoder._pushBuffer(gen, Buffer.from(obj))
    }
};

It's not perfect but it does the job. Is it worth searching for an alternative?

hildjj commented 1 year ago

You could wrap your Buffer in a Tag, like so:

const buf = cbor.encode(...)
const t = new cbor.Tagged(24, buf)
return cbor.encode(t)