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

Commit 16d7af5

Browse files
committed
fix: support multiple refs
1 parent 7b9ec28 commit 16d7af5

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
'use strict'
22

3-
const cleanCID = require('../utils/clean-cid')
4-
const v = require('is-ipfs')
53
const pull = require('pull-stream')
64
const toPull = require('stream-to-pull-stream')
75
const deferred = require('pull-defer')
86
const moduleConfig = require('../utils/module-config')
7+
const { checkArgs, normalizeOpts } = require('./refs')
98

109
module.exports = (send) => {
1110
send = moduleConfig(send)
1211

13-
return (hash, opts) => {
14-
opts = opts || {}
12+
return (args, opts) => {
13+
opts = normalizeOpts(opts)
1514

1615
const p = deferred.source()
1716

1817
try {
19-
hash = cleanCID(hash)
18+
args = checkArgs(args)
2019
} catch (err) {
21-
if (!v.ipfsPath(hash)) {
22-
return p.end(err)
23-
}
20+
return p.end(err)
2421
}
2522

26-
send({ path: 'refs', args: hash, qs: opts }, (err, stream) => {
23+
send({ path: 'refs', args, qs: opts }, (err, stream) => {
2724
if (err) { return p.resolve(pull.error(err)) }
2825

2926
p.resolve(pull(

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
'use strict'
22

3-
const cleanCID = require('../utils/clean-cid')
4-
const v = require('is-ipfs')
53
const Stream = require('readable-stream')
64
const pump = require('pump')
75
const through = require('through2')
6+
const { checkArgs, normalizeOpts } = require('./refs')
87

98
module.exports = (send) => {
10-
return (hash, opts) => {
11-
opts = opts || {}
9+
return (args, opts) => {
10+
opts = normalizeOpts(opts)
1211

1312
const pt = new Stream.PassThrough({ objectMode: true })
1413

1514
try {
16-
hash = cleanCID(hash)
15+
args = checkArgs(args)
1716
} catch (err) {
18-
if (!v.ipfsPath(hash)) {
19-
return pt.destroy(err)
20-
}
17+
return pt.destroy(err)
2118
}
2219

23-
send({ path: 'refs', args: hash, qs: opts }, (err, stream) => {
20+
send({ path: 'refs', args, qs: opts }, (err, stream) => {
2421
if (err) { return pt.destroy(err) }
2522

2623
pump(stream, through.obj(function (r, enc, cb) {

src/files-regular/refs.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,21 @@ module.exports = (arg) => {
1414
callback = opts
1515
opts = {}
1616
}
17+
opts = module.exports.normalizeOpts(opts)
1718

1819
try {
19-
args = cleanCID(args)
20+
args = module.exports.checkArgs(args)
2021
} catch (err) {
21-
if (!IsIpfs.ipfsPath(args)) {
22-
return callback(err)
23-
}
22+
return callback(err)
2423
}
2524

2625
const transform = (res, cb) => {
2726
cb(null, res.map(r => ({ ref: r.Ref, err: r.Err })))
2827
}
2928

3029
const request = {
30+
args,
3131
path: 'refs',
32-
args: args,
3332
qs: opts
3433
}
3534
send(request, (err, result) => {
@@ -47,3 +46,30 @@ module.exports = (arg) => {
4746

4847
return refs
4948
}
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+
}

0 commit comments

Comments
 (0)