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

Commit 95f6bda

Browse files
Gozalavmx
authored andcommitted
feat: serialize Uint8Array as binary (like Buffer)
1 parent 5d3389a commit 95f6bda

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function replaceCIDbyTAG (dagNode) {
3535
}
3636

3737
function transform (obj) {
38-
if (!obj || Buffer.isBuffer(obj) || typeof obj === 'string') {
38+
if (!obj || obj instanceof Uint8Array || typeof obj === 'string') {
3939
return obj
4040
}
4141

test/util.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,17 @@ describe('util', () => {
109109
expect(decoded).to.eql(original)
110110
}
111111
})
112+
113+
it('.serialize and .deserialize object with Uint8Array field', () => {
114+
const buffer = Buffer.from('some data')
115+
const bytes = Uint8Array.from(buffer)
116+
117+
const s1 = dagCBOR.util.serialize({ data: buffer })
118+
const s2 = dagCBOR.util.serialize({ data: bytes })
119+
120+
expect(s1).to.be.eql(s2)
121+
122+
expect(dagCBOR.util.deserialize(s1)).to.be.eql({ data: bytes })
123+
expect(dagCBOR.util.deserialize(s2)).to.be.eql({ data: bytes })
124+
})
112125
})

0 commit comments

Comments
 (0)