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
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function numberEncoder (num) {
return null
}

const encodeOptions = {
const _encodeOptions = {
float64: true,
typeEncoders: {
Object: cidEncoder,
Expand All @@ -73,6 +73,13 @@ const encodeOptions = {
}
}

export const encodeOptions = {
..._encodeOptions,
typeEncoders: {
..._encodeOptions.typeEncoders
}
}

tabcat marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param {Uint8Array} bytes
* @returns {CID}
Expand All @@ -84,7 +91,7 @@ function cidDecoder (bytes) {
return CID.decode(bytes.subarray(1)) // ignore leading 0x00
}

const decodeOptions = {
const _decodeOptions = {
allowIndefinite: false,
coerceUndefinedToNull: true,
allowNaN: false,
Expand All @@ -97,7 +104,12 @@ const decodeOptions = {
/** @type {import('cborg').TagDecoder[]} */
tags: []
}
decodeOptions.tags[CID_CBOR_TAG] = cidDecoder
_decodeOptions.tags[CID_CBOR_TAG] = cidDecoder

export const decodeOptions = {
..._decodeOptions,
tags: _decodeOptions.tags.slice()
}

export const name = 'dag-cbor'
export const code = 0x71
Expand All @@ -107,11 +119,11 @@ export const code = 0x71
* @param {T} node
* @returns {ByteView<T>}
*/
export const encode = (node) => cborg.encode(node, encodeOptions)
export const encode = (node) => cborg.encode(node, _encodeOptions)

/**
* @template T
* @param {ByteView<T>} data
* @returns {T}
*/
export const decode = (data) => cborg.decode(data, decodeOptions)
export const decode = (data) => cborg.decode(data, _decodeOptions)
21 changes: 21 additions & 0 deletions test/test-basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import { Buffer } from 'buffer'
import { garbage } from '@ipld/garbage'
import { assert } from 'aegir/chai'
import { decodeFirst } from 'cborg'
import { encodedLength } from 'cborg/length'
import { bytes, CID } from 'multiformats'
import * as dagcbor from '../src/index.js'

Expand Down Expand Up @@ -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?

const { encodeOptions } = dagcbor

const length = encodedLength(obj, encodeOptions)
same(length, serializedObj.length)
})

test('.deserialize the first of concatenated serialized objects', () => {
const { decodeOptions } = dagcbor

const concatSerializedObjs = new Uint8Array(serializedObj.length * 2)
concatSerializedObjs.set(serializedObj)
concatSerializedObjs.set(serializedObj, serializedObj.length)

const [deserializedObj, remainder] = decodeFirst(concatSerializedObjs, decodeOptions)
same(deserializedObj, obj)
same(remainder, serializedObj)
})
})
Loading