diff --git a/package.json b/package.json index e11f6d4..00f20e3 100644 --- a/package.json +++ b/package.json @@ -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", @@ -57,4 +57,4 @@ "pre-commit": "^1.1.2", "rimraf": "^2.5.0" } -} \ No newline at end of file +} diff --git a/src/dag-node.js b/src/dag-node.js index 72ecefc..9233401 100644 --- a/src/dag-node.js +++ b/src/dag-node.js @@ -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;}' @@ -193,6 +194,15 @@ 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. @@ -200,4 +210,12 @@ 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() + } + } } diff --git a/test/merkle-dag-tests.js b/test/merkle-dag-tests.js index 323f8c1..1d6e687 100644 --- a/test/merkle-dag-tests.js +++ b/test/merkle-dag-tests.js @@ -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 () {