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

Commit

Permalink
feat: expose cid property on DAGLinks and DAGNodes
Browse files Browse the repository at this point in the history
Implements the change proposed in #81 (comment)
  • Loading branch information
achingbrain authored and vmx committed Oct 12, 2018
1 parent fcdd73c commit af3b44d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/dag-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
8 changes: 8 additions & 0 deletions src/dag-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
17 changes: 17 additions & 0 deletions test/dag-link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,22 @@ module.exports = (repo) => {

expect(link.toString()).to.equal('DAGLink <QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U - name: "hello", size: 3>')
})

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")
}
})
})
}
22 changes: 22 additions & 0 deletions test/dag-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
})
})
}

0 comments on commit af3b44d

Please sign in to comment.