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

Commit

Permalink
feat: refs-local reconstructs CIDs
Browse files Browse the repository at this point in the history
Related to #2415
  • Loading branch information
AuHau committed Oct 8, 2019
1 parent c9be79e commit 6d6e8c6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"ipfs-http-response": "~0.3.1",
"ipfs-mfs": "^0.13.0",
"ipfs-multipart": "^0.2.0",
"ipfs-repo": "^0.28.0",
"ipfs-repo": "github:ipfs/js-ipfs-repo#auhau/feat/multihash_keys_in_datastore",
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-exporter": "^0.38.0",
"ipfs-unixfs-importer": "^0.40.0",
Expand Down Expand Up @@ -203,7 +203,7 @@
"execa": "^2.0.4",
"form-data": "^2.5.1",
"hat": "0.0.3",
"interface-ipfs-core": "^0.117.2",
"interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#auhau/feat/multihash_keys_in_datastore",
"ipfs-interop": "^0.1.1",
"ipfsd-ctl": "^0.47.2",
"libp2p-websocket-star": "~0.10.2",
Expand Down
4 changes: 4 additions & 0 deletions src/cli/commands/refs-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module.exports = {

describe: 'List all local references.',

builder (yargs) {
return yargs.epilog('CIDs are reconstructed therefore they might differ from those under which the blocks were originally stored.')
},

handler ({ getIpfs, print, resolve }) {
resolve((async () => {
const ipfs = await getIpfs()
Expand Down
22 changes: 7 additions & 15 deletions src/core/components/files-regular/refs-local-pull-stream.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
'use strict'

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

module.exports = function (self) {
return () => {
return itToPull((async function * () {
for await (const result of self._repo.blocks.query({ keysOnly: true })) {
yield dsKeyToRef(result.key)
for await (const { key: k } of self._repo.blocks.query({ keysOnly: true })) {
try {
yield { ref: keyToCid(k).toString() }
} catch (err) {
yield { err: `Could not convert block with key '${k.toString()}' to CID: ${err.message}` }
}
}
})())
}
}

function dsKeyToRef (key) {
try {
// Block key is of the form /<base32 encoded string>
const decoder = new base32.Decoder()
const buff = Buffer.from(decoder.write(key.toString().slice(1)).finalize())
return { ref: new CID(buff).toString() }
} catch (err) {
return { err: `Could not convert block with key '${key}' to CID: ${err.message}` }
}
}
4 changes: 2 additions & 2 deletions test/cli/refs-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('refs-local', () => runOnAndOff((thing) => {
const out = await ipfs('refs-local')
const lines = out.split('\n')

expect(lines.includes('QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN')).to.eql(true)
expect(lines.includes('QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU')).to.eql(true)
expect(lines.includes('bafkreicjl7v3vyyv4zlryihez5xhunqmriry6styhil7z5lhd3r4prnz6y')).to.eql(true)
expect(lines.includes('bafkreidj5bovvm25wszvajfshj7m7m2efpswcs6dsz7giz52ovlquxc4o4')).to.eql(true)
})
}))
2 changes: 1 addition & 1 deletion test/fixtures/go-ipfs-repo/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7
8

0 comments on commit 6d6e8c6

Please sign in to comment.