Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 83b606f

Browse files
committed
feat: refs-local reconstructs CIDs
Related to #2415
1 parent 50f3667 commit 83b606f

File tree

5 files changed

+16
-20
lines changed

5 files changed

+16
-20
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
"ipfs-http-response": "~0.3.1",
104104
"ipfs-mfs": "^0.13.0",
105105
"ipfs-multipart": "^0.2.0",
106-
"ipfs-repo": "^0.28.0",
106+
"ipfs-repo": "github:ipfs/js-ipfs-repo#auhau/feat/multihash_keys_in_datastore",
107107
"ipfs-unixfs": "~0.1.16",
108108
"ipfs-unixfs-exporter": "^0.38.0",
109109
"ipfs-unixfs-importer": "^0.40.0",
@@ -203,7 +203,7 @@
203203
"execa": "^2.0.4",
204204
"form-data": "^2.5.1",
205205
"hat": "0.0.3",
206-
"interface-ipfs-core": "^0.117.2",
206+
"interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#auhau/feat/multihash_keys_in_datastore",
207207
"ipfs-interop": "^0.1.1",
208208
"ipfsd-ctl": "^0.47.2",
209209
"libp2p-websocket-star": "~0.10.2",

src/cli/commands/refs-local.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ module.exports = {
55

66
describe: 'List all local references.',
77

8+
builder (yargs) {
9+
return yargs.epilog('CIDs are reconstructed therefore they might differ from those under which the blocks were originally stored.')
10+
},
11+
812
handler ({ getIpfs, print, resolve }) {
913
resolve((async () => {
1014
const ipfs = await getIpfs()
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
'use strict'
22

3-
const CID = require('cids')
4-
const base32 = require('base32.js')
3+
const { keyToCid } = require('ipfs-repo/src/blockstore-utils')
54
const itToPull = require('async-iterator-to-pull-stream')
65

76
module.exports = function (self) {
87
return () => {
98
return itToPull((async function * () {
10-
for await (const result of self._repo.blocks.query({ keysOnly: true })) {
11-
yield dsKeyToRef(result.key)
9+
for await (const { key: k } of self._repo.blocks.query({ keysOnly: true })) {
10+
try {
11+
yield { ref: keyToCid(k).toString() }
12+
} catch (err) {
13+
yield { err: `Could not convert block with key '${k.toString()}' to CID: ${err.message}` }
14+
}
1215
}
1316
})())
1417
}
1518
}
16-
17-
function dsKeyToRef (key) {
18-
try {
19-
// Block key is of the form /<base32 encoded string>
20-
const decoder = new base32.Decoder()
21-
const buff = Buffer.from(decoder.write(key.toString().slice(1)).finalize())
22-
return { ref: new CID(buff).toString() }
23-
} catch (err) {
24-
return { err: `Could not convert block with key '${key}' to CID: ${err.message}` }
25-
}
26-
}

test/cli/refs-local.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('refs-local', () => runOnAndOff((thing) => {
1818
const out = await ipfs('refs-local')
1919
const lines = out.split('\n')
2020

21-
expect(lines.includes('QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN')).to.eql(true)
22-
expect(lines.includes('QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU')).to.eql(true)
21+
expect(lines.includes('bafkreicjl7v3vyyv4zlryihez5xhunqmriry6styhil7z5lhd3r4prnz6y')).to.eql(true)
22+
expect(lines.includes('bafkreidj5bovvm25wszvajfshj7m7m2efpswcs6dsz7giz52ovlquxc4o4')).to.eql(true)
2323
})
2424
}))

test/fixtures/go-ipfs-repo/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7
1+
8

0 commit comments

Comments
 (0)