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

Serialize Uint8Array as binary (like Buffer) #129

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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 })
})
})