File tree Expand file tree Collapse file tree 3 files changed +35
-9
lines changed Expand file tree Collapse file tree 3 files changed +35
-9
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " e-ipfs-core-lib" ,
3
- "version" : " 0.5.0 " ,
3
+ "version" : " 0.5.1 " ,
4
4
"description" : " E-IPFS core library" ,
5
5
"license" : " (Apache-2.0 AND MIT)" ,
6
6
"homepage" : " https://github.com/elastic-ipfs/core-lib" ,
26
26
"protobufjs" : " ^7.1.2" ,
27
27
"sodium-native" : " ^3.4.1" ,
28
28
"undici" : " ^5.13.0" ,
29
+ "varint" : " ^6.0.0" ,
29
30
"xml-js" : " ^1.6.11"
30
31
},
31
32
"devDependencies" : {
Original file line number Diff line number Diff line change 3
3
import { default as protobuf } from 'protobufjs'
4
4
import path from 'path'
5
5
6
- import { dirname } from '../src/util.js'
6
+ import { dirname , varintEncoder } from '../src/util.js'
7
7
import { CID } from 'multiformats/cid'
8
8
9
9
const definitions = protobuf . loadSync ( path . join ( dirname ( import . meta. url ) , './bitswap.proto' ) )
@@ -144,15 +144,17 @@ class WantList {
144
144
class Block {
145
145
constructor ( prefixOrCid , data ) {
146
146
if ( prefixOrCid instanceof CID ) {
147
- prefixOrCid = Buffer . from ( [
148
- prefixOrCid . version ,
149
- prefixOrCid . code ,
150
- prefixOrCid . multihash . bytes [ 0 ] ,
151
- prefixOrCid . multihash . bytes [ 1 ]
147
+ const version = prefixOrCid . version
148
+ const codec = prefixOrCid . code
149
+ const multihash = prefixOrCid . multihash . code
150
+ const digestLength = prefixOrCid . multihash . digest . length
151
+ this . prefix = varintEncoder ( [
152
+ version , codec , multihash , digestLength
152
153
] )
154
+ } else {
155
+ this . prefix = prefixOrCid
153
156
}
154
157
155
- this . prefix = prefixOrCid
156
158
this . data = data
157
159
}
158
160
Original file line number Diff line number Diff line change 2
2
import fs from 'fs'
3
3
import path from 'path'
4
4
import url from 'url'
5
+ import * as varint from 'varint'
5
6
import { base58btc as base58 } from 'multiformats/bases/base58'
6
7
7
8
function dirname ( importMetaUrl ) {
@@ -21,4 +22,26 @@ function cidToKey (cid) {
21
22
}
22
23
}
23
24
24
- export { dirname , version , cidToKey }
25
+ /**
26
+ * from https://github.com/ipfs/js-ipfs-bitswap
27
+ * @param {Array<number> } buf
28
+ * @returns {Uint8Array }
29
+ */
30
+ function varintEncoder ( buf ) {
31
+ let out = new Uint8Array ( buf . reduce ( ( acc , curr ) => {
32
+ return acc + varint . default . encodingLength ( curr )
33
+ } , 0 ) )
34
+
35
+ let offset = 0
36
+
37
+ for ( const num of buf ) {
38
+ out = varint . encode ( num , out , offset )
39
+
40
+ // @ts -expect-error types are wrong
41
+ offset += varint . default . encodingLength ( num )
42
+ }
43
+
44
+ return out
45
+ }
46
+
47
+ export { dirname , version , cidToKey , varintEncoder }
You can’t perform that action at this time.
0 commit comments