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 @@ -83,7 +83,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 @@ -94,8 +94,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 @@ -15,7 +15,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: undefined,
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 @@ -29,12 +29,12 @@ module.exports = ({ ipld, name }) => {
}
}

const [, , hash, ...rest] = path.split('/') // ['', 'ipfs', 'hash', ...path]
const [, scheme, hash, ...rest] = path.split('/') // ['', 'ipfs', 'hash', ...path]
const cid = new CID(hash)

// nothing to resolve return the input
if (rest.length === 0) {
return `/ipfs/${cidToString(cid, { base: opts.cidBase })}`
return `/${scheme}/${cidToString(cid, { base: opts.cidBase })}`
}

path = rest.join('/')
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-message-port-client/src/client/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = class Query {
this.namespace = namespace
this.method = method
this.timeout = input.timeout == null ? Infinity : input.timeout
/** @type {number|null} */
/** @type {ReturnType<typeof setTimeout>|null} */
this.timerID = null
})
}
Expand Down