ipld / ipld/js-dag-cbor

Decoding with simple type 23 does not throw?

Open
#131 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
36
Forks
16
PR merge metrics
No merged PRs in 30d

Description

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

I can use `cborg` to encode it:

```ts
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](https://cbor.me/?bytes=a16161f7) 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:

```ts
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`:

```ts
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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.