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

Commit

Permalink
feat: refs
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Apr 24, 2019
1 parent 17d49de commit 806dfff
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 149 deletions.
4 changes: 3 additions & 1 deletion src/files-regular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module.exports = (arg) => {
getPullStream: require('../files-regular/get-pull-stream')(send),
ls: require('../files-regular/ls')(send),
lsReadableStream: require('../files-regular/ls-readable-stream')(send),
lsPullStream: require('../files-regular/ls-pull-stream')(send)
lsPullStream: require('../files-regular/ls-pull-stream')(send),
refs: require('../files-regular/refs')(send),
refsPullStream: require('../files-regular/refs-pull-stream')(send)
}
}
23 changes: 23 additions & 0 deletions src/files-regular/refs-pull-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const pullError = require('pull-stream/sources/error')
const pullValues = require('pull-stream/sources/values')
const pullDefer = require('pull-defer')
const refs = require('./refs')

module.exports = (arg) => {
const refsFn = refs(arg)

return (args, opts) => {
const p = pullDefer.source()

refsFn(args, opts, (err, res) => {
if (err) {
return p.resolve(pullError(err))
}
p.resolve(pullValues(res))
})

return p
}
}
58 changes: 58 additions & 0 deletions src/files-regular/refs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict'

const IsIpfs = require('is-ipfs')
const promisify = require('promisify-es6')
const streamToValue = require('../utils/stream-to-value')
const moduleConfig = require('../utils/module-config')
const cleanCID = require('../utils/clean-cid')

function valueOrStreamToValue (response, callback) {
if (typeof response.pipe === 'function') {
streamToValue(response, callback)
} else {
callback(null, response)
}
}

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

const refs = promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}

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

const request = {
path: 'refs',
args: args,
qs: opts
}

send.andTransform(request, valueOrStreamToValue, callback)
})

refs.local = promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}

const request = {
path: 'refs/local',
qs: opts
}

send.andTransform(request, valueOrStreamToValue, callback)
})

return refs
}
40 changes: 0 additions & 40 deletions src/refs.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function requireCommands () {
ls: require('../files-regular/ls'),
lsReadableStream: require('../files-regular/ls-readable-stream'),
lsPullStream: require('../files-regular/ls-pull-stream'),
refs: require('../files-regular/refs'),
refsPullStream: require('../files-regular/refs-pull-stream'),

// Files MFS (Mutable Filesystem)
files: require('../files-mfs'),
Expand Down Expand Up @@ -50,7 +52,6 @@ function requireCommands () {
key: require('../key'),
log: require('../log'),
mount: require('../mount'),
refs: require('../refs'),
repo: require('../repo'),
stop: require('../stop'),
shutdown: require('../stop'),
Expand Down
100 changes: 0 additions & 100 deletions test/refs.spec.js

This file was deleted.

10 changes: 3 additions & 7 deletions test/sub-modules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ describe('submodules', () => {
expect(filesRegular.ls).to.be.a('function')
expect(filesRegular.lsReadableStream).to.be.a('function')
expect(filesRegular.lsPullStream).to.be.a('function')
expect(filesRegular.refs).to.be.a('function')
expect(filesRegular.refs.local).to.be.a('function')
expect(filesRegular.refsPullStream).to.be.a('function')
})

it('files MFS API', () => {
Expand All @@ -209,11 +212,4 @@ describe('submodules', () => {

expect(mount).to.be.a('function')
})

it('refs', () => {
const refs = require('../src/refs')(config)

expect(refs).to.be.a('function')
expect(refs.local).to.be.a('function')
})
})

0 comments on commit 806dfff

Please sign in to comment.