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

Commit

Permalink
fix: support multiple refs
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed May 10, 2019
1 parent 5b4f1f4 commit 64ac19e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
15 changes: 6 additions & 9 deletions src/files-regular/refs-pull-stream.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
'use strict'

const cleanCID = require('../utils/clean-cid')
const v = require('is-ipfs')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
const deferred = require('pull-defer')
const moduleConfig = require('../utils/module-config')
const { checkArgs, normalizeOpts } = require('./refs')

module.exports = (send) => {
send = moduleConfig(send)

return (hash, opts) => {
opts = opts || {}
return (args, opts) => {
opts = normalizeOpts(opts)

const p = deferred.source()

try {
hash = cleanCID(hash)
args = checkArgs(args)
} catch (err) {
if (!v.ipfsPath(hash)) {
return p.end(err)
}
return p.end(err)
}

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

p.resolve(pull(
Expand Down
15 changes: 6 additions & 9 deletions src/files-regular/refs-readable-stream.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
'use strict'

const cleanCID = require('../utils/clean-cid')
const v = require('is-ipfs')
const Stream = require('readable-stream')
const pump = require('pump')
const through = require('through2')
const { checkArgs, normalizeOpts } = require('./refs')

module.exports = (send) => {
return (hash, opts) => {
opts = opts || {}
return (args, opts) => {
opts = normalizeOpts(opts)

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

try {
hash = cleanCID(hash)
args = checkArgs(args)
} catch (err) {
if (!v.ipfsPath(hash)) {
return pt.destroy(err)
}
return pt.destroy(err)
}

send({ path: 'refs', args: hash, qs: opts }, (err, stream) => {
send({ path: 'refs', args, qs: opts }, (err, stream) => {
if (err) { return pt.destroy(err) }

pump(stream, through.obj(function (r, enc, cb) {
Expand Down
36 changes: 31 additions & 5 deletions src/files-regular/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ module.exports = (arg) => {
callback = opts
opts = {}
}
opts = module.exports.normalizeOpts(opts)

try {
args = cleanCID(args)
args = module.exports.checkArgs(args)
} catch (err) {
if (!IsIpfs.ipfsPath(args)) {
return callback(err)
}
return callback(err)
}

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

const request = {
args,
path: 'refs',
args: args,
qs: opts
}
send(request, (err, result) => {
Expand All @@ -47,3 +46,30 @@ module.exports = (arg) => {

return refs
}

module.exports.checkArgs = (args) => {
const isArray = Array.isArray(args)
args = isArray ? args : [args]

const res = []
for (let arg of args) {
try {
arg = cleanCID(arg)
} catch (err) {
if (!IsIpfs.ipfsPath(arg)) {
throw err
}
}
res.push(arg)
}

return isArray ? res : res[0]
}

module.exports.normalizeOpts = (opts) => {
opts = opts || {}
if (typeof opts.maxDepth === 'number') {
opts['max-depth'] = opts.maxDepth
}
return opts
}

0 comments on commit 64ac19e

Please sign in to comment.