Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 21525d4

Browse files
author
Alan Shaw
committed
refactor: return CIDs from core
1 parent 2aaf632 commit 21525d4

File tree

10 files changed

+45
-26
lines changed

10 files changed

+45
-26
lines changed

src/bitswap/stat.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const configure = require('../lib/configure')
44
const Big = require('bignumber.js')
5+
const CID = require('cids')
56

67
module.exports = configure(({ ky }) => {
78
return async (options) => {
@@ -21,8 +22,8 @@ module.exports = configure(({ ky }) => {
2122
function toCoreInterface (res) {
2223
return {
2324
provideBufLen: res.ProvideBufLen,
24-
wantlist: res.Wantlist || [],
25-
peers: res.Peers || [],
25+
wantlist: (res.Wantlist || []).map(k => new CID(k['/'])),
26+
peers: (res.Peers || []).map(p => new CID(p)),
2627
blocksReceived: new Big(res.BlocksReceived),
2728
dataReceived: new Big(res.DataReceived),
2829
blocksSent: new Big(res.BlocksSent),

src/bitswap/wantlist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ module.exports = configure(({ ky }) => {
2424
searchParams
2525
}).json()
2626

27-
return res
27+
return (res.Keys || []).map(k => new CID(k['/']))
2828
}
2929
})

src/block/stat.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const CID = require('cids')
44
const { Buffer } = require('buffer')
55
const configure = require('../lib/configure')
6-
const toCamel = require('../lib/object-to-camel')
76

87
module.exports = configure(({ ky }) => {
98
return async (cid, options) => {
@@ -23,6 +22,6 @@ module.exports = configure(({ ky }) => {
2322
searchParams
2423
}).json()
2524

26-
return toCamel(res)
25+
return { cid: new CID(res.Key), size: res.Size }
2726
}
2827
})

src/files/ls.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = configure(({ ky }) => {
4242
})
4343

4444
function toCoreInterface (entry) {
45-
entry.cid = new CID(entry.hash)
45+
if (entry.hash) entry.cid = new CID(entry.hash)
4646
delete entry.hash
4747
return entry
4848
}

src/pin/add.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const CID = require('cids')
34
const configure = require('../lib/configure')
45

56
module.exports = configure(({ ky }) => {
@@ -17,6 +18,6 @@ module.exports = configure(({ ky }) => {
1718
searchParams
1819
}).json()
1920

20-
return (res.Pins || []).map(hash => ({ hash }))
21+
return (res.Pins || []).map(cid => ({ cid: new CID(cid) }))
2122
}
2223
})

src/pin/ls.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

33
const ndjson = require('iterable-ndjson')
4+
const CID = require('cids')
45
const configure = require('../lib/configure')
56
const toIterable = require('stream-to-it/source')
6-
const toCamel = require('../lib/object-to-camel')
77

88
module.exports = configure(({ ky }) => {
99
return async function * ls (path, options) {
@@ -31,12 +31,12 @@ module.exports = configure(({ ky }) => {
3131
for await (const pin of ndjson(toIterable(res.body))) {
3232
// For nodes that do not understand the `stream option`
3333
if (pin.Keys) {
34-
for (const hash of Object.keys(pin.Keys)) {
35-
yield { hash, type: pin.Keys[hash].Type }
34+
for (const cid of Object.keys(pin.Keys)) {
35+
yield { cid: new CID(cid), type: pin.Keys[cid].Type }
3636
}
3737
return
3838
}
39-
yield toCamel(pin)
39+
yield { cid: new CID(pin.Hash), type: pin.Type }
4040
}
4141
}
4242
})

src/pin/rm.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const CID = require('cids')
34
const configure = require('../lib/configure')
45

56
module.exports = configure(({ ky }) => {
@@ -17,6 +18,6 @@ module.exports = configure(({ ky }) => {
1718
searchParams
1819
}).json()
1920

20-
return (res.Pins || []).map(hash => ({ hash }))
21+
return (res.Pins || []).map(cid => ({ cid: new CID(cid) }))
2122
}
2223
})

test/files-mfs.spec.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
const { expect } = require('interface-ipfs-core/src/utils/mocha')
66
const loadFixture = require('aegir/fixtures')
77
const mh = require('multihashes')
8-
const CID = require('cids')
98
const all = require('it-all')
109
const pipe = require('it-pipe')
1110
const { TimeoutError } = require('ky-universal')
@@ -43,7 +42,7 @@ describe('.files (the MFS API part)', function () {
4342
const res = await all(ipfs.add(testfile))
4443

4544
expect(res).to.have.length(1)
46-
expect(res[0].hash).to.equal(expectedMultihash)
45+
expect(res[0].cid.toString()).to.equal(expectedMultihash)
4746
expect(res[0].path).to.equal(expectedMultihash)
4847
})
4948

@@ -56,7 +55,7 @@ describe('.files (the MFS API part)', function () {
5655
const res = await all(ipfs.add(file))
5756

5857
expect(res).to.have.length(1)
59-
expect(res[0].hash).to.equal(expectedBufferMultihash)
58+
expect(res[0].cid.toString()).to.equal(expectedBufferMultihash)
6059
expect(res[0].path).to.equal(expectedBufferMultihash)
6160
})
6261

@@ -67,7 +66,7 @@ describe('.files (the MFS API part)', function () {
6766
const res = await all(ipfs.add([{ path: '', content }]))
6867

6968
expect(res).to.have.length(1)
70-
expect(res[0].hash).to.equal(expectedHash)
69+
expect(res[0].cid.toString()).to.equal(expectedHash)
7170
expect(res[0].path).to.equal(expectedHash)
7271
})
7372

@@ -78,7 +77,7 @@ describe('.files (the MFS API part)', function () {
7877
const res = await all(ipfs.add(testfile, options))
7978

8079
expect(res).to.have.length(1)
81-
expect(res[0].hash).to.equal(expectedCid)
80+
expect(res[0].cid.toString()).to.equal(expectedCid)
8281
expect(res[0].path).to.equal(expectedCid)
8382
})
8483

@@ -89,15 +88,15 @@ describe('.files (the MFS API part)', function () {
8988
expect(files).to.have.length(1)
9089

9190
// 'ipfs.object.get(<hash>)' should timeout because content wasn't actually added
92-
return expect(ipfs.object.get(files[0].hash, { timeout: 2000 }))
91+
return expect(ipfs.object.get(files[0].cid, { timeout: 2000 }))
9392
.to.be.rejectedWith(TimeoutError)
9493
})
9594

9695
it('.add with options', async () => {
9796
const res = await all(ipfs.add(testfile, { pin: false }))
9897

9998
expect(res).to.have.length(1)
100-
expect(res[0].hash).to.equal(expectedMultihash)
99+
expect(res[0].cid.toString()).to.equal(expectedMultihash)
101100
expect(res[0].path).to.equal(expectedMultihash)
102101
})
103102

@@ -137,7 +136,7 @@ describe('.files (the MFS API part)', function () {
137136
const res = await all(ipfs.add([file], options))
138137

139138
expect(res).to.have.length(1)
140-
const cid = new CID(res[0].hash)
139+
const { cid } = res[0]
141140
expect(mh.decode(cid.multihash).name).to.equal(name)
142141
})
143142
})
@@ -210,7 +209,7 @@ describe('.files (the MFS API part)', function () {
210209
const res = await all(ipfs.add([file], options))
211210

212211
expect(res).to.have.length(1)
213-
const cid = new CID(res[0].hash)
212+
const { cid } = res[0]
214213
expect(mh.decode(cid.multihash).name).to.equal(name)
215214
})
216215
})
@@ -225,15 +224,17 @@ describe('.files (the MFS API part)', function () {
225224
)
226225

227226
expect(res).to.have.length(1)
228-
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
227+
res[0].cid = res[0].cid.toString()
228+
expect(res[0]).to.deep.equal({ path: expectedCid, cid: expectedCid, size: 12 })
229229
})
230230

231231
it('.add with iterable', async () => {
232232
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
233233
const res = await all(ipfs.add([Buffer.from('test')]))
234234

235235
expect(res).to.have.length(1)
236-
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
236+
res[0].cid = res[0].cid.toString()
237+
expect(res[0]).to.deep.equal({ path: expectedCid, cid: expectedCid, size: 12 })
237238
})
238239

239240
it('files.mkdir', async () => {
@@ -346,9 +347,10 @@ describe('.files (the MFS API part)', function () {
346347
})
347348

348349
const stats = await ipfs.files.stat(file)
350+
stats.cid = stats.cid.toString()
349351

350352
expect(stats).to.deep.equal({
351-
hash: 'QmQhouoDPAnzhVM148yCa9CbUXK65wSEAZBtgrLGHtmdmP',
353+
cid: 'QmQhouoDPAnzhVM148yCa9CbUXK65wSEAZBtgrLGHtmdmP',
352354
size: 12,
353355
cumulativeSize: 70,
354356
blocks: 1,

test/get.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('.get (specific go-ipfs features)', function () {
6969
content: Buffer.from(path)
7070
}]))
7171

72-
expect(files[2].hash).to.equal(expectedCid)
72+
expect(files[2].cid.toString()).to.equal(expectedCid)
7373
})
7474

7575
it('get path containing "+"s', async () => {

test/interface.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,22 @@ describe('interface-ipfs-core tests', () => {
8484

8585
tests.dht(commonFactory)
8686

87-
tests.files(commonFactory)
87+
tests.files(commonFactory, {
88+
skip: [
89+
{
90+
name: 'should ls directory',
91+
reason: 'TODO unskip when go-ipfs supports --long https://github.com/ipfs/go-ipfs/pull/6528'
92+
},
93+
{
94+
name: 'should read from outside of mfs',
95+
reason: 'TODO not implemented in go-ipfs yet'
96+
},
97+
{
98+
name: 'should ls from outside of mfs',
99+
reason: 'TODO not implemented in go-ipfs yet'
100+
}
101+
]
102+
})
88103

89104
tests.key(commonFactory, {
90105
skip: [

0 commit comments

Comments
 (0)