@@ -8,8 +8,12 @@ const dagpb = require('ipld-dag-pb')
88const { DAGNode, DAGLink } = dagpb
99const multicodec = require ( 'multicodec' )
1010const pbSchema = require ( './pin.proto' )
11- const { Buffer } = require ( 'buffer' )
1211const { cidToKey, DEFAULT_FANOUT , MAX_ITEMS , EMPTY_KEY } = require ( './utils' )
12+ const uint8ArrayConcat = require ( 'uint8arrays/concat' )
13+ const uint8ArrayCompare = require ( 'uint8arrays/compare' )
14+ const uint8ArrayToString = require ( 'uint8arrays/to-string' )
15+ const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
16+ const uint8ArrayEquals = require ( 'uint8arrays/equals' )
1317
1418const pb = protobuf ( pbSchema )
1519
@@ -50,12 +54,13 @@ function readHeader (rootNode) {
5054}
5155
5256function hash ( seed , key ) {
53- const buf = Buffer . alloc ( 4 )
54- buf . writeUInt32LE ( seed , 0 )
55- const data = Buffer . concat ( [
56- buf , Buffer . from ( toB58String ( key ) )
57- ] )
58- return fnv1a ( data . toString ( 'binary' ) )
57+ const buffer = new ArrayBuffer ( 4 )
58+ const dataView = new DataView ( buffer )
59+ dataView . setUint32 ( 0 , seed , true )
60+ const encodedKey = uint8ArrayFromString ( toB58String ( key ) )
61+ const data = uint8ArrayConcat ( [ buf , encodedKey ] , buf . length + encodedKey . length )
62+
63+ return fnv1a ( uint8ArrayToString ( data ) )
5964}
6065
6166async function * walkItems ( blockstore , node ) {
@@ -68,7 +73,7 @@ async function * walkItems (blockstore, node) {
6873 // if a fanout bin is not 'empty', dig into and walk its DAGLinks
6974 const linkHash = link . Hash
7075
71- if ( ! EMPTY_KEY . equals ( linkHash . buffer ) ) {
76+ if ( ! uint8ArrayEquals ( EMPTY_KEY , linkHash . bytes ) ) {
7277 // walk the links of this fanout bin
7378 const buf = await blockstore . get ( cidToKey ( linkHash ) )
7479 const node = dagpb . util . deserialize ( buf )
@@ -106,9 +111,9 @@ function storeItems (blockstore, items) {
106111 fanout : DEFAULT_FANOUT ,
107112 seed : depth
108113 } )
109- const headerBuf = Buffer . concat ( [
110- Buffer . from ( varint . encode ( pbHeader . length ) ) , pbHeader
111- ] )
114+
115+ const header = varint . encode ( pbHeader . length )
116+ const headerBuf = uint8ArrayConcat ( [ header , pbHeader ] )
112117 const fanoutLinks = [ ]
113118
114119 for ( let i = 0 ; i < DEFAULT_FANOUT ; i ++ ) {
@@ -120,16 +125,16 @@ function storeItems (blockstore, items) {
120125 . map ( item => {
121126 return ( {
122127 link : new DAGLink ( '' , 1 , item . key ) ,
123- data : item . data || Buffer . alloc ( 0 )
128+ data : item . data || new Uint8Array ( )
124129 } )
125130 } )
126131 // sorting makes any ordering of `pins` produce the same DAGNode
127- . sort ( ( a , b ) => Buffer . compare ( a . link . Hash . buffer , b . link . Hash . buffer ) )
132+ . sort ( ( a , b ) => {
133+ return uint8ArrayCompare ( a . link . Hash . bytes , b . link . Hash . bytes )
134+ } )
128135
129136 const rootLinks = fanoutLinks . concat ( nodes . map ( item => item . link ) )
130- const rootData = Buffer . concat (
131- [ headerBuf ] . concat ( nodes . map ( item => item . data ) )
132- )
137+ const rootData = uint8ArrayConcat ( [ headerBuf , ...nodes . map ( item => item . data ) ] )
133138
134139 return new DAGNode ( rootData , rootLinks )
135140 } else {
@@ -162,13 +167,6 @@ function storeItems (blockstore, items) {
162167 }
163168
164169 async function storeChild ( child , binIdx ) {
165- const opts = {
166- version : 0 ,
167- format : multicodec . DAG_PB ,
168- hashAlg : multicodec . SHA2_256 ,
169- preload : false
170- }
171-
172170 const buf = dagpb . util . serialize ( child )
173171 const cid = dagpb . util . cid ( buf , {
174172 cidVersion : 0 ,
0 commit comments