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

Commit

Permalink
fix: allow adding links from DAGNode.Links
Browse files Browse the repository at this point in the history
`DAGNode.Links` converts `DAGLink` objects into vanilla JS objects.
`DAGNode.addLink` accepts vanilla JS objects as links but looks for
different properties from the ones the return value of `DAGNode.Links`
have (e.g. `.Hash` vs `.cid`, etc)..

This PR corrects the behaviour to look for those properties names and
adds a test.
  • Loading branch information
achingbrain authored and vmx committed May 17, 2019
1 parent cfc97f9 commit a5d300f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dag-node/addLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const asDAGLink = async (link) => {
}

// It's a Object with name, multihash/hash/cid and size
return new DAGLink(link.name, link.size, link.multihash || link.hash || link.cid)
return new DAGLink(link.Name || link.name, link.Tsize || link.size, link.Hash || link.multihash || link.hash || link.cid)
}

const addLink = async (node, link) => {
Expand Down
19 changes: 19 additions & 0 deletions test/dag-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,25 @@ module.exports = (repo) => {
expect(node1c.Links.length).to.equal(2)
})

it('addLink by DAGNode.Links', async () => {
const linkName = 'link-name'
const remote = DAGNode.create(Buffer.from('2'))
const source = await DAGNode.addLink(
DAGNode.create(Buffer.from('1')), await toDAGLink(remote, {
name: linkName
})
)

expect(source.Links.length).to.equal(1)

let target = new DAGNode(null, [], 0)
target = await DAGNode.addLink(target, source.Links[0])

expect(target.Links.length).to.equal(1)
expect(target.Links[0].Tsize).to.eql(remote.size)
expect(target.Links[0].Name).to.be.eql(linkName)
})

it('rmLink by name', async () => {
const node1a = DAGNode.create(Buffer.from('1'))
expect(node1a.Links.length).to.eql(0)
Expand Down

0 comments on commit a5d300f

Please sign in to comment.