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

Commit

Permalink
feat: remove DAGNode.create()
Browse files Browse the repository at this point in the history
BREAKING CHANGE: DAGNode.create() is removed

Instead of `DAGNode.create()`, please use `new DAGNode()` instead. It
takes the same parameters and is compatible to `create()`.

Example:

Prior to this change:

    const node = DAGNode.create('some data', links)

Now:

    const node = new DAGNode('some data', links)

Closes #132.
  • Loading branch information
vmx committed Jul 19, 2019
1 parent d5e1135 commit 029174d
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 138 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- [Add and remove a Link](#add-and-remove-a-link)
- [API](#api)
- [DAGNode functions](#dagnode-functions)
- [DAGNode.create(data, links)](#dagnodecreatedata-links)
- [DAGNode constructor](#dagnode-constructor)
- [addLink(node, link)](#addlinknode-link)
- [rmLink(node, nameOrCid)](#rmlinknode-nameorcid)
- [DAGNode instance methods and properties](#dagnode-instance-methods-and-properties)
Expand Down Expand Up @@ -65,7 +65,6 @@
```JavaScript
const dagPB = require('ipld-dag-pb')

dagPB.DAGNode.create // create a DAGNode
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

Expand All @@ -79,10 +78,10 @@ dagPB.util
#### Create a DAGNode

```JavaScript
const node1 = DAGNode.create(Buffer.from('some data'))
const node1 = new DAGNode(Buffer.from('some data'))

// node2 will have the same data as node1
const node2 = DAGNode.create('some data')
const node2 = new DAGNode('some data')
```

#### Add and remove a Link
Expand Down Expand Up @@ -114,15 +113,16 @@ const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
```

#### DAGNode.create(data, links)
#### DAGNode constructor

- `data` - type: Buffer
- `links`- type: Array of DAGLink instances or Array of DAGLink instances in its json format (link.toJSON)
- `links`- (optional) type: Array of DAGLink instances or Array of DAGLink instances in its json format (link.toJSON)
- `serializedSize`- (optional) type: Number of bytes the serialized node has. If none is given, it will automatically be calculated.

Create a DAGNode.

```JavaScript
const dagNode = DAGNode.create('data', links)
const dagNode = new DAGNode('data', links)
```

links can be a single or an array of DAGLinks instances or objects with the following pattern
Expand Down
6 changes: 3 additions & 3 deletions src/dag-node/addLink.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const sort = require('stable')
const { linkSort, toDAGLink } = require('./util')
const sortLinks = require('./sortLinks')
const toDAGLink = require('./toDagLink')
const DAGLink = require('../dag-link')
const DAGNode = require('./index')

Expand All @@ -27,7 +27,7 @@ const asDAGLink = async (link) => {
const addLink = async (node, link) => {
const dagLink = await asDAGLink(link)
node._links.push(dagLink)
node._links = sort(node._links, linkSort)
node._links = sortLinks(node._links)
}

module.exports = addLink
33 changes: 0 additions & 33 deletions src/dag-node/create.js

This file was deleted.

32 changes: 27 additions & 5 deletions src/dag-node/dagNode.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
'use strict'

const assert = require('assert')
const withIs = require('class-is')
const sortLinks = require('./sortLinks')
const visibility = require('../visibility')
const DAGLink = require('../dag-link/dagLink')
const { serializeDAGNode } = require('../serialize.js')

class DAGNode {
constructor (data, links, serializedSize) {
if (serializedSize !== 0) {
assert(serializedSize, 'A DAGNode requires it\'s serialized size')
constructor (data, links = [], serializedSize = 0) {
if (!data) {
data = Buffer.alloc(0)
}
if (typeof data === 'string') {
data = Buffer.from(data)
}
if (!Buffer.isBuffer(data)) {
throw new Error('Passed \'data\' is not a buffer or a string!')
}

links = links.map((link) => {
return DAGLink.isDAGLink(link)
? link
: DAGLink.util.createDagLinkFromB58EncodedHash(link)
})
links = sortLinks(links)

if (serializedSize === 0) {
serializedSize = serializeDAGNode({
Data: data,
Links: links
}).length
}

this._data = data || Buffer.alloc(0)
this._data = data
this._links = links
this._serializedSize = serializedSize

Expand Down
1 change: 0 additions & 1 deletion src/dag-node/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

exports = module.exports = require('./dagNode')
exports.create = require('./create')
exports.addLink = require('./addLink')
exports.rmLink = require('./rmLink')
13 changes: 13 additions & 0 deletions src/dag-node/sortLinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const sort = require('stable')

const linkSort = (a, b) => {
return Buffer.compare(a.nameAsBuffer, b.nameAsBuffer)
}

const sortLinks = (links) => {
return sort(links, linkSort)
}

module.exports = sortLinks
14 changes: 2 additions & 12 deletions src/dag-node/util.js → src/dag-node/toDagLink.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
'use strict'

const DAGLink = require('./../dag-link/dagLink')
const {
cid,
serialize
} = require('../util')

exports = module.exports

function linkSort (a, b) {
return Buffer.compare(a.nameAsBuffer, b.nameAsBuffer)
}
const { cid, serialize } = require('../util')

/*
* toDAGLink converts a DAGNode to a DAGLink
Expand All @@ -21,5 +12,4 @@ const toDAGLink = async (node, options = {}) => {
return new DAGLink(options.name || '', serialized.length, nodeCid)
}

exports.linkSort = linkSort
exports.toDAGLink = toDAGLink
module.exports = toDAGLink
64 changes: 64 additions & 0 deletions src/serialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict'

const protons = require('protons')
const proto = protons(require('./dag.proto.js'))
const DAGLink = require('./dag-link/dagLink')

exports = module.exports

const toProtoBuf = (node) => {
const pbn = {}

if (node.Data && node.Data.length > 0) {
pbn.Data = node.Data
} else {
// NOTE: this has to be null in order to match go-ipfs serialization
// `null !== new Buffer(0)`
pbn.Data = null
}

if (node.Links && node.Links.length > 0) {
pbn.Links = node.Links
.map((link) => ({
Hash: link.Hash.buffer,
Name: link.Name,
Tsize: link.Tsize
}))
} else {
pbn.Links = null
}

return pbn
}

/**
* Serialize internal representation into a binary PB block.
*
* @param {Object} node - Internal representation of a CBOR block
* @returns {Buffer} - The encoded binary representation
*/
const serializeDAGNode = (node) => {
const data = node.Data
const links = node.Links || []

const serialized = proto.PBNode.encode(toProtoBuf({
Data: data,
Links: links
}))

return serialized
}

// Serialize an object where the `Links` might not be a `DAGLink` instance yet
const serializeDAGNodeLike = (data, links = []) => {
const node = { Data: data }
node.Links = links.map((link) => {
return DAGLink.isDAGLink(link)
? link
: DAGLink.util.createDagLinkFromB58EncodedHash(link)
})
return serializeDAGNode(node)
}

exports.serializeDAGNode = serializeDAGNode
exports.serializeDAGNodeLike = serializeDAGNodeLike
44 changes: 5 additions & 39 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const DAGLink = require('./dag-link/dagLink')
const DAGNode = require('./dag-node/dagNode')
const multicodec = require('multicodec')
const multihashing = require('multihashing-async')
const { serializeDAGNode, serializeDAGNodeLike } = require('./serialize')

exports = module.exports

Expand Down Expand Up @@ -40,22 +41,11 @@ const cid = async (binaryBlob, userOptions) => {
* @returns {Buffer} - The encoded binary representation
*/
const serialize = (node) => {
const data = node.Data
let links = node.Links || []

// If the node is not an instance of a DAGNode, the link.hash might be a Base58 encoded string; decode it
if (!DAGNode.isDAGNode(node) && links) {
links = links.map((link) => {
return DAGLink.isDAGLink(link) ? link : DAGLink.util.createDagLinkFromB58EncodedHash(link)
})
if (DAGNode.isDAGNode(node)) {
return serializeDAGNode(node)
} else {
return serializeDAGNodeLike(node.Data, node.Links)
}

const serialized = proto.PBNode.encode(toProtoBuf({
Data: data,
Links: links
}))

return serialized
}

/**
Expand All @@ -76,30 +66,6 @@ const deserialize = (buffer) => {
return new DAGNode(data, links, buffer.length)
}

function toProtoBuf (node) {
const pbn = {}

if (node.Data && node.Data.length > 0) {
pbn.Data = node.Data
} else {
// NOTE: this has to be null in order to match go-ipfs serialization `null !== new Buffer(0)`
pbn.Data = null
}

if (node.Links && node.Links.length > 0) {
pbn.Links = node.Links
.map((link) => ({
Hash: link.Hash.buffer,
Name: link.Name,
Tsize: link.Tsize
}))
} else {
pbn.Links = null
}

return pbn
}

exports.serialize = serialize
exports.deserialize = deserialize
exports.cid = cid
Loading

0 comments on commit 029174d

Please sign in to comment.