diff --git a/package.json b/package.json index d2e2b1ea8..92783586a 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "chai": "^3.5.0", "gulp": "^3.9.1", "hapi": "^14.1.0", - "interface-ipfs-core": "^0.8.0", + "interface-ipfs-core": "^0.9.0", "ipfsd-ctl": "^0.14.0", "passthrough-counter": "^1.0.0", "pre-commit": "^1.1.3", diff --git a/src/api/pin.js b/src/api/pin.js index d8f170447..cbd96eee0 100644 --- a/src/api/pin.js +++ b/src/api/pin.js @@ -1,49 +1,68 @@ 'use strict' +const promisify = require('promisify-es6') + module.exports = (send) => { return { - add (hash, opts, callback) { + add: promisify((hash, opts, callback) => { if (typeof opts === 'function') { callback = opts opts = null } - return send({ + send({ path: 'pin/add', args: hash, qs: opts - }, callback) - }, - remove (hash, opts, callback) { + }, (err, res) => { + if (err) { + return callback(err) + } + callback(null, res.Pins) + }) + }), + rm: promisify((hash, opts, callback) => { if (typeof opts === 'function') { callback = opts opts = null } - return send({ + send({ path: 'pin/rm', args: hash, qs: opts - }, callback) - }, - list (type, callback) { - if (typeof type === 'function') { - callback = type - type = null + }, (err, res) => { + if (err) { + return callback(err) + } + callback(null, res.Pins) + }) + }), + ls: promisify((hash, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} } - let opts = null - let hash = null - if (typeof type === 'string') { - opts = { type: type } - } else if (type && type.hash) { - hash = type.hash - type.hash = null - opts = type + if (typeof hash === 'object') { + opts = hash + hash = undefined } - return send({ + + if (typeof hash === 'function') { + callback = hash + hash = undefined + opts = {} + } + + send({ path: 'pin/ls', args: hash, qs: opts - }, callback) - } + }, (err, res) => { + if (err) { + return callback(err) + } + callback(null, res.Keys) + }) + }) } } diff --git a/test/interface-ipfs-core/pin.spec.js b/test/interface-ipfs-core/pin.spec.js index ead1db451..3d05068b8 100644 --- a/test/interface-ipfs-core/pin.spec.js +++ b/test/interface-ipfs-core/pin.spec.js @@ -1,117 +1,20 @@ /* eslint-env mocha */ +/* eslint max-nested-callbacks: ["error", 8] */ 'use strict' -const expect = require('chai').expect +const test = require('interface-ipfs-core') const FactoryClient = require('../factory/factory-client') -const fs = require('fs') -const path = require('path') -const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt')) +let fc -describe('.pin', () => { - let ipfs - let fc - - before(function (done) { - this.timeout(20 * 1000) // slow CI +const common = { + setup: function (callback) { fc = new FactoryClient() - fc.spawnNode((err, node) => { - expect(err).to.not.exist - ipfs = node - done() - }) - }) - - after((done) => { - fc.dismantle(done) - }) - - it('add file for testing', (done) => { - const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' - - ipfs.files.add(testfile, (err, res) => { - expect(err).to.not.exist - - expect(res).to.have.length(1) - expect(res[0].hash).to.equal(expectedMultihash) - expect(res[0].path).to.equal(expectedMultihash) - done() - }) - }) - - it('.pin.remove', (done) => { - ipfs.pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: true}, (err, res) => { - expect(err).to.not.exist - expect(res).to.exist - ipfs.pin.list('direct', (err, res) => { - expect(err).to.not.exist - expect(res).to.exist - expect(res.Keys).to.be.empty - done() - }) - }) - }) - - it('.pin.add', (done) => { - ipfs.pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, (err, res) => { - expect(err).to.not.exist - expect(res.Pins[0]).to.be.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') - done() - }) - }) - - it('.pin.list', (done) => { - ipfs.pin.list((err, res) => { - expect(err).to.not.exist - expect(res).to.exist - done() - }) - }) - - it('.pin.list hash', (done) => { - ipfs.pin.list({hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'}, (err, res) => { - expect(err).to.not.exist - expect(res).to.exist - done() - }) - }) - - describe('promise', () => { - it('.pin.add', () => { - return ipfs.pin - .add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}) - .then((res) => { - expect(res.Pins[0]).to.be.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') - }) - }) - - it('.pin.list', () => { - return ipfs.pin.list() - .then((res) => { - expect(res).to.exist - }) - }) - - it('.pin.list hash', () => { - return ipfs.pin.list({ - hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' - }) - .then((res) => { - expect(res).to.exist - }) - }) - - it('.pin.remove', () => { - return ipfs.pin - .remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}) - .then((res) => { - expect(res).to.exist - return ipfs.pin.list('direct') - }) - .then((res) => { - expect(res).to.exist - expect(res.Keys).to.be.empty - }) - }) - }) -}) + callback(null, fc) + }, + teardown: function (callback) { + fc.dismantle(callback) + } +} + +test.pin(common)