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

Commit 326f4ca

Browse files
dirkmcAlan Shaw
authored and
Alan Shaw
committed
test: add tests for refs endpoint (#460)
* feat: refs endpoint * test: add tests for refs local * test: add tests for refs local pull / streamable * test: use expect.includes() * fix: async.map -> map * fix: ref -> Ref * chore: add tests for multiple refs, CBOR * chore: add REFS spec * test: add test for Object.links with CBOR
1 parent 9007b31 commit 326f4ca

File tree

11 files changed

+752
-1
lines changed

11 files changed

+752
-1
lines changed

SPEC/REFS.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# Refs API
2+
3+
* [refs](#refs)
4+
* [refsReadableStream](#refsreadablestream)
5+
* [refsPullStream](#refspullstream)
6+
* [refs.local](#refslocal)
7+
* [refs.localReadableStream](#refslocalreadablestream)
8+
* [refs.localPullStream](#refslocalpullstream)
9+
10+
#### `refs`
11+
12+
> Get links (references) from an object.
13+
14+
##### `ipfs.refs(ipfsPath, [options], [callback])`
15+
16+
`ipfsPath` can be of type:
17+
18+
- [`cid`][cid] of type:
19+
- a [CID](https://github.com/ipfs/js-cid) instance
20+
- [Buffer][b], the raw Buffer of the cid
21+
- String, the base58 encoded version of the cid
22+
- String, including the ipfs handler, a cid and a path to traverse to, ie:
23+
- '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66'
24+
- '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
25+
- 'QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
26+
27+
`options` is an optional object that may contain the following keys:
28+
- `recursive (false)`: recursively list references of child nodes
29+
- `unique (false)`: omit duplicate references from output
30+
- `format ("<dst>")`: output edges with given format. Available tokens: `<src>`, `<dst>`, `<linkname>`
31+
- `edges (false)`: output references in edge format: `"<src> -> <dst>"`
32+
- `maxDepth (1)`: only for recursive refs, limits fetch and listing to the given depth
33+
34+
`callback` must follow `function (err, refs) {}` signature, where `err` is an error if the operation was not successful and `refs` is an array of `{ ref: "myref", err: "error msg" }`
35+
36+
If no `callback` is passed, a promise is returned.
37+
38+
**Example:**
39+
40+
```JavaScript
41+
ipfs.refs(ipfsPath, { recursive: true }, function (err, refs) {
42+
if (err) {
43+
throw err
44+
}
45+
46+
for (const ref of refs) {
47+
if (ref.err) {
48+
console.error(ref.err)
49+
} else {
50+
console.log(ref.ref)
51+
// output: "QmHash"
52+
}
53+
}
54+
})
55+
```
56+
57+
#### `refsReadableStream`
58+
59+
> Output references using a [Readable Stream][rs]
60+
61+
##### `ipfs.refsReadableStream(ipfsPath, [options])` -> [Readable Stream][rs]
62+
63+
`options` is an optional object argument identical to the options for [ipfs.refs](#refs)
64+
65+
**Example:**
66+
67+
```JavaScript
68+
const stream = ipfs.refsReadableStream(ipfsPath, { recursive: true })
69+
stream.on('data', function (ref) {
70+
// 'ref' will be of the form
71+
// {
72+
// ref: 'QmHash',
73+
// err: 'err message'
74+
// }
75+
})
76+
```
77+
78+
#### `refsPullStream`
79+
80+
> Output references using a [Pull Stream][ps].
81+
82+
##### `ipfs.refsReadableStream(ipfsPath, [options])` -> [Pull Stream][ps]
83+
84+
`options` is an optional object argument identical to the options for [ipfs.refs](#refs)
85+
86+
**Example:**
87+
88+
```JavaScript
89+
const stream = ipfs.refsPullStream(ipfsPath, { recursive: true })
90+
91+
pull(
92+
stream,
93+
pull.collect((err, values) => {
94+
// values will be an array of objects, each one of the form
95+
// {
96+
// ref: 'QmHash',
97+
// err: 'err message'
98+
// }
99+
})
100+
)
101+
```
102+
103+
#### `refs.local`
104+
105+
> Output all local references (CIDs of all blocks in the blockstore)
106+
107+
##### `ipfs.refs.local([callback])`
108+
109+
`callback` must follow `function (err, refs) {}` signature, where `err` is an error if the operation was not successful and `refs` is an array of `{ ref: "myref", err: "error msg" }`
110+
111+
If no `callback` is passed, a promise is returned.
112+
113+
**Example:**
114+
115+
```JavaScript
116+
ipfs.refs.local(function (err, refs) {
117+
if (err) {
118+
throw err
119+
}
120+
121+
for (const ref of refs) {
122+
if (ref.err) {
123+
console.error(ref.err)
124+
} else {
125+
console.log(ref.ref)
126+
// output: "QmHash"
127+
}
128+
}
129+
})
130+
```
131+
132+
#### `refs.localReadableStream`
133+
134+
> Output all local references using a [Readable Stream][rs]
135+
136+
##### `ipfs.localReadableStream()` -> [Readable Stream][rs]
137+
138+
**Example:**
139+
140+
```JavaScript
141+
const stream = ipfs.refs.localReadableStream()
142+
stream.on('data', function (ref) {
143+
// 'ref' will be of the form
144+
// {
145+
// ref: 'QmHash',
146+
// err: 'err message'
147+
// }
148+
})
149+
```
150+
151+
#### `refs.localPullStream`
152+
153+
> Output all local references using a [Pull Stream][ps].
154+
155+
##### `ipfs.refs.localReadableStream()` -> [Pull Stream][ps]
156+
157+
**Example:**
158+
159+
```JavaScript
160+
const stream = ipfs.refs.localPullStream()
161+
162+
pull(
163+
stream,
164+
pull.collect((err, values) => {
165+
// values will be an array of objects, each one of the form
166+
// {
167+
// ref: 'QmHash',
168+
// err: 'err message'
169+
// }
170+
})
171+
)
172+
```
173+
174+
A great source of [examples][] can be found in the tests for this API.
175+
176+
[examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/files-regular
177+
[b]: https://www.npmjs.com/package/buffer
178+
[rs]: https://www.npmjs.com/package/readable-stream
179+
[ps]: https://www.npmjs.com/package/pull-stream
180+
[cid]: https://www.npmjs.com/package/cids
181+
[blob]: https://developer.mozilla.org/en-US/docs/Web/API/Blob

src/files-regular/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ const tests = {
1717
getPullStream: require('./get-pull-stream'),
1818
ls: require('./ls'),
1919
lsReadableStream: require('./ls-readable-stream'),
20-
lsPullStream: require('./ls-pull-stream')
20+
lsPullStream: require('./ls-pull-stream'),
21+
refs: require('./refs'),
22+
refsReadableStream: require('./refs-readable-stream'),
23+
refsPullStream: require('./refs-pull-stream'),
24+
refsLocal: require('./refs-local'),
25+
refsLocalPullStream: require('./refs-local-pull-stream'),
26+
refsLocalReadableStream: require('./refs-local-readable-stream')
2127
}
2228

2329
module.exports = createSuite(tests)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const pull = require('pull-stream')
5+
6+
module.exports = (createCommon, options) => {
7+
const ipfsRefsLocal = (ipfs) => {
8+
return (cb) => {
9+
const stream = ipfs.refs.localPullStream()
10+
pull(stream, pull.collect(cb))
11+
}
12+
}
13+
require('./refs-local-tests')(createCommon, '.refs.localPullStream', ipfsRefsLocal, options)
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const concat = require('concat-stream')
5+
6+
module.exports = (createCommon, options) => {
7+
const ipfsRefsLocal = (ipfs) => {
8+
return (cb) => {
9+
const stream = ipfs.refs.localReadableStream()
10+
stream.on('error', cb)
11+
stream.pipe(concat((refs) => cb(null, refs)))
12+
}
13+
}
14+
require('./refs-local-tests')(createCommon, '.refs.localReadableStream', ipfsRefsLocal, options)
15+
}

src/files-regular/refs-local-tests.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { fixtures } = require('./utils')
5+
const { getDescribe, getIt, expect } = require('../utils/mocha')
6+
7+
module.exports = (createCommon, suiteName, ipfsRefsLocal, options) => {
8+
const describe = getDescribe(options)
9+
const it = getIt(options)
10+
const common = createCommon()
11+
12+
describe(suiteName, function () {
13+
this.timeout(40 * 1000)
14+
15+
let ipfs
16+
17+
before(function (done) {
18+
// CI takes longer to instantiate the daemon, so we need to increase the
19+
// timeout for the before step
20+
this.timeout(60 * 1000)
21+
22+
common.setup((err, factory) => {
23+
expect(err).to.not.exist()
24+
factory.spawnNode((err, node) => {
25+
expect(err).to.not.exist()
26+
ipfs = node
27+
done()
28+
})
29+
})
30+
})
31+
32+
after((done) => common.teardown(done))
33+
34+
it('should get local refs', function (done) {
35+
const content = (name) => ({
36+
path: `test-folder/${name}`,
37+
content: fixtures.directory.files[name]
38+
})
39+
40+
const dirs = [
41+
content('pp.txt'),
42+
content('holmes.txt')
43+
]
44+
45+
ipfs.add(dirs, (err, res) => {
46+
expect(err).to.not.exist()
47+
48+
ipfsRefsLocal(ipfs)((err, refs) => {
49+
expect(err).to.not.exist()
50+
51+
const cids = refs.map(r => r.ref)
52+
expect(cids).to.include('QmVwdDCY4SPGVFnNCiZnX5CtzwWDn6kAM98JXzKxE3kCmn')
53+
expect(cids).to.include('QmR4nFjTu18TyANgC65ArNWp5Yaab1gPzQ4D8zp7Kx3vhr')
54+
55+
done()
56+
})
57+
})
58+
})
59+
})
60+
}

src/files-regular/refs-local.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
module.exports = (createCommon, options) => {
5+
const ipfsRefsLocal = (ipfs) => (cb) => ipfs.refs.local(cb)
6+
require('./refs-local-tests')(createCommon, '.refs.local', ipfsRefsLocal, options)
7+
}

src/files-regular/refs-pull-stream.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const pull = require('pull-stream')
5+
6+
module.exports = (createCommon, options) => {
7+
const ipfsRefs = (ipfs) => {
8+
return (path, params, cb) => {
9+
const stream = ipfs.refsPullStream(path, params)
10+
pull(stream, pull.collect(cb))
11+
}
12+
}
13+
require('./refs-tests')(createCommon, '.refsPullStream', ipfsRefs, options)
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const concat = require('concat-stream')
5+
6+
module.exports = (createCommon, options) => {
7+
const ipfsRefs = (ipfs) => {
8+
return (path, params, cb) => {
9+
const stream = ipfs.refsReadableStream(path, params)
10+
stream.on('error', cb)
11+
stream.pipe(concat((refs) => cb(null, refs)))
12+
}
13+
}
14+
require('./refs-tests')(createCommon, '.refsReadableStream', ipfsRefs, options)
15+
}

0 commit comments

Comments
 (0)