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

refs endpoint #978

Merged
merged 8 commits into from
May 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: support multiple refs
  • Loading branch information
dirkmc committed May 10, 2019
commit c49143baff0e7499671303189c113b77fcb41f5c
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
}