You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes it is beneficial to carry an embedded CBOR data item that is not meant to be decoded immediately at the time the enclosing data item is being decoded. Tag number 24 (CBOR data item) can be used to tag the embedded byte string as a single data item encoded in CBOR format.
Let's assume for now that we want to encode/decode immediately.
I've tried different versions of the following encoding function but I can't make it work
const{ Encoder, addExtension }=require('cbor-x');//this is an example cbor which seems to be properly using tag 24:constencoded=Buffer.from('d8184bb9000163666f6f6362617a','hex');constencoderDefaults={tagUint8Array: false,useRecords: false,mapsAsObjects: true};constencoder=newEncoder(encoderDefaults);classDataItem{constructor(data){this.data=data;}}addExtension({Class: DataItem,tag: 24,encode: (obj,enc)=>{//here we need to encode the obj.data into a cbor//and then call the encode callback with the bufferconstencodedByteString=encoder.encode(obj.data);console.log(`enc byte string: ${encodedByteString.toString('hex')}`);enc(encodedByteString);},decode: (data)=>{//we get a buffer here and we have to decode it.constdecoded=encoder.decode(data);returnnewDataItem(decoded);},});console.log('data item: %o',encoder.decode(encoded));constencodedByCBORX=encoder.encode(newDataItem({foo: 'baz'}));console.log(`example: ${encoded.toString('hex')}`);console.log(`encoded: ${encodedByCBORX.toString('hex')}`);
Decoding works just fine, the issue seems to be in the encoding function of the extension. The encoded result is much larger than it should and fails to decode in most of the cbor tooling.
Thank you in advance!
The text was updated successfully, but these errors were encountered:
So, it seems like the problem is calling an extra encode inside the encode function. I've also tried creating a new instance of the encoder inside the encode function of the extension but it didn't work.
I am trying to implement an extension for "DataItems" Tag 24 as described in RFC8949:
https://www.rfc-editor.org/rfc/rfc8949.html#name-encoded-cbor-data-item
Let's assume for now that we want to encode/decode immediately.
I've tried different versions of the following encoding function but I can't make it work
This example shows the following output:
Decoding works just fine, the issue seems to be in the encoding function of the extension. The encoded result is much larger than it should and fails to decode in most of the cbor tooling.
Thank you in advance!
The text was updated successfully, but these errors were encountered: