Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fix: make cbor Decoder configurable (#90)
Browse files Browse the repository at this point in the history
It's now possible to encode data bigger than the default size.
  • Loading branch information
brandonwestcott authored and vmx committed Feb 13, 2019
1 parent 4469954 commit dfb9137
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ function tagCID (cid) {
]))
}

const decoder = new cbor.Decoder({
tags: {
[CID_CBOR_TAG]: (val) => {
// remove that 0
val = val.slice(1)
return new CID(val)
}
}
})

function replaceCIDbyTAG (dagNode) {
let circular
try {
Expand Down Expand Up @@ -87,6 +77,24 @@ function replaceCIDbyTAG (dagNode) {

exports = module.exports

let decoder = null

exports.configureDecoder = (opts) => {
opts = opts || {}
decoder = new cbor.Decoder({
tags: Object.assign({
[CID_CBOR_TAG]: (val) => {
// remove that 0
val = val.slice(1)
return new CID(val)
}
}, opts.tags || {}),
size: opts.size
})
}

exports.configureDecoder() // Setup default cbor.Decoder

exports.serialize = (dagNode, callback) => {
let serialized

Expand Down
21 changes: 21 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ describe('util', () => {
})
})

it('.serialize and .deserialize large objects', (done) => {
const size = 65536 * 2
const largeObj = { someKey: [].slice.call(new Uint8Array(size)) }
// Configure decoder with custom size
dagCBOR.util.configureDecoder({ size: size + 1 })

dagCBOR.util.serialize(largeObj, (err, serialized) => {
expect(err).to.not.exist()
expect(Buffer.isBuffer(serialized)).to.equal(true)

dagCBOR.util.deserialize(serialized, (err, deserialized) => {
expect(err).to.not.exist()
expect(largeObj).to.eql(deserialized)

// Reset decoder back to default
dagCBOR.util.configureDecoder()
done()
})
})
})

it('error catching', (done) => {
const circlarObj = {}
circlarObj.a = circlarObj
Expand Down

0 comments on commit dfb9137

Please sign in to comment.