Skip to content

parity with cbor-x #69

@jimmywarting

Description

@jimmywarting

I'm trying out both cbor-x and cborg...

Here is the deal:

  • cbor-x supports both typed arrays and arrayBuffer
  • cborg don't support typed arrays and produces regular byte array instead (same as arrayBuffer)
  • when i use ArrayBuffer with cbor-x then it will produce similar response as when i'm using cborg (example at bottom)

So here is an example where i use cbor-x and encoding Uint8Array's (it will produce a uint8 arrays)

const { encode } = await import('https://cdn.jsdelivr.net/npm/cbor-x/+esm')
const { encode: toHex } = await import('https://jspm.dev/hex-string')

toHex(encode({ data: new Uint8Array([97,98,99]) }))

/*
HEX: b900016464617461d84043616263

b9 0001        # map(1)
   64          #   text(4)
      64617461 #     "data"
   d8 40       #   typed array of u8, tag(64)
      43       #     bytes(3)
         61    #       unsigned(97)
         62    #       unsigned(98)
         63    #       unsigned(99)
*/

Where as when i using cborg it would produce another result:

const { encode } = await import('https://cdn.jsdelivr.net/npm/cborg/+esm')
const { encode: toHex } = await import('https://jspm.dev/hex-string')

toHex(encode({ data: new Uint8Array([97,98,99]) }))
/*
HEX: a1646461746143616263

a1             # map(1)
   64          #   text(4)
      64617461 #     "data"
   43          #   bytes(3)
      616263   #     "abc"
*/
To achieve the same result using `cbor-x` then i can use regular ArrayBuffers
const { encode } = await import('https://cdn.jsdelivr.net/npm/cbor-x/+esm')
const { encode: toHex } = await import('https://jspm.dev/hex-string')

toHex(encode({ data: new Uint8Array([97,98,99]).buffer }))

/*
HEX: b90001646461746143616263

b9 0001        # map(1)
   64          #   text(4)
      64617461 #     "data"
   43          #   bytes(3)
      616263   #     "abc
*/

Now the actual issue/feature request:

I'm not exactly asking you to support TypedArrays or any other tags.
I'm asking you to rather stop using Uint8Arrays in your codebase and your code example and switch to using ArrayBuffer instead of Uint8Arrays.

Why?

  • To make it easier to switch between other cbor encoder/decoders
  • to make parity with other encoders to work similar
  • And lastly allow plugin to write tag supports for Uint8Array and other typed arrays

(credit to @Nemo157 for its online diagnostic tool
(also want to ping @kriszyp to ask if he has any saying to this)
(ps: i don't like how every package name their own function encode/decode it's conflicting with other hex/base64/TextDecoder etc - just name name according to what kind of thing they do: like `encodeToHex(input)`)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions