Skip to content

Commit

Permalink
add toJSON feature
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed May 12, 2016
1 parent 7cfdb3e commit 0976e7a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"stable": "^0.1.5"
},
"devDependencies": {
"aegir": "^3.0.0",
"aegir": "^3.0.1",
"async": "^1.5.2",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
Expand All @@ -57,4 +57,4 @@
"pre-commit": "^1.1.2",
"rimraf": "^2.5.0"
}
}
}
24 changes: 21 additions & 3 deletions src/dag-node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'

var util = require('./util')
var protobuf = require('protocol-buffers')
var stable = require('stable')
const util = require('./util')
const protobuf = require('protocol-buffers')
const stable = require('stable')
const bs58 = require('bs58')

var schema = 'message PBLink {optional bytes Hash = 1; optional string Name = 2;optional uint64 Tsize = 3;} message PBNode {repeated PBLink Links = 2; optional bytes Data = 1;}'

Expand Down Expand Up @@ -193,11 +194,28 @@ function DAGNode (data, links) {

return pbn
}

this.toJSON = () => {
return {
Data: this.data,
Links: this.links.map((l) => { return l.toJSON() }),
Hash: bs58.encode(this.multihash()).toString(),
Size: this.size()
}
}
}

// Link represents an IPFS Merkle DAG Link between Nodes.
function DAGLink (name, size, hash) {
this.name = name
this.size = size
this.hash = hash

this.toJSON = () => {
return {
Name: this.name,
Size: this.size,
Hash: bs58.encode(this.hash).toString()
}
}
}
36 changes: 36 additions & 0 deletions test/merkle-dag-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,42 @@ module.exports = function (repo) {
done()
})
})

it('dagNode.toJSON with empty Node', (done) => {
const node = new DAGNode(new Buffer(0))
const nodeJSON = node.toJSON()
expect(nodeJSON.Data).to.deep.equal(new Buffer(0))
expect(nodeJSON.Links).to.deep.equal([])
expect(nodeJSON.Hash).to.exist
expect(nodeJSON.Size).to.exist
done()
})

it('dagNode.toJSON with data no links', (done) => {
const node = new DAGNode(new Buffer('La cucaracha'))
const nodeJSON = node.toJSON()
expect(nodeJSON.Data).to.deep.equal(new Buffer('La cucaracha'))
expect(nodeJSON.Links).to.deep.equal([])
expect(nodeJSON.Hash).to.exist
expect(nodeJSON.Size).to.exist
done()
})

it('dagNode.toJSON with data and links', (done) => {
var node1 = new DAGNode(new Buffer('hello'))
var node2 = new DAGNode(new Buffer('world'))
node1.addNodeLink('continuation', node2)
const node1JSON = node1.toJSON()
expect(node1JSON.Data).to.deep.equal(new Buffer('hello'))
expect(node1JSON.Links).to.deep.equal([{
Hash: 'QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE',
Name: 'continuation',
Size: 7
}])
expect(node1JSON.Hash).to.exist
expect(node1JSON.Size).to.exist
done()
})
})

describe('dag-service', function () {
Expand Down

0 comments on commit 0976e7a

Please sign in to comment.