Skip to content

Decoding with simple type 23 does not throw? #131

@achingbrain

Description

@achingbrain

I have an object with a value type that is not supported by IPLD { "a": undefined }.

I can use cborg to encode it:

import * as cborg from 'cborg'
import * as ipldDagCbor from '@ipld/dag-cbor'
import { toString } from 'uint8arrays'

const obj = { a: undefined }

const cborgBytes = cborg.encode(obj)
console.info(toString(cborgBytes, 'hex'))
// a16161f7

cbor.me displays this as:

A1       # map(1)
   61    # text(1)
      61 # "a"
   F7    # primitive(23)

So far, so good.

@ipld/dag-cbor refuses to encode the object, which seems correct:

ipldDagCbor.encode(obj)
// Error: `undefined` is not supported by the IPLD Data Model and cannot be encoded

However @ipld/dag-cbor decodes the output of cborg just fine though converts undefined to null:

console.info(cborg.decode(cborgBytes))
// { a: undefined }
console.info(ipldDagCbor.decode(cborgBytes))
// { a: null }

I would have expected @ipld/dag-cbor to throw on decode when encountering a forbidden type instead of interpreting it as a different type?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions