1
1
'use strict'
2
2
3
- const CID = require ( 'cids ' )
3
+ const { CID } = require ( 'multiformats ' )
4
4
const {
5
5
ipfs : {
6
6
pin : {
@@ -12,39 +12,35 @@ const {
12
12
// @ts -ignore
13
13
const fnv1a = require ( 'fnv1a' )
14
14
const varint = require ( 'varint' )
15
- const dagpb = require ( 'ipld-dag-pb' )
16
- const DAGNode = require ( 'ipld-dag-pb/src/dag-node/dagNode' )
17
- const DAGLink = require ( 'ipld-dag-pb/src/dag-link/dagLink' )
18
- const multihash = require ( 'multihashing-async' ) . multihash
15
+ const dagPb = require ( '@ipld/dag-pb' )
19
16
const { cidToKey, DEFAULT_FANOUT , MAX_ITEMS , EMPTY_KEY } = require ( './utils' )
20
17
const uint8ArrayConcat = require ( 'uint8arrays/concat' )
21
18
const uint8ArrayCompare = require ( 'uint8arrays/compare' )
22
19
const uint8ArrayToString = require ( 'uint8arrays/to-string' )
23
20
const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
24
- const uint8ArrayEquals = require ( 'uint8arrays/equals ' )
21
+ const { sha256 } = require ( 'multiformats/hashes/sha2 ' )
25
22
26
23
/**
27
24
* @typedef {import('interface-datastore').Datastore } Datastore
25
+ * @typedef {import('@ipld/dag-pb').PBNode } PBNode
28
26
*
29
27
* @typedef {object } Pin
30
28
* @property {CID } key
31
29
* @property {Uint8Array } [data]
32
30
*/
33
31
34
32
/**
35
- * @param {Uint8Array | CID } hash
36
- */
37
- function toB58String ( hash ) {
38
- return new CID ( hash ) . toBaseEncodedString ( )
39
- }
40
-
41
- /**
42
- * @param {DAGNode } rootNode
33
+ * @param {PBNode } rootNode
43
34
*/
44
35
function readHeader ( rootNode ) {
45
36
// rootNode.data should be a buffer of the format:
46
37
// < varint(headerLength) | header | itemData... >
47
38
const rootData = rootNode . Data
39
+
40
+ if ( ! rootData ) {
41
+ throw new Error ( 'No data present' )
42
+ }
43
+
48
44
const hdrLength = varint . decode ( rootData )
49
45
const vBytes = varint . decode . bytes
50
46
@@ -86,15 +82,15 @@ function hash (seed, key) {
86
82
const buffer = new Uint8Array ( 4 )
87
83
const dataView = new DataView ( buffer . buffer )
88
84
dataView . setUint32 ( 0 , seed , true )
89
- const encodedKey = uint8ArrayFromString ( toB58String ( key ) )
85
+ const encodedKey = uint8ArrayFromString ( key . toString ( ) )
90
86
const data = uint8ArrayConcat ( [ buffer , encodedKey ] , buffer . byteLength + encodedKey . byteLength )
91
87
92
88
return fnv1a ( uint8ArrayToString ( data ) )
93
89
}
94
90
95
91
/**
96
92
* @param {Datastore } blockstore
97
- * @param {DAGNode } node
93
+ * @param {PBNode } node
98
94
* @returns {AsyncGenerator<CID, void, undefined> }
99
95
*/
100
96
async function * walkItems ( blockstore , node ) {
@@ -107,10 +103,10 @@ async function * walkItems (blockstore, node) {
107
103
// if a fanout bin is not 'empty', dig into and walk its DAGLinks
108
104
const linkHash = link . Hash
109
105
110
- if ( ! uint8ArrayEquals ( EMPTY_KEY , linkHash . bytes ) ) {
106
+ if ( ! EMPTY_KEY . equals ( linkHash ) ) {
111
107
// walk the links of this fanout bin
112
108
const buf = await blockstore . get ( cidToKey ( linkHash ) )
113
- const node = dagpb . util . deserialize ( buf )
109
+ const node = dagPb . decode ( buf )
114
110
115
111
yield * walkItems ( blockstore , node )
116
112
}
@@ -125,7 +121,7 @@ async function * walkItems (blockstore, node) {
125
121
126
122
/**
127
123
* @param {Datastore } blockstore
128
- * @param {DAGNode } rootNode
124
+ * @param {PBNode } rootNode
129
125
* @param {string } name
130
126
*/
131
127
async function * loadSet ( blockstore , rootNode , name ) {
@@ -136,7 +132,7 @@ async function * loadSet (blockstore, rootNode, name) {
136
132
}
137
133
138
134
const buf = await blockstore . get ( cidToKey ( link . Hash ) )
139
- const node = dagpb . util . deserialize ( buf )
135
+ const node = dagPb . decode ( buf )
140
136
141
137
yield * walkItems ( blockstore , node )
142
138
}
@@ -164,26 +160,38 @@ function storeItems (blockstore, items) {
164
160
const fanoutLinks = [ ]
165
161
166
162
for ( let i = 0 ; i < DEFAULT_FANOUT ; i ++ ) {
167
- fanoutLinks . push ( new DAGLink ( '' , 1 , EMPTY_KEY ) )
163
+ fanoutLinks . push ( {
164
+ Name : '' ,
165
+ Tsize : 1 ,
166
+ Hash : EMPTY_KEY
167
+ } )
168
168
}
169
169
170
170
if ( pins . length <= MAX_ITEMS ) {
171
171
const nodes = pins
172
172
. map ( item => {
173
173
return ( {
174
- link : new DAGLink ( '' , 1 , item . key ) ,
174
+ link : {
175
+ Name : '' ,
176
+ Tsize : 1 ,
177
+ Hash : item . key
178
+ } ,
175
179
data : item . data || new Uint8Array ( )
176
180
} )
177
181
} )
178
182
// sorting makes any ordering of `pins` produce the same DAGNode
179
183
. sort ( ( a , b ) => {
184
+ //return a.link.Name.localeCompare(b.link.Name)
180
185
return uint8ArrayCompare ( a . link . Hash . bytes , b . link . Hash . bytes )
181
186
} )
182
187
183
188
const rootLinks = fanoutLinks . concat ( nodes . map ( item => item . link ) )
184
189
const rootData = uint8ArrayConcat ( [ headerBuf , ...nodes . map ( item => item . data ) ] )
185
190
186
- return new DAGNode ( rootData , rootLinks )
191
+ return {
192
+ Data : rootData ,
193
+ Links : rootLinks
194
+ }
187
195
} else {
188
196
// If the array of pins is > MAX_ITEMS, we:
189
197
// - distribute the pins among `DEFAULT_FANOUT` bins
@@ -212,22 +220,30 @@ function storeItems (blockstore, items) {
212
220
idx ++
213
221
}
214
222
215
- return new DAGNode ( headerBuf , fanoutLinks )
223
+ return {
224
+ Data : headerBuf ,
225
+ Links : fanoutLinks
226
+ }
216
227
}
217
228
218
229
/**
219
- * @param {DAGNode } child
230
+ * @param {PBNode } child
220
231
* @param {number } binIdx
221
232
*/
222
233
async function storeChild ( child , binIdx ) {
223
- const buf = dagpb . util . serialize ( child )
224
- const cid = await dagpb . util . cid ( buf , {
225
- cidVersion : 0 ,
226
- hashAlg : multihash . names [ 'sha2-256' ]
227
- } )
234
+ const buf = dagPb . encode ( child )
235
+ const digest = await sha256 . digest ( buf )
236
+ const cid = CID . createV0 ( digest )
237
+
228
238
await blockstore . put ( cidToKey ( cid ) , buf )
229
239
230
- fanoutLinks [ binIdx ] = new DAGLink ( '' , child . size , cid )
240
+ let size = child . Links . reduce ( ( acc , curr ) => acc + ( curr ?. Tsize || 0 ) , 0 ) + buf . length
241
+
242
+ fanoutLinks [ binIdx ] = {
243
+ Name : '' ,
244
+ Tsize : size ,
245
+ Hash : cid
246
+ }
231
247
}
232
248
}
233
249
}
@@ -243,15 +259,19 @@ async function storeSet (blockstore, type, cids) {
243
259
key : cid
244
260
}
245
261
} ) )
246
- const buf = rootNode . serialize ( )
247
- const cid = await dagpb . util . cid ( buf , {
248
- cidVersion : 0 ,
249
- hashAlg : multihash . names [ 'sha2-256' ]
250
- } )
262
+ const buf = dagPb . encode ( rootNode )
263
+ const digest = await sha256 . digest ( buf )
264
+ const cid = CID . createV0 ( digest )
251
265
252
266
await blockstore . put ( cidToKey ( cid ) , buf )
253
267
254
- return new DAGLink ( type , rootNode . size , cid )
268
+ let size = rootNode . Links . reduce ( ( acc , curr ) => acc + curr . Tsize , 0 ) + buf . length
269
+
270
+ return {
271
+ Name : type ,
272
+ Tsize : size ,
273
+ Hash : cid
274
+ }
255
275
}
256
276
257
277
module . exports = {
0 commit comments