diff --git a/src/dag-link/index.js b/src/dag-link/index.js index f5ef225..fdd4854 100644 --- a/src/dag-link/index.js +++ b/src/dag-link/index.js @@ -56,6 +56,14 @@ class DAGLink { set multihash (multihash) { throw new Error("Can't set property: 'multihash' is immutable") } + + get cid () { + return this._cid + } + + set cid (cid) { + throw new Error("Can't set property: 'cid' is immutable") + } } exports = module.exports = withIs(DAGLink, { className: 'DAGLink', symbolName: '@ipld/js-ipld-dag-pb/daglink' }) diff --git a/src/dag-node/index.js b/src/dag-node/index.js index 1293eb3..bdc1aa2 100644 --- a/src/dag-node/index.js +++ b/src/dag-node/index.js @@ -75,6 +75,14 @@ class DAGNode { set multihash (multihash) { throw new Error("Can't set property: 'multihash' is immutable") } + + get cid () { + return this._cid + } + + set cid (cid) { + throw new Error("Can't set property: 'cid' is immutable") + } } exports = module.exports = withIs(DAGNode, { className: 'DAGNode', symbolName: '@ipld/js-ipld-dag-pb/dagnode' }) diff --git a/test/dag-link-test.js b/test/dag-link-test.js index f5bd692..00c7d1f 100644 --- a/test/dag-link-test.js +++ b/test/dag-link-test.js @@ -53,5 +53,22 @@ module.exports = (repo) => { expect(link.toString()).to.equal('DAGLink ') }) + + it('exposes a CID', () => { + const cid = 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U' + const link = new DAGLink('hello', 3, cid) + expect(link.cid.toBaseEncodedString()).to.equal(cid) + }) + + it('has an immutable CID', () => { + const link = new DAGLink('hello', 3, 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U') + + try { + link.cid = 'foo' + throw new Error('Should not be able to update CID') + } catch (error) { + expect(error.message).to.include("'cid' is immutable") + } + }) }) } diff --git a/test/dag-node-test.js b/test/dag-node-test.js index 3c99cb9..0ce6e9f 100644 --- a/test/dag-node-test.js +++ b/test/dag-node-test.js @@ -663,5 +663,27 @@ module.exports = (repo) => { }) }) }).timeout(6000) + + it('exposes a CID', (done) => { + DAGNode.create(Buffer.from('hello world'), (err, node) => { + expect(err).to.not.exist() + expect(node.cid.toBaseEncodedString()).to.equal('QmU1Sq1B7RPQD2XcQNLB58qJUyJffVJqihcxmmN1STPMxf') + done() + }) + }) + + it('has an immutable CID', (done) => { + DAGNode.create(Buffer.from('hello world'), (err, node) => { + expect(err).to.not.exist() + + try { + node.cid = 'foo' + throw new Error('Should not be able to update CID') + } catch (error) { + expect(error.message).to.include("'cid' is immutable") + done() + } + }) + }) }) }