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

Commit 795e4f3

Browse files
committed
feat: allow array-like objects as input
Support not only Node.js Buffers, but also Uint8Arrays and normal Arrays as input for CIDs.
1 parent 537f604 commit 795e4f3

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

src/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,22 @@ class CID {
9292
return
9393
}
9494

95-
if (Buffer.isBuffer(version)) {
96-
const firstByte = version.slice(0, 1)
97-
const v = parseInt(firstByte.toString('hex'), 16)
95+
// It is an array-like object, e.g. Arraym, Buffer or TypedArray
96+
if (version.length) {
97+
// The first byte is the actual version
98+
const v = version[0]
9899
if (v === 0 || v === 1) {
99100
// version is a CID buffer
100101
const cid = version
101102
this.version = v
102103
this.codec = multicodec.getCodec(cid.slice(1))
103-
this.multihash = multicodec.rmPrefix(cid.slice(1))
104+
this.multihash = Buffer.from(multicodec.rmPrefix(cid.slice(1)))
104105
this.multibaseName = (v === 0) ? 'base58btc' : multibaseName
105106
} else {
106107
// version is a raw multihash buffer, so v0
107108
this.version = 0
108109
this.codec = 'dag-pb'
109-
this.multihash = version
110+
this.multihash = Buffer.from(version)
110111
this.multibaseName = 'base58btc'
111112
}
112113
CID.validateCID(this)

test/index.spec.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,40 @@ describe('CID', () => {
5252
})
5353
})
5454

55+
it('handles Uint8Array multihash', (done) => {
56+
multihashing(Buffer.from('hello world'), 'sha2-256', (err, mh) => {
57+
expect(err).to.not.exist()
58+
const mhStr = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'
59+
const mhUint8 = Uint8Array.from(mh)
60+
61+
const cid = new CID(mhUint8)
62+
63+
expect(cid).to.have.property('codec', 'dag-pb')
64+
expect(cid).to.have.property('version', 0)
65+
expect(cid).to.have.property('multihash').that.eql(mh)
66+
67+
expect(cid.toBaseEncodedString()).to.eql(mhStr)
68+
done()
69+
})
70+
})
71+
72+
it('handles Array multihash', (done) => {
73+
multihashing(Buffer.from('hello world'), 'sha2-256', (err, mh) => {
74+
expect(err).to.not.exist()
75+
const mhStr = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'
76+
const mhArray = Uint8Array.from(mh)
77+
78+
const cid = new CID(mhArray)
79+
80+
expect(cid).to.have.property('codec', 'dag-pb')
81+
expect(cid).to.have.property('version', 0)
82+
expect(cid).to.have.property('multihash').that.eql(mh)
83+
84+
expect(cid.toBaseEncodedString()).to.eql(mhStr)
85+
done()
86+
})
87+
})
88+
5589
it('create by parts', () => {
5690
const cid = new CID(0, 'dag-pb', hash)
5791

@@ -125,6 +159,34 @@ describe('CID', () => {
125159
expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
126160
})
127161

162+
it('handles CID by Uint8Array', () => {
163+
const cidStr = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
164+
const cidBuf = Buffer.from('017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5', 'hex')
165+
const cidUint8 = Uint8Array.from(cidBuf)
166+
167+
const cid = new CID(cidUint8)
168+
169+
expect(cid).to.have.property('codec', 'dag-pb')
170+
expect(cid).to.have.property('version', 1)
171+
expect(cid).to.have.property('multihash')
172+
173+
expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
174+
})
175+
176+
it('handles CID by Array', () => {
177+
const cidStr = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
178+
const cidBuf = Buffer.from('017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5', 'hex')
179+
const cidArray = Array.from(cidBuf)
180+
181+
const cid = new CID(cidArray)
182+
183+
expect(cid).to.have.property('codec', 'dag-pb')
184+
expect(cid).to.have.property('version', 1)
185+
expect(cid).to.have.property('multihash')
186+
187+
expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
188+
})
189+
128190
it('create by parts', () => {
129191
const cid = new CID(1, 'dag-cbor', hash)
130192

0 commit comments

Comments
 (0)