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

Commit cfb6a45

Browse files
author
Alan Shaw
committed
refactor: return CIDs from core
1 parent 1a568c9 commit cfb6a45

File tree

18 files changed

+135
-158
lines changed

18 files changed

+135
-158
lines changed

SPEC/BITSWAP.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,18 @@
1313

1414
| Type | Description |
1515
| -------- | -------- |
16-
| `Promise<Object>` | An object representing the wantlist |
17-
18-
the returned object contains the following keys:
19-
20-
- `Keys` An array of objects containing the following keys:
21-
- `/` A string multihash
16+
| `Promise<CID[]>` | An array of [CID][cid]s currently in the wantlist |
2217

2318
**Example:**
2419

2520
```JavaScript
2621
const list = await ipfs.bitswap.wantlist()
2722
console.log(list)
28-
// { Keys: [{ '/': 'QmHash' }] }
23+
// [ CID('QmHash') ]
2924

3025
const list2 = await ipfs.bitswap.wantlist(peerId)
3126
console.log(list2)
32-
// { Keys: [{ '/': 'QmHash' }] }
27+
// [ CID('QmHash') ]
3328
```
3429

3530
A great source of [examples][] can be found in the tests for this API.
@@ -48,11 +43,11 @@ Note: `bitswap.stat` and `stats.bitswap` can be used interchangeably.
4843
| -------- | -------- |
4944
| `Promise<Object>` | An object that contains information about the bitswap agent |
5045

51-
the returned object contains the following keys:
46+
The returned object contains the following keys:
5247

5348
- `provideBufLen` is an integer.
54-
- `wantlist` (array of CIDs)
55-
- `peers` (array of peer IDs)
49+
- `wantlist` (array of [CID][cid]s)
50+
- `peers` (array of peer IDs as [CID][cid] instances)
5651
- `blocksReceived` is a [BigNumber Int][1]
5752
- `dataReceived` is a [BigNumber Int][1]
5853
- `blocksSent` is a [BigNumber Int][1]
@@ -67,21 +62,22 @@ const stats = await ipfs.bitswap.stat()
6762
console.log(stats)
6863
// {
6964
// provideBufLen: 0,
70-
// wantlist: [ { '/': 'QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM' } ],
65+
// wantlist: [ CID('QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM') ],
7166
// peers:
72-
// [ 'QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
73-
// 'QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
74-
// 'QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd' ],
67+
// [ CID('QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM'),
68+
// CID('QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu'),
69+
// CID('QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd') ],
7570
// blocksReceived: 0,
7671
// dataReceived: 0,
7772
// blocksSent: 0,
7873
// dataSent: 0,
7974
// dupBlksReceived: 0,
80-
// dupDataReceived: 0
75+
// dupDataReceived: 0
8176
// }
8277
```
8378

8479
A great source of [examples][] can be found in the tests for this API.
8580

8681
[1]: https://github.com/MikeMcl/bignumber.js/
8782
[examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/bitswap
83+
[cid]: https://www.npmjs.com/package/cids

SPEC/PIN.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ Where:
1414

1515
- `hash` is an IPFS multihash.
1616
- `options` is an object that can contain the following keys
17-
- 'recursive' - Recursively pin the object linked. Type: bool. Default: `true`
17+
- `recursive` - Recursively pin the object linked. Type: bool. Default: `true`
1818

1919
**Returns**
2020

2121
| Type | Description |
2222
| -------- | -------- |
23-
| `Promise<Array>` | An array of objects that represent the files that were pinned |
23+
| `Promise<{ cid: CID }>` | An array of objects that represent the files that were pinned |
2424

2525
an array of objects is returned, each of the form:
2626

2727
```JavaScript
2828
{
29-
hash: 'QmHash'
29+
cid: CID('QmHash')
3030
}
3131
```
3232

@@ -36,7 +36,7 @@ an array of objects is returned, each of the form:
3636
const pinset = await ipfs.pin.add('QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u')
3737
console.log(pinset)
3838
// Logs:
39-
// [ { hash: 'QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u' } ]
39+
// [ { cid: CID('QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u') } ]
4040
```
4141

4242
A great source of [examples][] can be found in the tests for this API.
@@ -57,17 +57,17 @@ Where:
5757

5858
| Type | Description |
5959
| -------- | -------- |
60-
| `AsyncIterable<{ hash: string, type: string }>` | An async iterable that yields currently pinned objects with `hash` and `type` properties. `hash` is a string CID of the pinned node, `type` is the pin type ("recursive", "direct" or "indirect") |
60+
| `AsyncIterable<{ cid: CID, type: string }>` | An async iterable that yields currently pinned objects with `cid` and `type` properties. `cid` is a [CID][cid] of the pinned node, `type` is the pin type ("recursive", "direct" or "indirect") |
6161

6262
**Example:**
6363

6464
```JavaScript
65-
for await (const { hash, type } of ipfs.pin.ls()) {
66-
console.log(pinset)
65+
for await (const { cid, type } of ipfs.pin.ls()) {
66+
console.log({ cid, type })
6767
}
68-
// { hash: Qmc5XkteJdb337s7VwFBAGtiaoj2QCEzyxtNRy3iMudc3E, type: 'recursive' }
69-
// { hash: QmZbj5ruYneZb8FuR9wnLqJCpCXMQudhSdWhdhp5U1oPWJ, type: 'indirect' }
70-
// { hash: QmSo73bmN47gBxMNqbdV6rZ4KJiqaArqJ1nu5TvFhqqj1R, type: 'indirect' }
68+
// { cid: CID(Qmc5XkteJdb337s7VwFBAGtiaoj2QCEzyxtNRy3iMudc3E), type: 'recursive' }
69+
// { cid: CID(QmZbj5ruYneZb8FuR9wnLqJCpCXMQudhSdWhdhp5U1oPWJ), type: 'indirect' }
70+
// { cid: CID(QmSo73bmN47gBxMNqbdV6rZ4KJiqaArqJ1nu5TvFhqqj1R), type: 'indirect' }
7171
```
7272

7373
A great source of [examples][] can be found in the tests for this API.
@@ -87,14 +87,15 @@ Where:
8787

8888
| Type | Description |
8989
| -------- | -------- |
90-
| `Promise<Array>` | An array of unpinned objects |
90+
| `Promise<{ cid: CID }>` | An array of unpinned objects |
9191

9292
**Example:**
9393

9494
```JavaScript
95-
const pinset = await ipfs.pin.rm(hash)
95+
const pinset = await ipfs.pin.rm('QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u')
9696
console.log(pinset)
9797
// prints the hashes that were unpinned
98+
// [ { cid: CID('QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u') } ]
9899
```
99100

100101
A great source of [examples][] can be found in the tests for this API.

src/bitswap/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async function waitForWantlistKey (ipfs, key, opts = {}) {
1111
while (Date.now() < end) {
1212
const list = await ipfs.bitswap.wantlist(opts.peerId)
1313

14-
if (list && list.Keys && list.Keys.some(k => k['/'] === key)) {
14+
if (list.some(cid => cid.toString() === key)) {
1515
return
1616
}
1717

src/block/stat.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ module.exports = (common, options) => {
2727

2828
it('should stat by CID', async () => {
2929
const cid = new CID(hash)
30-
3130
const stats = await ipfs.block.stat(cid)
32-
33-
expect(stats).to.have.property('key')
31+
expect(stats.cid.toString()).to.equal(cid.toString())
3432
expect(stats).to.have.property('size')
3533
})
3634

src/cat.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = (common, options) => {
5454

5555
const res = await all(ipfs.add(input, { cidVersion: 0 }))
5656

57-
const cidv0 = new CID(res[0].hash)
57+
const cidv0 = res[0].cid
5858
expect(cidv0.version).to.equal(0)
5959

6060
const cidv1 = cidv0.toV1()
@@ -68,7 +68,7 @@ module.exports = (common, options) => {
6868

6969
const res = await all(ipfs.add(input, { cidVersion: 1, rawLeaves: false }))
7070

71-
const cidv1 = new CID(res[0].hash)
71+
const cidv1 = res[0].cid
7272
expect(cidv1.version).to.equal(1)
7373

7474
const cidv0 = cidv1.toV0()
@@ -98,7 +98,7 @@ module.exports = (common, options) => {
9898
const file = await filesAdded.find((f) => f.path === 'a')
9999
expect(file).to.exist()
100100

101-
const data = await concat(ipfs.cat(`/ipfs/${file.hash}/testfile.txt`))
101+
const data = await concat(ipfs.cat(`/ipfs/${file.cid}/testfile.txt`))
102102

103103
expect(data.toString()).to.contain('Plz add me!')
104104
})
@@ -111,7 +111,7 @@ module.exports = (common, options) => {
111111
const file = filesAdded.find((f) => f.path === 'a')
112112
expect(file).to.exist()
113113

114-
const data = await concat(ipfs.cat(`/ipfs/${file.hash}/b/testfile.txt`))
114+
const data = await concat(ipfs.cat(`/ipfs/${file.cid}/b/testfile.txt`))
115115
expect(data.toString()).to.contain('Plz add me!')
116116
})
117117

@@ -142,7 +142,7 @@ module.exports = (common, options) => {
142142

143143
const dir = files[0]
144144

145-
const err = await expect(concat(ipfs.cat(dir.hash))).to.be.rejected()
145+
const err = await expect(concat(ipfs.cat(dir.cid))).to.be.rejected()
146146
expect(err.message).to.contain('this dag node is a directory')
147147
})
148148

src/dag/get.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const dagPB = require('ipld-dag-pb')
55
const DAGNode = dagPB.DAGNode
66
const dagCBOR = require('ipld-dag-cbor')
77
const Unixfs = require('ipfs-unixfs')
8-
const CID = require('cids')
98
const all = require('it-all')
109
const { getDescribe, getIt, expect } = require('../utils/mocha')
1110

@@ -157,7 +156,7 @@ module.exports = (common, options) => {
157156

158157
const res = await all(ipfs.add(input, { cidVersion: 1, rawLeaves: false }))
159158

160-
const cidv1 = new CID(res[0].hash)
159+
const cidv1 = res[0].cid
161160
expect(cidv1.version).to.equal(1)
162161

163162
const cidv0 = cidv1.toV0()

src/dht/provide.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = (common, options) => {
3030
it('should provide local CID', async () => {
3131
const res = await all(ipfs.add(Buffer.from('test')))
3232

33-
await all(ipfs.dht.provide(new CID(res[0].hash)))
33+
await all(ipfs.dht.provide(res[0].cid))
3434
})
3535

3636
it('should not provide if block not found locally', () => {
@@ -48,18 +48,12 @@ module.exports = (common, options) => {
4848
{ content: Buffer.from('t1') }
4949
]))
5050

51-
await all(ipfs.dht.provide([
52-
new CID(res[0].hash),
53-
new CID(res[1].hash)
54-
]))
51+
await all(ipfs.dht.provide(res.map(f => f.cid)))
5552
})
5653

5754
it('should provide a CIDv1', async () => {
5855
const res = await all(ipfs.add(Buffer.from('test'), { cidVersion: 1 }))
59-
60-
const cid = new CID(res[0].hash)
61-
62-
await all(ipfs.dht.provide(cid))
56+
await all(ipfs.dht.provide(res[0].cid))
6357
})
6458

6559
it('should error on non CID arg', () => {

src/files/cp.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ module.exports = (common, options) => {
5353
})
5454

5555
it('should copy from outside of mfs', async () => {
56-
const [{
57-
hash
58-
}] = await all(ipfs.add(fixtures.smallFile.data))
56+
const [{ cid }] = await all(ipfs.add(fixtures.smallFile.data))
5957
const testFilePath = `/${hat()}`
60-
await ipfs.files.cp(`/ipfs/${hash}`, testFilePath)
58+
await ipfs.files.cp(`/ipfs/${cid}`, testFilePath)
6159
const testFileData = await concat(ipfs.files.read(testFilePath))
6260
expect(testFileData.slice()).to.eql(fixtures.smallFile.data)
6361
})

src/files/read.js

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

44
const hat = require('hat')
55
const concat = require('it-concat')
6+
const all = require('it-all')
67
const { fixtures } = require('../utils')
78
const { getDescribe, getIt, expect } = require('../utils/mocha')
89

@@ -45,10 +46,8 @@ module.exports = (common, options) => {
4546
})
4647

4748
it('should read from outside of mfs', async () => {
48-
const [{
49-
hash
50-
}] = await ipfs.add(fixtures.smallFile.data)
51-
const testFileData = await concat(ipfs.files.read(`/ipfs/${hash}`))
49+
const [{ cid }] = await all(ipfs.add(fixtures.smallFile.data))
50+
const testFileData = await concat(ipfs.files.read(`/ipfs/${cid}`))
5251
expect(testFileData.slice()).to.eql(fixtures.smallFile.data)
5352
})
5453
})

src/get.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = (common, options) => {
5050

5151
const res = await all(ipfs.add(input, { cidVersion: 0 }))
5252

53-
const cidv0 = new CID(res[0].hash)
53+
const cidv0 = res[0].cid
5454
expect(cidv0.version).to.equal(0)
5555

5656
const cidv1 = cidv0.toV1()
@@ -64,7 +64,7 @@ module.exports = (common, options) => {
6464

6565
const res = await all(ipfs.add(input, { cidVersion: 1, rawLeaves: false }))
6666

67-
const cidv1 = new CID(res[0].hash)
67+
const cidv1 = res[0].cid
6868
expect(cidv1.version).to.equal(1)
6969

7070
const cidv0 = cidv1.toV0()
@@ -105,7 +105,7 @@ module.exports = (common, options) => {
105105
const root = res[res.length - 1]
106106

107107
expect(root.path).to.equal('test-folder')
108-
expect(root.hash).to.equal(fixtures.directory.cid)
108+
expect(root.cid.toString()).to.equal(fixtures.directory.cid)
109109

110110
let files = await all((async function * () {
111111
for await (let { path, content } of ipfs.get(fixtures.directory.cid)) {
@@ -156,7 +156,7 @@ module.exports = (common, options) => {
156156

157157
filesAdded.forEach(async (file) => {
158158
if (file.path === 'a') {
159-
const files = await all(ipfs.get(`/ipfs/${file.hash}/testfile.txt`))
159+
const files = await all(ipfs.get(`/ipfs/${file.cid}/testfile.txt`))
160160
expect(files).to.be.length(1)
161161
expect((await concat(files[0].content)).toString()).to.contain('Plz add me!')
162162
}
@@ -173,7 +173,7 @@ module.exports = (common, options) => {
173173

174174
filesAdded.forEach(async (file) => {
175175
if (file.path === 'a') {
176-
const files = await all(ipfs.get(`/ipfs/${file.hash}/testfile.txt`))
176+
const files = await all(ipfs.get(`/ipfs/${file.cid}/testfile.txt`))
177177
expect(files).to.be.length(1)
178178
expect((await concat(files[0].content)).toString()).to.contain('Plz add me!')
179179
}

src/miscellaneous/resolve.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ module.exports = (common, options) => {
3030
it('should resolve an IPFS hash', async () => {
3131
const content = loadFixture('test/fixtures/testfile.txt', 'interface-ipfs-core')
3232

33-
const [{ hash }] = await all(ipfs.add(content))
34-
const path = await ipfs.resolve(`/ipfs/${hash}`)
35-
expect(path).to.equal(`/ipfs/${hash}`)
33+
const [{ cid }] = await all(ipfs.add(content))
34+
const path = await ipfs.resolve(`/ipfs/${cid}`)
35+
expect(path).to.equal(`/ipfs/${cid}`)
3636
})
3737

3838
it('should resolve an IPFS hash and return a base64url encoded CID in path', async () => {
39-
const [{ hash }] = await all(ipfs.add(Buffer.from('base64url encoded')))
40-
const path = await ipfs.resolve(`/ipfs/${hash}`, { cidBase: 'base64url' })
41-
const [,, cid] = path.split('/')
39+
const [{ cid }] = await all(ipfs.add(Buffer.from('base64url encoded')))
40+
const path = await ipfs.resolve(`/ipfs/${cid}`, { cidBase: 'base64url' })
41+
const [,, cidStr] = path.split('/')
4242

43-
expect(multibase.isEncoded(cid)).to.equal('base64url')
43+
expect(multibase.isEncoded(cidStr)).to.equal('base64url')
4444
})
4545

4646
// Test resolve turns /ipfs/QmRootHash/path/to/file into /ipfs/QmFileHash
4747
it('should resolve an IPFS path link', async () => {
4848
const path = 'path/to/testfile.txt'
4949
const content = loadFixture('test/fixtures/testfile.txt', 'interface-ipfs-core')
50-
const [{ hash: fileHash }, , , { hash: rootHash }] = await all(ipfs.add([{ path, content }], { wrapWithDirectory: true }))
51-
const resolve = await ipfs.resolve(`/ipfs/${rootHash}/${path}`)
50+
const [{ cid: fileCid }, , , { cid: rootCid }] = await all(ipfs.add([{ path, content }], { wrapWithDirectory: true }))
51+
const resolve = await ipfs.resolve(`/ipfs/${rootCid}/${path}`)
5252

53-
expect(resolve).to.equal(`/ipfs/${fileHash}`)
53+
expect(resolve).to.equal(`/ipfs/${fileCid}`)
5454
})
5555

5656
it('should resolve up to the last node', async () => {

src/object/get.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ module.exports = (common, options) => {
138138
content: data
139139
}))
140140

141-
const node = await ipfs.object.get(result[0].hash)
141+
const node = await ipfs.object.get(result[0].cid)
142142
const meta = UnixFs.unmarshal(node.Data)
143143

144144
expect(meta.fileSize()).to.equal(data.length)

0 commit comments

Comments
 (0)