11'use strict'
22
3- const util = require ( './util' )
43const protobuf = require ( 'protocol-buffers' )
54const stable = require ( 'stable' )
6- const bs58 = require ( 'bs58' )
5+ const fs = require ( 'fs' )
6+ const path = require ( 'path' )
7+ const mh = require ( 'multihashes' )
78
8- 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;}'
9+ const util = require ( './util' )
10+ const DAGLink = require ( './dag-link' )
911
10- var mdagpb = protobuf ( schema )
12+ const proto = protobuf ( fs . readFileSync ( path . join ( __dirname , 'merkledag.proto' ) ) )
1113
1214function linkSort ( a , b ) {
1315 return ( new Buffer ( a . name , 'ascii' ) . compare ( new Buffer ( b . name , 'ascii' ) ) )
1416}
1517
1618// Helper method to get a protobuf object equivalent
1719function toProtoBuf ( node ) {
18- var pbn = { }
20+ const pbn = { }
1921
2022 if ( node . data && node . data . length > 0 ) {
2123 pbn . Data = node . data
@@ -24,24 +26,21 @@ function toProtoBuf (node) {
2426 }
2527
2628 if ( node . links . length > 0 ) {
27- pbn . Links = [ ]
28-
29- for ( var i = 0 ; i < node . links . length ; i ++ ) {
30- var link = node . links [ i ]
31- pbn . Links . push ( {
29+ pbn . Links = node . links . map ( ( link ) => {
30+ return {
3231 Hash : link . hash ,
3332 Name : link . name ,
3433 Tsize : link . size
35- } )
36- }
34+ }
35+ } )
3736 } else {
3837 pbn . Links = null
3938 }
4039
4140 return pbn
4241}
4342
44- class DAGNode {
43+ module . exports = class DAGNode {
4544 constructor ( data , links ) {
4645 this . _cached = null
4746 this . _encoded = null
@@ -65,9 +64,9 @@ class DAGNode {
6564
6665 // copy - returns a clone of the DAGNode
6766 copy ( ) {
68- var clone = new DAGNode ( )
67+ const clone = new DAGNode ( )
6968 if ( this . data && this . data . length > 0 ) {
70- var buf = new Buffer ( this . data . length )
69+ const buf = new Buffer ( this . data . length )
7170 this . data . copy ( buf )
7271 clone . data = buf
7372 }
@@ -84,7 +83,7 @@ class DAGNode {
8483 if ( typeof name !== 'string' ) {
8584 return
8685 }
87- var link = this . makeLink ( node )
86+ const link = this . makeLink ( node )
8887
8988 link . name = name
9089 this . addRawLink ( link )
@@ -101,7 +100,7 @@ class DAGNode {
101100 // that. If a link of the same name existed, it is replaced.
102101 // TODO this would make more sense as an utility
103102 updateNodeLink ( name , node ) {
104- var newnode = this . copy ( )
103+ const newnode = this . copy ( )
105104 newnode . removeNodeLink ( name )
106105 newnode . addNodeLink ( name , node )
107106 return newnode
@@ -134,9 +133,7 @@ class DAGNode {
134133 // makeLink returns a DAGLink node from a DAGNode
135134 // TODO: this would make more sense as an utility
136135 makeLink ( node ) {
137- var size = node . size ( )
138- var mh = node . multihash ( )
139- return new DAGLink ( null , size , mh )
136+ return new DAGLink ( null , node . size ( ) , node . multihash ( ) )
140137 }
141138
142139 // multihash - returns the multihash value of this DAGNode
@@ -148,15 +145,12 @@ class DAGNode {
148145 // Size returns the total size of the data addressed by node,
149146 // including the total sizes of references.
150147 size ( ) {
151- var buf = this . encoded ( )
148+ const buf = this . encoded ( )
152149 if ( ! buf ) {
153150 return 0
154151 }
155- var size = buf . length
156- for ( var i = 0 ; i < this . links . length ; i ++ ) {
157- size += this . links [ i ] . size
158- }
159- return size
152+
153+ return this . links . reduce ( ( sum , l ) => sum + l . size , buf . length )
160154 }
161155
162156 // Encoded returns the encoded raw data version of a Node instance.
@@ -174,21 +168,17 @@ class DAGNode {
174168
175169 // marshal - encodes the DAGNode into a probuf
176170 marshal ( ) {
177- var pbn = toProtoBuf ( this )
178- var data = mdagpb . PBNode . encode ( pbn )
179- return data
171+ return proto . PBNode . encode ( toProtoBuf ( this ) )
180172 }
181173
182174 // unMarshal - decodes a protobuf into a DAGNode
183175 // TODO: this would make more sense as an utility
184176 unMarshal ( data ) {
185- var pbn = mdagpb . PBNode . decode ( data )
186- this . links = [ ]
187- for ( var i = 0 ; i < pbn . Links . length ; i ++ ) {
188- var link = pbn . Links [ i ]
189- var lnk = new DAGLink ( link . Name , link . Tsize , link . Hash )
190- this . links . push ( lnk )
191- }
177+ const pbn = proto . PBNode . decode ( data )
178+ this . links = pbn . Links . map ( ( link ) => {
179+ return new DAGLink ( link . Name , link . Tsize , link . Hash )
180+ } )
181+
192182 stable . inplace ( this . links , linkSort )
193183 this . data = pbn . Data || new Buffer ( 0 )
194184 return this
@@ -197,34 +187,14 @@ class DAGNode {
197187 toJSON ( ) {
198188 return {
199189 Data : this . data ,
200- Links : this . links . map ( ( l ) => { return l . toJSON ( ) } ) ,
201- Hash : bs58 . encode ( this . multihash ( ) ) . toString ( ) ,
190+ Links : this . links . map ( ( l ) => l . toJSON ( ) ) ,
191+ Hash : mh . toB58String ( this . multihash ( ) ) ,
202192 Size : this . size ( )
203193 }
204194 }
205- }
206195
207- // Link represents an IPFS Merkle DAG Link between Nodes.
208- function DAGLink ( name , size , hash ) {
209- if ( typeof hash === 'string' ) {
210- this . hash = new Buffer ( bs58 . decode ( hash ) )
211- } else {
212- this . hash = hash
196+ toString ( ) {
197+ const hash = mh . toB58String ( this . multihash ( ) )
198+ return `DAGNode <${ hash } - data: "${ this . data . toString ( ) } ", links: ${ this . links . length } , size: ${ this . size ( ) } >`
213199 }
214-
215- this . name = name
216- this . size = size
217-
218- this . toJSON = ( ) => {
219- return {
220- Name : this . name ,
221- Size : this . size ,
222- Hash : bs58 . encode ( this . hash ) . toString ( )
223- }
224- }
225- }
226-
227- exports = module . exports = {
228- DAGLink : DAGLink ,
229- DAGNode : DAGNode
230200}
0 commit comments