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

feat: export encode/decode -Options #113

Merged
merged 5 commits into from
Jan 30, 2024
Merged

Conversation

tabcat
Copy link
Contributor

@tabcat tabcat commented Jan 26, 2024

These options are being exported so they can be used with
cborg's API, specifically the decodeFirst and encodedLength functions.

Closes #112

These options are being exported so they can be used with
cborg's API, specifically the `decodeFirst` and `encodedLength` functions.
@rvagg
Copy link
Member

rvagg commented Jan 26, 2024

Ah ok, I imagined you'd do a decodeFirst. I think I would prefer to not export the same options that are used in the encode and decode operations because I don't think it'd be healthy to introduce a way to be able to change those options. Maybe you could make a private objects and assemble or Object.assign them for the exported form as separate from the ones used internally. Note that the tags and typeEncoders would also need to be nested copied so that they can't be messed with.

src/index.js Show resolved Hide resolved
@@ -181,4 +183,23 @@ describe('dag-cbor', () => {
const encoded = bytes.fromHex('a3636261720363666f6f0163666f6f02')
assert.throws(() => decode(encoded), /CBOR decode error: found repeat map key "foo"/)
})

test('determine encoded length of obj', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are both really nice examples of using this, I think they would go well on the README, would you mind adding something there that demonstrates the ability to do encodedLength and decodeFirst?

README.md Outdated
Comment on lines 46 to 65

// encode/decode options are exported for use with cborg's encodedLength and decodeFirst
import { encodeOptions, decodeOptions } from '@ipld/dag-cbor'
import { encodedLength } from 'cborg/length'
import { decodeFirst } from 'cborg'

// dag-cbor encoded length of obj in bytes
const byteLength = encodedLength(obj, encodeOptions)
byteLength // 104


// concatenate two dag-cbor encoded obj
const concatenatedData = new Uint8Array(data.length * 2)
concatenatedData.set(data)
concatenatedData.set(data, data.length)

// returns dag-cbor decoded obj at the beginning of the buffer as well as the remaining bytes
const [first, remainder] = decodeFirst(concatenatedData, decodeOptions)
assert.deepEqual(first, obj) // true
assert.deepEqual(remainder, data) // true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added usage to readme inside of the current usage code block.

@tabcat tabcat force-pushed the feat/decodeOptions branch from 2a94b4e to a2bf381 Compare January 30, 2024 04:38
README.md Outdated Show resolved Hide resolved
@rvagg rvagg merged commit d2094ca into ipld:master Jan 30, 2024
16 checks passed
@rvagg
Copy link
Member

rvagg commented Jan 30, 2024

awesome, expect a semver-minor release soon with this, thanks!

github-actions bot pushed a commit that referenced this pull request Jan 30, 2024
## [9.1.0](v9.0.8...v9.1.0) (2024-01-30)

### Features

* export encode/decode options ([#113](#113)) ([d2094ca](d2094ca))
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

Successfully merging this pull request may close these issues.

feature request: export encode/decode -Options
2 participants