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

Commit ea98e4c

Browse files
committed
feat: refs endpoint
1 parent fa21abb commit ea98e4c

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

src/files-regular/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ 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+
refsPullStream: require('./refs-pull-stream')
2123
}
2224

2325
module.exports = createSuite(tests)

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 6] */
3+
'use strict'
4+
5+
const pull = require('pull-stream')
6+
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
const { expectedResults, addTestFolder } = require('./refs')
8+
9+
module.exports = (createCommon, options) => {
10+
const describe = getDescribe(options)
11+
const it = getIt(options)
12+
const common = createCommon()
13+
14+
describe('.refsPullStream', function () {
15+
this.timeout(40 * 1000)
16+
17+
let ipfs
18+
19+
before(function (done) {
20+
// CI takes longer to instantiate the daemon, so we need to increase the
21+
// timeout for the before step
22+
this.timeout(60 * 1000)
23+
24+
common.setup((err, factory) => {
25+
expect(err).to.not.exist()
26+
factory.spawnNode((err, node) => {
27+
expect(err).to.not.exist()
28+
ipfs = node
29+
done()
30+
})
31+
})
32+
})
33+
34+
after((done) => common.teardown(done))
35+
36+
it('should pull stream refs', function (done) {
37+
addTestFolder(ipfs, (err, cid) => {
38+
expect(err).to.not.exist()
39+
40+
const stream = ipfs.refsPullStream(cid, { format: expectedResults.format })
41+
pull(
42+
stream,
43+
pull.collect((err, refs) => {
44+
expect(err).to.not.exist()
45+
46+
expect(refs.map(r => r.Ref)).to.eql(expectedResults.results)
47+
done()
48+
})
49+
)
50+
})
51+
})
52+
})
53+
}

src/files-regular/refs.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 6] */
3+
'use strict'
4+
5+
const { fixtures } = require('./utils')
6+
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
8+
module.exports = (createCommon, options) => {
9+
const describe = getDescribe(options)
10+
const it = getIt(options)
11+
const common = createCommon()
12+
13+
describe('.refs', function () {
14+
this.timeout(40 * 1000)
15+
16+
let ipfs
17+
18+
before(function (done) {
19+
// CI takes longer to instantiate the daemon, so we need to increase the
20+
// timeout for the before step
21+
this.timeout(60 * 1000)
22+
23+
common.setup((err, factory) => {
24+
expect(err).to.not.exist()
25+
factory.spawnNode((err, node) => {
26+
expect(err).to.not.exist()
27+
ipfs = node
28+
done()
29+
})
30+
})
31+
})
32+
33+
after((done) => common.teardown(done))
34+
35+
it('refs', function (done) {
36+
module.exports.addTestFolder(ipfs, (err, cid) => {
37+
expect(err).to.not.exist()
38+
39+
ipfs.refs(cid, { format: module.exports.expectedResults.format }, (err, refs) => {
40+
expect(err).to.not.exist()
41+
42+
expect(refs.map(r => r.Ref)).to.eql(module.exports.expectedResults.results)
43+
done()
44+
})
45+
})
46+
})
47+
})
48+
}
49+
50+
module.exports.expectedResults = {
51+
format: '<src> <dst> <linkname>',
52+
results: [
53+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmZyUEQVuRK3XV7L9Dk26pg6RVSgaYkiSTEdnT2kZZdwoi alice.txt',
54+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn empty-folder',
55+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmZ25UfTqXGz9RsEJFg7HUAuBcmfx5dQZDXQd2QEZ8Kj74 files',
56+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmR4nFjTu18TyANgC65ArNWp5Yaab1gPzQ4D8zp7Kx3vhr holmes.txt',
57+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmT6orWioMiSqXXPGsUi71CKRRUmJ8YkuueV2DPV34E9y9 jungle.txt',
58+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmVwdDCY4SPGVFnNCiZnX5CtzwWDn6kAM98JXzKxE3kCmn pp.txt'
59+
]
60+
}
61+
62+
module.exports.addTestFolder = (ipfs, callback) => {
63+
const content = (name) => ({
64+
path: `test-folder/${name}`,
65+
content: fixtures.directory.files[name]
66+
})
67+
68+
const emptyDir = (name) => ({ path: `test-folder/${name}` })
69+
70+
const dirs = [
71+
content('pp.txt'),
72+
content('holmes.txt'),
73+
content('jungle.txt'),
74+
content('alice.txt'),
75+
emptyDir('empty-folder'),
76+
content('files/hello.txt'),
77+
content('files/ipfs.txt'),
78+
emptyDir('files/empty')
79+
]
80+
81+
ipfs.add(dirs, (err, res) => {
82+
expect(err).to.not.exist()
83+
const root = res[res.length - 1]
84+
85+
expect(root.path).to.equal('test-folder')
86+
expect(root.hash).to.equal(fixtures.directory.cid)
87+
88+
const cid = 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP'
89+
90+
callback(null, cid)
91+
})
92+
}

0 commit comments

Comments
 (0)