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

Commit a741e10

Browse files
dirkmcAlan Shaw
authored and
Alan Shaw
committed
feat: refs endpoint (#978)
BREAKING CHANGE: ipfs.refs now returns objects with camelCase properties not PascalCase properties. i.e. `{ ref, err }` not `{ Ref, Err }`
1 parent 775ce80 commit a741e10

12 files changed

+242
-149
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ const ipfs = ipfsClient({
220220
- [`ipfs.block.put(block, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md#blockput)
221221
- [`ipfs.block.stat(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md#blockstat)
222222

223+
- [refs](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/REFS.md)
224+
- [`ipfs.refs(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/REFS.md#refs)
225+
- [`ipfs.refsReadableStream(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/REFS.md#refsreadablestream)
226+
- [`ipfs.refsPullStream(ipfsPath, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/REFS.md#refspullstream)
227+
- [`ipfs.refs.local([callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/REFS.md#refslocal)
228+
- [`ipfs.refs.localReadableStream([callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/REFS.md#refslocalreadablestream)
229+
- [`ipfs.refs.localPullStream([callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/REFS.md#refslocalpullstream)
230+
223231
#### Graph
224232

225233
- [dag](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DAG.md)

src/files-regular/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ module.exports = (arg) => {
2020
getPullStream: require('../files-regular/get-pull-stream')(send),
2121
ls: require('../files-regular/ls')(send),
2222
lsReadableStream: require('../files-regular/ls-readable-stream')(send),
23-
lsPullStream: require('../files-regular/ls-pull-stream')(send)
23+
lsPullStream: require('../files-regular/ls-pull-stream')(send),
24+
refs: require('../files-regular/refs')(send),
25+
refsReadableStream: require('../files-regular/refs-readable-stream')(send),
26+
refsPullStream: require('../files-regular/refs-pull-stream')(send)
2427
}
2528
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const pull = require('pull-stream')
4+
const toPull = require('stream-to-pull-stream')
5+
const deferred = require('pull-defer')
6+
const moduleConfig = require('../utils/module-config')
7+
8+
module.exports = (send) => {
9+
send = moduleConfig(send)
10+
11+
return (opts) => {
12+
opts = opts || {}
13+
14+
const p = deferred.source()
15+
16+
send({ path: 'refs/local', qs: opts }, (err, stream) => {
17+
if (err) { return p.resolve(pull.error(err)) }
18+
19+
p.resolve(pull(
20+
toPull.source(stream),
21+
pull.map(r => ({ ref: r.Ref, err: r.Err }))
22+
))
23+
})
24+
25+
return p
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const Stream = require('readable-stream')
4+
const pump = require('pump')
5+
const through = require('through2')
6+
7+
module.exports = (send) => {
8+
return (opts) => {
9+
opts = opts || {}
10+
11+
const pt = new Stream.PassThrough({ objectMode: true })
12+
13+
send({ path: 'refs/local', qs: opts }, (err, stream) => {
14+
if (err) { return pt.destroy(err) }
15+
16+
pump(stream, through.obj(function (r, enc, cb) {
17+
cb(null, { ref: r.Ref, err: r.Err })
18+
}), pt)
19+
})
20+
21+
return pt
22+
}
23+
}

src/files-regular/refs-local.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')
5+
const moduleConfig = require('../utils/module-config')
6+
7+
module.exports = (arg) => {
8+
const send = moduleConfig(arg)
9+
10+
return promisify((opts, callback) => {
11+
if (typeof (opts) === 'function') {
12+
callback = opts
13+
opts = {}
14+
}
15+
16+
const transform = (res, cb) => {
17+
cb(null, res.map(r => ({ ref: r.Ref, err: r.Err })))
18+
}
19+
20+
const request = {
21+
path: 'refs/local',
22+
qs: opts
23+
}
24+
send(request, (err, result) => {
25+
if (err) {
26+
return callback(err)
27+
}
28+
29+
streamToValueWithTransformer(result, transform, callback)
30+
})
31+
})
32+
}

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

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict'
2+
3+
const pull = require('pull-stream')
4+
const toPull = require('stream-to-pull-stream')
5+
const deferred = require('pull-defer')
6+
const moduleConfig = require('../utils/module-config')
7+
const { checkArgs, normalizeOpts } = require('./refs')
8+
9+
module.exports = (send) => {
10+
send = moduleConfig(send)
11+
12+
return (args, opts) => {
13+
opts = normalizeOpts(opts)
14+
15+
const p = deferred.source()
16+
17+
try {
18+
args = checkArgs(args)
19+
} catch (err) {
20+
return p.end(err)
21+
}
22+
23+
send({ path: 'refs', args, qs: opts }, (err, stream) => {
24+
if (err) { return p.resolve(pull.error(err)) }
25+
26+
p.resolve(pull(
27+
toPull.source(stream),
28+
pull.map(r => ({ ref: r.Ref, err: r.Err }))
29+
))
30+
})
31+
32+
return p
33+
}
34+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
const Stream = require('readable-stream')
4+
const pump = require('pump')
5+
const through = require('through2')
6+
const { checkArgs, normalizeOpts } = require('./refs')
7+
8+
module.exports = (send) => {
9+
return (args, opts) => {
10+
opts = normalizeOpts(opts)
11+
12+
const pt = new Stream.PassThrough({ objectMode: true })
13+
14+
try {
15+
args = checkArgs(args)
16+
} catch (err) {
17+
return pt.destroy(err)
18+
}
19+
20+
send({ path: 'refs', args, qs: opts }, (err, stream) => {
21+
if (err) { return pt.destroy(err) }
22+
23+
pump(stream, through.obj(function (r, enc, cb) {
24+
cb(null, { ref: r.Ref, err: r.Err })
25+
}), pt)
26+
})
27+
28+
return pt
29+
}
30+
}

src/files-regular/refs.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict'
2+
3+
const IsIpfs = require('is-ipfs')
4+
const promisify = require('promisify-es6')
5+
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')
6+
const moduleConfig = require('../utils/module-config')
7+
const cleanCID = require('../utils/clean-cid')
8+
9+
module.exports = (arg) => {
10+
const send = moduleConfig(arg)
11+
12+
const refs = promisify((args, opts, callback) => {
13+
if (typeof (opts) === 'function') {
14+
callback = opts
15+
opts = {}
16+
}
17+
opts = module.exports.normalizeOpts(opts)
18+
19+
try {
20+
args = module.exports.checkArgs(args)
21+
} catch (err) {
22+
return callback(err)
23+
}
24+
25+
const transform = (res, cb) => {
26+
cb(null, res.map(r => ({ ref: r.Ref, err: r.Err })))
27+
}
28+
29+
const request = {
30+
args,
31+
path: 'refs',
32+
qs: opts
33+
}
34+
send(request, (err, result) => {
35+
if (err) {
36+
return callback(err)
37+
}
38+
39+
streamToValueWithTransformer(result, transform, callback)
40+
})
41+
})
42+
43+
refs.local = require('./refs-local')(arg)
44+
refs.localReadableStream = require('./refs-local-readable-stream')(arg)
45+
refs.localPullStream = require('./refs-local-pull-stream')(arg)
46+
47+
return refs
48+
}
49+
50+
module.exports.checkArgs = (args) => {
51+
const isArray = Array.isArray(args)
52+
args = isArray ? args : [args]
53+
54+
const res = []
55+
for (let arg of args) {
56+
try {
57+
arg = cleanCID(arg)
58+
} catch (err) {
59+
if (!IsIpfs.ipfsPath(arg)) {
60+
throw err
61+
}
62+
}
63+
res.push(arg)
64+
}
65+
66+
return isArray ? res : res[0]
67+
}
68+
69+
module.exports.normalizeOpts = (opts) => {
70+
opts = opts || {}
71+
if (typeof opts.maxDepth === 'number') {
72+
opts['max-depth'] = opts.maxDepth
73+
}
74+
return opts
75+
}

src/refs.js

-40
This file was deleted.

src/utils/load-commands.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ function requireCommands () {
1818
ls: require('../files-regular/ls'),
1919
lsReadableStream: require('../files-regular/ls-readable-stream'),
2020
lsPullStream: require('../files-regular/ls-pull-stream'),
21+
refs: require('../files-regular/refs'),
22+
refsReadableStream: require('../files-regular/refs-readable-stream'),
23+
refsPullStream: require('../files-regular/refs-pull-stream'),
2124

2225
// Files MFS (Mutable Filesystem)
2326
files: require('../files-mfs'),
@@ -50,7 +53,6 @@ function requireCommands () {
5053
key: require('../key'),
5154
log: require('../log'),
5255
mount: require('../mount'),
53-
refs: require('../refs'),
5456
repo: require('../repo'),
5557
stop: require('../stop'),
5658
shutdown: require('../stop'),

0 commit comments

Comments
 (0)