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

Commit

Permalink
feat: make addLink()/rmLink() instance methods
Browse files Browse the repository at this point in the history
As `addLink()` and `rmLink()` manipulate the DAGNode directly, they
can as well be instance methods.

BREAKING CHANGE: `addLink()` and `rmLink()` are now instance methods.

Prior to this change:

    DAGNode.addLink(node, link)
    DAGNode.rmLink(node, name)

Now:

    node.addLink(link)
    node.rmLink(name)
  • Loading branch information
vmx committed Jul 19, 2019
1 parent 7f1a00a commit a9aa0a0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 58 deletions.
74 changes: 35 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
- [API](#api)
- [DAGNode functions](#dagnode-functions)
- [DAGNode constructor](#dagnode-constructor)
- [addLink(node, link)](#addlinknode-link)
- [rmLink(node, nameOrCid)](#rmlinknode-nameorcid)
- [DAGNode instance methods and properties](#dagnode-instance-methods-and-properties)
- [`node.Data`](#nodedata)
- [`node.Links`](#nodelinks)
- [`node.size`](#nodesize)
- [`node.toJSON()`](#nodetojson)
- [`node.toString()`](#nodetostring)
- [`node.toDAGLink(options)`](#nodetodaglinkoptions)
- [`node.addLink(link)`](#nodeaddlinklink)
- [`node.rmLink(nameOrCid)`](#nodermlinknameorcid)
- [DAGLink functions](#daglink-functions)
- [DAGLink constructor](#daglink-constructor)
- [DAGLink instance methods and properties](#daglink-instance-methods-and-properties)
Expand Down Expand Up @@ -66,9 +66,6 @@
```JavaScript
const dagPB = require('ipld-dag-pb')

dagPB.DAGNode.addLink // add a Link to a DAGNode, creating a new one
dagPB.DAGNode.rmLink // remove a Link to a DAGNode, creating a new one

// IPLD Format specifics
dagPB.resolver
dagPB.util
Expand All @@ -94,10 +91,10 @@ const link = {
Tsize: 42
}

DAGNode.addLink(node, link)
node.addLink(link)
console.log('with link', node.toJSON())

DAGNode.rmLink(nodeA, 'I am a link')
nodeA.rmLink('I am a link')
console.log('now without link', node.toJSON())
```

Expand Down Expand Up @@ -136,38 +133,6 @@ links can be a single or an array of DAGLinks instances or objects with the foll
}
```

#### addLink(node, link)

- `node` - type: DAGNode
- `link` - type: DAGLink or DAGLink in its json format

Creates a link on node A. Modifies the node.

`link` can be:
- DAGLink instance
- DAGNode instance
- Object with the following properties:

```JavaScript
const link = {
Name: '<some string>', // optional
Tsize: <size in bytes>,
Hash: <cid> // can be a String CID, CID buffer or CID object
}

DAGNode.addLink(node, link)
```

#### rmLink(node, nameOrCid)

- `node` - type: DAGNode
- `nameOrCid` - type: String, CID object or CID buffer

Removes a link from the node by name. Modifies the node.

```JavaScript
DAGNode.rmLink(node, 'Link1')
```


### DAGNode instance methods and properties
Expand Down Expand Up @@ -201,6 +166,37 @@ const link = node.toDAGLink()
const link = node.toDAGLink({ name: 'name-of-the-link' })
```

#### addLink(link)

- `link` - type: DAGLink or DAGLink in its json format

Creates a link on node A. Modifies the node.

`link` can be:
- DAGLink instance
- DAGNode instance
- Object with the following properties:

```JavaScript
const link = {
Name: '<some string>', // optional
Tsize: <size in bytes>,
Hash: <cid> // can be a String CID, CID buffer or CID object
}

node.addLink(link)
```

#### rmLink(nameOrCid)

- `nameOrCid` - type: String, CID object or CID buffer

Removes a link from the node by name. Modifies the node.

```JavaScript
node.rmLink('Link1')
```


### DAGLink functions

Expand Down
9 changes: 7 additions & 2 deletions src/dag-node/addLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const sortLinks = require('./sortLinks')
const DAGLink = require('../dag-link')
const DAGNode = require('./index')

const asDAGLink = (link) => {
if (DAGLink.isDAGLink(link)) {
Expand All @@ -11,7 +10,13 @@ const asDAGLink = (link) => {
return link
}

if (DAGNode.isDAGNode(link)) {
// DAGNode.isDagNode() would be more appropriate here, but it can't be used
// as it would lead to circular dependencies as `addLink` is called from
// within the DAGNode object.
if (!('cid' in link ||
'hash' in link ||
'Hash' in link ||
'multihash' in link)) {
throw new Error('Link must be a DAGLink or DAGLink-like. Convert the DAGNode into a DAGLink via `node.toDAGLink()`.')
}

Expand Down
10 changes: 10 additions & 0 deletions src/dag-node/dagNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const visibility = require('../visibility')
const DAGLink = require('../dag-link/dagLink')
const { serializeDAGNode } = require('../serialize.js')
const toDAGLink = require('./toDagLink')
const addLink = require('./addLink')
const rmLink = require('./rmLink')

class DAGNode {
constructor (data, links = [], serializedSize = 0) {
Expand Down Expand Up @@ -58,6 +60,14 @@ class DAGNode {
return `DAGNode <data: "${this.Data.toString('base64')}", links: ${this.Links.length}, size: ${this.size}>`
}

addLink (link) {
return addLink(this, link)
}

rmLink (link) {
return rmLink(this, link)
}

// @returns {Promise.<DAGLink>}
toDAGLink (options) {
return toDAGLink(this, options)
Expand Down
2 changes: 0 additions & 2 deletions src/dag-node/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

exports = module.exports = require('./dagNode')
exports.addLink = require('./addLink')
exports.rmLink = require('./rmLink')
27 changes: 12 additions & 15 deletions test/dag-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module.exports = (repo) => {
it('addLink by DAGNode', async () => {
const node1 = new DAGNode(Buffer.from('1'))
const node2 = new DAGNode(Buffer.from('2'))
DAGNode.addLink(node1, await node2.toDAGLink())
node1.addLink(await node2.toDAGLink())
expect(node1.Links.length).to.equal(1)
expect(node1.Links[0].Tsize).to.eql(node2.size)
expect(node1.Links[0].Name).to.be.eql('')
Expand All @@ -135,7 +135,7 @@ module.exports = (repo) => {
const node1 = new DAGNode(Buffer.from('1'))
const node2 = new DAGNode(Buffer.from('2'))
const link = await node2.toDAGLink()
DAGNode.addLink(node1, link)
node1.addLink(link)
expect(node1.Links.length).to.equal(1)
expect(node1.Links[0].Tsize).to.eql(node2.size)
expect(node1.Links[0].Name).to.be.eql('')
Expand All @@ -146,7 +146,7 @@ module.exports = (repo) => {
const node2 = new DAGNode(Buffer.from('2'))
const link = await node2.toDAGLink()
const linkObject = link.toJSON()
DAGNode.addLink(node1, linkObject)
node1.addLink(linkObject)
expect(node1.Links.length).to.equal(1)
expect(node1.Links[0].Tsize).to.eql(node2.size)
expect(node1.Links[0].Name).to.be.eql('')
Expand All @@ -157,7 +157,7 @@ module.exports = (repo) => {
const node2 = new DAGNode(Buffer.from('2'))
const link = await node2.toDAGLink({ name: 'banana' })
expect(node1.Links.length).to.equal(0)
DAGNode.addLink(node1, link)
node1.addLink(link)
expect(node1.Links.length).to.equal(1)
expect(node1.Links[0].Tsize).to.eql(node2.size)
expect(node1.Links[0].Name).to.eql('banana')
Expand All @@ -168,27 +168,24 @@ module.exports = (repo) => {
expect(node1.Links.length).to.equal(0)

const node2 = new DAGNode(Buffer.from('2'))
DAGNode.addLink(node1, await node2.toDAGLink())
node1.addLink(await node2.toDAGLink())
expect(node1.Links.length).to.equal(1)

const node3 = new DAGNode(Buffer.from('3'))
DAGNode.addLink(node1, await node3.toDAGLink())
node1.addLink(await node3.toDAGLink())
expect(node1.Links.length).to.equal(2)
})

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

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

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

expect(target.Links.length).to.equal(1)
expect(target.Links[0].Tsize).to.eql(remote.size)
Expand All @@ -203,9 +200,9 @@ module.exports = (repo) => {
const node2 = new DAGNode(Buffer.from('2'))
const link = await node2.toDAGLink({ name: 'banana' })

DAGNode.addLink(node1, link)
node1.addLink(link)
expect(node1.Links.length).to.eql(1)
DAGNode.rmLink(node1, 'banana')
node1.rmLink('banana')
expect(node1.Links.length).to.eql(0)
expect(node1.toJSON()).to.eql(withoutLink)
})
Expand All @@ -218,9 +215,9 @@ module.exports = (repo) => {
const node2 = new DAGNode(Buffer.from('2'))
const link = await node2.toDAGLink({ name: 'banana' })

DAGNode.addLink(node1, link)
node1.addLink(link)
expect(node1.Links.length).to.eql(1)
DAGNode.rmLink(node1, node1.Links[0].Hash)
node1.rmLink(node1.Links[0].Hash)
expect(node1.Links.length).to.eql(0)
expect(node1.toJSON()).to.eql(withoutLink)
})
Expand Down

0 comments on commit a9aa0a0

Please sign in to comment.