From 806dfff6f941d3b6db53a0aa2f70d3f4e78b0cdd Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Wed, 24 Apr 2019 13:47:10 +0800 Subject: [PATCH] feat: refs --- src/files-regular/index.js | 4 +- src/files-regular/refs-pull-stream.js | 23 ++++++ src/files-regular/refs.js | 58 +++++++++++++++ src/refs.js | 40 ----------- src/utils/load-commands.js | 3 +- test/refs.spec.js | 100 -------------------------- test/sub-modules.spec.js | 10 +-- 7 files changed, 89 insertions(+), 149 deletions(-) create mode 100644 src/files-regular/refs-pull-stream.js create mode 100644 src/files-regular/refs.js delete mode 100644 src/refs.js delete mode 100644 test/refs.spec.js diff --git a/src/files-regular/index.js b/src/files-regular/index.js index a52e7505a..764f75c56 100644 --- a/src/files-regular/index.js +++ b/src/files-regular/index.js @@ -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) } } diff --git a/src/files-regular/refs-pull-stream.js b/src/files-regular/refs-pull-stream.js new file mode 100644 index 000000000..089ae6dba --- /dev/null +++ b/src/files-regular/refs-pull-stream.js @@ -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 + } +} diff --git a/src/files-regular/refs.js b/src/files-regular/refs.js new file mode 100644 index 000000000..76e990cc8 --- /dev/null +++ b/src/files-regular/refs.js @@ -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 +} diff --git a/src/refs.js b/src/refs.js deleted file mode 100644 index 97d204124..000000000 --- a/src/refs.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') -const streamToValue = require('./utils/stream-to-value') -const moduleConfig = require('./utils/module-config') - -module.exports = (arg) => { - const send = moduleConfig(arg) - - const refs = promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - - const request = { - path: 'refs', - args: args, - qs: opts - } - - send.andTransform(request, streamToValue, callback) - }) - - refs.local = promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - - const request = { - path: 'refs/local', - qs: opts - } - - send.andTransform(request, streamToValue, callback) - }) - - return refs -} diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 030aef5ab..f383cf4f9 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -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'), @@ -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'), diff --git a/test/refs.spec.js b/test/refs.spec.js deleted file mode 100644 index 9ffcf4ebe..000000000 --- a/test/refs.spec.js +++ /dev/null @@ -1,100 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const chai = require('chai') -const dirtyChai = require('dirty-chai') -const expect = chai.expect -chai.use(dirtyChai) -const isNode = require('detect-node') -const waterfall = require('async/waterfall') -const path = require('path') -const fs = require('fs') - -const ipfsClient = require('../src') -const f = require('./utils/factory') - -describe('.refs', function () { - this.timeout(80 * 1000) - - if (!isNode) { return } - - let ipfs - let ipfsd - let folder - - before((done) => { - const filesPath = path.join(__dirname, '/fixtures/test-folder') - - // Symlinks in a repo don't always clone well, especially on Windows. - // So if the 'hello-link' is not a symlink, then make it one. - const symlinkPath = filesPath + '/hello-link' - const symlinkTarget = 'files/hello.txt' - if (!fs.lstatSync(symlinkPath).isSymbolicLink()) { - fs.unlinkSync(symlinkPath) - fs.symlinkSync(symlinkTarget, symlinkPath) - } - - waterfall([ - (cb) => f.spawn({ initOptions: { bits: 1024, profile: 'test' } }, cb), - (_ipfsd, cb) => { - ipfsd = _ipfsd - ipfs = ipfsClient(_ipfsd.apiAddr) - ipfs.addFromFs(filesPath, { recursive: true }, cb) - }, - (hashes, cb) => { - folder = hashes[hashes.length - 1].hash - expect(folder).to.be.eql('QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh') - cb() - } - ], done) - }) - - after((done) => { - if (!ipfsd) return done() - ipfsd.stop(done) - }) - - const result = [ - { - Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmcUYKmQxmTcFom4R4UZP7FWeQzgJkwcFn51XrvsMy7PE9 add', - Err: '' - }, { - Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmNeHxDfQfjVFyYj2iruvysLH9zpp78v3cu1s3BZq1j5hY cat', - Err: '' - }, { - Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmTYFLz5vsdMpq4XXw1a1pSxujJc9Z5V3Aw1Qg64d849Zy files', - Err: '' - }, { - Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu hello-link', - Err: '' - }, { - Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmU7wetVaAqc3Meurif9hcYBHGvQmL5QdpPJYBoZizyTNL ipfs-add', - Err: '' - }, { - Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmctZfSuegbi2TMFY2y3VQjxsH5JbRBu7XmiLfHNvshhio ls', - Err: '' - }, { - Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmbkMNB6rwfYAxRvnG9CWJ6cKKHEdq2ZKTozyF5FQ7H8Rs version', - Err: '' - } - ] - - describe('Callback API', () => { - it('refs', (done) => { - ipfs.refs(folder, { format: ' ' }, (err, objs) => { - expect(err).to.not.exist() - expect(objs).to.eql(result) - done() - }) - }) - }) - - describe('Promise API', () => { - it('refs', () => { - return ipfs.refs(folder, { format: ' ' }) - .then((objs) => { - expect(objs).to.eql(result) - }) - }) - }) -}) diff --git a/test/sub-modules.spec.js b/test/sub-modules.spec.js index 333407ea4..b0e8cb9eb 100644 --- a/test/sub-modules.spec.js +++ b/test/sub-modules.spec.js @@ -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', () => { @@ -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') - }) })