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

fix: make "ipfs resolve" cli command recursive by default #3707

Merged
merged 11 commits into from
Jul 19, 2021
19 changes: 17 additions & 2 deletions packages/interface-ipfs-core/src/miscellaneous/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module.exports = (common, options) => {
expect(isIpfs.ipfsPath(resolved)).to.be.true()
})

it('should resolve IPNS link recursively', async function () {
it('should resolve IPNS link recursively by default', async function () {
this.timeout(20 * 1000)
// webworkers are not dialable because webrtc is not available
const node = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
Expand All @@ -96,8 +96,23 @@ module.exports = (common, options) => {
await ipfs.name.publish(path, { allowOffline: true })
await ipfs.name.publish(`/ipns/${ipfs.peerId.id}`, { allowOffline: true, key: 'key-name', resolve: false })

return expect(await ipfs.resolve(`/ipns/${keyId}`, { recursive: true }))
return expect(await ipfs.resolve(`/ipns/${keyId}`))
.to.eq(`/ipfs/${path}`)
})

it('should resolve IPNS link non-recursively if recursive==false', async function () {
this.timeout(20 * 1000)
// webworkers are not dialable because webrtc is not available
const node = (await common.spawn({ type: isWebWorker ? 'go' : undefined })).api
await ipfs.swarm.connect(node.peerId.addresses[0])
const { path } = await ipfs.add(uint8ArrayFromString('should resolve an IPNS key if recursive === false'))
const { id: keyId } = await ipfs.key.gen('new-key-name', { type: 'rsa', size: 2048 })

await ipfs.name.publish(path, { allowOffline: true })
await ipfs.name.publish(`/ipns/${ipfs.peerId.id}`, { allowOffline: true, key: 'new-key-name', resolve: false })

return expect(await ipfs.resolve(`/ipns/${keyId}`, { recursive: false }))
.to.eq(`/ipns/${ipfs.peerId.id}`)
})
})
}
2 changes: 1 addition & 1 deletion packages/ipfs-cli/src/commands/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
recursive: {
alias: 'r',
type: 'boolean',
default: false
default: true
},
'cid-base': {
describe: 'Number base to display CIDs in.',
Expand Down
15 changes: 6 additions & 9 deletions packages/ipfs-cli/test/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const cli = require('./utils/cli')
const sinon = require('sinon')

const defaultOptions = {
recursive: false,
recursive: true,
cidBase: 'base58btc',
timeout: undefined
}
Expand All @@ -31,27 +31,24 @@ describe('resolve', () => {
expect(out).to.equal(resolved + '\n')
})

it('resolves a CID recursively', async () => {
it('resolves a CID recursively by default', async () => {
const resolved = `/ipfs/${cid}`

ipfs.resolve.withArgs(cid.toString(), {
...defaultOptions,
recursive: true
}).resolves(resolved)
ipfs.resolve.withArgs(cid.toString(), defaultOptions).resolves(resolved)

const out = await cli(`resolve ${cid} --recursive`, { ipfs })
expect(out).to.equal(resolved + '\n')
})

it('resolves a CID recursively (short option)', async () => {
it('allows non-recursive lookups with flag', async () => {
const resolved = `/ipfs/${cid}`

ipfs.resolve.withArgs(cid.toString(), {
...defaultOptions,
recursive: true
recursive: false
}).resolves(resolved)

const out = await cli(`resolve ${cid} -r`, { ipfs })
const out = await cli(`resolve ${cid} --recursive=false`, { ipfs })
expect(out).to.equal(resolved + '\n')
})

Expand Down
4 changes: 2 additions & 2 deletions packages/ipfs-core/src/components/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ module.exports = ({ repo, codecs, bases, name }) => {
}
}

const [, , hash, ...rest] = path.split('/') // ['', 'ipfs', 'hash', ...path]
const [, schema, hash, ...rest] = path.split('/') // ['', 'ipfs', 'hash', ...path]
const cid = CID.parse(hash)
const base = opts.cidBase ? await bases.getBase(opts.cidBase) : undefined

// nothing to resolve return the input
if (rest.length === 0) {
return `/ipfs/${cid.toString(base && base.encoder)}`
return `/${schema}/${cid.toString(base && base.encoder)}`
}

path = rest.join('/')
Expand Down