1
1
'use strict'
2
2
3
- const util = require ( './util' )
4
3
const protobuf = require ( 'protocol-buffers' )
5
4
const stable = require ( 'stable' )
6
- const bs58 = require ( 'bs58' )
5
+ const fs = require ( 'fs' )
6
+ const path = require ( 'path' )
7
+ const mh = require ( 'multihashes' )
7
8
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' )
9
11
10
- var mdagpb = protobuf ( schema )
12
+ const proto = protobuf ( fs . readFileSync ( path . join ( __dirname , 'merkledag.proto' ) ) )
11
13
12
14
function linkSort ( a , b ) {
13
15
return ( new Buffer ( a . name , 'ascii' ) . compare ( new Buffer ( b . name , 'ascii' ) ) )
14
16
}
15
17
16
18
// Helper method to get a protobuf object equivalent
17
19
function toProtoBuf ( node ) {
18
- var pbn = { }
20
+ const pbn = { }
19
21
20
22
if ( node . data && node . data . length > 0 ) {
21
23
pbn . Data = node . data
@@ -24,24 +26,21 @@ function toProtoBuf (node) {
24
26
}
25
27
26
28
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 {
32
31
Hash : link . hash ,
33
32
Name : link . name ,
34
33
Tsize : link . size
35
- } )
36
- }
34
+ }
35
+ } )
37
36
} else {
38
37
pbn . Links = null
39
38
}
40
39
41
40
return pbn
42
41
}
43
42
44
- class DAGNode {
43
+ module . exports = class DAGNode {
45
44
constructor ( data , links ) {
46
45
this . _cached = null
47
46
this . _encoded = null
@@ -65,9 +64,9 @@ class DAGNode {
65
64
66
65
// copy - returns a clone of the DAGNode
67
66
copy ( ) {
68
- var clone = new DAGNode ( )
67
+ const clone = new DAGNode ( )
69
68
if ( this . data && this . data . length > 0 ) {
70
- var buf = new Buffer ( this . data . length )
69
+ const buf = new Buffer ( this . data . length )
71
70
this . data . copy ( buf )
72
71
clone . data = buf
73
72
}
@@ -84,7 +83,7 @@ class DAGNode {
84
83
if ( typeof name !== 'string' ) {
85
84
return
86
85
}
87
- var link = this . makeLink ( node )
86
+ const link = this . makeLink ( node )
88
87
89
88
link . name = name
90
89
this . addRawLink ( link )
@@ -101,7 +100,7 @@ class DAGNode {
101
100
// that. If a link of the same name existed, it is replaced.
102
101
// TODO this would make more sense as an utility
103
102
updateNodeLink ( name , node ) {
104
- var newnode = this . copy ( )
103
+ const newnode = this . copy ( )
105
104
newnode . removeNodeLink ( name )
106
105
newnode . addNodeLink ( name , node )
107
106
return newnode
@@ -134,9 +133,7 @@ class DAGNode {
134
133
// makeLink returns a DAGLink node from a DAGNode
135
134
// TODO: this would make more sense as an utility
136
135
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 ( ) )
140
137
}
141
138
142
139
// multihash - returns the multihash value of this DAGNode
@@ -148,15 +145,12 @@ class DAGNode {
148
145
// Size returns the total size of the data addressed by node,
149
146
// including the total sizes of references.
150
147
size ( ) {
151
- var buf = this . encoded ( )
148
+ const buf = this . encoded ( )
152
149
if ( ! buf ) {
153
150
return 0
154
151
}
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 )
160
154
}
161
155
162
156
// Encoded returns the encoded raw data version of a Node instance.
@@ -174,21 +168,17 @@ class DAGNode {
174
168
175
169
// marshal - encodes the DAGNode into a probuf
176
170
marshal ( ) {
177
- var pbn = toProtoBuf ( this )
178
- var data = mdagpb . PBNode . encode ( pbn )
179
- return data
171
+ return proto . PBNode . encode ( toProtoBuf ( this ) )
180
172
}
181
173
182
174
// unMarshal - decodes a protobuf into a DAGNode
183
175
// TODO: this would make more sense as an utility
184
176
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
+
192
182
stable . inplace ( this . links , linkSort )
193
183
this . data = pbn . Data || new Buffer ( 0 )
194
184
return this
@@ -197,34 +187,14 @@ class DAGNode {
197
187
toJSON ( ) {
198
188
return {
199
189
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 ( ) ) ,
202
192
Size : this . size ( )
203
193
}
204
194
}
205
- }
206
195
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 ( ) } >`
213
199
}
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
230
200
}
0 commit comments