diff --git a/README.md b/README.md index 32d018ac7..4cb6a9dcd 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,7 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://exam ### API -> `WIP` - -`js-ipfs-api` follows the spec defined by [`interface-ipfs-core`](https://github.com/ipfs/interface-ipfs-core), which concerns the interface to expect from IPFS implementations. This interface is a currently active endeavor - expect it to be complete in the next few weeks (August 2016). You can use it today to consult the methods available. +> `js-ipfs-api` follows the spec defined by [`interface-ipfs-core`](https://github.com/ipfs/interface-ipfs-core), which concerns the interface to expect from IPFS implementations. This interface is a currently active endeavor - expect it to be complete in the next few weeks (August 2016). You can use it today to consult the methods available. ### Utility functions @@ -140,6 +138,7 @@ This is very similar to `ipfs.files.add({path:'', content: stream})`. It is like ```JavaScript ``` +>>>>>> refactor(module+tests): Enable running the tests ### Callbacks and promises @@ -166,8 +165,6 @@ We run tests by executing `npm test` in a terminal window. This will run both No ->>>>>>> kick off ipfs-api next generation - ## Contribute The js-ipfs-api is a work in progress. As such, there's a few things you can do right now to help out: diff --git a/package.json b/package.json index 2236b624f..d2e2b1ea8 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "ipfs-api", "version": "6.0.3", - "description": "A client library for the IPFS API", - "main": "src/index.js", + "description": "A client library for the IPFS HTTP API. Follows interface-ipfs-core spec", + "main": "lib/index.js", "jsnext:main": "src/index.js", "scripts": { "test": "gulp test", diff --git a/src/api/add-files.js b/src/api/add-files.js index 26f71097d..4fb0f5b7e 100644 --- a/src/api/add-files.js +++ b/src/api/add-files.js @@ -4,22 +4,27 @@ const isNode = require('detect-node') const addToDagNodesTransform = require('../add-to-dagnode-transform') module.exports = (send) => { - return function add (path, opts, cb) { - if (typeof (opts) === 'function' && cb === undefined) { - cb = opts + return function add (path, opts, callback) { + if (typeof (opts) === 'function' && + callback === undefined) { + callback = opts opts = {} } if (!isNode) { - return cb(new Error('Recursive uploads are not supported in the browser')) + return callback(new Error('Recursive uploads are not supported in the browser')) } - if (typeof (path) !== 'string') { - return cb(new Error('"path" must be a string')) + if (typeof path !== 'string') { + return callback(new Error('"path" must be a string')) } - var sendWithTransform = send.withTransform(addToDagNodesTransform) + const sendWithTransform = send.withTransform(addToDagNodesTransform) - return sendWithTransform('add', null, opts, path, cb) + return sendWithTransform({ + path: 'add', + qs: opts, + files: path + }, callback) } } diff --git a/src/api/add-url.js b/src/api/add-url.js index fccb96e75..d571cd898 100644 --- a/src/api/add-url.js +++ b/src/api/add-url.js @@ -4,22 +4,30 @@ const Wreck = require('wreck') const addToDagNodesTransform = require('../add-to-dagnode-transform') module.exports = (send) => { - return function add (url, opts, cb) { - if (typeof (opts) === 'function' && cb === undefined) { - cb = opts + return function add (url, opts, callback) { + if (typeof (opts) === 'function' && + callback === undefined) { + callback = opts opts = {} } - if (typeof url !== 'string' || !url.startsWith('http')) { - return cb(new Error('"url" param must be an http(s) url')) + if (typeof url !== 'string' || + !url.startsWith('http')) { + return callback(new Error('"url" param must be an http(s) url')) } - var sendWithTransform = send.withTransform(addToDagNodesTransform) + const sendWithTransform = send.withTransform(addToDagNodesTransform) Wreck.request('GET', url, null, (err, res) => { - if (err) return cb(err) + if (err) { + return callback(err) + } - sendWithTransform('add', null, opts, res, cb) + sendWithTransform({ + path: 'add', + qs: opts, + files: res + }, callback) }) } } diff --git a/src/api/add.js b/src/api/add.js index 2dfea23b9..1379a4296 100644 --- a/src/api/add.js +++ b/src/api/add.js @@ -16,6 +16,9 @@ module.exports = (send) => { const sendWithTransform = send.withTransform(addToDagNodesTransform) - sendWithTransform('add', null, {}, files, callback) + return sendWithTransform({ + path: 'add', + files: files + }, callback) }) } diff --git a/src/api/bitswap.js b/src/api/bitswap.js index c2f9749b4..89c34acac 100644 --- a/src/api/bitswap.js +++ b/src/api/bitswap.js @@ -1,15 +1,27 @@ 'use strict' -const argCommand = require('../cmd-helpers').argCommand - module.exports = (send) => { return { - wantlist (cb) { - return send('bitswap/wantlist', {}, null, null, cb) + wantlist (callback) { + return send({ + path: 'bitswap/wantlist' + }, callback) }, - stat (cb) { - return send('bitswap/stat', {}, null, null, cb) + stat (callback) { + return send({ + path: 'bitswap/stat' + }, callback) }, - unwant: argCommand(send, 'bitswap/unwant') + unwant (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'bitswap/unwant', + args: args, + qs: opts + }, callback) + } } } diff --git a/src/api/block.js b/src/api/block.js index 8a6ec4529..500d0521e 100644 --- a/src/api/block.js +++ b/src/api/block.js @@ -1,20 +1,43 @@ 'use strict' -const argCommand = require('../cmd-helpers').argCommand - module.exports = (send) => { return { - get: argCommand(send, 'block/get'), - stat: argCommand(send, 'block/stat'), - put (file, cb) { + get (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'block/get', + args: args, + qs: opts + }, callback) + }, + stat (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'block/stat', + args: args, + qs: opts + }, callback) + }, + put (file, callback) { if (Array.isArray(file)) { - let err = new Error('block.put() only accepts 1 file') - if (typeof cb !== 'function' && typeof Promise !== 'undefined') { + const err = new Error('block.put() only accepts 1 file') + if (typeof callback !== 'function' && + typeof Promise !== 'undefined') { return new Promise((resolve, reject) => reject(err)) } - return cb(err) + return callback(err) } - return send('block/put', null, null, file, cb) + + return send({ + path: 'block/put', + files: file + }, callback) } } } diff --git a/src/api/bootstrap.js b/src/api/bootstrap.js index 1c8ee0d59..c5d682830 100644 --- a/src/api/bootstrap.js +++ b/src/api/bootstrap.js @@ -1,23 +1,40 @@ 'use strict' -const command = require('../cmd-helpers').command - module.exports = (send) => { return { - add: (arg, opts, cb) => { - if (typeof opts === 'function' && cb === undefined) { - cb = opts + add (args, opts, callback) { + if (typeof opts === 'function' && + callback === undefined) { + callback = opts opts = {} } - return send('bootstrap/add', arg, opts, null, cb) + return send({ + path: 'bootstrap/add', + args: args, + qs: opts + }, callback) }, - rm: (arg, opts, cb) => { - if (typeof opts === 'function' && cb === undefined) { - cb = opts + rm (args, opts, callback) { + if (typeof opts === 'function' && + callback === undefined) { + callback = opts opts = {} } - return send('bootstrap/rm', arg, opts, null, cb) + return send({ + path: 'bootstrap/rm', + args: args, + qs: opts + }, callback) }, - list: command(send, 'bootstrap/list') + list (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'bootstrap/list', + qs: opts + }, callback) + } } } diff --git a/src/api/cat.js b/src/api/cat.js index 06e9bac90..829b225be 100644 --- a/src/api/cat.js +++ b/src/api/cat.js @@ -4,13 +4,16 @@ const promisify = require('promisify-es6') const cleanMultihash = require('../clean-multihash') module.exports = (send) => { - const cat = promisify((multihash, callback) => { + return promisify((hash, callback) => { try { - multihash = cleanMultihash(multihash) + hash = cleanMultihash(hash) } catch (err) { return callback(err) } - send('cat', multihash, null, null, callback) + + return send({ + path: 'cat', + args: hash + }, callback) }) - return cat } diff --git a/src/api/commands.js b/src/api/commands.js index 18eaee4d5..4666feefd 100644 --- a/src/api/commands.js +++ b/src/api/commands.js @@ -1,7 +1,9 @@ 'use strict' -const command = require('../cmd-helpers').command - module.exports = (send) => { - return command(send, 'commands') + return (callback) => { + return send({ + path: 'commands' + }, callback) + } } diff --git a/src/api/config.js b/src/api/config.js index 670a3265e..148a39a50 100644 --- a/src/api/config.js +++ b/src/api/config.js @@ -11,14 +11,21 @@ module.exports = (send) => { } if (!key) { - return send('config/show', null, null, null, true, callback) + return send({ + path: 'config/show', + buffer: true + }, callback) } - return send('config', key, null, null, (err, result) => { + return send({ + path: 'config', + args: key, + buffer: true + }, (err, response) => { if (err) { return callback(err) } - callback(null, result.Value) + callback(null, response.Value) }) }, set (key, value, opts, callback) { @@ -46,19 +53,24 @@ module.exports = (send) => { opts = { bool: true } } - return send('config', [key, value], opts, null, callback) + return send({ + path: 'config', + args: [key, value], + qs: opts, + files: undefined, + buffer: true + }, callback) }, replace (config, callback) { - // Its a path - if (typeof config === 'string') { - return send('config/replace', null, null, config, callback) - } - - // Its a config obj if (typeof config === 'object') { config = streamifier.createReadStream(new Buffer(JSON.stringify(config))) - return send('config/replace', null, null, config, callback) } + + return send({ + path: 'config/replace', + files: config, + buffer: true + }, callback) } } } diff --git a/src/api/dht.js b/src/api/dht.js index aa02da5ce..c7abff7cb 100644 --- a/src/api/dht.js +++ b/src/api/dht.js @@ -1,20 +1,35 @@ 'use strict' -const argCommand = require('../cmd-helpers').argCommand - module.exports = (send) => { return { - findprovs: argCommand(send, 'dht/findprovs'), - get (key, opts, cb) { - if (typeof (opts) === 'function' && !cb) { - cb = opts + findprovs (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'dht/findprovs', + args: args, + qs: opts + }, callback) + }, + get (key, opts, callback) { + if (typeof (opts) === 'function' && + !callback) { + callback = opts opts = null } const handleResult = (done, err, res) => { - if (err) return done(err) - if (!res) return done(new Error('empty response')) - if (res.length === 0) return done(new Error('no value returned for key')) + if (err) { + return done(err) + } + if (!res) { + return done(new Error('empty response')) + } + if (res.length === 0) { + return done(new Error('no value returned for key')) + } // Inconsistent return values in the browser vs node if (Array.isArray(res)) { @@ -29,28 +44,36 @@ module.exports = (send) => { } } - if (typeof cb !== 'function' && typeof Promise !== 'undefined') { + if (typeof callback !== 'function' && typeof Promise !== 'undefined') { const done = (err, res) => { if (err) throw err return res } - return send('dht/get', key, opts) - .then( - (res) => handleResult(done, null, res), - (err) => handleResult(done, err) - ) + return send({ + path: 'dht/get', + args: key, + qs: opts + }).then((res) => handleResult(done, null, res), + (err) => handleResult(done, err)) } - return send('dht/get', key, opts, null, handleResult.bind(null, cb)) + return send({ + path: 'dht/get', + args: key, + qs: opts + }, handleResult.bind(null, callback)) }, - put (key, value, opts, cb) { - if (typeof (opts) === 'function' && !cb) { - cb = opts + put (key, value, opts, callback) { + if (typeof (opts) === 'function' && !callback) { + callback = opts opts = null } - - return send('dht/put', [key, value], opts, null, cb) + return send({ + path: 'dht/put', + args: [key, value], + qs: opts + }, callback) } } } diff --git a/src/api/diag.js b/src/api/diag.js index 438817446..668521587 100644 --- a/src/api/diag.js +++ b/src/api/diag.js @@ -1,11 +1,39 @@ 'use strict' -const command = require('../cmd-helpers').command - module.exports = (send) => { return { - net: command(send, 'diag/net'), - sys: command(send, 'diag/sys'), - cmds: command(send, 'diag/sys') + net (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + + return send({ + path: 'diag/net', + qs: opts + }, callback) + }, + sys (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + + return send({ + path: 'diag/sys', + qs: opts + }, callback) + }, + cmds (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + + return send({ + path: 'diag/cmds', + qs: opts + }, callback) + } } } diff --git a/src/api/files.js b/src/api/files.js index 2e848c39e..41efe651d 100644 --- a/src/api/files.js +++ b/src/api/files.js @@ -1,28 +1,98 @@ 'use strict' -const argCommand = require('../cmd-helpers').argCommand - module.exports = (send) => { return { - cp: argCommand(send, 'files/cp'), - ls: argCommand(send, 'files/ls'), - mkdir: argCommand(send, 'files/mkdir'), - stat: argCommand(send, 'files/stat'), - rm: function (path, opts, cb) { - if (typeof opts === 'function' && cb === undefined) { - cb = opts + cp (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'files/cp', + args: args, + qs: opts + }, callback) + }, + ls (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'files/ls', + args: args, + qs: opts + }, callback) + }, + mkdir (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'files/mkdir', + args: args, + qs: opts + }, callback) + }, + stat (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts opts = {} } - return send('files/rm', path, opts, null, cb) + return send({ + path: 'files/stat', + args: args, + qs: opts + }, callback) }, - read: argCommand(send, 'files/read'), - write: function (pathDst, files, opts, cb) { - if (typeof opts === 'function' && cb === undefined) { - cb = opts + rm (path, opts, callback) { + if (typeof opts === 'function' && + callback === undefined) { + callback = opts opts = {} } - return send('files/write', pathDst, opts, files, cb) + return send({ + path: 'files/rm', + args: path, + qs: opts + }, callback) }, - mv: argCommand(send, 'files/mv') + read (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'files/read', + args: args, + qs: opts + }, callback) + }, + write (pathDst, files, opts, callback) { + if (typeof opts === 'function' && callback === undefined) { + callback = opts + opts = {} + } + + return send({ + path: 'files/write', + args: pathDst, + qs: opts, + files: files + }, callback) + }, + mv (args, opts, callback) { + if (typeof opts === 'function' && + callback === undefined) { + callback = opts + opts = {} + } + return send({ + path: 'files/mv', + args: args, + qs: opts + }, callback) + } } } diff --git a/src/api/get.js b/src/api/get.js index b1b833d9d..3f4c5f83e 100644 --- a/src/api/get.js +++ b/src/api/get.js @@ -5,26 +5,32 @@ const cleanMultihash = require('../clean-multihash') const promisify = require('promisify-es6') module.exports = (send) => { - return promisify(function get (path, opts, cb) { - if (typeof opts === 'function' && !cb) { - cb = opts + return promisify(function get (path, opts, callback) { + if (typeof opts === 'function' && + !callback) { + callback = opts opts = {} } - // opts is the real callback -- 'cb' is being injected by promisify - if (typeof opts === 'function' && typeof cb === 'function') { - cb = opts + // opts is the real callback -- 'callback' is being injected by promisify + if (typeof opts === 'function' && + typeof callback === 'function') { + callback = opts opts = {} } try { path = cleanMultihash(path) } catch (err) { - return cb(err) + return callback(err) } var sendWithTransform = send.withTransform(tarStreamToObjects) - return sendWithTransform('get', path, opts, null, cb) + return sendWithTransform({ + path: 'get', + args: path, + qs: opts + }, callback) }) } diff --git a/src/api/id.js b/src/api/id.js index ab4d44e8d..ad73897a4 100644 --- a/src/api/id.js +++ b/src/api/id.js @@ -1,11 +1,14 @@ 'use strict' module.exports = (send) => { - return function id (idParam, cb) { - if (typeof idParam === 'function') { - cb = idParam - idParam = null + return function id (opts, callback) { + if (typeof opts === 'function') { + callback = opts + opts = undefined } - return send('id', idParam, null, null, cb) + return send({ + path: 'id', + args: opts + }, callback) } } diff --git a/src/api/log.js b/src/api/log.js index 1c85767d8..ef57f6fdb 100644 --- a/src/api/log.js +++ b/src/api/log.js @@ -4,15 +4,21 @@ const ndjson = require('ndjson') module.exports = (send) => { return { - tail (cb) { - if (typeof cb !== 'function' && typeof Promise !== 'undefined') { - return send('log/tail', null, {}, null, false) - .then((res) => res.pipe(ndjson.parse())) + tail (callback) { + if (typeof callback !== 'function' && + typeof Promise !== 'undefined') { + return send({ + path: 'log/tail' + }).then((res) => res.pipe(ndjson.parse())) } - return send('log/tail', null, {}, null, false, (err, res) => { - if (err) return cb(err) - cb(null, res.pipe(ndjson.parse())) + return send({ + path: 'log/tail' + }, (err, response) => { + if (err) { + return callback(err) + } + callback(null, response.pipe(ndjson.parse())) }) } } diff --git a/src/api/ls.js b/src/api/ls.js index 4f109b6fe..7bd2a2e07 100644 --- a/src/api/ls.js +++ b/src/api/ls.js @@ -1,7 +1,15 @@ 'use strict' -const argCommand = require('../cmd-helpers').argCommand - module.exports = (send) => { - return argCommand(send, 'ls') + return (args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'ls', + args: args, + qs: opts + }, callback) + } } diff --git a/src/api/mount.js b/src/api/mount.js index 917fa0b90..170d976e9 100644 --- a/src/api/mount.js +++ b/src/api/mount.js @@ -1,17 +1,25 @@ 'use strict' module.exports = (send) => { - return function mount (ipfs, ipns, cb) { + return function mount (ipfs, ipns, callback) { if (typeof ipfs === 'function') { - cb = ipfs + callback = ipfs ipfs = null } else if (typeof ipns === 'function') { - cb = ipns + callback = ipns ipns = null } const opts = {} - if (ipfs) opts.f = ipfs - if (ipns) opts.n = ipns - return send('mount', null, opts, null, cb) + if (ipfs) { + opts.f = ipfs + } + if (ipns) { + opts.n = ipns + } + + return send({ + path: 'mount', + qs: opts + }, callback) } } diff --git a/src/api/name.js b/src/api/name.js index 70e7b72b9..fab9a55fd 100644 --- a/src/api/name.js +++ b/src/api/name.js @@ -1,10 +1,28 @@ 'use strict' -const argCommand = require('../cmd-helpers').argCommand - module.exports = (send) => { return { - publish: argCommand(send, 'name/publish'), - resolve: argCommand(send, 'name/resolve') + publish (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'name/publish', + args: args, + qs: opts + }, callback) + }, + resolve (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'name/resolve', + args: args, + qs: opts + }, callback) + } } } diff --git a/src/api/object.js b/src/api/object.js index 0c6d2a05c..787b36276 100644 --- a/src/api/object.js +++ b/src/api/object.js @@ -5,6 +5,7 @@ const DAGLink = require('ipfs-merkle-dag').DAGLink const promisify = require('promisify-es6') const bs58 = require('bs58') const bl = require('bl') +const cleanMultihash = require('../clean-multihash') module.exports = (send) => { const api = { @@ -23,7 +24,10 @@ module.exports = (send) => { return callback(err) } - send('object/get', multihash, null, null, (err, result) => { + send({ + path: 'object/get', + args: multihash + }, (err, result) => { if (err) { return callback(err) } @@ -73,7 +77,11 @@ module.exports = (send) => { } const enc = options.enc || 'json' - send('object/put', null, {inputenc: enc}, buf, (err, result) => { + send({ + path: 'object/put', + qs: { inputenc: enc }, + files: buf + }, (err, result) => { if (err) { return callback(err) } @@ -117,7 +125,10 @@ module.exports = (send) => { return callback(err) } - send('object/data', multihash, null, null, (err, result) => { + send({ + path: 'object/data', + args: multihash + }, (err, result) => { if (err) { return callback(err) } @@ -144,7 +155,10 @@ module.exports = (send) => { return callback(err) } - send('object/links', multihash, null, null, (err, result) => { + send({ + path: 'object/links', + args: multihash + }, (err, result) => { if (err) { return callback(err) } @@ -159,25 +173,30 @@ module.exports = (send) => { callback(null, links) }) }), - stat: promisify((multihash, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} + stat: promisify((multihash, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} } - if (!options) { - options = {} + if (!opts) { + opts = {} } try { - multihash = cleanMultihash(multihash, options) + multihash = cleanMultihash(multihash, opts) } catch (err) { return callback(err) } - send('object/stat', multihash, null, null, callback) + send({ + path: 'object/stat', + args: multihash + }, callback) }), new: promisify((callback) => { - send('object/new', null, null, null, (err, result) => { + send({ + path: 'object/new' + }, (err, result) => { if (err) { return callback(err) } @@ -191,88 +210,102 @@ module.exports = (send) => { }) }), patch: { - addLink: promisify((multihash, dLink, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} + addLink: promisify((multihash, dLink, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} } - if (!options) { - options = {} + if (!opts) { + opts = {} } try { - multihash = cleanMultihash(multihash, options) + multihash = cleanMultihash(multihash, opts) } catch (err) { return callback(err) } - send('object/patch/add-link', [multihash, dLink.name, bs58.encode(dLink.hash).toString()], null, null, (err, result) => { + send({ + path: 'object/patch/add-link', + args: [multihash, dLink.name, bs58.encode(dLink.hash).toString()] + }, (err, result) => { if (err) { return callback(err) } api.get(result.Hash, { enc: 'base58' }, callback) }) }), - rmLink: promisify((multihash, dLink, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} + rmLink: promisify((multihash, dLink, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} } - if (!options) { - options = {} + if (!opts) { + opts = {} } try { - multihash = cleanMultihash(multihash, options) + multihash = cleanMultihash(multihash, opts) } catch (err) { return callback(err) } - send('object/patch/rm-link', [multihash, dLink.name], null, null, (err, result) => { + send({ + path: 'object/patch/rm-link', + args: [multihash, dLink.name] + }, (err, result) => { if (err) { return callback(err) } api.get(result.Hash, { enc: 'base58' }, callback) }) }), - setData: promisify((multihash, data, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} + setData: promisify((multihash, data, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} } - if (!options) { - options = {} + if (!opts) { + opts = {} } try { - multihash = cleanMultihash(multihash, options) + multihash = cleanMultihash(multihash, opts) } catch (err) { return callback(err) } - send('object/patch/set-data', [multihash], null, data, (err, result) => { + send({ + path: 'object/patch/set-data', + args: [multihash], + files: data + }, (err, result) => { if (err) { return callback(err) } api.get(result.Hash, { enc: 'base58' }, callback) }) }), - appendData: promisify((multihash, data, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} + appendData: promisify((multihash, data, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} } - if (!options) { - options = {} + if (!opts) { + opts = {} } try { - multihash = cleanMultihash(multihash, options) + multihash = cleanMultihash(multihash, opts) } catch (err) { return callback(err) } - send('object/patch/append-data', [multihash], null, data, (err, result) => { + send({ + path: 'object/patch/append-data', + args: [multihash], + files: data + }, (err, result) => { if (err) { return callback(err) } @@ -281,34 +314,6 @@ module.exports = (send) => { }) } } - return api -} -function cleanMultihash (multihash, options) { - if (Buffer.isBuffer(multihash)) { - if (options.enc) { - switch (options.enc) { - case 'base58': { - multihash = multihash.toString() - break - } - default: throw new Error('invalid multihash') - } - } else { - multihash = bs58.encode(multihash).toString() - } - } else if (typeof multihash === 'string') { - if (options.enc) { - // For the future, when we support more than one enc - // switch (options.enc) { - // case 'base58': // It is good - // } - } else { - throw new Error('not valid multihash') - } - } else if (!multihash) { - throw new Error('missing valid multihash') - } - - return multihash + return api } diff --git a/src/api/pin.js b/src/api/pin.js index e79e1623c..d8f170447 100644 --- a/src/api/pin.js +++ b/src/api/pin.js @@ -2,29 +2,36 @@ module.exports = (send) => { return { - add (hash, opts, cb) { + add (hash, opts, callback) { if (typeof opts === 'function') { - cb = opts + callback = opts opts = null } - - return send('pin/add', hash, opts, null, cb) + return send({ + path: 'pin/add', + args: hash, + qs: opts + }, callback) }, - remove (hash, opts, cb) { + remove (hash, opts, callback) { if (typeof opts === 'function') { - cb = opts + callback = opts opts = null } - - return send('pin/rm', hash, opts, null, cb) + return send({ + path: 'pin/rm', + args: hash, + qs: opts + }, callback) }, - list (type, cb) { + list (type, callback) { if (typeof type === 'function') { - cb = type + callback = type type = null } let opts = null let hash = null + if (typeof type === 'string') { opts = { type: type } } else if (type && type.hash) { @@ -32,8 +39,11 @@ module.exports = (send) => { type.hash = null opts = type } - - return send('pin/ls', hash, opts, null, cb) + return send({ + path: 'pin/ls', + args: hash, + qs: opts + }, callback) } } } diff --git a/src/api/ping.js b/src/api/ping.js index f2853a675..777496c4a 100644 --- a/src/api/ping.js +++ b/src/api/ping.js @@ -1,15 +1,25 @@ 'use strict' module.exports = (send) => { - return function ping (id, cb) { - if (typeof cb !== 'function' && typeof Promise !== 'undefined') { - return send('ping', id, {n: 1}, null) - .then((res) => res[1]) + return function ping (id, callback) { + if (typeof callback !== 'function' && + typeof Promise !== 'undefined') { + return send({ + path: 'ping', + args: id, + qs: { n: 1 } + }).then((res) => res[1]) } - return send('ping', id, { n: 1 }, null, function (err, res) { - if (err) return cb(err, null) - cb(null, res[1]) + return send({ + path: 'ping', + args: id, + qs: { n: 1 } + }, function (err, res) { + if (err) { + return callback(err, null) + } + callback(null, res[1]) }) } } diff --git a/src/api/refs.js b/src/api/refs.js index 1788512f7..d7e6147a3 100644 --- a/src/api/refs.js +++ b/src/api/refs.js @@ -1,10 +1,27 @@ 'use strict' -const cmds = require('../cmd-helpers') - module.exports = (send) => { - const refs = cmds.argCommand(send, 'refs') - refs.local = cmds.command(send, 'refs/local') + const refs = (args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'refs', + args: args, + qs: opts + }, callback) + } + refs.local = (opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'refs', + qs: opts + }, callback) + } return refs } diff --git a/src/api/repo.js b/src/api/repo.js index e6211c713..277c5facb 100644 --- a/src/api/repo.js +++ b/src/api/repo.js @@ -1,10 +1,26 @@ 'use strict' -const cmds = require('../cmd-helpers') - module.exports = (send) => { return { - gc: cmds.command(send, 'repo/gc'), - stat: cmds.command(send, 'repo/stat') + gc (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'repo/gc', + qs: opts + }, callback) + }, + stat (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'repo/stat', + qs: opts + }, callback) + } } } diff --git a/src/api/swarm.js b/src/api/swarm.js index 08b87c6f2..73147c485 100644 --- a/src/api/swarm.js +++ b/src/api/swarm.js @@ -1,13 +1,58 @@ 'use strict' -const cmds = require('../cmd-helpers') - module.exports = (send) => { return { - peers: cmds.command(send, 'swarm/peers'), - connect: cmds.argCommand(send, 'swarm/connect'), - disconnect: cmds.argCommand(send, 'swarm/disconnect'), - addrs: cmds.command(send, 'swarm/addrs'), - localAddrs: cmds.command(send, 'swarm/addrs/local') + peers (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'swarm/peers', + qs: opts + }, callback) + }, + connect (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'swarm/connect', + args: args, + qs: opts + }, callback) + }, + disconnect (args, opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'swarm/disconnect', + args: args, + qs: opts + }, callback) + }, + addrs (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'swarm/addrs', + qs: opts + }, callback) + }, + localAddrs (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'swarm/addrs/local', + qs: opts + }, callback) + } } } diff --git a/src/api/update.js b/src/api/update.js index 5eca2c3ec..8f8e7635c 100644 --- a/src/api/update.js +++ b/src/api/update.js @@ -1,11 +1,36 @@ 'use strict' -const command = require('../cmd-helpers').command - module.exports = (send) => { return { - apply: command(send, 'update'), - check: command(send, 'update/check'), - log: command(send, 'update/log') + apply (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'update', + qs: opts + }, callback) + }, + check (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'update/check', + qs: opts + }, callback) + }, + log (opts, callback) { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'update/log', + qs: opts + }, callback) + } } } diff --git a/src/api/version.js b/src/api/version.js index 9221b6275..d3fce426a 100644 --- a/src/api/version.js +++ b/src/api/version.js @@ -1,7 +1,15 @@ 'use strict' -const command = require('../cmd-helpers').command - module.exports = (send) => { - return command(send, 'version') + return (opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + + return send({ + path: 'version', + qs: opts + }, callback) + } } diff --git a/src/cmd-helpers.js b/src/cmd-helpers.js deleted file mode 100644 index 072c34825..000000000 --- a/src/cmd-helpers.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict' - -exports.command = function command (send, name) { - return (opts, cb) => { - if (typeof (opts) === 'function') { - cb = opts - opts = {} - } - return send(name, null, opts, null, cb) - } -} - -exports.argCommand = function argCommand (send, name) { - return (arg, opts, cb) => { - if (typeof (opts) === 'function') { - cb = opts - opts = {} - } - return send(name, arg, opts, null, cb) - } -} diff --git a/src/config.js b/src/default-config.js similarity index 100% rename from src/config.js rename to src/default-config.js diff --git a/src/get-dagnode.js b/src/get-dagnode.js index 3d60844c9..ce0afd674 100644 --- a/src/get-dagnode.js +++ b/src/get-dagnode.js @@ -4,38 +4,44 @@ const DAGNode = require('ipfs-merkle-dag').DAGNode const bl = require('bl') const async = require('async') -module.exports = function (send, hash, cb) { +module.exports = function (send, hash, callback) { // Retrieve the object and its data in parallel, then produce a DAGNode // instance using this information. async.parallel([ function get (done) { - send('object/get', hash, null, null, done) + send({ + path: 'object/get', + args: hash + }, done) }, function data (done) { // WORKAROUND: request the object's data separately, since raw bits in JSON // are interpreted as UTF-8 and corrupt the data. // See https://github.com/ipfs/go-ipfs/issues/1582 for more details. - send('object/data', hash, null, null, done) + send({ + path: 'object/data', + args: hash + }, done) }], function done (err, res) { if (err) { - return cb(err) + return callback(err) } var object = res[0] var stream = res[1] if (Buffer.isBuffer(stream)) { - cb(err, new DAGNode(stream, object.Links)) + callback(err, new DAGNode(stream, object.Links)) } else { stream.pipe(bl(function (err, data) { if (err) { - return cb(err) + return callback(err) } - cb(err, new DAGNode(data, object.Links)) + callback(err, new DAGNode(data, object.Links)) })) } }) diff --git a/src/index.js b/src/index.js index a150458d3..e5b4f3399 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ const multiaddr = require('multiaddr') const loadCommands = require('./load-commands') -const getConfig = require('./config') +const getConfig = require('./default-config') const getRequestAPI = require('./request-api') function IpfsAPI (hostOrMultiaddr, port, opts) { diff --git a/src/request-api.js b/src/request-api.js index f56907fcc..b2f0f8448 100644 --- a/src/request-api.js +++ b/src/request-api.js @@ -63,39 +63,47 @@ function onRes (buffer, cb, uri) { } } -function requestAPI (config, path, args, qs, files, buffer, cb) { - qs = qs || {} +function requestAPI (config, options, callback) { + options.qs = options.qs || {} - if (Array.isArray(files)) { - qs.recursive = true + if (Array.isArray(options.files)) { + options.qs.recursive = true } - if (Array.isArray(path)) path = path.join('/') - if (args && !Array.isArray(args)) args = [args] - if (args) qs.arg = args - if (files && !Array.isArray(files)) files = [files] + if (Array.isArray(options.path)) { + options.path = options.path.join('/') + } + if (options.args && !Array.isArray(options.args)) { + options.args = [options.args] + } + if (options.args) { + options.qs.arg = options.args + } + if (options.files && !Array.isArray(options.files)) { + options.files = [options.files] + } - if (qs.r) { - qs.recursive = qs.r + if (options.qs.r) { + options.qs.recursive = options.qs.r // From IPFS 0.4.0, it throws an error when both r and recursive are passed - delete qs.r + delete options.qs.r } - qs['stream-channels'] = true + options.qs['stream-channels'] = true let stream - if (files) { - stream = getFilesStream(files, qs) + if (options.files) { + stream = getFilesStream(options.files, options.qs) } // this option is only used internally, not passed to daemon - delete qs.followSymlinks + delete options.qs.followSymlinks const port = config.port ? `:${config.port}` : '' const opts = { method: 'POST', - uri: `${config.protocol}://${config.host}${port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`, + uri: `${config.protocol}://${config.host}${port}${config['api-path']}${options.path}?${Qs.stringify(options.qs, {arrayFormat: 'repeat'})}`, headers: {} } @@ -104,50 +112,60 @@ function requestAPI (config, path, args, qs, files, buffer, cb) { opts.headers['User-Agent'] = config['user-agent'] } - if (files) { + if (options.files) { if (!stream.boundary) { - return cb(new Error('No boundary in multipart stream')) + return callback(new Error('No boundary in multipart stream')) } opts.headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}` opts.payload = stream } - return Wreck.request(opts.method, opts.uri, opts, onRes(buffer, cb, opts.uri)) + return Wreck.request(opts.method, opts.uri, opts, onRes(options.buffer, callback, opts.uri)) } -// -- Interface +// +// -- Module Interface exports = module.exports = function getRequestAPI (config) { - var send = function (path, args, qs, files, buffer, cb) { - if (typeof buffer === 'function') { - cb = buffer - buffer = false + /* + * options: { + * path: // API path (like /add or /config) + * args: // Arguments to the command + * qs: // Opts as query string opts to the command --something + * files: // files to be sent + * buffer: // buffer the request before sending it + * } + */ + const send = function (options, callback) { + if (typeof options !== 'object') { + return callback(new Error('no options were passed')) } - if (typeof cb !== 'function' && typeof Promise !== 'undefined') { + if (typeof callback !== 'function' && typeof Promise !== 'undefined') { return new Promise(function (resolve, reject) { - requestAPI(config, path, args, qs, files, buffer, function (err, res) { - if (err) return reject(err) + requestAPI(config, options, function (err, res) { + if (err) { + return reject(err) + } resolve(res) }) }) } - return requestAPI(config, path, args, qs, files, buffer, cb) + return requestAPI(config, options, callback) } // Wraps the 'send' function such that an asynchronous // transform may be applied to its result before // passing it on to either its callback or promise. send.withTransform = function (transform) { - return function (path, args, qs, files, buffer, cb) { - if (typeof buffer === 'function') { - cb = buffer - buffer = false + return function (options, callback) { + if (typeof options !== 'object') { + return callback(new Error('no options were passed')) } - var p = send(path, args, qs, files, buffer, wrap(cb)) + const p = send(options, wrap(callback)) if (p instanceof Promise) { return p.then((res) => { @@ -165,10 +183,10 @@ exports = module.exports = function getRequestAPI (config) { return p } - function wrap (done) { - if (done) { + function wrap (func) { + if (func) { return function (err, res) { - transform(err, res, send, done) + transform(err, res, send, func) } } } diff --git a/test/interface-ipfs-core/block.spec.js b/test/interface-ipfs-core/block.spec.js index 5fedd8f0e..2bfa89e09 100644 --- a/test/interface-ipfs-core/block.spec.js +++ b/test/interface-ipfs-core/block.spec.js @@ -1,22 +1,39 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 8] */ -/* globals apiClients */ 'use strict' +const FactoryClient = require('../factory/factory-client') const expect = require('chai').expect describe('.block', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + + after((done) => { + fc.dismantle(done) + }) + const blorbKey = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ' const blorb = Buffer('blorb') it('returns an error when putting an array of files', () => { - return apiClients.a.block.put([blorb, blorb], (err) => { + return ipfs.block.put([blorb, blorb], (err) => { expect(err).to.be.an.instanceof(Error) }) }) it('block.put', (done) => { - apiClients.a.block.put(blorb, (err, res) => { + ipfs.block.put(blorb, (err, res) => { expect(err).to.not.exist expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') done() @@ -24,7 +41,7 @@ describe('.block', () => { }) it('block.get', (done) => { - apiClients.a.block.get(blorbKey, (err, res) => { + ipfs.block.get(blorbKey, (err, res) => { expect(err).to.not.exist let buf = '' @@ -38,7 +55,7 @@ describe('.block', () => { }) it('block.stat', (done) => { - apiClients.a.block.stat(blorbKey, (err, res) => { + ipfs.block.stat(blorbKey, (err, res) => { expect(err).to.not.exist expect(res).to.have.property('Key') expect(res).to.have.property('Size') @@ -48,21 +65,21 @@ describe('.block', () => { describe('promise', () => { it('returns an error when putting an array of files', () => { - return apiClients.a.block.put([blorb, blorb]) + return ipfs.block.put([blorb, blorb]) .catch((err) => { expect(err).to.be.an.instanceof(Error) }) }) it('block.put', () => { - return apiClients.a.block.put(blorb) + return ipfs.block.put(blorb) .then((res) => { expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ') }) }) it('block.get', (done) => { - apiClients.a.block.get(blorbKey) + ipfs.block.get(blorbKey) .then((res) => { let buf = '' res @@ -76,7 +93,7 @@ describe('.block', () => { }) it('block.stat', () => { - return apiClients.a.block.stat(blorbKey) + return ipfs.block.stat(blorbKey) .then((res) => { expect(res).to.have.property('Key') expect(res).to.have.property('Size') diff --git a/test/interface-ipfs-core/bootstrap.spec.js b/test/interface-ipfs-core/bootstrap.spec.js index 8cecb7978..48448a5b8 100644 --- a/test/interface-ipfs-core/bootstrap.spec.js +++ b/test/interface-ipfs-core/bootstrap.spec.js @@ -1,33 +1,50 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 8] */ -/* globals apiClients */ 'use strict' const expect = require('chai').expect +const FactoryClient = require('../factory/factory-client') const invalidArg = 'this/Is/So/Invalid/' const validIp4 = '/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z' describe('.bootstrap', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + + after((done) => { + fc.dismantle(done) + }) + let peers describe('.add', () => { it('returns an error when called without args or options', (done) => { - return apiClients.a.bootstrap.add(null, (err) => { + return ipfs.bootstrap.add(null, (err) => { expect(err).to.be.an.instanceof(Error) done() }) }) it('returns an error when called with an invalid arg', (done) => { - return apiClients.a.bootstrap.add(invalidArg, (err) => { + return ipfs.bootstrap.add(invalidArg, (err) => { expect(err).to.be.an.instanceof(Error) done() }) }) it('returns a list of containing the bootstrap peer when called with a valid arg (ip4)', (done) => { - return apiClients.a.bootstrap.add(validIp4, (err, res) => { + return ipfs.bootstrap.add(validIp4, (err, res) => { expect(err).to.not.exist expect(res).to.be.eql({ Peers: [validIp4] }) peers = res.Peers @@ -38,7 +55,7 @@ describe('.bootstrap', () => { }) it('returns a list of bootstrap peers when called with the default option', (done) => { - return apiClients.a.bootstrap.add(null, { default: true }, (err, res) => { + return ipfs.bootstrap.add(null, { default: true }, (err, res) => { expect(err).to.not.exist peers = res.Peers expect(peers).to.exist @@ -50,7 +67,7 @@ describe('.bootstrap', () => { describe('.list', () => { it('returns a list of peers', (done) => { - return apiClients.a.bootstrap.list((err, res) => { + return ipfs.bootstrap.list((err, res) => { expect(err).to.not.exist peers = res.Peers expect(peers).to.exist @@ -61,14 +78,14 @@ describe('.bootstrap', () => { describe('.rm', () => { it('returns an error when called with an invalid arg', (done) => { - return apiClients.a.bootstrap.rm(invalidArg, (err) => { + return ipfs.bootstrap.rm(invalidArg, (err) => { expect(err).to.be.an.instanceof(Error) done() }) }) it('returns empty list because no peers removed when called without an arg or options', (done) => { - return apiClients.a.bootstrap.rm(null, (err, res) => { + return ipfs.bootstrap.rm(null, (err, res) => { expect(err).to.not.exist peers = res.Peers expect(peers).to.exist @@ -78,7 +95,7 @@ describe('.bootstrap', () => { }) it('returns list containing the peer removed when called with a valid arg (ip4)', (done) => { - return apiClients.a.bootstrap.rm(null, (err, res) => { + return ipfs.bootstrap.rm(null, (err, res) => { expect(err).to.not.exist peers = res.Peers expect(peers).to.exist @@ -88,7 +105,7 @@ describe('.bootstrap', () => { }) it('returns list of all peers removed when all option is passed', (done) => { - return apiClients.a.bootstrap.rm(null, { all: true }, (err, res) => { + return ipfs.bootstrap.rm(null, { all: true }, (err, res) => { expect(err).to.not.exist peers = res.Peers expect(peers).to.exist @@ -100,21 +117,21 @@ describe('.bootstrap', () => { describe('.promise', () => { describe('.add', () => { it('returns an error when called without args or options', () => { - return apiClients.a.bootstrap.add(null) + return ipfs.bootstrap.add(null) .catch((err) => { expect(err).to.be.an.instanceof(Error) }) }) it('returns an error when called with an invalid arg', () => { - return apiClients.a.bootstrap.add(invalidArg) + return ipfs.bootstrap.add(invalidArg) .catch((err) => { expect(err).to.be.an.instanceof(Error) }) }) it('returns a list of peers when called with a valid arg (ip4)', () => { - return apiClients.a.bootstrap.add(validIp4) + return ipfs.bootstrap.add(validIp4) .then((res) => { expect(res).to.be.eql({ Peers: [validIp4] }) peers = res.Peers @@ -124,7 +141,7 @@ describe('.bootstrap', () => { }) it('returns a list of default peers when called with the default option', () => { - return apiClients.a.bootstrap.add(null, { default: true }) + return ipfs.bootstrap.add(null, { default: true }) .then((res) => { peers = res.Peers expect(peers).to.exist @@ -135,7 +152,7 @@ describe('.bootstrap', () => { describe('.list', () => { it('returns a list of peers', () => { - return apiClients.a.bootstrap.list() + return ipfs.bootstrap.list() .then((res) => { peers = res.Peers expect(peers).to.exist @@ -145,14 +162,14 @@ describe('.bootstrap', () => { describe('.rm', () => { it('returns an error when called with an invalid arg', () => { - return apiClients.a.bootstrap.rm(invalidArg) + return ipfs.bootstrap.rm(invalidArg) .catch((err) => { expect(err).to.be.an.instanceof(Error) }) }) it('returns empty list when called without an arg or options', () => { - return apiClients.a.bootstrap.rm(null) + return ipfs.bootstrap.rm(null) .then((res) => { peers = res.Peers expect(peers).to.exist @@ -161,7 +178,7 @@ describe('.bootstrap', () => { }) it('returns list containing the peer removed when called with a valid arg (ip4)', () => { - return apiClients.a.bootstrap.rm(null) + return ipfs.bootstrap.rm(null) .then((res) => { peers = res.Peers expect(peers).to.exist @@ -170,7 +187,7 @@ describe('.bootstrap', () => { }) it('returns list of all peers removed when all option is passed', () => { - return apiClients.a.bootstrap.rm(null, { all: true }) + return ipfs.bootstrap.rm(null, { all: true }) .then((res) => { peers = res.Peers expect(peers).to.exist diff --git a/test/interface-ipfs-core/commands.spec.js b/test/interface-ipfs-core/commands.spec.js index a3fff5556..e18b0ca24 100644 --- a/test/interface-ipfs-core/commands.spec.js +++ b/test/interface-ipfs-core/commands.spec.js @@ -1,12 +1,29 @@ /* eslint-env mocha */ -/* globals apiClients */ 'use strict' const expect = require('chai').expect +const FactoryClient = require('../factory/factory-client') describe('.commands', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + + after((done) => { + fc.dismantle(done) + }) + it('lists commands', (done) => { - apiClients.a.commands((err, res) => { + ipfs.commands((err, res) => { expect(err).to.not.exist expect(res).to.exist done() @@ -15,7 +32,7 @@ describe('.commands', () => { describe('promise', () => { it('lists commands', () => { - return apiClients.a.commands() + return ipfs.commands() .then((res) => { expect(res).to.exist }) diff --git a/test/interface-ipfs-core/config.spec.js b/test/interface-ipfs-core/config.spec.js index 0d87e0363..794fd4678 100644 --- a/test/interface-ipfs-core/config.spec.js +++ b/test/interface-ipfs-core/config.spec.js @@ -1,16 +1,19 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 8] */ -/* globals apiClients */ 'use strict' const test = require('interface-ipfs-core') +const FactoryClient = require('../factory/factory-client') + +let fc const common = { - setup: function (cb) { - cb(null, apiClients.a) + setup: function (callback) { + fc = new FactoryClient() + callback(null, fc) }, - teardown: function (cb) { - cb() + teardown: function (callback) { + fc.dismantle(callback) } } diff --git a/test/interface-ipfs-core/diag.spec.js b/test/interface-ipfs-core/diag.spec.js index a7097f7e5..198248343 100644 --- a/test/interface-ipfs-core/diag.spec.js +++ b/test/interface-ipfs-core/diag.spec.js @@ -1,12 +1,29 @@ /* eslint-env mocha */ -/* globals apiClients */ 'use strict' +const FactoryClient = require('../factory/factory-client') const expect = require('chai').expect describe('.diag', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + + after((done) => { + fc.dismantle(done) + }) + it('.diag.net', (done) => { - apiClients.a.diag.net((err, res) => { + ipfs.diag.net((err, res) => { expect(err).to.not.exist expect(res).to.exist done() @@ -14,7 +31,7 @@ describe('.diag', () => { }) it('.diag.sys', (done) => { - apiClients.a.diag.sys((err, res) => { + ipfs.diag.sys((err, res) => { expect(err).to.not.exist expect(res).to.exist expect(res).to.have.a.property('memory') @@ -24,7 +41,7 @@ describe('.diag', () => { }) it('.diag.cmds', (done) => { - apiClients.a.diag.cmds((err, res) => { + ipfs.diag.cmds((err, res) => { expect(err).to.not.exist expect(res).to.exist done() @@ -33,14 +50,14 @@ describe('.diag', () => { describe('promise', () => { it('.diag.net', () => { - return apiClients.a.diag.net() + return ipfs.diag.net() .then((res) => { expect(res).to.exist }) }) it('.diag.sys', () => { - return apiClients.a.diag.sys() + return ipfs.diag.sys() .then((res) => { expect(res).to.exist expect(res).to.have.a.property('memory') @@ -49,7 +66,7 @@ describe('.diag', () => { }) it('.diag.cmds', () => { - return apiClients.a.diag.cmds() + return ipfs.diag.cmds() .then((res) => { expect(res).to.exist }) diff --git a/test/interface-ipfs-core/files.spec.js b/test/interface-ipfs-core/files.spec.js index d40187b6a..bed5c1506 100644 --- a/test/interface-ipfs-core/files.spec.js +++ b/test/interface-ipfs-core/files.spec.js @@ -1,6 +1,5 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 8] */ -/* globals apiClients */ 'use strict' const expect = require('chai').expect @@ -28,10 +27,27 @@ test.files(common) // mfs tests describe('.files (pseudo mfs)', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + 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' - apiClients.a.files.add(testfile, (err, res) => { + ipfs.files.add(testfile, (err, res) => { expect(err).to.not.exist expect(res).to.have.length(1) @@ -42,14 +58,14 @@ describe('.files (pseudo mfs)', () => { }) it('files.mkdir', (done) => { - apiClients.a.files.mkdir('/test-folder', function (err) { + ipfs.files.mkdir('/test-folder', function (err) { expect(err).to.not.exist done() }) }) it('files.cp', (done) => { - apiClients.a.files + ipfs.files .cp(['/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', '/test-folder/test-file'], (err) => { expect(err).to.not.exist done() @@ -57,7 +73,7 @@ describe('.files (pseudo mfs)', () => { }) it('files.ls', (done) => { - apiClients.a.files.ls('/test-folder', (err, res) => { + ipfs.files.ls('/test-folder', (err, res) => { expect(err).to.not.exist expect(res.Entries.length).to.equal(1) done() @@ -65,11 +81,11 @@ describe('.files (pseudo mfs)', () => { }) it('files.write', (done) => { - apiClients.a.files + ipfs.files .write('/test-folder/test-file-2.txt', new Buffer('hello world'), {create: true}, (err) => { expect(err).to.not.exist - apiClients.a.files.read('/test-folder/test-file-2.txt', (err, stream) => { + ipfs.files.read('/test-folder/test-file-2.txt', (err, stream) => { expect(err).to.not.exist let buf = '' @@ -89,11 +105,11 @@ describe('.files (pseudo mfs)', () => { }) it('files.write without options', (done) => { - apiClients.a.files + ipfs.files .write('/test-folder/test-file-2.txt', new Buffer('hello world'), (err) => { expect(err).to.not.exist - apiClients.a.files.read('/test-folder/test-file-2.txt', (err, stream) => { + ipfs.files.read('/test-folder/test-file-2.txt', (err, stream) => { expect(err).to.not.exist let buf = '' @@ -113,7 +129,7 @@ describe('.files (pseudo mfs)', () => { }) it('files.stat', (done) => { - apiClients.a.files.stat('/test-folder/test-file', (err, res) => { + ipfs.files.stat('/test-folder/test-file', (err, res) => { expect(err).to.not.exist expect(res).to.deep.equal({ Hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', @@ -128,7 +144,7 @@ describe('.files (pseudo mfs)', () => { }) it('files.stat file that does not exist', (done) => { - apiClients.a.files.stat('/test-folder/does-not-exist', (err, res) => { + ipfs.files.stat('/test-folder/does-not-exist', (err, res) => { expect(err).to.exist if (err.code === 0) { return done() @@ -142,7 +158,7 @@ describe('.files (pseudo mfs)', () => { return done() } - apiClients.a.files.read('/test-folder/test-file', (err, stream) => { + ipfs.files.read('/test-folder/test-file', (err, stream) => { expect(err).to.not.exist let buf = '' stream @@ -160,14 +176,14 @@ describe('.files (pseudo mfs)', () => { }) it('files.rm without options', (done) => { - apiClients.a.files.rm('/test-folder/test-file-2.txt', (err) => { + ipfs.files.rm('/test-folder/test-file-2.txt', (err) => { expect(err).to.not.exist done() }) }) it('files.rm', (done) => { - apiClients.a.files.rm('/test-folder', {recursive: true}, (err) => { + ipfs.files.rm('/test-folder', {recursive: true}, (err) => { expect(err).to.not.exist done() }) @@ -175,26 +191,26 @@ describe('.files (pseudo mfs)', () => { describe('promise', () => { it('files.mkdir', () => { - return apiClients.a.files.mkdir('/test-folder') + return ipfs.files.mkdir('/test-folder') }) it('files.cp', () => { - return apiClients.a.files + return ipfs.files .cp(['/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', '/test-folder/test-file']) }) it('files.ls', () => { - return apiClients.a.files.ls('/test-folder') + return ipfs.files.ls('/test-folder') .then((res) => { expect(res.Entries.length).to.equal(1) }) }) it('files.write', (done) => { - apiClients.a.files + ipfs.files .write('/test-folder/test-file-2.txt', new Buffer('hello world'), {create: true}) .then(() => { - return apiClients.a.files.read('/test-folder/test-file-2.txt') + return ipfs.files.read('/test-folder/test-file-2.txt') }) .then((stream) => { let buf = '' @@ -214,10 +230,10 @@ describe('.files (pseudo mfs)', () => { }) it('files.write without options', (done) => { - apiClients.a.files + ipfs.files .write('/test-folder/test-file-2.txt', new Buffer('hello world')) .then(() => { - return apiClients.a.files.read('/test-folder/test-file-2.txt') + return ipfs.files.read('/test-folder/test-file-2.txt') }) .then((stream) => { let buf = '' @@ -237,7 +253,7 @@ describe('.files (pseudo mfs)', () => { }) it('files.stat', () => { - return apiClients.a.files.stat('/test-folder/test-file') + return ipfs.files.stat('/test-folder/test-file') .then((res) => { expect(res).to.deep.equal({ Hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', @@ -250,7 +266,7 @@ describe('.files (pseudo mfs)', () => { }) it('files.stat file that does not exist', () => { - return apiClients.a.files.stat('/test-folder/does-not-exist') + return ipfs.files.stat('/test-folder/does-not-exist') .catch((err) => { expect(err).to.exist expect(err.code).to.be.eql(0) @@ -262,7 +278,7 @@ describe('.files (pseudo mfs)', () => { return done() } - apiClients.a.files.read('/test-folder/test-file') + ipfs.files.read('/test-folder/test-file') .then((stream) => { let buf = '' stream @@ -280,11 +296,11 @@ describe('.files (pseudo mfs)', () => { }) it('files.rm without options', () => { - return apiClients.a.files.rm('/test-folder/test-file-2.txt') + return ipfs.files.rm('/test-folder/test-file-2.txt') }) it('files.rm', () => { - return apiClients.a.files.rm('/test-folder', {recursive: true}) + return ipfs.files.rm('/test-folder', {recursive: true}) }) }) }) diff --git a/test/interface-ipfs-core/get.spec.js b/test/interface-ipfs-core/get.spec.js index 2bf1f9f73..b989811c7 100644 --- a/test/interface-ipfs-core/get.spec.js +++ b/test/interface-ipfs-core/get.spec.js @@ -1,6 +1,5 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 8] */ -/* globals apiClients */ 'use strict' @@ -11,6 +10,7 @@ const concat = require('concat-stream') const through = require('through2') const streamEqual = require('stream-equal') const path = require('path') +const FactoryClient = require('../factory/factory-client') const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt')) @@ -22,8 +22,38 @@ if (isNode) { } describe('.get', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + 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('get with no compression args', (done) => { - apiClients.a + ipfs .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { expect(err).to.not.exist @@ -46,7 +76,7 @@ describe('.get', () => { }) it('get with archive true', (done) => { - apiClients.a + ipfs .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {archive: true}, (err, res) => { expect(err).to.not.exist @@ -69,7 +99,7 @@ describe('.get', () => { }) it('get err with out of range compression level', (done) => { - apiClients.a + ipfs .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {compress: true, 'compression-level': 10}, (err, res) => { expect(err).to.exist expect(err.toString()).to.equal('Error: Compression level must be between 1 and 9') @@ -78,7 +108,7 @@ describe('.get', () => { }) it('get with compression level', (done) => { - apiClients.a + ipfs .get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {compress: true, 'compression-level': 1}, (err, res) => { expect(err).to.not.exist done() @@ -93,7 +123,7 @@ describe('.get', () => { const bigFile = fs.readFileSync(tfbPath) const expectedMultihash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq' - apiClients.a.files.add(bigFile, (err, res) => { + ipfs.files.add(bigFile, (err, res) => { expect(err).to.not.exist expect(res).to.have.length(1) @@ -108,7 +138,7 @@ describe('.get', () => { return done() } - apiClients.a.get('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, files) => { + ipfs.get('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, files) => { expect(err).to.not.exist files.on('data', (file) => { @@ -124,7 +154,7 @@ describe('.get', () => { describe('promise', () => { it('get', (done) => { - apiClients.a.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') + ipfs.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') .then((files) => { files.on('data', (file) => { let buf = '' diff --git a/test/interface-ipfs-core/id.spec.js b/test/interface-ipfs-core/id.spec.js index 26187e67c..936910853 100644 --- a/test/interface-ipfs-core/id.spec.js +++ b/test/interface-ipfs-core/id.spec.js @@ -1,12 +1,29 @@ /* eslint-env mocha */ -/* globals apiClients */ 'use strict' const expect = require('chai').expect +const FactoryClient = require('../factory/factory-client') describe('.id', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + + after((done) => { + fc.dismantle(done) + }) + it('id', (done) => { - apiClients.a.id((err, res) => { + ipfs.id((err, res) => { expect(err).to.not.exist expect(res).to.have.a.property('ID') expect(res).to.have.a.property('PublicKey') @@ -16,7 +33,7 @@ describe('.id', () => { describe('promise', () => { it('id', () => { - return apiClients.a.id() + return ipfs.id() .then((res) => { expect(res).to.have.a.property('ID') expect(res).to.have.a.property('PublicKey') diff --git a/test/interface-ipfs-core/index.spec.js b/test/interface-ipfs-core/index.spec.js deleted file mode 100644 index a92b33245..000000000 --- a/test/interface-ipfs-core/index.spec.js +++ /dev/null @@ -1,12 +0,0 @@ -/* eslint-env mocha */ -/* globals apiClients */ -'use strict' - -const expect = require('chai').expect - -describe('API', () => { - it('has the api object', () => { - expect(apiClients.a).to.exist - expect(apiClients.a).to.have.a.property('id') - }) -}) diff --git a/test/interface-ipfs-core/log.spec.js b/test/interface-ipfs-core/log.spec.js index 65ac3c0ac..11710b780 100644 --- a/test/interface-ipfs-core/log.spec.js +++ b/test/interface-ipfs-core/log.spec.js @@ -1,42 +1,54 @@ /* eslint-env mocha */ /* eslint max-nested-callbacks: ["error", 8] */ -/* globals apiClients */ 'use strict' const expect = require('chai').expect const isNode = require('detect-node') +const FactoryClient = require('../factory/factory-client') // For some reason these tests time out in PhantomJS so we need to skip them const isPhantom = !isNode && typeof navigator !== 'undefined' && navigator.userAgent.match(/PhantomJS/) -describe('.log', () => { - it('.log.tail', (done) => { - if (isPhantom) { - return done() - } - const req = apiClients.a.log.tail((err, res) => { - expect(err).to.not.exist - expect(req).to.exist - - res.once('data', (obj) => { - expect(obj).to.be.an('object') +if (!isPhantom) { + describe('.log', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node done() }) }) - }) - describe('promise', () => { - it('.log.tail', () => { - if (isPhantom) { - return - } + after((done) => { + fc.dismantle(done) + }) - return apiClients.a.log.tail() - .then((res) => { - res.once('data', (obj) => { - expect(obj).to.be.an('object') - }) + it('.log.tail', (done) => { + const req = ipfs.log.tail((err, res) => { + expect(err).to.not.exist + expect(req).to.exist + + res.once('data', (obj) => { + expect(obj).to.be.an('object') + done() }) + }) + }) + + describe('promise', () => { + it('.log.tail', () => { + return ipfs.log.tail() + .then((res) => { + res.once('data', (obj) => { + expect(obj).to.be.an('object') + }) + }) + }) }) }) -}) +} diff --git a/test/interface-ipfs-core/ls.spec.js b/test/interface-ipfs-core/ls.spec.js index 2dbcd075a..43082cb66 100644 --- a/test/interface-ipfs-core/ls.spec.js +++ b/test/interface-ipfs-core/ls.spec.js @@ -1,15 +1,33 @@ /* eslint-env mocha */ -/* globals apiClients */ 'use strict' const expect = require('chai').expect const isNode = require('detect-node') - +const FactoryClient = require('../factory/factory-client') describe('ls', function () { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + + after((done) => { + fc.dismantle(done) + }) + it('should correctly retrieve links', function (done) { - if (!isNode) return done() + if (!isNode) { + return done() + } - apiClients.a.ls('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG', (err, res) => { + ipfs.ls('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG', (err, res) => { expect(err).to.not.exist expect(res).to.have.a.property('Objects') @@ -20,7 +38,7 @@ describe('ls', function () { }) it('should correctly handle a nonexisting hash', function (done) { - apiClients.a.ls('surelynotavalidhashheh?', (err, res) => { + ipfs.ls('surelynotavalidhashheh?', (err, res) => { expect(err).to.exist expect(res).to.not.exist done() @@ -30,7 +48,7 @@ describe('ls', function () { it('should correctly handle a nonexisting path', function (done) { if (!isNode) return done() - apiClients.a.ls('QmRNjDeKStKGTQXnJ2NFqeQ9oW/folder_that_isnt_there', (err, res) => { + ipfs.ls('QmRNjDeKStKGTQXnJ2NFqeQ9oW/folder_that_isnt_there', (err, res) => { expect(err).to.exist expect(res).to.not.exist done() @@ -41,7 +59,7 @@ describe('ls', function () { it('should correctly retrieve links', () => { if (!isNode) return - return apiClients.a.ls('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG') + return ipfs.ls('QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG') .then((res) => { expect(res).to.have.a.property('Objects') expect(res.Objects[0]).to.have.a.property('Links') @@ -50,7 +68,7 @@ describe('ls', function () { }) it('should correctly handle a nonexisting hash', () => { - return apiClients.a.ls('surelynotavalidhashheh?') + return ipfs.ls('surelynotavalidhashheh?') .catch((err) => { expect(err).to.exist }) @@ -59,7 +77,7 @@ describe('ls', function () { it('should correctly handle a nonexisting path', () => { if (!isNode) return - return apiClients.a.ls('QmRNjDeKStKGTQXnJ3NFqeQ9oW/folder_that_isnt_there') + return ipfs.ls('QmRNjDeKStKGTQXnJ3NFqeQ9oW/folder_that_isnt_there') .catch((err) => { expect(err).to.exist }) diff --git a/test/interface-ipfs-core/name.spec.js b/test/interface-ipfs-core/name.spec.js index 6cfd93788..4e3e9194f 100644 --- a/test/interface-ipfs-core/name.spec.js +++ b/test/interface-ipfs-core/name.spec.js @@ -3,10 +3,27 @@ 'use strict' const expect = require('chai').expect +const fs = require('fs') +const path = require('path') + +const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt')) describe('.name', () => { let name + it('add file for testing', (done) => { + const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' + + apiClients.a.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('.name.publish', (done) => { apiClients.a.name.publish('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { expect(err).to.not.exist diff --git a/test/interface-ipfs-core/pin.spec.js b/test/interface-ipfs-core/pin.spec.js index 56be6bf2a..ead1db451 100644 --- a/test/interface-ipfs-core/pin.spec.js +++ b/test/interface-ipfs-core/pin.spec.js @@ -1,39 +1,49 @@ /* eslint-env mocha */ -/* globals apiClients */ 'use strict' const expect = require('chai').expect +const FactoryClient = require('../factory/factory-client') +const fs = require('fs') +const path = require('path') + +const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt')) describe('.pin', () => { - it('.pin.add', (done) => { - apiClients.b.pin.add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, (err, res) => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { expect(err).to.not.exist - expect(res.Pins[0]).to.be.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') + ipfs = node done() }) }) - it('.pin.list', (done) => { - apiClients.b.pin.list((err, res) => { - expect(err).to.not.exist - expect(res).to.exist - done() - }) + after((done) => { + fc.dismantle(done) }) - it('.pin.list hash', (done) => { - apiClients.b.pin.list({hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'}, (err, res) => { + it('add file for testing', (done) => { + const expectedMultihash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' + + ipfs.files.add(testfile, (err, res) => { expect(err).to.not.exist - expect(res).to.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) => { - apiClients.b.pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}, (err, res) => { + ipfs.pin.remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: true}, (err, res) => { expect(err).to.not.exist expect(res).to.exist - apiClients.b.pin.list('direct', (err, res) => { + ipfs.pin.list('direct', (err, res) => { expect(err).to.not.exist expect(res).to.exist expect(res.Keys).to.be.empty @@ -42,9 +52,33 @@ describe('.pin', () => { }) }) + 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 apiClients.b.pin + return ipfs.pin .add('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}) .then((res) => { expect(res.Pins[0]).to.be.equal('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') @@ -52,14 +86,14 @@ describe('.pin', () => { }) it('.pin.list', () => { - return apiClients.b.pin.list() + return ipfs.pin.list() .then((res) => { expect(res).to.exist }) }) it('.pin.list hash', () => { - return apiClients.b.pin.list({ + return ipfs.pin.list({ hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP' }) .then((res) => { @@ -68,11 +102,11 @@ describe('.pin', () => { }) it('.pin.remove', () => { - return apiClients.b.pin + return ipfs.pin .remove('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {recursive: false}) .then((res) => { expect(res).to.exist - return apiClients.b.pin.list('direct') + return ipfs.pin.list('direct') }) .then((res) => { expect(res).to.exist diff --git a/test/interface-ipfs-core/refs.spec.js b/test/interface-ipfs-core/refs.spec.js index d8dad1345..e687290e6 100644 --- a/test/interface-ipfs-core/refs.spec.js +++ b/test/interface-ipfs-core/refs.spec.js @@ -1,11 +1,28 @@ /* eslint-env mocha */ -/* globals apiClients */ 'use strict' const expect = require('chai').expect const isNode = require('detect-node') +const FactoryClient = require('../factory/factory-client') describe('.refs', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + + after((done) => { + fc.dismantle(done) + }) + const folder = 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG' const result = [{ Ref: 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG QmZTR5bcpQD7cFgTorqxZDYaew1Wqgfbd2ud9QqGPAkK2V about', @@ -32,7 +49,7 @@ describe('.refs', () => { return done() } - apiClients.a.refs(folder, {format: ' '}, (err, objs) => { + ipfs.refs(folder, {format: ' '}, (err, objs) => { expect(err).to.not.exist expect(objs).to.eql(result) @@ -42,9 +59,11 @@ describe('.refs', () => { describe('promise', () => { it('refs', () => { - if (!isNode) return + if (!isNode) { + return + } - return apiClients.a.refs(folder, {format: ' '}) + return ipfs.refs(folder, {format: ' '}) .then((objs) => { expect(objs).to.eql(result) }) diff --git a/test/interface-ipfs-core/repo.spec.js b/test/interface-ipfs-core/repo.spec.js index 6930bcc4f..778e06426 100644 --- a/test/interface-ipfs-core/repo.spec.js +++ b/test/interface-ipfs-core/repo.spec.js @@ -1,12 +1,29 @@ /* eslint-env mocha */ -/* globals apiClients */ 'use strict' +const FactoryClient = require('../factory/factory-client') const expect = require('chai').expect describe('.repo', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + + after((done) => { + fc.dismantle(done) + }) + it('.repo.gc', (done) => { - apiClients.a.repo.gc((err, res) => { + ipfs.repo.gc((err, res) => { expect(err).to.not.exist expect(res).to.exist done() @@ -14,7 +31,7 @@ describe('.repo', () => { }) it('.repo.stat', (done) => { - apiClients.a.repo.stat((err, res) => { + ipfs.repo.stat((err, res) => { expect(err).to.not.exist expect(res).to.exist expect(res).to.have.a.property('NumObjects') @@ -25,14 +42,14 @@ describe('.repo', () => { describe('promise', () => { it('.repo.gc', () => { - return apiClients.a.repo.gc() + return ipfs.repo.gc() .then((res) => { expect(res).to.exist }) }) it('.repo.stat', () => { - return apiClients.a.repo.stat() + return ipfs.repo.stat() .then((res) => { expect(res).to.exist expect(res).to.have.a.property('NumObjects') diff --git a/test/interface-ipfs-core/send.spec.js b/test/interface-ipfs-core/send.spec.js deleted file mode 100644 index 9714a72d3..000000000 --- a/test/interface-ipfs-core/send.spec.js +++ /dev/null @@ -1,6 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -describe('.send', () => { - it('used by every command') -}) diff --git a/test/interface-ipfs-core/version.spec.js b/test/interface-ipfs-core/version.spec.js index c7b9b0298..c07ed5599 100644 --- a/test/interface-ipfs-core/version.spec.js +++ b/test/interface-ipfs-core/version.spec.js @@ -1,14 +1,31 @@ /* eslint-env mocha */ -/* globals apiClients */ 'use strict' const expect = require('chai').expect +const FactoryClient = require('../factory/factory-client') describe('.version', () => { + let ipfs + let fc + + before(function (done) { + this.timeout(20 * 1000) // slow CI + fc = new FactoryClient() + fc.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + + after((done) => { + fc.dismantle(done) + }) + // note, IPFS HTTP-API returns always the same object, the filtering // happens on the CLI it('checks the version', (done) => { - apiClients.a.version((err, res) => { + ipfs.version((err, res) => { expect(err).to.not.exist expect(res).to.have.a.property('Version') expect(res).to.have.a.property('Commit') @@ -18,7 +35,7 @@ describe('.version', () => { }) it('with number option', (done) => { - apiClients.a.version({number: true}, (err, res) => { + ipfs.version({number: true}, (err, res) => { expect(err).to.not.exist expect(res).to.have.a.property('Version') expect(res).to.have.a.property('Commit') @@ -28,7 +45,7 @@ describe('.version', () => { }) it('with commit option', (done) => { - apiClients.a.version({commit: true}, (err, res) => { + ipfs.version({commit: true}, (err, res) => { expect(err).to.not.exist expect(res).to.have.a.property('Version') expect(res).to.have.a.property('Commit') @@ -38,7 +55,7 @@ describe('.version', () => { }) it('with repo option', (done) => { - apiClients.a.version({commit: true}, (err, res) => { + ipfs.version({commit: true}, (err, res) => { expect(err).to.not.exist expect(res).to.have.a.property('Version') expect(res).to.have.a.property('Commit') @@ -49,7 +66,7 @@ describe('.version', () => { describe('promise', () => { it('checks the version', () => { - return apiClients.a.version() + return ipfs.version() .then((res) => { expect(res).to.have.a.property('Version') expect(res).to.have.a.property('Commit') @@ -58,7 +75,7 @@ describe('.version', () => { }) it('with number option', () => { - return apiClients.a.version({number: true}) + return ipfs.version({number: true}) .then((res) => { expect(res).to.have.a.property('Version') expect(res).to.have.a.property('Commit') diff --git a/test/tmp-disposable-nodes-addrs.json b/test/tmp-disposable-nodes-addrs.json new file mode 100644 index 000000000..851a00021 --- /dev/null +++ b/test/tmp-disposable-nodes-addrs.json @@ -0,0 +1 @@ +{"c":"/ip4/127.0.0.1/tcp/56459","a":"/ip4/127.0.0.1/tcp/56466","b":"/ip4/127.0.0.1/tcp/56479"} \ No newline at end of file