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

Commit f08e50e

Browse files
committed
feat: cache string represntation
store the string form of the CID as a non-enumerable property where an instance is constructed from a valid string, or after the first call to toBaseEncodedString where the supplied base matches the default base. adds tests for string caching, and some missing test cases. License: MIT Signed-off-by: Oli Evans <[email protected]>
1 parent 302f5cc commit f08e50e

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

src/index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class CID {
8888
this.multibaseName = 'base58btc'
8989
}
9090
CID.validateCID(this)
91+
Object.defineProperty(this, 'string', { value: version })
9192
return
9293
}
9394

@@ -221,18 +222,25 @@ class CID {
221222
* @returns {string}
222223
*/
223224
toBaseEncodedString (base = this.multibaseName) {
224-
switch (this.version) {
225-
case 0: {
226-
if (base !== 'base58btc') {
227-
throw new Error('not supported with CIDv0, to support different bases, please migrate the instance do CIDv1, you can do that through cid.toV1()')
228-
}
229-
return mh.toB58String(this.multihash)
225+
if (this.string && base === this.multibaseName) {
226+
return this.string
227+
}
228+
let str = null
229+
if (this.version === 0) {
230+
if (base !== 'base58btc') {
231+
throw new Error('not supported with CIDv0, to support different bases, please migrate the instance do CIDv1, you can do that through cid.toV1()')
230232
}
231-
case 1:
232-
return multibase.encode(base, this.buffer).toString()
233-
default:
234-
throw new Error('Unsupported version')
233+
str = mh.toB58String(this.multihash)
234+
} else if (this.version === 1) {
235+
str = multibase.encode(base, this.buffer).toString()
236+
} else {
237+
throw new Error('unsupported version')
238+
}
239+
if (base === this.multibaseName) {
240+
// cache the string value
241+
Object.defineProperty(this, 'string', { value: str })
235242
}
243+
return str
236244
}
237245

238246
toString (base) {

test/index.spec.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ describe('CID', () => {
266266
invalid.forEach((i) => it(`new CID(1, 'dag-pb', ${Buffer.isBuffer(i) ? 'buffer' : 'string'}<${i.toString()}>)`, () => {
267267
expect(() => new CID(1, 'dag-pb', i)).to.throw()
268268
}))
269+
270+
const invalidVersions = [-1, 2]
271+
invalidVersions.forEach((i) => it(`new CID(${i}, 'dag-pb', buffer)`, () => {
272+
expect(() => new CID(i, 'dag-pb', hash)).to.throw()
273+
}))
269274
})
270275

271276
describe('idempotence', () => {
@@ -326,14 +331,44 @@ describe('CID', () => {
326331
})
327332
})
328333

329-
describe('buffer reuse', () => {
334+
describe('caching', () => {
330335
it('should cache CID as buffer', done => {
331336
multihashing(Buffer.from(`TEST${Date.now()}`), 'sha2-256', (err, hash) => {
332337
if (err) return done(err)
333338
const cid = new CID(1, 'dag-pb', hash)
334339
expect(cid.buffer).to.equal(cid.buffer)
340+
expect(Object.hasOwnProperty('buffer')).to.be.false()
335341
done()
336342
})
337343
})
344+
it('should cache string representation when it matches the multibaseName it was constructed with', () => {
345+
// not string to cache yet
346+
const cid = new CID(1, 'dag-pb', hash, 'base32')
347+
expect(cid.string).to.be.undefined()
348+
349+
// we dont cache alternate base encodings yet.
350+
expect(cid.toBaseEncodedString('base64')).to.equal('mAXASILp4Fr+PAc/qQUFA3l2uIiOwA2Gjlhd6nLQQ/2HyABWt')
351+
expect(cid.string).to.be.undefined()
352+
353+
const base32String = 'bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu'
354+
expect(cid.toBaseEncodedString()).to.equal(base32String)
355+
356+
// it cached!
357+
expect(cid.string).to.equal(base32String)
358+
expect(Object.hasOwnProperty('_string')).to.be.false()
359+
expect(cid.toBaseEncodedString()).to.equal(base32String)
360+
expect(cid.toBaseEncodedString('base64')).to.equal('mAXASILp4Fr+PAc/qQUFA3l2uIiOwA2Gjlhd6nLQQ/2HyABWt')
361+
362+
// alternate base not cached!
363+
expect(cid.string).to.equal(base32String)
364+
})
365+
it('should cache string representation when constructed with one', () => {
366+
const base32String = 'bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu'
367+
const cid = new CID(base32String)
368+
expect(cid.string).to.equal(base32String)
369+
expect(cid.toBaseEncodedString('base64')).to.equal('mAXASILp4Fr+PAc/qQUFA3l2uIiOwA2Gjlhd6nLQQ/2HyABWt')
370+
expect(cid.string).to.equal(base32String)
371+
expect(cid.toBaseEncodedString()).to.equal(base32String)
372+
})
338373
})
339374
})

0 commit comments

Comments
 (0)