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

Commit

Permalink
feat: serialize Uint8Array as binary (like Buffer)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala authored and vmx committed Jun 10, 2020
1 parent 5d3389a commit 95f6bda
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function replaceCIDbyTAG (dagNode) {
}

function transform (obj) {
if (!obj || Buffer.isBuffer(obj) || typeof obj === 'string') {
if (!obj || obj instanceof Uint8Array || typeof obj === 'string') {
return obj
}

Expand Down
13 changes: 13 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,17 @@ describe('util', () => {
expect(decoded).to.eql(original)
}
})

it('.serialize and .deserialize object with Uint8Array field', () => {
const buffer = Buffer.from('some data')
const bytes = Uint8Array.from(buffer)

const s1 = dagCBOR.util.serialize({ data: buffer })
const s2 = dagCBOR.util.serialize({ data: bytes })

expect(s1).to.be.eql(s2)

expect(dagCBOR.util.deserialize(s1)).to.be.eql({ data: bytes })
expect(dagCBOR.util.deserialize(s2)).to.be.eql({ data: bytes })
})
})

0 comments on commit 95f6bda

Please sign in to comment.