From 95f6bda126a7bab8f942a31a7922a0fb83258c0f Mon Sep 17 00:00:00 2001 From: Irakli Gozalishvili Date: Tue, 9 Jun 2020 00:21:35 -0700 Subject: [PATCH] feat: serialize Uint8Array as binary (like Buffer) --- src/util.js | 2 +- test/util.spec.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/util.js b/src/util.js index 0ffcf15..cfb5c20 100644 --- a/src/util.js +++ b/src/util.js @@ -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 } diff --git a/test/util.spec.js b/test/util.spec.js index 8e0cf21..e065481 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -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 }) + }) })