Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cbor-diag #168

Closed
CodeOn-ArK opened this issue Jun 9, 2022 · 5 comments
Closed

cbor-diag #168

CodeOn-ArK opened this issue Jun 9, 2022 · 5 comments

Comments

@CodeOn-ArK
Copy link

Hi does it supports cbor-diag?
context : cbor/cbor.github.io#89

@hildjj
Copy link
Owner

hildjj commented Jun 9, 2022

Yes. From the API:

await cbor.diagnose(cbor.encode(Buffer.from('foo')))
// "h'666f6f'\n"

From the CLI:

> cbor2diag -x 43666f6f
h'666f6f'

@CodeOn-ArK
Copy link
Author

CodeOn-ArK commented Jun 10, 2022

Hi
It would be very helpful if you can show the way to encode this using APIs:

{
"val1" : "p",
"val2" : h'1234abcd'
}

Also can you guide to some examples
Regards

@CodeOn-ArK
Copy link
Author

Anyway how to use this line of code

await cbor.diagnose(cbor.encode(Buffer.from('foo')))

suppose I want to send some binary data instead of foo how to achieve that.

@hildjj
Copy link
Owner

hildjj commented Jun 10, 2022

There are lots of examples in the tests, e.g. https://github.com/hildjj/node-cbor/blob/main/packages/cbor/test/diagnose.ava.js

Here is a more full-featured example for your data above:

const cbor = require('cbor');
const input = {
  val1: "p",
  val2: Buffer.from("1234abcd", "hex") // Put any binary data in a Buffer or Uint8Array
}
const encoded = cbor.encode(input); // `encoded` is now a Buffer containing the binary CBOR-encoded version of `input`
console.log(encoded.toString('hex')); // a26476616c3161706476616c32441234abcd
cbor.diagnose(encoded).then(result => {
  // `diagnose` returns a Promise.  If you're in an async function, you can
  // use `await` instead of the `then` syntax.

  // `result` is now a string that
  // contains the diag formatted version of `input`
  console.log(result); // {"val1": "p", "val2": h'1234abcd'}
});

Documentation for the diagnose function is here.

@CodeOn-ArK
Copy link
Author

Thanks a lot for saving the day!!

@hildjj hildjj closed this as completed Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants