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

Commit bb8f8bc

Browse files
authoredMay 7, 2021
fix: only accept cid for ipfs.dag.get (#3675)
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
1 parent 0b2d98c commit bb8f8bc

File tree

6 files changed

+19
-43
lines changed

6 files changed

+19
-43
lines changed
 

‎examples/traverse-ipld-graphs/eth.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ async function main () {
3434
await ipfs.block.put(new Block(data, cid))
3535
}
3636

37-
const block302516 = 'z43AaGEywSDX5PUJcrn5GfZmb6FjisJyR7uahhWPk456f7k7LDA'
38-
const block302517 = 'z43AaGF42R2DXsU65bNnHRCypLPr9sg6D7CUws5raiqATVaB1jj'
37+
const block302516 = new CID('z43AaGEywSDX5PUJcrn5GfZmb6FjisJyR7uahhWPk456f7k7LDA')
38+
const block302517 = new CID('z43AaGF42R2DXsU65bNnHRCypLPr9sg6D7CUws5raiqATVaB1jj')
3939
let res
4040

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

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

‎examples/traverse-ipld-graphs/get-path.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ async function main () {
1515
const cid = await ipfs.dag.put(myData, { format: 'dag-cbor', hashAlg: 'sha2-256' })
1616
let result
1717

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

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

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

‎examples/traverse-ipld-graphs/git.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async function main () {
4242
await ipfs.block.put(new Block(data, cid))
4343
}))
4444

45-
const v1tag = 'z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue'
45+
const v1tag = new CID('z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue')
4646

4747
async function logResult (fn, comment) {
4848
const result = await fn()
@@ -56,11 +56,11 @@ async function main () {
5656
console.log(result.value)
5757
}
5858

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

6666
main()

‎packages/interface-ipfs-core/src/dag/get.js

+4-18
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,8 @@ module.exports = (common, options) => {
135135
expect(result.value).to.eql(uint8ArrayFromString('I am inside a Protobuf'))
136136
})
137137

138-
it('should get by CID string', async () => {
139-
const cidCborStr = cidCbor.toBaseEncodedString()
140-
141-
const result = await ipfs.dag.get(cidCborStr)
142-
143-
const node = result.value
144-
145-
const cid = await dagCBOR.util.cid(dagCBOR.util.serialize(node))
146-
expect(cid).to.eql(cidCbor)
147-
})
148-
149-
it('should get by CID string + path', async function () {
150-
const cidCborStr = cidCbor.toBaseEncodedString()
151-
152-
const result = await ipfs.dag.get(cidCborStr + '/pb/Data')
138+
it('should get by CID with path option', async function () {
139+
const result = await ipfs.dag.get(cidCbor, { path: '/pb/Data' })
153140
expect(result.value).to.eql(uint8ArrayFromString('I am inside a Protobuf'))
154141
})
155142

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

205-
let cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
192+
const cid = await ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' })
206193
expect(cid.codec).to.equal('dag-cbor')
207-
cid = cid.toBaseEncodedString('base32')
208-
expect(cid).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')
194+
expect(cid.toBaseEncodedString('base32')).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')
209195

210196
const result = await ipfs.dag.get(cid, {
211197
path: 'foo'

‎packages/ipfs-core/src/components/dag/get.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const withTimeoutOption = require('ipfs-core-utils/src/with-timeout-option')
44
const first = require('it-first')
55
const last = require('it-last')
6-
const toCidAndPath = require('ipfs-core-utils/src/to-cid-and-path')
76

87
/**
98
* @param {Object} config
@@ -14,16 +13,7 @@ module.exports = ({ ipld, preload }) => {
1413
/**
1514
* @type {import('ipfs-core-types/src/dag').API["get"]}
1615
*/
17-
const get = async function get (ipfsPath, options = {}) {
18-
const {
19-
cid,
20-
path
21-
} = toCidAndPath(ipfsPath)
22-
23-
if (path) {
24-
options.path = path
25-
}
26-
16+
const get = async function get (cid, options = {}) {
2717
if (options.preload !== false) {
2818
preload(cid)
2919
}

‎packages/ipfs/test/interface-http-go.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ describe('interface-ipfs-core over ipfs-http-client tests against go-ipfs', () =
138138
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'
139139
},
140140
{
141-
name: 'should get by CID string + path',
141+
name: 'should get by CID with path option',
142142
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'
143143
},
144144
{

0 commit comments

Comments
 (0)
This repository has been archived.