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

Commit 6fecca8

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

File tree

8 files changed

+49
-31
lines changed

8 files changed

+49
-31
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@
9999
"ipfs-bitswap": "^0.26.0",
100100
"ipfs-block": "~0.8.1",
101101
"ipfs-block-service": "~0.16.0",
102-
"ipfs-http-client": "^38.2.0",
102+
"ipfs-http-client": "github:ipfs/js-ipfs-http-client#auhau/feat/multihash_keys_in_datastore",
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

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@ module.exports = {
55

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

8-
handler ({ getIpfs, print, resolve }) {
8+
builder (yargs) {
9+
return yargs
10+
.option('multihash', {
11+
desc: 'Shows base32 encoded multihashes instead of reconstructed CIDs',
12+
type: 'boolean',
13+
default: false
14+
})
15+
.epilog('CIDs are reconstructed therefore they might differ from those under which the blocks were originally stored.')
16+
},
17+
18+
handler ({ getIpfs, print, resolve, multihash }) {
919
resolve((async () => {
1020
const ipfs = await getIpfs()
1121

1222
return new Promise((resolve, reject) => {
13-
const stream = ipfs.refs.localReadableStream()
23+
const stream = ipfs.refs.localReadableStream({ multihash })
1424

1525
stream.on('error', reject)
1626
stream.on('end', resolve)
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) {
8-
return () => {
7+
return ({ multihash }) => {
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: multihash ? k.toString().substr(1) : 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-
}

src/core/components/files-regular/refs-local-readable-stream.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const toStream = require('pull-stream-to-stream')
44

55
module.exports = function (self) {
6-
return (ipfsPath, options) => {
7-
return toStream.source(self.refs.localPullStream())
6+
return (options) => {
7+
return toStream.source(self.refs.localPullStream(options))
88
}
99
}

src/core/components/files-regular/refs-local.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ const promisify = require('promisify-es6')
44
const pull = require('pull-stream')
55

66
module.exports = function (self) {
7-
return promisify((callback) => {
7+
return promisify((options, callback) => {
8+
if (typeof options === 'function') {
9+
callback = options
10+
options = {}
11+
}
12+
13+
options = options || {}
14+
815
pull(
9-
self.refs.localPullStream(),
16+
self.refs.localPullStream(options),
1017
pull.collect((err, values) => {
1118
if (err) {
1219
return callback(err)

src/http/api/resources/files-regular.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports.parseKey = (request, h) => {
3737
const { arg } = request.query
3838

3939
if (!arg) {
40-
throw Boom.badRequest("Argument 'key' is required")
40+
throw Boom.badRequest('Argument \'key\' is required')
4141
}
4242

4343
const isArray = Array.isArray(arg)
@@ -235,7 +235,7 @@ exports.add = {
235235
)
236236
.then(() => {
237237
if (!filesParsed) {
238-
throw new Error("File argument 'data' is required.")
238+
throw new Error('File argument \'data\' is required.')
239239
}
240240
})
241241
.catch(err => {
@@ -342,10 +342,19 @@ exports.refs = {
342342
}
343343

344344
exports.refs.local = {
345+
validate: {
346+
query: Joi.object().keys({
347+
multihash: Joi.boolean().default(false),
348+
}).unknown()
349+
},
350+
345351
// main route handler
346352
handler (request, h) {
347353
const { ipfs } = request.server.app
348-
const source = ipfs.refs.localPullStream()
354+
355+
const multihash = request.query.multihash
356+
357+
const source = ipfs.refs.localPullStream({ multihash })
349358
return sendRefsReplyStream(request, h, 'local refs', source)
350359
}
351360
}

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)