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

Commit

Permalink
fix: only accept cid for ipfs.dag.get (#3675)
Browse files Browse the repository at this point in the history
If you have a path within the DAG to resolve, pass it as `path` in the options object.

Makes the API conform to the documentation.

Fixes #3637
  • Loading branch information
achingbrain authored May 7, 2021
1 parent 0b2d98c commit bb8f8bc
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 43 deletions.
8 changes: 4 additions & 4 deletions examples/traverse-ipld-graphs/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ async function main () {
await ipfs.block.put(new Block(data, cid))
}

const block302516 = 'z43AaGEywSDX5PUJcrn5GfZmb6FjisJyR7uahhWPk456f7k7LDA'
const block302517 = 'z43AaGF42R2DXsU65bNnHRCypLPr9sg6D7CUws5raiqATVaB1jj'
const block302516 = new CID('z43AaGEywSDX5PUJcrn5GfZmb6FjisJyR7uahhWPk456f7k7LDA')
const block302517 = new CID('z43AaGF42R2DXsU65bNnHRCypLPr9sg6D7CUws5raiqATVaB1jj')
let res

res = await ipfs.dag.get(block302516 + '/number')
res = await ipfs.dag.get(block302516, { path: 'number' })
console.log(uint8ArrayToString(res.value, 'base16'))

res = await ipfs.dag.get(block302517 + '/parent/number')
res = await ipfs.dag.get(block302517, { path: 'parent/number' })
console.log(uint8ArrayToString(res.value, 'base16'))
}

Expand Down
6 changes: 3 additions & 3 deletions examples/traverse-ipld-graphs/get-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ async function main () {
const cid = await ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha2-256' })
let result

result = await ipfs.dag.get(cid, 'name')
result = await ipfs.dag.get(cid, { path: 'name' })
console.log(result.value)

result = await ipfs.dag.get(cid, 'likes')
result = await ipfs.dag.get(cid, { path: 'likes' })
console.log(result.value)

result = await ipfs.dag.get(cid + '/likes/0')
result = await ipfs.dag.get(cid, { path: '/likes/0' })
console.log(result.value)
}

Expand Down
12 changes: 6 additions & 6 deletions examples/traverse-ipld-graphs/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function main () {
await ipfs.block.put(new Block(data, cid))
}))

const v1tag = 'z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue'
const v1tag = new CID('z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue')

async function logResult (fn, comment) {
const result = await fn()
Expand All @@ -56,11 +56,11 @@ async function main () {
console.log(result.value)
}

await logResult(() => ipfs.dag.get(v1tag + '/'), 'Tag object:')
await logResult(() => ipfs.dag.get(v1tag + '/object/message'), 'Tagged commit message:')
await logResult(() => ipfs.dag.get(v1tag + '/object/parents/0/message'), 'Parent of tagged commit:')
await logResult(() => ipfs.dag.get(v1tag + '/object/tree/src/hash/hello/hash'), '/src/hello file:')
await logResult(() => ipfs.dag.get(v1tag + '/object/parents/0/tree/src/hash/hello/hash'), 'previous version of /src/hello file:')
await logResult(() => ipfs.dag.get(v1tag), 'Tag object:')
await logResult(() => ipfs.dag.get(v1tag, { path: '/object/message' }), 'Tagged commit message:')
await logResult(() => ipfs.dag.get(v1tag, { path: '/object/parents/0/message' }), 'Parent of tagged commit:')
await logResult(() => ipfs.dag.get(v1tag, { path: '/object/tree/src/hash/hello/hash' }), '/src/hello file:')
await logResult(() => ipfs.dag.get(v1tag, { path: '/object/parents/0/tree/src/hash/hello/hash' }), 'previous version of /src/hello file:')
}

main()
22 changes: 4 additions & 18 deletions packages/interface-ipfs-core/src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,8 @@ module.exports = (common, options) => {
expect(result.value).to.eql(uint8ArrayFromString('I am inside a Protobuf'))
})

it('should get by CID string', async () => {
const cidCborStr = cidCbor.toBaseEncodedString()

const result = await ipfs.dag.get(cidCborStr)

const node = result.value

const cid = await dagCBOR.util.cid(dagCBOR.util.serialize(node))
expect(cid).to.eql(cidCbor)
})

it('should get by CID string + path', async function () {
const cidCborStr = cidCbor.toBaseEncodedString()

const result = await ipfs.dag.get(cidCborStr + '/pb/Data')
it('should get by CID with path option', async function () {
const result = await ipfs.dag.get(cidCbor, { path: '/pb/Data' })
expect(result.value).to.eql(uint8ArrayFromString('I am inside a Protobuf'))
})

Expand Down Expand Up @@ -202,10 +189,9 @@ module.exports = (common, options) => {
foo: 'dag-cbor-bar'
}

let cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
const cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
expect(cid.codec).to.equal('dag-cbor')
cid = cid.toBaseEncodedString('base32')
expect(cid).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')
expect(cid.toBaseEncodedString('base32')).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')

const result = await ipfs.dag.get(cid, {
path: 'foo'
Expand Down
12 changes: 1 addition & 11 deletions packages/ipfs-core/src/components/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
const first = require('it-first')
const last = require('it-last')
const toCidAndPath = require('ipfs-core-utils/src/to-cid-and-path')

/**
* @param {Object} config
Expand All @@ -14,16 +13,7 @@ module.exports = ({ ipld, preload }) => {
/**
* @type {import('ipfs-core-types/src/dag').API["get"]}
*/
const get = async function get (ipfsPath, options = {}) {
const {
cid,
path
} = toCidAndPath(ipfsPath)

if (path) {
options.path = path
}

const get = async function get (cid, options = {}) {
if (options.preload !== false) {
preload(cid)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs/test/interface-http-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('interface-ipfs-core over ipfs-http-client tests against go-ipfs', () =
reason: 'FIXME vmx 2018-02-22: Currently not supported in go-ipfs, it might be possible once https://github.com/ipfs/go-ipfs/issues/4728 is done'
},
{
name: 'should get by CID string + path',
name: 'should get by CID with path option',
reason: 'FIXME vmx 2018-02-22: Currently not supported in go-ipfs, it might be possible once https://github.com/ipfs/go-ipfs/issues/4728 is done'
},
{
Expand Down

0 comments on commit bb8f8bc

Please sign in to comment.