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

Commit

Permalink
feat: cache string represntation
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
olizilla authored and vmx committed Apr 4, 2019
1 parent 0e94b55 commit 537f604
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class CID {
this.multibaseName = 'base58btc'
}
CID.validateCID(this)
Object.defineProperty(this, 'string', { value: version })
return
}

Expand Down Expand Up @@ -221,18 +222,25 @@ class CID {
* @returns {string}
*/
toBaseEncodedString (base = this.multibaseName) {
switch (this.version) {
case 0: {
if (base !== 'base58btc') {
throw new Error('not supported with CIDv0, to support different bases, please migrate the instance do CIDv1, you can do that through cid.toV1()')
}
return mh.toB58String(this.multihash)
if (this.string && base === this.multibaseName) {
return this.string
}
let str = null
if (this.version === 0) {
if (base !== 'base58btc') {
throw new Error('not supported with CIDv0, to support different bases, please migrate the instance do CIDv1, you can do that through cid.toV1()')
}
case 1:
return multibase.encode(base, this.buffer).toString()
default:
throw new Error('Unsupported version')
str = mh.toB58String(this.multihash)
} else if (this.version === 1) {
str = multibase.encode(base, this.buffer).toString()
} else {
throw new Error('unsupported version')
}
if (base === this.multibaseName) {
// cache the string value
Object.defineProperty(this, 'string', { value: str })
}
return str
}

toString (base) {
Expand Down
37 changes: 36 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ describe('CID', () => {
invalid.forEach((i) => it(`new CID(1, 'dag-pb', ${Buffer.isBuffer(i) ? 'buffer' : 'string'}<${i.toString()}>)`, () => {
expect(() => new CID(1, 'dag-pb', i)).to.throw()
}))

const invalidVersions = [-1, 2]
invalidVersions.forEach((i) => it(`new CID(${i}, 'dag-pb', buffer)`, () => {
expect(() => new CID(i, 'dag-pb', hash)).to.throw()
}))
})

describe('idempotence', () => {
Expand Down Expand Up @@ -326,14 +331,44 @@ describe('CID', () => {
})
})

describe('buffer reuse', () => {
describe('caching', () => {
it('should cache CID as buffer', done => {
multihashing(Buffer.from(`TEST${Date.now()}`), 'sha2-256', (err, hash) => {
if (err) return done(err)
const cid = new CID(1, 'dag-pb', hash)
expect(cid.buffer).to.equal(cid.buffer)
expect(Object.hasOwnProperty('buffer')).to.be.false()
done()
})
})
it('should cache string representation when it matches the multibaseName it was constructed with', () => {
// not string to cache yet
const cid = new CID(1, 'dag-pb', hash, 'base32')
expect(cid.string).to.be.undefined()

// we dont cache alternate base encodings yet.
expect(cid.toBaseEncodedString('base64')).to.equal('mAXASILp4Fr+PAc/qQUFA3l2uIiOwA2Gjlhd6nLQQ/2HyABWt')
expect(cid.string).to.be.undefined()

const base32String = 'bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu'
expect(cid.toBaseEncodedString()).to.equal(base32String)

// it cached!
expect(cid.string).to.equal(base32String)
expect(Object.hasOwnProperty('_string')).to.be.false()
expect(cid.toBaseEncodedString()).to.equal(base32String)
expect(cid.toBaseEncodedString('base64')).to.equal('mAXASILp4Fr+PAc/qQUFA3l2uIiOwA2Gjlhd6nLQQ/2HyABWt')

// alternate base not cached!
expect(cid.string).to.equal(base32String)
})
it('should cache string representation when constructed with one', () => {
const base32String = 'bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu'
const cid = new CID(base32String)
expect(cid.string).to.equal(base32String)
expect(cid.toBaseEncodedString('base64')).to.equal('mAXASILp4Fr+PAc/qQUFA3l2uIiOwA2Gjlhd6nLQQ/2HyABWt')
expect(cid.string).to.equal(base32String)
expect(cid.toBaseEncodedString()).to.equal(base32String)
})
})
})

0 comments on commit 537f604

Please sign in to comment.