diff --git a/.gitignore b/.gitignore index 510263842..b4c23c2fb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ test/setup/tmp-disposable-nodes-addrs.json dist coverage **/*.swp +examples/sub-module/**/bundle.js +examples/sub-module/**/*-minified.js +examples/sub-module/*-bundle.js diff --git a/README.md b/README.md index cc3b8503c..59deaa913 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ ipfs-api - [Install](#install) - [Running the daemon with the right port](#running-the-daemon-with-the-right-port) - [Importing the module and usage](#importing-the-module-and-usage) + - [Importing a sub-module and usage](#importing-a-sub-module-and-usage) - [In a web browser through Browserify](#in-a-web-browser-through-browserify) - [In a web browser from CDN](#in-a-web-browser-from-cdn) - [CORS](#cors) @@ -74,6 +75,14 @@ var ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/5001') // or using options var ipfs = ipfsAPI({host: 'localhost', port: '5001', protocol: 'http'}) ``` +### Importing a sub-module and usage +```javascript +const bitswap = require('ipfs-api/src/bitswap')('/ip4/127.0.0.1/tcp/5001') + +bitswap.unwant(key, (err) => { + // ... +} +``` ### In a web browser through Browserify @@ -224,12 +233,15 @@ This means: - [`ipfs.id([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#id) - [`ipfs.version([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#version) - [`ipfs.ping()`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#ping) -- [`ipfs.log()`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic#log) -#### [key](https://github.com/ipfs/interface-ipfs-core/tree/master/API/key) +- log: + - [`ipfs.log.ls([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic) + - [`ipfs.log.tail([callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic) + - [`ipfs.log.level(subsystem, level, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/generic) -- [`ipfs.key.gen(name, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/key#gen) -- [`ipfs.key.list([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/key#list) +- key: + - [`ipfs.key.gen(name, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/key#gen) + - [`ipfs.key.list([options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/API/key#list) ##### [name](https://github.com/ipfs/interface-ipfs-core/tree/master/API/name) diff --git a/examples/sub-module/bundles-size-KBs.csv b/examples/sub-module/bundles-size-KBs.csv new file mode 100644 index 000000000..8101c63cd --- /dev/null +++ b/examples/sub-module/bundles-size-KBs.csv @@ -0,0 +1,28 @@ +name, bundled (KBs), minified (KBs) +IPFS, 1685.41, 661.64 +add, 968.11, 344.13 +bitswap, 684.80, 232.13 +block, 724.77, 250.23 +bootstrap, 685.64, 232.39 +cat, 725.05, 250.33 +commands, 683.89, 231.76 +config, 686.87, 233.10 +dht, 688.51, 233.41 +diag, 684.97, 232.18 +files, 1120.38, 404.30 +get, 907.74, 318.35 +id, 684.31, 231.95 +key, 684.59, 232.03 +log, 685.40, 232.38 +ls, 684.00, 231.80 +mount, 684.18, 231.86 +name, 684.63, 232.06 +object, 923.66, 340.57 +pin, 685.51, 232.39 +ping, 684.59, 231.90 +pubsub, 740.40, 249.54 +refs, 684.39, 231.94 +repo, 684.56, 232.02 +swarm, 1324.18, 527.03 +update, 684.45, 231.96 +version, 684.21, 231.88 diff --git a/examples/sub-module/complete-module.js b/examples/sub-module/complete-module.js new file mode 100644 index 000000000..c6a3c99e9 --- /dev/null +++ b/examples/sub-module/complete-module.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../src')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/add/add.js b/examples/sub-module/modules/add/add.js new file mode 100644 index 000000000..cbb27a7e1 --- /dev/null +++ b/examples/sub-module/modules/add/add.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/add')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/bitswap/bitswap.js b/examples/sub-module/modules/bitswap/bitswap.js new file mode 100644 index 000000000..ed4c91068 --- /dev/null +++ b/examples/sub-module/modules/bitswap/bitswap.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/bitswap')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/block/block.js b/examples/sub-module/modules/block/block.js new file mode 100644 index 000000000..587bf3cf9 --- /dev/null +++ b/examples/sub-module/modules/block/block.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/block')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/bootstrap/bootstrap.js b/examples/sub-module/modules/bootstrap/bootstrap.js new file mode 100644 index 000000000..19c78b4c4 --- /dev/null +++ b/examples/sub-module/modules/bootstrap/bootstrap.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/bootstrap')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/cat/cat.js b/examples/sub-module/modules/cat/cat.js new file mode 100644 index 000000000..079aaf908 --- /dev/null +++ b/examples/sub-module/modules/cat/cat.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/cat')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/commands/commands.js b/examples/sub-module/modules/commands/commands.js new file mode 100644 index 000000000..2a1286f35 --- /dev/null +++ b/examples/sub-module/modules/commands/commands.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/commands')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/config/config.js b/examples/sub-module/modules/config/config.js new file mode 100644 index 000000000..e49c0d49a --- /dev/null +++ b/examples/sub-module/modules/config/config.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/config')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/dht/dht.js b/examples/sub-module/modules/dht/dht.js new file mode 100644 index 000000000..4ea8b78bc --- /dev/null +++ b/examples/sub-module/modules/dht/dht.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/dht')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/diag/diag.js b/examples/sub-module/modules/diag/diag.js new file mode 100644 index 000000000..f4be953be --- /dev/null +++ b/examples/sub-module/modules/diag/diag.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/diag')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/files/files.js b/examples/sub-module/modules/files/files.js new file mode 100644 index 000000000..85de08a17 --- /dev/null +++ b/examples/sub-module/modules/files/files.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/files')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/get/get.js b/examples/sub-module/modules/get/get.js new file mode 100644 index 000000000..9fee179ff --- /dev/null +++ b/examples/sub-module/modules/get/get.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/get')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/id/id.js b/examples/sub-module/modules/id/id.js new file mode 100644 index 000000000..046ebac9e --- /dev/null +++ b/examples/sub-module/modules/id/id.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/id')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/key/key.js b/examples/sub-module/modules/key/key.js new file mode 100644 index 000000000..875122892 --- /dev/null +++ b/examples/sub-module/modules/key/key.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/key')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/log/log.js b/examples/sub-module/modules/log/log.js new file mode 100644 index 000000000..91e436bd8 --- /dev/null +++ b/examples/sub-module/modules/log/log.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/log')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/ls/ls.js b/examples/sub-module/modules/ls/ls.js new file mode 100644 index 000000000..b134b37fc --- /dev/null +++ b/examples/sub-module/modules/ls/ls.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/ls')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/mount/mount.js b/examples/sub-module/modules/mount/mount.js new file mode 100644 index 000000000..9facf27a6 --- /dev/null +++ b/examples/sub-module/modules/mount/mount.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/mount')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/name/name.js b/examples/sub-module/modules/name/name.js new file mode 100644 index 000000000..c8dcacc6e --- /dev/null +++ b/examples/sub-module/modules/name/name.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/name')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/object/object.js b/examples/sub-module/modules/object/object.js new file mode 100644 index 000000000..0ea29a8a8 --- /dev/null +++ b/examples/sub-module/modules/object/object.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/object')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/pin/pin.js b/examples/sub-module/modules/pin/pin.js new file mode 100644 index 000000000..1ce452cb8 --- /dev/null +++ b/examples/sub-module/modules/pin/pin.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/pin')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/ping/ping.js b/examples/sub-module/modules/ping/ping.js new file mode 100644 index 000000000..6f849708b --- /dev/null +++ b/examples/sub-module/modules/ping/ping.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/ping')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/pubsub/pubsub.js b/examples/sub-module/modules/pubsub/pubsub.js new file mode 100644 index 000000000..4a4ca8656 --- /dev/null +++ b/examples/sub-module/modules/pubsub/pubsub.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/pubsub')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/refs/refs.js b/examples/sub-module/modules/refs/refs.js new file mode 100644 index 000000000..25034ba4f --- /dev/null +++ b/examples/sub-module/modules/refs/refs.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/refs')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/repo/repo.js b/examples/sub-module/modules/repo/repo.js new file mode 100644 index 000000000..552b2891e --- /dev/null +++ b/examples/sub-module/modules/repo/repo.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/repo')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/swarm/swarm.js b/examples/sub-module/modules/swarm/swarm.js new file mode 100644 index 000000000..627649c14 --- /dev/null +++ b/examples/sub-module/modules/swarm/swarm.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/swarm')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/update/update.js b/examples/sub-module/modules/update/update.js new file mode 100644 index 000000000..948ac29e6 --- /dev/null +++ b/examples/sub-module/modules/update/update.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/update')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/modules/version/version.js b/examples/sub-module/modules/version/version.js new file mode 100644 index 000000000..21a47fdf1 --- /dev/null +++ b/examples/sub-module/modules/version/version.js @@ -0,0 +1,3 @@ +'use strict' + +require('../../../../src/version')('/ip4/127.0.0.1/tcp/5001') diff --git a/examples/sub-module/package.json b/examples/sub-module/package.json new file mode 100644 index 000000000..5cdec6d0f --- /dev/null +++ b/examples/sub-module/package.json @@ -0,0 +1,17 @@ +{ + "name": "sub-module", + "version": "1.0.0", + "description": "", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Nuno Nogueira", + "license": "MIT", + "devDependencies": { + "babel-core": "^6.25.0", + "babel-loader": "^7.1.0", + "babel-preset-env": "^1.5.2", + "babili": "^0.1.4", + "webpack": "^3.0.0" + } +} diff --git a/examples/sub-module/test-modules-size.sh b/examples/sub-module/test-modules-size.sh new file mode 100644 index 000000000..ef9955438 --- /dev/null +++ b/examples/sub-module/test-modules-size.sh @@ -0,0 +1,26 @@ +#!/bin/sh +set -e + +modules=($(ls modules/)) + +echo "name, bundled (KBs), minified (KBs)" + +# Full IPFS module +webpack --display none --config webpack.confg.js complete-module.js complete-bundle.js +babili complete-bundle.js -o complete-bundle-minified.js + +ipfsBundleSize=($(wc -c < complete-bundle.js | awk '{b=$1/1024; printf "%.2f\n", b}' | sed 's/,/./g')) +ipfsMinSize=($(wc -c < complete-bundle-minified.js | awk '{b=$1/1024; printf "%.2f\n", b}' | sed 's/,/./g')) + +echo IPFS, $ipfsBundleSize, $ipfsMinSize + +for module in "${modules[@]}" +do + moduledir="modules/$module" + webpack --display none --config webpack.config.js $moduledir/$module.js $moduledir/bundle.js + babili $moduledir/bundle.js -o $moduledir/bundle-minified.js + + bundlesize=($(wc -c < $moduledir/bundle.js | awk '{b=$1/1024; printf "%.2f\n", b}' | sed 's/,/./g')) + minsize=($(wc -c < $moduledir/bundle-minified.js | awk '{b=$1/1024; printf "%.2f\n", b}' | sed 's/,/./g')) + echo $module, $bundlesize, $minsize +done diff --git a/examples/sub-module/webpack.config.js b/examples/sub-module/webpack.config.js new file mode 100644 index 000000000..7124eb3db --- /dev/null +++ b/examples/sub-module/webpack.config.js @@ -0,0 +1,15 @@ +'use strict' + +module.exports = { + module: { + loaders: [{ + test: /\.js$/, + loaders: ['babel-loader'] + }] + }, + node: { + fs: 'empty', + net: 'empty', + tls: 'empty' + } +} diff --git a/src/add.js b/src/add.js new file mode 100644 index 000000000..07915370e --- /dev/null +++ b/src/add.js @@ -0,0 +1,9 @@ +'use strict' + +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return require('./files/add')(send) +} diff --git a/src/api/bitswap.js b/src/api/bitswap.js deleted file mode 100644 index 98704caca..000000000 --- a/src/api/bitswap.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - wantlist: promisify((callback) => { - send({ - path: 'bitswap/wantlist' - }, callback) - }), - stat: promisify((callback) => { - send({ - path: 'bitswap/stat' - }, callback) - }), - unwant: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'bitswap/unwant', - args: args, - qs: opts - }, callback) - }) - } -} diff --git a/src/api/block.js b/src/api/block.js deleted file mode 100644 index 910fed3db..000000000 --- a/src/api/block.js +++ /dev/null @@ -1,113 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') -const Block = require('ipfs-block') -const multihash = require('multihashes') -const CID = require('cids') -const streamToValue = require('../stream-to-value') - -module.exports = (send) => { - return { - get: promisify((args, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - - // TODO this needs to be adjusted with the new go-ipfs http-api - let cid - try { - if (CID.isCID(args)) { - cid = args - args = cid.toBaseEncodedString() - } else if (Buffer.isBuffer(args)) { - cid = new CID(args) - args = cid.toBaseEncodedString() - } else if (typeof args === 'string') { - cid = new CID(args) - } else { - return callback(new Error('invalid argument')) - } - } catch (err) { - return callback(err) - } - - // Transform the response from Buffer or a Stream to a Block - const transform = (res, callback) => { - if (Buffer.isBuffer(res)) { - callback(null, new Block(res, cid)) - } else { - streamToValue(res, (err, data) => { - if (err) { - return callback(err) - } - callback(null, new Block(data, cid)) - }) - } - } - - const request = { - path: 'block/get', - args: args, - qs: opts - } - - send.andTransform(request, transform, callback) - }), - stat: promisify((args, opts, callback) => { - // TODO this needs to be adjusted with the new go-ipfs http-api - if (args && CID.isCID(args)) { - args = multihash.toB58String(args.multihash) - } - - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - - const request = { - path: 'block/stat', - args: args, - qs: opts - } - - // Transform the response from { Key, Size } objects to { key, size } objects - const transform = (stats, callback) => { - callback(null, { - key: stats.Key, - size: stats.Size - }) - } - - send.andTransform(request, transform, callback) - }), - put: promisify((block, cid, callback) => { - // TODO this needs to be adjusted with the new go-ipfs http-api - if (typeof cid === 'function') { - callback = cid - cid = {} - } - - if (Array.isArray(block)) { - const err = new Error('block.put() only accepts 1 file') - return callback(err) - } - - if (typeof block === 'object' && block.data) { - block = block.data - } - - const request = { - path: 'block/put', - files: block - } - - // Transform the response to a Block - const transform = (info, callback) => { - callback(null, new Block(block, new CID(info.Key))) - } - - send.andTransform(request, transform, callback) - }) - } -} diff --git a/src/api/bootstrap.js b/src/api/bootstrap.js deleted file mode 100644 index 89f67a0aa..000000000 --- a/src/api/bootstrap.js +++ /dev/null @@ -1,70 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - add: promisify((args, opts, callback) => { - if (typeof opts === 'function' && - !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && - typeof callback === 'function') { - callback = opts - opts = {} - } - - if (args && typeof args === 'object') { - opts = args - args = undefined - } - - send({ - path: 'bootstrap/add', - args: args, - qs: opts - }, callback) - }), - rm: promisify((args, opts, callback) => { - if (typeof opts === 'function' && - !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && - typeof callback === 'function') { - callback = opts - opts = {} - } - - if (args && typeof args === 'object') { - opts = args - args = undefined - } - - send({ - path: 'bootstrap/rm', - args: args, - qs: opts - }, callback) - }), - list: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'bootstrap/list', - qs: opts - }, callback) - }) - } -} diff --git a/src/api/config.js b/src/api/config.js deleted file mode 100644 index 33aaa03cd..000000000 --- a/src/api/config.js +++ /dev/null @@ -1,78 +0,0 @@ -'use strict' - -const streamifier = require('streamifier') -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - get: promisify((key, callback) => { - if (typeof key === 'function') { - callback = key - key = undefined - } - - if (!key) { - send({ - path: 'config/show', - buffer: true - }, callback) - return - } - - send({ - path: 'config', - args: key, - buffer: true - }, (err, response) => { - if (err) { - return callback(err) - } - callback(null, response.Value) - }) - }), - set: promisify((key, value, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - if (typeof key !== 'string') { - return callback(new Error('Invalid key type')) - } - - if (typeof value !== 'object' && - typeof value !== 'boolean' && - typeof value !== 'string') { - return callback(new Error('Invalid value type')) - } - - if (typeof value === 'object') { - value = JSON.stringify(value) - opts = { json: true } - } - - if (typeof value === 'boolean') { - value = value.toString() - opts = { bool: true } - } - - send({ - path: 'config', - args: [key, value], - qs: opts, - files: undefined, - buffer: true - }, callback) - }), - replace: promisify((config, callback) => { - if (typeof config === 'object') { - config = streamifier.createReadStream(new Buffer(JSON.stringify(config))) - } - - send({ - path: 'config/replace', - files: config, - buffer: true - }, callback) - }) - } -} diff --git a/src/api/dht.js b/src/api/dht.js deleted file mode 100644 index 3f2ef6f4e..000000000 --- a/src/api/dht.js +++ /dev/null @@ -1,157 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') -const streamToValue = require('../stream-to-value') - -module.exports = (send) => { - return { - get: promisify((key, opts, callback) => { - if (typeof opts === 'function' && !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && typeof callback === 'function') { - callback = opts - opts = {} - } - - function 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')) - } - - // Inconsistent return values in the browser vs node - if (Array.isArray(res)) { - res = res[0] - } - - if (res.Type === 5) { - done(null, res.Extra) - } else { - let error = new Error('key was not found (type 6)') - done(error) - } - } - - send({ - path: 'dht/get', - args: key, - qs: opts - }, handleResult.bind(null, callback)) - }), - - put: promisify((key, value, opts, callback) => { - if (typeof opts === 'function' && !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && typeof callback === 'function') { - callback = opts - opts = {} - } - - send({ - path: 'dht/put', - args: [key, value], - qs: opts - }, callback) - }), - - findprovs: promisify((cid, opts, callback) => { - if (typeof opts === 'function' && !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && typeof callback === 'function') { - callback = opts - opts = {} - } - - send.andTransform({ - path: 'dht/findprovs', - args: cid, - qs: opts - }, streamToValue, callback) - }), - - findpeer: promisify((peerId, opts, callback) => { - if (typeof opts === 'function' && !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && typeof callback === 'function') { - callback = opts - opts = {} - } - - send.andTransform({ - path: 'dht/findpeer', - args: peerId, - qs: opts - }, streamToValue, callback) - }), - - provide: promisify((cids, opts, callback) => { - if (typeof opts === 'function' && !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && typeof callback === 'function') { - callback = opts - opts = {} - } - - if (!Array.isArray(cids)) { - cids = [cids] - } - - send({ - path: 'dht/provide', - args: cids, - qs: opts - }, callback) - }), - - // find closest peerId to given peerId - query: promisify((peerId, opts, callback) => { - if (typeof opts === 'function' && !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && typeof callback === 'function') { - callback = opts - opts = {} - } - - send.andTransform({ - path: 'dht/query', - args: peerId, - qs: opts - }, streamToValue, callback) - }) - } -} diff --git a/src/api/diag.js b/src/api/diag.js deleted file mode 100644 index 15ae62301..000000000 --- a/src/api/diag.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - net: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - - send({ - path: 'diag/net', - qs: opts - }, callback) - }), - sys: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - - send({ - path: 'diag/sys', - qs: opts - }, callback) - }), - cmds: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - - send({ - path: 'diag/cmds', - qs: opts - }, callback) - }) - } -} diff --git a/src/api/files.js b/src/api/files.js deleted file mode 100644 index 2fbf827eb..000000000 --- a/src/api/files.js +++ /dev/null @@ -1,118 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - cp: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'files/cp', - args: args, - qs: opts - }, callback) - }), - ls: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - return send({ - path: 'files/ls', - args: args, - qs: opts - }, callback) - }), - mkdir: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'files/mkdir', - args: args, - qs: opts - }, callback) - }), - stat: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'files/stat', - args: args, - qs: opts - }, callback) - }), - rm: promisify((path, opts, callback) => { - if (typeof opts === 'function' && - !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && - typeof callback === 'function') { - callback = opts - opts = {} - } - - send({ - path: 'files/rm', - args: path, - qs: opts - }, callback) - }), - read: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'files/read', - args: args, - qs: opts - }, callback) - }), - write: promisify((pathDst, files, opts, callback) => { - if (typeof opts === 'function' && - !callback) { - callback = opts - opts = {} - } - - // opts is the real callback -- - // 'callback' is being injected by promisify - if (typeof opts === 'function' && - typeof callback === 'function') { - callback = opts - opts = {} - } - - send({ - path: 'files/write', - args: pathDst, - qs: opts, - files: files - }, callback) - }), - mv: promisify((args, opts, callback) => { - if (typeof opts === 'function' && - callback === undefined) { - callback = opts - opts = {} - } - send({ - path: 'files/mv', - args: args, - qs: opts - }, callback) - }) - } -} diff --git a/src/api/key.js b/src/api/key.js deleted file mode 100644 index ae1a3686a..000000000 --- a/src/api/key.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - gen: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'key/gen', - args: args, - qs: opts - }, callback) - }), - list: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'key/list', - qs: opts - }, callback) - }) - } -} diff --git a/src/api/log.js b/src/api/log.js deleted file mode 100644 index 74c750d5e..000000000 --- a/src/api/log.js +++ /dev/null @@ -1,21 +0,0 @@ -'use strict' - -const pump = require('pump') -const ndjson = require('ndjson') -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - tail: promisify((callback) => { - return send({ - path: 'log/tail' - }, (err, response) => { - if (err) { - return callback(err) - } - const outputStream = pump(response, ndjson.parse()) - callback(null, outputStream) - }) - }) - } -} diff --git a/src/api/name.js b/src/api/name.js deleted file mode 100644 index 77fd39fe0..000000000 --- a/src/api/name.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - publish: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'name/publish', - args: args, - qs: opts - }, callback) - }), - resolve: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'name/resolve', - args: args, - qs: opts - }, callback) - }) - } -} diff --git a/src/api/object.js b/src/api/object.js deleted file mode 100644 index 64549dc95..000000000 --- a/src/api/object.js +++ /dev/null @@ -1,410 +0,0 @@ -'use strict' - -const dagPB = require('ipld-dag-pb') -const DAGNode = dagPB.DAGNode -const DAGLink = dagPB.DAGLink -const promisify = require('promisify-es6') -const bs58 = require('bs58') -const streamToValue = require('../stream-to-value') -const cleanMultihash = require('../clean-multihash') -const LRU = require('lru-cache') -const lruOptions = { - max: 128 -} -const Unixfs = require('ipfs-unixfs') -const cache = LRU(lruOptions) - -module.exports = (send) => { - const api = { - get: promisify((multihash, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} - } - - if (!options) { - options = {} - } - - try { - multihash = cleanMultihash(multihash, options) - } catch (err) { - return callback(err) - } - - const node = cache.get(multihash) - - if (node) { - return callback(null, node) - } - - send({ - path: 'object/get', - args: multihash - }, (err, result) => { - if (err) { - return callback(err) - } - - const links = result.Links.map((l) => { - return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash))) - }) - - DAGNode.create(result.Data, links, (err, node) => { - if (err) { - return callback(err) - } - cache.set(multihash, node) - callback(null, node) - }) - }) - }), - - put: promisify((obj, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} - } - if (!options) { - options = {} - } - - let tmpObj = { - Data: null, - Links: [] - } - - if (Buffer.isBuffer(obj)) { - if (!options.enc) { - tmpObj = { - Data: obj.toString(), - Links: [] - } - } - } else if (obj.multihash) { - tmpObj = { - Data: obj.data.toString(), - Links: obj.links.map((l) => { - const link = l.toJSON() - link.hash = link.multihash - return link - }) - } - } else if (typeof obj === 'object') { - tmpObj.Data = obj.Data.toString() - tmpObj.Links = obj.Links - } else { - return callback(new Error('obj not recognized')) - } - - let buf - if (Buffer.isBuffer(obj) && options.enc) { - buf = obj - } else { - buf = new Buffer(JSON.stringify(tmpObj)) - } - const enc = options.enc || 'json' - - send({ - path: 'object/put', - qs: { inputenc: enc }, - files: buf - }, (err, result) => { - if (err) { - return callback(err) - } - - if (Buffer.isBuffer(obj)) { - if (!options.enc) { - obj = { Data: obj, Links: [] } - } else if (options.enc === 'json') { - obj = JSON.parse(obj.toString()) - } - } - - let node - - if (obj.multihash) { - node = obj - } else if (options.enc === 'protobuf') { - dagPB.util.deserialize(obj, (err, _node) => { - if (err) { - return callback(err) - } - node = _node - next() - }) - return - } else { - DAGNode.create(new Buffer(obj.Data), obj.Links, (err, _node) => { - if (err) { - return callback(err) - } - node = _node - next() - }) - return - } - next() - - function next () { - const nodeJSON = node.toJSON() - if (nodeJSON.multihash !== result.Hash) { - const err = new Error('multihashes do not match') - return callback(err) - } - - cache.set(result.Hash, node) - callback(null, node) - } - }) - }), - data: promisify((multihash, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} - } - if (!options) { - options = {} - } - - try { - multihash = cleanMultihash(multihash, options) - } catch (err) { - return callback(err) - } - - const node = cache.get(multihash) - - if (node) { - return callback(null, node.data) - } - - send({ - path: 'object/data', - args: multihash - }, (err, result) => { - if (err) { - return callback(err) - } - - if (typeof result.pipe === 'function') { - streamToValue(result, callback) - } else { - callback(null, result) - } - }) - }), - links: promisify((multihash, options, callback) => { - if (typeof options === 'function') { - callback = options - options = {} - } - if (!options) { - options = {} - } - - try { - multihash = cleanMultihash(multihash, options) - } catch (err) { - return callback(err) - } - - const node = cache.get(multihash) - - if (node) { - return callback(null, node.links) - } - - send({ - path: 'object/links', - args: multihash - }, (err, result) => { - if (err) { - return callback(err) - } - - let links = [] - - if (result.Links) { - links = result.Links.map((l) => { - return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash))) - }) - } - callback(null, links) - }) - }), - stat: promisify((multihash, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - if (!opts) { - opts = {} - } - - try { - multihash = cleanMultihash(multihash, opts) - } catch (err) { - return callback(err) - } - - send({ - path: 'object/stat', - args: multihash - }, callback) - }), - new: promisify((template, callback) => { - if (typeof template === 'function') { - callback = template - template = undefined - } - send({ - path: 'object/new', - args: template - }, (err, result) => { - if (err) { - return callback(err) - } - - let data - - if (template) { - if (template !== 'unixfs-dir') { - return callback(new Error('unkown template: ' + template)) - } - data = (new Unixfs('directory')).marshal() - } else { - data = new Buffer(0) - } - - DAGNode.create(data, (err, node) => { - if (err) { - return callback(err) - } - - if (node.toJSON().multihash !== result.Hash) { - console.log(node.toJSON()) - console.log(result) - return callback(new Error('multihashes do not match')) - } - - callback(null, node) - }) - }) - }), - patch: { - addLink: promisify((multihash, dLink, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - if (!opts) { - opts = {} - } - - try { - multihash = cleanMultihash(multihash, opts) - } catch (err) { - return callback(err) - } - - send({ - path: 'object/patch/add-link', - args: [ - multihash, - dLink.name, - bs58.encode(dLink.multihash).toString() - ] - }, (err, result) => { - if (err) { - return callback(err) - } - api.get(result.Hash, { enc: 'base58' }, callback) - }) - }), - rmLink: promisify((multihash, dLink, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - if (!opts) { - opts = {} - } - - try { - multihash = cleanMultihash(multihash, opts) - } catch (err) { - return callback(err) - } - - 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, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - if (!opts) { - opts = {} - } - - try { - multihash = cleanMultihash(multihash, opts) - } catch (err) { - return callback(err) - } - - 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, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - if (!opts) { - opts = {} - } - - try { - multihash = cleanMultihash(multihash, opts) - } catch (err) { - return callback(err) - } - - send({ - path: 'object/patch/append-data', - args: [multihash], - files: data - }, (err, result) => { - if (err) { - return callback(err) - } - api.get(result.Hash, { enc: 'base58' }, callback) - }) - }) - } - } - - return api -} diff --git a/src/api/pin.js b/src/api/pin.js deleted file mode 100644 index cbd96eee0..000000000 --- a/src/api/pin.js +++ /dev/null @@ -1,68 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - add: promisify((hash, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = null - } - send({ - path: 'pin/add', - args: hash, - qs: opts - }, (err, res) => { - if (err) { - return callback(err) - } - callback(null, res.Pins) - }) - }), - rm: promisify((hash, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = null - } - send({ - path: 'pin/rm', - args: hash, - qs: opts - }, (err, res) => { - if (err) { - return callback(err) - } - callback(null, res.Pins) - }) - }), - ls: promisify((hash, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = {} - } - - if (typeof hash === 'object') { - opts = hash - hash = undefined - } - - if (typeof hash === 'function') { - callback = hash - hash = undefined - opts = {} - } - - send({ - path: 'pin/ls', - args: hash, - qs: opts - }, (err, res) => { - if (err) { - return callback(err) - } - callback(null, res.Keys) - }) - }) - } -} diff --git a/src/api/repo.js b/src/api/repo.js deleted file mode 100644 index 32cde20c8..000000000 --- a/src/api/repo.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') - -module.exports = (send) => { - return { - gc: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'repo/gc', - qs: opts - }, callback) - }), - stat: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'repo/stat', - qs: opts - }, callback) - }) - } -} diff --git a/src/api/swarm.js b/src/api/swarm.js deleted file mode 100644 index 2574405bd..000000000 --- a/src/api/swarm.js +++ /dev/null @@ -1,131 +0,0 @@ -'use strict' - -const promisify = require('promisify-es6') -const multiaddr = require('multiaddr') -const PeerId = require('peer-id') -const PeerInfo = require('peer-info') - -module.exports = (send) => { - return { - peers: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - - const verbose = opts.v || opts.verbose - - send({ - path: 'swarm/peers', - qs: opts - }, (err, result) => { - if (err) { - return callback(err) - } - - if (result.Strings) { - // go-ipfs <= 0.4.4 - callback(null, result.Strings.map((p) => { - const res = {} - - if (verbose) { - const parts = p.split(' ') - res.addr = multiaddr(parts[0]) - res.latency = parts[1] - } else { - res.addr = multiaddr(p) - } - - res.peer = PeerId.createFromB58String( - res.addr.decapsulate('ipfs') - ) - - return res - })) - } else if (result.Peers) { - // go-ipfs >= 0.4.5 - callback(null, result.Peers.map((p) => { - const res = { - addr: multiaddr(p.Addr), - peer: PeerId.createFromB58String(p.Peer), - muxer: p.Muxer - } - - if (p.Latency) { - res.latency = p.Latency - } - - if (p.Streams) { - res.streams = p.Streams - } - - return res - })) - } - }) - }), - connect: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'swarm/connect', - args: args, - qs: opts - }, callback) - }), - disconnect: promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'swarm/disconnect', - args: args, - qs: opts - }, callback) - }), - addrs: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'swarm/addrs', - qs: opts - }, (err, result) => { - if (err) { - return callback(err) - } - - const peers = Object.keys(result.Addrs).map((id) => { - const peerInfo = new PeerInfo(PeerId.createFromB58String(id)) - result.Addrs[id].forEach((addr) => { - peerInfo.multiaddrs.add(multiaddr(addr)) - }) - return peerInfo - }) - - callback(null, peers) - }) - }), - localAddrs: promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } - send({ - path: 'swarm/addrs/local', - qs: opts - }, (err, result) => { - if (err) { - return callback(err) - } - callback(null, result.Strings.map((addr) => { - return multiaddr(addr) - })) - }) - }) - } -} diff --git a/src/bitswap/index.js b/src/bitswap/index.js new file mode 100644 index 000000000..62f9fb58a --- /dev/null +++ b/src/bitswap/index.js @@ -0,0 +1,13 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + wantlist: require('./wantlist')(send), + stat: require('./stat')(send), + unwant: require('./unwant')(send) + } +} diff --git a/src/api/commands.js b/src/bitswap/stat.js similarity index 85% rename from src/api/commands.js rename to src/bitswap/stat.js index b0b4490b8..e29d9c7ad 100644 --- a/src/api/commands.js +++ b/src/bitswap/stat.js @@ -5,7 +5,7 @@ const promisify = require('promisify-es6') module.exports = (send) => { return promisify((callback) => { send({ - path: 'commands' + path: 'bitswap/stat' }, callback) }) } diff --git a/src/bitswap/unwant.js b/src/bitswap/unwant.js new file mode 100644 index 000000000..267568ee1 --- /dev/null +++ b/src/bitswap/unwant.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'bitswap/unwant', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/bitswap/wantlist.js b/src/bitswap/wantlist.js new file mode 100644 index 000000000..c96aaaaac --- /dev/null +++ b/src/bitswap/wantlist.js @@ -0,0 +1,11 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((callback) => { + send({ + path: 'bitswap/wantlist' + }, callback) + }) +} diff --git a/src/block/get.js b/src/block/get.js new file mode 100644 index 000000000..c30381893 --- /dev/null +++ b/src/block/get.js @@ -0,0 +1,55 @@ +'use strict' + +const promisify = require('promisify-es6') +const Block = require('ipfs-block') +const CID = require('cids') +const streamToValue = require('../utils/stream-to-value') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + + // TODO this needs to be adjusted with the new go-ipfs http-api + let cid + try { + if (CID.isCID(args)) { + cid = args + args = cid.toBaseEncodedString() + } else if (Buffer.isBuffer(args)) { + cid = new CID(args) + args = cid.toBaseEncodedString() + } else if (typeof args === 'string') { + cid = new CID(args) + } else { + return callback(new Error('invalid argument')) + } + } catch (err) { + return callback(err) + } + + // Transform the response from Buffer or a Stream to a Block + const transform = (res, callback) => { + if (Buffer.isBuffer(res)) { + callback(null, new Block(res, cid)) + } else { + streamToValue(res, (err, data) => { + if (err) { + return callback(err) + } + callback(null, new Block(data, cid)) + }) + } + } + + const request = { + path: 'block/get', + args: args, + qs: opts + } + + send.andTransform(request, transform, callback) + }) +} diff --git a/src/block/index.js b/src/block/index.js new file mode 100644 index 000000000..f346aa454 --- /dev/null +++ b/src/block/index.js @@ -0,0 +1,13 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + get: require('./get')(send), + stat: require('./stat')(send), + put: require('./put')(send) + } +} diff --git a/src/block/put.js b/src/block/put.js new file mode 100644 index 000000000..d27f95314 --- /dev/null +++ b/src/block/put.js @@ -0,0 +1,36 @@ +'use strict' + +const promisify = require('promisify-es6') +const Block = require('ipfs-block') +const CID = require('cids') + +module.exports = (send) => { + return promisify((block, cid, callback) => { + // TODO this needs to be adjusted with the new go-ipfs http-api + if (typeof cid === 'function') { + callback = cid + cid = {} + } + + if (Array.isArray(block)) { + const err = new Error('block.put() only accepts 1 file') + return callback(err) + } + + if (typeof block === 'object' && block.data) { + block = block.data + } + + const request = { + path: 'block/put', + files: block + } + + // Transform the response to a Block + const transform = (info, callback) => { + callback(null, new Block(block, new CID(info.Key))) + } + + send.andTransform(request, transform, callback) + }) +} diff --git a/src/block/stat.js b/src/block/stat.js new file mode 100644 index 000000000..9d44ac9f8 --- /dev/null +++ b/src/block/stat.js @@ -0,0 +1,35 @@ +'use strict' + +const promisify = require('promisify-es6') +const CID = require('cids') +const multihash = require('multihashes') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + // TODO this needs to be adjusted with the new go-ipfs http-api + if (args && CID.isCID(args)) { + args = multihash.toB58String(args.multihash) + } + + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + + const request = { + path: 'block/stat', + args: args, + qs: opts + } + + // Transform the response from { Key, Size } objects to { key, size } objects + const transform = (stats, callback) => { + callback(null, { + key: stats.Key, + size: stats.Size + }) + } + + send.andTransform(request, transform, callback) + }) +} diff --git a/src/bootstrap/add.js b/src/bootstrap/add.js new file mode 100644 index 000000000..978a3eeda --- /dev/null +++ b/src/bootstrap/add.js @@ -0,0 +1,32 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof opts === 'function' && + !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && + typeof callback === 'function') { + callback = opts + opts = {} + } + + if (args && typeof args === 'object') { + opts = args + args = undefined + } + + send({ + path: 'bootstrap/add', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/bootstrap/index.js b/src/bootstrap/index.js new file mode 100644 index 000000000..31cb3fa2f --- /dev/null +++ b/src/bootstrap/index.js @@ -0,0 +1,13 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + add: require('./add')(send), + rm: require('./rm')(send), + list: require('./list')(send) + } +} diff --git a/src/bootstrap/list.js b/src/bootstrap/list.js new file mode 100644 index 000000000..79690ef19 --- /dev/null +++ b/src/bootstrap/list.js @@ -0,0 +1,16 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'bootstrap/list', + qs: opts + }, callback) + }) +} diff --git a/src/bootstrap/rm.js b/src/bootstrap/rm.js new file mode 100644 index 000000000..c7f55368b --- /dev/null +++ b/src/bootstrap/rm.js @@ -0,0 +1,32 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof opts === 'function' && + !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && + typeof callback === 'function') { + callback = opts + opts = {} + } + + if (args && typeof args === 'object') { + opts = args + args = undefined + } + + send({ + path: 'bootstrap/rm', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/cat.js b/src/cat.js new file mode 100644 index 000000000..2d381efbc --- /dev/null +++ b/src/cat.js @@ -0,0 +1,9 @@ +'use strict' + +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return require('./files/cat')(send) +} diff --git a/src/commands.js b/src/commands.js new file mode 100644 index 000000000..8327e103a --- /dev/null +++ b/src/commands.js @@ -0,0 +1,14 @@ +'use strict' + +const promisify = require('promisify-es6') +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return promisify((callback) => { + send({ + path: 'commands' + }, callback) + }) +} diff --git a/src/config/get.js b/src/config/get.js new file mode 100644 index 000000000..fa1d605f8 --- /dev/null +++ b/src/config/get.js @@ -0,0 +1,31 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((key, callback) => { + if (typeof key === 'function') { + callback = key + key = undefined + } + + if (!key) { + send({ + path: 'config/show', + buffer: true + }, callback) + return + } + + send({ + path: 'config', + args: key, + buffer: true + }, (err, response) => { + if (err) { + return callback(err) + } + callback(null, response.Value) + }) + }) +} diff --git a/src/config/index.js b/src/config/index.js new file mode 100644 index 000000000..109865ab0 --- /dev/null +++ b/src/config/index.js @@ -0,0 +1,13 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + get: require('./get')(send), + set: require('./set')(send), + replace: require('./replace')(send) + } +} diff --git a/src/config/replace.js b/src/config/replace.js new file mode 100644 index 000000000..14f4294ca --- /dev/null +++ b/src/config/replace.js @@ -0,0 +1,18 @@ +'use strict' + +const streamifier = require('streamifier') +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((config, callback) => { + if (typeof config === 'object') { + config = streamifier.createReadStream(new Buffer(JSON.stringify(config))) + } + + send({ + path: 'config/replace', + files: config, + buffer: true + }, callback) + }) +} diff --git a/src/config/set.js b/src/config/set.js new file mode 100644 index 000000000..fba4bd5bf --- /dev/null +++ b/src/config/set.js @@ -0,0 +1,39 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((key, value, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + if (typeof key !== 'string') { + return callback(new Error('Invalid key type')) + } + + if (typeof value !== 'object' && + typeof value !== 'boolean' && + typeof value !== 'string') { + return callback(new Error('Invalid value type')) + } + + if (typeof value === 'object') { + value = JSON.stringify(value) + opts = { json: true } + } + + if (typeof value === 'boolean') { + value = value.toString() + opts = { bool: true } + } + + send({ + path: 'config', + args: [key, value], + qs: opts, + files: undefined, + buffer: true + }, callback) + }) +} diff --git a/src/dht/findpeer.js b/src/dht/findpeer.js new file mode 100644 index 000000000..4b19cafbb --- /dev/null +++ b/src/dht/findpeer.js @@ -0,0 +1,26 @@ +'use strict' + +const promisify = require('promisify-es6') +const streamToValue = require('../utils/stream-to-value') + +module.exports = (send) => { + return promisify((peerId, opts, callback) => { + if (typeof opts === 'function' && !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && typeof callback === 'function') { + callback = opts + opts = {} + } + + send.andTransform({ + path: 'dht/findpeer', + args: peerId, + qs: opts + }, streamToValue, callback) + }) +} diff --git a/src/dht/findprovs.js b/src/dht/findprovs.js new file mode 100644 index 000000000..23aec0127 --- /dev/null +++ b/src/dht/findprovs.js @@ -0,0 +1,26 @@ +'use strict' + +const promisify = require('promisify-es6') +const streamToValue = require('../utils/stream-to-value') + +module.exports = (send) => { + return promisify((cid, opts, callback) => { + if (typeof opts === 'function' && !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && typeof callback === 'function') { + callback = opts + opts = {} + } + + send.andTransform({ + path: 'dht/findprovs', + args: cid, + qs: opts + }, streamToValue, callback) + }) +} diff --git a/src/dht/get.js b/src/dht/get.js new file mode 100644 index 000000000..279ad51bf --- /dev/null +++ b/src/dht/get.js @@ -0,0 +1,49 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((key, opts, callback) => { + if (typeof opts === 'function' && !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && typeof callback === 'function') { + callback = opts + opts = {} + } + + function 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')) + } + + // Inconsistent return values in the browser vs node + if (Array.isArray(res)) { + res = res[0] + } + + if (res.Type === 5) { + done(null, res.Extra) + } else { + let error = new Error('key was not found (type 6)') + done(error) + } + } + + send({ + path: 'dht/get', + args: key, + qs: opts + }, handleResult.bind(null, callback)) + }) +} diff --git a/src/dht/index.js b/src/dht/index.js new file mode 100644 index 000000000..d3677c49f --- /dev/null +++ b/src/dht/index.js @@ -0,0 +1,17 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + get: require('./get')(send), + put: require('./put')(send), + findprovs: require('./findprovs')(send), + findpeer: require('./findpeer')(send), + provide: require('./provide')(send), + // find closest peerId to given peerId + query: require('./query')(send) + } +} diff --git a/src/dht/provide.js b/src/dht/provide.js new file mode 100644 index 000000000..52fceb41e --- /dev/null +++ b/src/dht/provide.js @@ -0,0 +1,29 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((cids, opts, callback) => { + if (typeof opts === 'function' && !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && typeof callback === 'function') { + callback = opts + opts = {} + } + + if (!Array.isArray(cids)) { + cids = [cids] + } + + send({ + path: 'dht/provide', + args: cids, + qs: opts + }, callback) + }) +} diff --git a/src/dht/put.js b/src/dht/put.js new file mode 100644 index 000000000..c0937d158 --- /dev/null +++ b/src/dht/put.js @@ -0,0 +1,25 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((key, value, opts, callback) => { + if (typeof opts === 'function' && !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && typeof callback === 'function') { + callback = opts + opts = {} + } + + send({ + path: 'dht/put', + args: [key, value], + qs: opts + }, callback) + }) +} diff --git a/src/dht/query.js b/src/dht/query.js new file mode 100644 index 000000000..7de751466 --- /dev/null +++ b/src/dht/query.js @@ -0,0 +1,26 @@ +'use strict' + +const promisify = require('promisify-es6') +const streamToValue = require('../utils/stream-to-value') + +module.exports = (send) => { + return promisify((peerId, opts, callback) => { + if (typeof opts === 'function' && !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && typeof callback === 'function') { + callback = opts + opts = {} + } + + send.andTransform({ + path: 'dht/query', + args: peerId, + qs: opts + }, streamToValue, callback) + }) +} diff --git a/src/diag/cmds.js b/src/diag/cmds.js new file mode 100644 index 000000000..e717604fa --- /dev/null +++ b/src/diag/cmds.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + + send({ + path: 'diag/cmds', + qs: opts + }, callback) + }) +} diff --git a/src/diag/index.js b/src/diag/index.js new file mode 100644 index 000000000..b24f7af53 --- /dev/null +++ b/src/diag/index.js @@ -0,0 +1,13 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + net: require('./net')(send), + sys: require('./sys')(send), + cmds: require('./cmds')(send) + } +} diff --git a/src/diag/net.js b/src/diag/net.js new file mode 100644 index 000000000..9c84988d1 --- /dev/null +++ b/src/diag/net.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + + send({ + path: 'diag/net', + qs: opts + }, callback) + }) +} diff --git a/src/diag/sys.js b/src/diag/sys.js new file mode 100644 index 000000000..c06968942 --- /dev/null +++ b/src/diag/sys.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + + send({ + path: 'diag/sys', + qs: opts + }, callback) + }) +} diff --git a/src/api/add.js b/src/files/add.js similarity index 92% rename from src/api/add.js rename to src/files/add.js index eafee5285..214806a19 100644 --- a/src/api/add.js +++ b/src/files/add.js @@ -2,7 +2,7 @@ const isStream = require('isstream') const promisify = require('promisify-es6') -const DAGNodeStream = require('../dagnode-stream') +const DAGNodeStream = require('../utils/dagnode-stream') module.exports = (send) => { return promisify((files, callback) => { diff --git a/src/api/cat.js b/src/files/cat.js similarity index 91% rename from src/api/cat.js rename to src/files/cat.js index 2af53bd10..2382c2a20 100644 --- a/src/api/cat.js +++ b/src/files/cat.js @@ -1,7 +1,7 @@ 'use strict' const promisify = require('promisify-es6') -const cleanCID = require('../clean-cid') +const cleanCID = require('../utils/clean-cid') const v = require('is-ipfs') module.exports = (send) => { diff --git a/src/files/cp.js b/src/files/cp.js new file mode 100644 index 000000000..b808ccc39 --- /dev/null +++ b/src/files/cp.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'files/cp', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/api/create-add-stream.js b/src/files/create-add-stream.js similarity index 100% rename from src/api/create-add-stream.js rename to src/files/create-add-stream.js diff --git a/src/api/get.js b/src/files/get.js similarity index 87% rename from src/api/get.js rename to src/files/get.js index 6b6fc5f3c..056c46d01 100644 --- a/src/api/get.js +++ b/src/files/get.js @@ -1,8 +1,8 @@ 'use strict' const promisify = require('promisify-es6') -const cleanCID = require('../clean-cid') -const TarStreamToObjects = require('../tar-stream-to-objects') +const cleanCID = require('../utils/clean-cid') +const TarStreamToObjects = require('../utils/tar-stream-to-objects') const v = require('is-ipfs') module.exports = (send) => { diff --git a/src/files/index.js b/src/files/index.js new file mode 100644 index 000000000..2c2927af4 --- /dev/null +++ b/src/files/index.js @@ -0,0 +1,22 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + add: require('./add')(send), + createAddStream: require('./create-add-stream')(send), + get: require('./get')(send), + cat: require('./cat')(send), + cp: require('./cp')(send), + ls: require('./ls')(send), + mkdir: require('./mkdir')(send), + stat: require('./stat')(send), + rm: require('./rm')(send), + read: require('./read')(send), + write: require('./write')(send), + mv: require('./mv')(send) + } +} diff --git a/src/files/ls.js b/src/files/ls.js new file mode 100644 index 000000000..e87f07719 --- /dev/null +++ b/src/files/ls.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + return send({ + path: 'files/ls', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/files/mkdir.js b/src/files/mkdir.js new file mode 100644 index 000000000..393baecf4 --- /dev/null +++ b/src/files/mkdir.js @@ -0,0 +1,18 @@ + +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'files/mkdir', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/files/mv.js b/src/files/mv.js new file mode 100644 index 000000000..1f5a85f1f --- /dev/null +++ b/src/files/mv.js @@ -0,0 +1,18 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof opts === 'function' && + callback === undefined) { + callback = opts + opts = {} + } + send({ + path: 'files/mv', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/files/read.js b/src/files/read.js new file mode 100644 index 000000000..bf339c24b --- /dev/null +++ b/src/files/read.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'files/read', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/files/rm.js b/src/files/rm.js new file mode 100644 index 000000000..2cfd209b6 --- /dev/null +++ b/src/files/rm.js @@ -0,0 +1,27 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((path, opts, callback) => { + if (typeof opts === 'function' && + !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && + typeof callback === 'function') { + callback = opts + opts = {} + } + + send({ + path: 'files/rm', + args: path, + qs: opts + }, callback) + }) +} diff --git a/src/files/stat.js b/src/files/stat.js new file mode 100644 index 000000000..845901b98 --- /dev/null +++ b/src/files/stat.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'files/stat', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/files/write.js b/src/files/write.js new file mode 100644 index 000000000..17c7ce8df --- /dev/null +++ b/src/files/write.js @@ -0,0 +1,28 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((pathDst, files, opts, callback) => { + if (typeof opts === 'function' && + !callback) { + callback = opts + opts = {} + } + + // opts is the real callback -- + // 'callback' is being injected by promisify + if (typeof opts === 'function' && + typeof callback === 'function') { + callback = opts + opts = {} + } + + send({ + path: 'files/write', + args: pathDst, + qs: opts, + files: files + }, callback) + }) +} diff --git a/src/get.js b/src/get.js new file mode 100644 index 000000000..a7e04bce3 --- /dev/null +++ b/src/get.js @@ -0,0 +1,9 @@ +'use strict' + +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return require('./files/get')(send) +} diff --git a/src/api/id.js b/src/id.js similarity index 83% rename from src/api/id.js rename to src/id.js index beb970f42..1db0f7a0c 100644 --- a/src/api/id.js +++ b/src/id.js @@ -1,8 +1,11 @@ 'use strict' const promisify = require('promisify-es6') +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) -module.exports = (send) => { return promisify((opts, callback) => { if (typeof opts === 'function') { callback = opts diff --git a/src/index.js b/src/index.js index 7fcdd676e..4e6443cf3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,9 @@ 'use strict' const multiaddr = require('multiaddr') -const loadCommands = require('./load-commands') -const getConfig = require('./default-config') -const getRequestAPI = require('./request-api') +const loadCommands = require('./utils/load-commands') +const getConfig = require('./utils/default-config') +const getRequestAPI = require('./utils/request-api') function IpfsAPI (hostOrMultiaddr, port, opts) { const config = getConfig() diff --git a/src/api/ls.js b/src/key/gen.js similarity index 92% rename from src/api/ls.js rename to src/key/gen.js index 8098fdeb8..1523adc33 100644 --- a/src/api/ls.js +++ b/src/key/gen.js @@ -9,7 +9,7 @@ module.exports = (send) => { opts = {} } send({ - path: 'ls', + path: 'key/gen', args: args, qs: opts }, callback) diff --git a/src/key/index.js b/src/key/index.js new file mode 100644 index 000000000..396b4a078 --- /dev/null +++ b/src/key/index.js @@ -0,0 +1,12 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + gen: require('./gen')(send), + list: require('./list')(send) + } +} diff --git a/src/key/list.js b/src/key/list.js new file mode 100644 index 000000000..7b58c2a6d --- /dev/null +++ b/src/key/list.js @@ -0,0 +1,16 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'key/list', + qs: opts + }, callback) + }) +} diff --git a/src/load-commands.js b/src/load-commands.js deleted file mode 100644 index 810381d3d..000000000 --- a/src/load-commands.js +++ /dev/null @@ -1,69 +0,0 @@ -'use strict' - -function requireCommands () { - const cmds = { - // add and createAddStream alias - add: require('./api/add'), - cat: require('./api/cat'), - createAddStream: require('./api/create-add-stream'), - bitswap: require('./api/bitswap'), - block: require('./api/block'), - bootstrap: require('./api/bootstrap'), - commands: require('./api/commands'), - config: require('./api/config'), - dht: require('./api/dht'), - diag: require('./api/diag'), - id: require('./api/id'), - key: require('./api/key'), - get: require('./api/get'), - log: require('./api/log'), - ls: require('./api/ls'), - mount: require('./api/mount'), - name: require('./api/name'), - object: require('./api/object'), - pin: require('./api/pin'), - ping: require('./api/ping'), - refs: require('./api/refs'), - repo: require('./api/repo'), - swarm: require('./api/swarm'), - pubsub: require('./api/pubsub'), - update: require('./api/update'), - version: require('./api/version') - } - - // TODO: crowding the 'files' namespace temporarily for interface-ipfs-core - // compatibility, until 'files vs mfs' naming decision is resolved. - cmds.files = function (send) { - const files = require('./api/files')(send) - files.add = require('./api/add')(send) - files.createAddStream = require('./api/create-add-stream.js')(send) - files.get = require('./api/get')(send) - files.cat = require('./api/cat')(send) - - return files - } - - cmds.util = function (send) { - const util = { - addFromFs: require('./api/util/fs-add')(send), - addFromStream: require('./api/add')(send), - addFromURL: require('./api/util/url-add')(send) - } - return util - } - - return cmds -} - -function loadCommands (send) { - const files = requireCommands() - const cmds = {} - - Object.keys(files).forEach((file) => { - cmds[file] = files[file](send) - }) - - return cmds -} - -module.exports = loadCommands diff --git a/src/log/index.js b/src/log/index.js new file mode 100644 index 000000000..94597bdc6 --- /dev/null +++ b/src/log/index.js @@ -0,0 +1,13 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + tail: require('./tail')(send), + ls: require('./ls')(send), + level: require('./level')(send) + } +} diff --git a/src/log/level.js b/src/log/level.js new file mode 100644 index 000000000..4304ff90f --- /dev/null +++ b/src/log/level.js @@ -0,0 +1,27 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((subsystem, level, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + if (typeof subsystem !== 'string') { + return callback(new Error('Invalid subsystem type')) + } + + if (typeof level !== 'string') { + return callback(new Error('Invalid level type')) + } + + send({ + path: 'log/level', + args: [subsystem, level], + qs: opts, + files: undefined, + buffer: true + }, callback) + }) +} diff --git a/src/log/ls.js b/src/log/ls.js new file mode 100644 index 000000000..ab243605b --- /dev/null +++ b/src/log/ls.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((callback) => { + send({ + path: 'log/ls' + }, (err, result) => { + if (err) { + return callback(err) + } + + callback(null, result.Strings) + }) + }) +} diff --git a/src/log/tail.js b/src/log/tail.js new file mode 100644 index 000000000..d5be9713f --- /dev/null +++ b/src/log/tail.js @@ -0,0 +1,19 @@ +'use strict' + +const promisify = require('promisify-es6') +const pump = require('pump') +const ndjson = require('ndjson') + +module.exports = (send) => { + return promisify((callback) => { + return send({ + path: 'log/tail' + }, (err, response) => { + if (err) { + return callback(err) + } + const outputStream = pump(response, ndjson.parse()) + callback(null, outputStream) + }) + }) +} diff --git a/src/ls.js b/src/ls.js new file mode 100644 index 000000000..20082301f --- /dev/null +++ b/src/ls.js @@ -0,0 +1,20 @@ +'use strict' + +const promisify = require('promisify-es6') +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'ls', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/api/mount.js b/src/mount.js similarity index 79% rename from src/api/mount.js rename to src/mount.js index a01c56825..7c2e92cab 100644 --- a/src/api/mount.js +++ b/src/mount.js @@ -1,8 +1,11 @@ 'use strict' const promisify = require('promisify-es6') +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) -module.exports = (send) => { return promisify((ipfs, ipns, callback) => { if (typeof ipfs === 'function') { callback = ipfs diff --git a/src/name/index.js b/src/name/index.js new file mode 100644 index 000000000..811357b7c --- /dev/null +++ b/src/name/index.js @@ -0,0 +1,12 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + publish: require('./publish')(send), + resolve: require('./resolve')(send) + } +} diff --git a/src/name/publish.js b/src/name/publish.js new file mode 100644 index 000000000..52b9d1694 --- /dev/null +++ b/src/name/publish.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'name/publish', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/name/resolve.js b/src/name/resolve.js new file mode 100644 index 000000000..421ba562c --- /dev/null +++ b/src/name/resolve.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'name/resolve', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/object/addLink.js b/src/object/addLink.js new file mode 100644 index 000000000..75bb337f4 --- /dev/null +++ b/src/object/addLink.js @@ -0,0 +1,39 @@ +'use strict' + +const promisify = require('promisify-es6') +const bs58 = require('bs58') +const cleanMultihash = require('../utils/clean-multihash') + +module.exports = (send) => { + const objectGet = require('./get')(send) + + return promisify((multihash, dLink, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + if (!opts) { + opts = {} + } + + try { + multihash = cleanMultihash(multihash, opts) + } catch (err) { + return callback(err) + } + + send({ + path: 'object/patch/add-link', + args: [ + multihash, + dLink.name, + bs58.encode(dLink.multihash).toString() + ] + }, (err, result) => { + if (err) { + return callback(err) + } + objectGet(result.Hash, { enc: 'base58' }, callback) + }) + }) +} diff --git a/src/object/appendData.js b/src/object/appendData.js new file mode 100644 index 000000000..daf2a572d --- /dev/null +++ b/src/object/appendData.js @@ -0,0 +1,35 @@ +'use strict' + +const promisify = require('promisify-es6') +const cleanMultihash = require('../utils/clean-multihash') + +module.exports = (send) => { + const objectGet = require('./get')(send) + + return promisify((multihash, data, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + if (!opts) { + opts = {} + } + + try { + multihash = cleanMultihash(multihash, opts) + } catch (err) { + return callback(err) + } + + send({ + path: 'object/patch/append-data', + args: [multihash], + files: data + }, (err, result) => { + if (err) { + return callback(err) + } + objectGet(result.Hash, { enc: 'base58' }, callback) + }) + }) +} diff --git a/src/object/data.js b/src/object/data.js new file mode 100644 index 000000000..9ba2ef100 --- /dev/null +++ b/src/object/data.js @@ -0,0 +1,50 @@ +'use strict' + +const promisify = require('promisify-es6') +const streamToValue = require('../utils/stream-to-value') +const cleanMultihash = require('../utils/clean-multihash') +const LRU = require('lru-cache') +const lruOptions = { + max: 128 +} + +const cache = LRU(lruOptions) + +module.exports = (send) => { + return promisify((multihash, options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} + } + if (!options) { + options = {} + } + + try { + multihash = cleanMultihash(multihash, options) + } catch (err) { + return callback(err) + } + + const node = cache.get(multihash) + + if (node) { + return callback(null, node.data) + } + + send({ + path: 'object/data', + args: multihash + }, (err, result) => { + if (err) { + return callback(err) + } + + if (typeof result.pipe === 'function') { + streamToValue(result, callback) + } else { + callback(null, result) + } + }) + }) +} diff --git a/src/object/get.js b/src/object/get.js new file mode 100644 index 000000000..319ab12ce --- /dev/null +++ b/src/object/get.js @@ -0,0 +1,60 @@ +'use strict' + +const promisify = require('promisify-es6') +const dagPB = require('ipld-dag-pb') +const DAGNode = dagPB.DAGNode +const DAGLink = dagPB.DAGLink +const bs58 = require('bs58') +const cleanMultihash = require('../utils/clean-multihash') +const LRU = require('lru-cache') +const lruOptions = { + max: 128 +} + +const cache = LRU(lruOptions) + +module.exports = (send) => { + return promisify((multihash, options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} + } + + if (!options) { + options = {} + } + + try { + multihash = cleanMultihash(multihash, options) + } catch (err) { + return callback(err) + } + + const node = cache.get(multihash) + + if (node) { + return callback(null, node) + } + + send({ + path: 'object/get', + args: multihash + }, (err, result) => { + if (err) { + return callback(err) + } + + const links = result.Links.map((l) => { + return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash))) + }) + + DAGNode.create(result.Data, links, (err, node) => { + if (err) { + return callback(err) + } + cache.set(multihash, node) + callback(null, node) + }) + }) + }) +} diff --git a/src/object/index.js b/src/object/index.js new file mode 100644 index 000000000..2966232c4 --- /dev/null +++ b/src/object/index.js @@ -0,0 +1,22 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + get: require('./get')(send), + put: require('./put')(send), + data: require('./data')(send), + links: require('./links')(send), + stat: require('./stat')(send), + new: require('./new')(send), + patch: { + addLink: require('./addLink')(send), + rmLink: require('./rmLink')(send), + setData: require('./setData')(send), + appendData: require('./appendData')(send) + } + } +} diff --git a/src/object/links.js b/src/object/links.js new file mode 100644 index 000000000..e3ab1c7dd --- /dev/null +++ b/src/object/links.js @@ -0,0 +1,55 @@ +'use strict' + +const promisify = require('promisify-es6') +const dagPB = require('ipld-dag-pb') +const DAGLink = dagPB.DAGLink +const cleanMultihash = require('../utils/clean-multihash') +const bs58 = require('bs58') +const LRU = require('lru-cache') +const lruOptions = { + max: 128 +} + +const cache = LRU(lruOptions) + +module.exports = (send) => { + return promisify((multihash, options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} + } + if (!options) { + options = {} + } + + try { + multihash = cleanMultihash(multihash, options) + } catch (err) { + return callback(err) + } + + const node = cache.get(multihash) + + if (node) { + return callback(null, node.links) + } + + send({ + path: 'object/links', + args: multihash + }, (err, result) => { + if (err) { + return callback(err) + } + + let links = [] + + if (result.Links) { + links = result.Links.map((l) => { + return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash))) + }) + } + callback(null, links) + }) + }) +} diff --git a/src/object/new.js b/src/object/new.js new file mode 100644 index 000000000..2d48e0464 --- /dev/null +++ b/src/object/new.js @@ -0,0 +1,48 @@ +'use strict' + +const promisify = require('promisify-es6') +const dagPB = require('ipld-dag-pb') +const DAGNode = dagPB.DAGNode +const Unixfs = require('ipfs-unixfs') + +module.exports = (send) => { + return promisify((template, callback) => { + if (typeof template === 'function') { + callback = template + template = undefined + } + send({ + path: 'object/new', + args: template + }, (err, result) => { + if (err) { + return callback(err) + } + + let data + + if (template) { + if (template !== 'unixfs-dir') { + return callback(new Error('unkown template: ' + template)) + } + data = (new Unixfs('directory')).marshal() + } else { + data = new Buffer(0) + } + + DAGNode.create(data, (err, node) => { + if (err) { + return callback(err) + } + + if (node.toJSON().multihash !== result.Hash) { + console.log(node.toJSON()) + console.log(result) + return callback(new Error('multihashes do not match')) + } + + callback(null, node) + }) + }) + }) +} diff --git a/src/object/put.js b/src/object/put.js new file mode 100644 index 000000000..e7ef76c39 --- /dev/null +++ b/src/object/put.js @@ -0,0 +1,113 @@ +'use strict' + +const promisify = require('promisify-es6') +const dagPB = require('ipld-dag-pb') +const DAGNode = dagPB.DAGNode +const LRU = require('lru-cache') +const lruOptions = { + max: 128 +} + +const cache = LRU(lruOptions) + +module.exports = (send) => { + return promisify((obj, options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} + } + if (!options) { + options = {} + } + + let tmpObj = { + Data: null, + Links: [] + } + + if (Buffer.isBuffer(obj)) { + if (!options.enc) { + tmpObj = { + Data: obj.toString(), + Links: [] + } + } + } else if (obj.multihash) { + tmpObj = { + Data: obj.data.toString(), + Links: obj.links.map((l) => { + const link = l.toJSON() + link.hash = link.multihash + return link + }) + } + } else if (typeof obj === 'object') { + tmpObj.Data = obj.Data.toString() + tmpObj.Links = obj.Links + } else { + return callback(new Error('obj not recognized')) + } + + let buf + if (Buffer.isBuffer(obj) && options.enc) { + buf = obj + } else { + buf = new Buffer(JSON.stringify(tmpObj)) + } + const enc = options.enc || 'json' + + send({ + path: 'object/put', + qs: { inputenc: enc }, + files: buf + }, (err, result) => { + if (err) { + return callback(err) + } + + if (Buffer.isBuffer(obj)) { + if (!options.enc) { + obj = { Data: obj, Links: [] } + } else if (options.enc === 'json') { + obj = JSON.parse(obj.toString()) + } + } + + let node + + if (obj.multihash) { + node = obj + } else if (options.enc === 'protobuf') { + dagPB.util.deserialize(obj, (err, _node) => { + if (err) { + return callback(err) + } + node = _node + next() + }) + return + } else { + DAGNode.create(new Buffer(obj.Data), obj.Links, (err, _node) => { + if (err) { + return callback(err) + } + node = _node + next() + }) + return + } + next() + + function next () { + const nodeJSON = node.toJSON() + if (nodeJSON.multihash !== result.Hash) { + const err = new Error('multihashes do not match') + return callback(err) + } + + cache.set(result.Hash, node) + callback(null, node) + } + }) + }) +} diff --git a/src/object/rmLink.js b/src/object/rmLink.js new file mode 100644 index 000000000..d127af1ab --- /dev/null +++ b/src/object/rmLink.js @@ -0,0 +1,37 @@ +'use strict' + +const promisify = require('promisify-es6') +const cleanMultihash = require('../utils/clean-multihash') + +module.exports = (send) => { + const objectGet = require('./get')(send) + + return promisify((multihash, dLink, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + if (!opts) { + opts = {} + } + + try { + multihash = cleanMultihash(multihash, opts) + } catch (err) { + return callback(err) + } + + send({ + path: 'object/patch/rm-link', + args: [ + multihash, + dLink.name + ] + }, (err, result) => { + if (err) { + return callback(err) + } + objectGet(result.Hash, { enc: 'base58' }, callback) + }) + }) +} diff --git a/src/object/setData.js b/src/object/setData.js new file mode 100644 index 000000000..a7380df60 --- /dev/null +++ b/src/object/setData.js @@ -0,0 +1,35 @@ +'use strict' + +const promisify = require('promisify-es6') +const cleanMultihash = require('../utils/clean-multihash') + +module.exports = (send) => { + const objectGet = require('./get')(send) + + return promisify((multihash, data, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + if (!opts) { + opts = {} + } + + try { + multihash = cleanMultihash(multihash, opts) + } catch (err) { + return callback(err) + } + + send({ + path: 'object/patch/set-data', + args: [multihash], + files: data + }, (err, result) => { + if (err) { + return callback(err) + } + objectGet(result.Hash, { enc: 'base58' }, callback) + }) + }) +} diff --git a/src/object/stat.js b/src/object/stat.js new file mode 100644 index 000000000..ba2c2b48c --- /dev/null +++ b/src/object/stat.js @@ -0,0 +1,27 @@ +'use strict' + +const promisify = require('promisify-es6') +const cleanMultihash = require('../utils/clean-multihash') + +module.exports = (send) => { + return promisify((multihash, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + if (!opts) { + opts = {} + } + + try { + multihash = cleanMultihash(multihash, opts) + } catch (err) { + return callback(err) + } + + send({ + path: 'object/stat', + args: multihash + }, callback) + }) +} diff --git a/src/pin/add.js b/src/pin/add.js new file mode 100644 index 000000000..946d32a3b --- /dev/null +++ b/src/pin/add.js @@ -0,0 +1,22 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((hash, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = null + } + send({ + path: 'pin/add', + args: hash, + qs: opts + }, (err, res) => { + if (err) { + return callback(err) + } + callback(null, res.Pins) + }) + }) +} diff --git a/src/pin/index.js b/src/pin/index.js new file mode 100644 index 000000000..859052e2c --- /dev/null +++ b/src/pin/index.js @@ -0,0 +1,13 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + add: require('./add')(send), + rm: require('./rm')(send), + ls: require('./ls')(send) + } +} diff --git a/src/pin/ls.js b/src/pin/ls.js new file mode 100644 index 000000000..5b919a673 --- /dev/null +++ b/src/pin/ls.js @@ -0,0 +1,34 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((hash, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = {} + } + + if (typeof hash === 'object') { + opts = hash + hash = undefined + } + + if (typeof hash === 'function') { + callback = hash + hash = undefined + opts = {} + } + + send({ + path: 'pin/ls', + args: hash, + qs: opts + }, (err, res) => { + if (err) { + return callback(err) + } + callback(null, res.Keys) + }) + }) +} diff --git a/src/pin/rm.js b/src/pin/rm.js new file mode 100644 index 000000000..82abb69ad --- /dev/null +++ b/src/pin/rm.js @@ -0,0 +1,22 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((hash, opts, callback) => { + if (typeof opts === 'function') { + callback = opts + opts = null + } + send({ + path: 'pin/rm', + args: hash, + qs: opts + }, (err, res) => { + if (err) { + return callback(err) + } + callback(null, res.Pins) + }) + }) +} diff --git a/src/api/ping.js b/src/ping.js similarity index 82% rename from src/api/ping.js rename to src/ping.js index eeaa3125a..4dbb77b6c 100644 --- a/src/api/ping.js +++ b/src/ping.js @@ -1,9 +1,12 @@ 'use strict' const promisify = require('promisify-es6') -const streamToValue = require('../stream-to-value') +const moduleConfig = require('./utils/module-config') +const streamToValue = require('./utils/stream-to-value') + +module.exports = (arg) => { + const send = moduleConfig(arg) -module.exports = (send) => { return promisify((id, callback) => { const request = { path: 'ping', diff --git a/src/api/pubsub.js b/src/pubsub.js similarity index 93% rename from src/api/pubsub.js rename to src/pubsub.js index 1afd05fab..ced2b2c32 100644 --- a/src/api/pubsub.js +++ b/src/pubsub.js @@ -4,13 +4,16 @@ const promisify = require('promisify-es6') const EventEmitter = require('events') const eos = require('end-of-stream') const isNode = require('detect-node') -const PubsubMessageStream = require('../pubsub-message-stream') -const stringlistToArray = require('../stringlist-to-array') +const PubsubMessageStream = require('./utils/pubsub-message-stream') +const stringlistToArray = require('./utils/stringlist-to-array') +const moduleConfig = require('./utils/module-config') const NotSupportedError = () => new Error('pubsub is currently not supported when run in the browser') /* Public API */ -module.exports = (send) => { +module.exports = (arg) => { + const send = moduleConfig(arg) + /* Internal subscriptions state and functions */ const ps = new EventEmitter() const subscriptions = {} diff --git a/src/api/refs.js b/src/refs.js similarity index 78% rename from src/api/refs.js rename to src/refs.js index df60da3f6..97d204124 100644 --- a/src/api/refs.js +++ b/src/refs.js @@ -1,9 +1,12 @@ 'use strict' const promisify = require('promisify-es6') -const streamToValue = require('../stream-to-value') +const streamToValue = require('./utils/stream-to-value') +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) -module.exports = (send) => { const refs = promisify((args, opts, callback) => { if (typeof (opts) === 'function') { callback = opts diff --git a/src/repo/gc.js b/src/repo/gc.js new file mode 100644 index 000000000..27a9d3ca8 --- /dev/null +++ b/src/repo/gc.js @@ -0,0 +1,16 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'repo/gc', + qs: opts + }, callback) + }) +} diff --git a/src/repo/index.js b/src/repo/index.js new file mode 100644 index 000000000..c70098d35 --- /dev/null +++ b/src/repo/index.js @@ -0,0 +1,12 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + gc: require('./gc')(send), + stat: require('./stat')(send) + } +} diff --git a/src/repo/stat.js b/src/repo/stat.js new file mode 100644 index 000000000..8ffbfe526 --- /dev/null +++ b/src/repo/stat.js @@ -0,0 +1,16 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'repo/stat', + qs: opts + }, callback) + }) +} diff --git a/src/swarm/addrs.js b/src/swarm/addrs.js new file mode 100644 index 000000000..103df148b --- /dev/null +++ b/src/swarm/addrs.js @@ -0,0 +1,33 @@ +'use strict' + +const promisify = require('promisify-es6') +const PeerInfo = require('peer-info') +const PeerId = require('peer-id') +const multiaddr = require('multiaddr') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'swarm/addrs', + qs: opts + }, (err, result) => { + if (err) { + return callback(err) + } + + const peers = Object.keys(result.Addrs).map((id) => { + const peerInfo = new PeerInfo(PeerId.createFromB58String(id)) + result.Addrs[id].forEach((addr) => { + peerInfo.multiaddrs.add(multiaddr(addr)) + }) + return peerInfo + }) + + callback(null, peers) + }) + }) +} diff --git a/src/swarm/connect.js b/src/swarm/connect.js new file mode 100644 index 000000000..9875af411 --- /dev/null +++ b/src/swarm/connect.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'swarm/connect', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/swarm/disconnect.js b/src/swarm/disconnect.js new file mode 100644 index 000000000..84b800f7e --- /dev/null +++ b/src/swarm/disconnect.js @@ -0,0 +1,17 @@ +'use strict' + +const promisify = require('promisify-es6') + +module.exports = (send) => { + return promisify((args, opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'swarm/disconnect', + args: args, + qs: opts + }, callback) + }) +} diff --git a/src/swarm/index.js b/src/swarm/index.js new file mode 100644 index 000000000..fe8c88b35 --- /dev/null +++ b/src/swarm/index.js @@ -0,0 +1,15 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + peers: require('./peers')(send), + connect: require('./connect')(send), + disconnect: require('./disconnect')(send), + addrs: require('./addrs')(send), + localAddrs: require('./localAddrs')(send) + } +} diff --git a/src/swarm/localAddrs.js b/src/swarm/localAddrs.js new file mode 100644 index 000000000..4798aad52 --- /dev/null +++ b/src/swarm/localAddrs.js @@ -0,0 +1,24 @@ +'use strict' + +const promisify = require('promisify-es6') +const multiaddr = require('multiaddr') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + send({ + path: 'swarm/addrs/local', + qs: opts + }, (err, result) => { + if (err) { + return callback(err) + } + callback(null, result.Strings.map((addr) => { + return multiaddr(addr) + })) + }) + }) +} diff --git a/src/swarm/peers.js b/src/swarm/peers.js new file mode 100644 index 000000000..4f6746219 --- /dev/null +++ b/src/swarm/peers.js @@ -0,0 +1,65 @@ +'use strict' + +const promisify = require('promisify-es6') +const multiaddr = require('multiaddr') +const PeerId = require('peer-id') + +module.exports = (send) => { + return promisify((opts, callback) => { + if (typeof (opts) === 'function') { + callback = opts + opts = {} + } + + const verbose = opts.v || opts.verbose + + send({ + path: 'swarm/peers', + qs: opts + }, (err, result) => { + if (err) { + return callback(err) + } + + if (result.Strings) { + // go-ipfs <= 0.4.4 + callback(null, result.Strings.map((p) => { + const res = {} + + if (verbose) { + const parts = p.split(' ') + res.addr = multiaddr(parts[0]) + res.latency = parts[1] + } else { + res.addr = multiaddr(p) + } + + res.peer = PeerId.createFromB58String( + res.addr.decapsulate('ipfs') + ) + + return res + })) + } else if (result.Peers) { + // go-ipfs >= 0.4.5 + callback(null, result.Peers.map((p) => { + const res = { + addr: multiaddr(p.Addr), + peer: PeerId.createFromB58String(p.Peer), + muxer: p.Muxer + } + + if (p.Latency) { + res.latency = p.Latency + } + + if (p.Streams) { + res.streams = p.Streams + } + + return res + })) + } + }) + }) +} diff --git a/src/api/update.js b/src/update.js similarity index 86% rename from src/api/update.js rename to src/update.js index 7d3e980a9..aee1a64a6 100644 --- a/src/api/update.js +++ b/src/update.js @@ -1,8 +1,11 @@ 'use strict' const promisify = require('promisify-es6') +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) -module.exports = (send) => { return { apply: promisify((opts, callback) => { if (typeof (opts) === 'function') { diff --git a/src/api/util/fs-add.js b/src/util/fs-add.js similarity index 84% rename from src/api/util/fs-add.js rename to src/util/fs-add.js index 6c63cf1ab..0803b36a9 100644 --- a/src/api/util/fs-add.js +++ b/src/util/fs-add.js @@ -2,9 +2,12 @@ const isNode = require('detect-node') const promisify = require('promisify-es6') -const DAGNodeStream = require('../../dagnode-stream') +const DAGNodeStream = require('../utils/dagnode-stream') +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) -module.exports = (send) => { return promisify((path, opts, callback) => { if (typeof opts === 'function' && callback === undefined) { diff --git a/src/api/util/url-add.js b/src/util/url-add.js similarity index 84% rename from src/api/util/url-add.js rename to src/util/url-add.js index 7db525ad7..7a394e42d 100644 --- a/src/api/util/url-add.js +++ b/src/util/url-add.js @@ -3,10 +3,13 @@ const promisify = require('promisify-es6') const once = require('once') const parseUrl = require('url').parse -const request = require('../../request') -const DAGNodeStream = require('../../dagnode-stream') +const request = require('../utils/request') +const DAGNodeStream = require('../utils/dagnode-stream') +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) -module.exports = (send) => { return promisify((url, opts, callback) => { if (typeof (opts) === 'function' && callback === undefined) { diff --git a/src/add-to-dagnode-transform.js b/src/utils/add-to-dagnode-transform.js similarity index 100% rename from src/add-to-dagnode-transform.js rename to src/utils/add-to-dagnode-transform.js diff --git a/src/clean-cid.js b/src/utils/clean-cid.js similarity index 100% rename from src/clean-cid.js rename to src/utils/clean-cid.js diff --git a/src/clean-multihash.js b/src/utils/clean-multihash.js similarity index 100% rename from src/clean-multihash.js rename to src/utils/clean-multihash.js diff --git a/src/dagnode-stream.js b/src/utils/dagnode-stream.js similarity index 100% rename from src/dagnode-stream.js rename to src/utils/dagnode-stream.js diff --git a/src/default-config.js b/src/utils/default-config.js similarity index 83% rename from src/default-config.js rename to src/utils/default-config.js index f929e6b86..5ae6ae82e 100644 --- a/src/default-config.js +++ b/src/utils/default-config.js @@ -1,6 +1,6 @@ 'use strict' -const pkg = require('../package.json') +const pkg = require('../../package.json') exports = module.exports = () => { return { diff --git a/src/get-dagnode.js b/src/utils/get-dagnode.js similarity index 100% rename from src/get-dagnode.js rename to src/utils/get-dagnode.js diff --git a/src/get-files-stream.js b/src/utils/get-files-stream.js similarity index 100% rename from src/get-files-stream.js rename to src/utils/get-files-stream.js diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js new file mode 100644 index 000000000..860a8f7f1 --- /dev/null +++ b/src/utils/load-commands.js @@ -0,0 +1,65 @@ +'use strict' + +function requireCommands () { + const cmds = { + // add and createAddStream alias + add: require('../files/add'), + cat: require('../files/cat'), + createAddStream: require('../files/create-add-stream'), + bitswap: require('../bitswap'), + block: require('../block'), + bootstrap: require('../bootstrap'), + commands: require('../commands'), + config: require('../config'), + dht: require('../dht'), + diag: require('../diag'), + id: require('../id'), + key: require('../key'), + get: require('../files/get'), + log: require('../log'), + ls: require('../ls'), + mount: require('../mount'), + name: require('../name'), + object: require('../object'), + pin: require('../pin'), + ping: require('../ping'), + refs: require('../refs'), + repo: require('../repo'), + swarm: require('../swarm'), + pubsub: require('../pubsub'), + update: require('../update'), + version: require('../version') + } + + // TODO: crowding the 'files' namespace temporarily for interface-ipfs-core + // compatibility, until 'files vs mfs' naming decision is resolved. + cmds.files = function (send) { + const files = require('../files')(send) + + return files + } + + cmds.util = function (send) { + const util = { + addFromFs: require('../util/fs-add')(send), + addFromStream: require('../files/add')(send), + addFromURL: require('../util/url-add')(send) + } + return util + } + + return cmds +} + +function loadCommands (send) { + const files = requireCommands() + const cmds = {} + + Object.keys(files).forEach((file) => { + cmds[file] = files[file](send) + }) + + return cmds +} + +module.exports = loadCommands diff --git a/src/utils/module-config.js b/src/utils/module-config.js new file mode 100644 index 000000000..a05b4d3ab --- /dev/null +++ b/src/utils/module-config.js @@ -0,0 +1,22 @@ +'use strict' + +const getConfig = require('./default-config') +const requestAPI = require('./request-api') +const multiaddr = require('multiaddr') + +module.exports = (arg) => { + const config = getConfig() + + if (typeof arg === 'function') { + return arg + } else if (typeof arg === 'object') { + return requestAPI(arg) + } else if (typeof arg === 'string') { + const maddr = multiaddr(arg).nodeAddress() + config.host = maddr.address + config.port = maddr.port + return requestAPI(config) + } else { + throw new Error('Argument must be a send function or a config object.') + } +} diff --git a/src/pubsub-message-stream.js b/src/utils/pubsub-message-stream.js similarity index 100% rename from src/pubsub-message-stream.js rename to src/utils/pubsub-message-stream.js diff --git a/src/pubsub-message-utils.js b/src/utils/pubsub-message-utils.js similarity index 100% rename from src/pubsub-message-utils.js rename to src/utils/pubsub-message-utils.js diff --git a/src/request-api.js b/src/utils/request-api.js similarity index 100% rename from src/request-api.js rename to src/utils/request-api.js diff --git a/src/request.js b/src/utils/request.js similarity index 100% rename from src/request.js rename to src/utils/request.js diff --git a/src/stream-to-json-value.js b/src/utils/stream-to-json-value.js similarity index 100% rename from src/stream-to-json-value.js rename to src/utils/stream-to-json-value.js diff --git a/src/stream-to-value.js b/src/utils/stream-to-value.js similarity index 100% rename from src/stream-to-value.js rename to src/utils/stream-to-value.js diff --git a/src/stringlist-to-array.js b/src/utils/stringlist-to-array.js similarity index 100% rename from src/stringlist-to-array.js rename to src/utils/stringlist-to-array.js diff --git a/src/tar-stream-to-objects.js b/src/utils/tar-stream-to-objects.js similarity index 100% rename from src/tar-stream-to-objects.js rename to src/utils/tar-stream-to-objects.js diff --git a/src/api/version.js b/src/version.js similarity index 80% rename from src/api/version.js rename to src/version.js index 5548b63f3..438cdd000 100644 --- a/src/api/version.js +++ b/src/version.js @@ -1,8 +1,11 @@ 'use strict' const promisify = require('promisify-es6') +const moduleConfig = require('./utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) -module.exports = (send) => { return promisify((opts, callback) => { if (typeof opts === 'function') { callback = opts diff --git a/test/log.spec.js b/test/log.spec.js index 011a9f332..4ed6b81bb 100644 --- a/test/log.spec.js +++ b/test/log.spec.js @@ -36,6 +36,30 @@ describe('.log', () => { }) }) }) + + it('.log.ls', (done) => { + ipfs.log.ls((err, res) => { + expect(err).to.not.exist() + expect(res).to.exist() + + expect(res).to.be.an('array') + + done() + }) + }) + + it('.log.level', (done) => { + ipfs.log.level('all', 'error', (err, res) => { + expect(err).to.not.exist() + expect(res).to.exist() + + expect(res).to.be.an('object') + expect(res).to.not.have.property('Error') + expect(res).to.have.property('Message') + + done() + }) + }) }) describe('Promise API', () => { @@ -47,5 +71,25 @@ describe('.log', () => { }) }) }) + + it('.log.ls', () => { + return ipfs.log.ls() + .then((res) => { + expect(res).to.exist() + + expect(res).to.be.an('array') + }) + }) + + it('.log.level', () => { + return ipfs.log.level('all', 'error') + .then((res) => { + expect(res).to.exist() + + expect(res).to.be.an('object') + expect(res).to.not.have.property('Error') + expect(res).to.have.property('Message') + }) + }) }) }) diff --git a/test/sub-modules.spec.js b/test/sub-modules.spec.js new file mode 100644 index 000000000..bd6d00c66 --- /dev/null +++ b/test/sub-modules.spec.js @@ -0,0 +1,227 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) + +const defaultConfig = require('../src/utils/default-config.js') +const config = defaultConfig() +config.host = 'test' +config.port = '1111' + +describe('submodules', () => { + it('bitswap', () => { + const bitswap = require('../src/bitswap')(config) + + expect(bitswap.wantlist).to.be.a('function') + expect(bitswap.stat).to.be.a('function') + expect(bitswap.unwant).to.be.a('function') + }) + + it('block', () => { + const block = require('../src/block')(config) + + expect(block.get).to.be.a('function') + expect(block.stat).to.be.a('function') + expect(block.put).to.be.a('function') + }) + + it('bootstrap', () => { + const bootstrap = require('../src/bootstrap')(config) + + expect(bootstrap.add).to.be.a('function') + expect(bootstrap.rm).to.be.a('function') + expect(bootstrap.list).to.be.a('function') + }) + + it('config', () => { + const cfg = require('../src/config')(config) + + expect(cfg.get).to.be.a('function') + expect(cfg.set).to.be.a('function') + expect(cfg.replace).to.be.a('function') + }) + + it('dht', () => { + const dht = require('../src/dht')(config) + + expect(dht.get).to.be.a('function') + expect(dht.put).to.be.a('function') + expect(dht.findprovs).to.be.a('function') + expect(dht.findpeer).to.be.a('function') + expect(dht.provide).to.be.a('function') + expect(dht.query).to.be.a('function') + }) + + it('id', () => { + const id = require('../src/id')(config) + + expect(id).to.be.a('function') + }) + + it('version', () => { + const version = require('../src/version')(config) + + expect(version).to.be.a('function') + }) + + it('ping', () => { + const ping = require('../src/ping')(config) + + expect(ping).to.be.a('function') + }) + + it('log', () => { + const log = require('../src/log')(config) + + expect(log.ls).to.be.a('function') + expect(log.tail).to.be.a('function') + expect(log.level).to.be.a('function') + }) + + it('key', () => { + const key = require('../src/key')(config) + + expect(key.gen).to.be.a('function') + expect(key.list).to.be.a('function') + }) + + it('name', () => { + const name = require('../src/name')(config) + + expect(name.publish).to.be.a('function') + expect(name.resolve).to.be.a('function') + }) + + it('pin', () => { + const pin = require('../src/pin')(config) + + expect(pin.add).to.be.a('function') + expect(pin.rm).to.be.a('function') + expect(pin.ls).to.be.a('function') + }) + + it('repo', () => { + const repo = require('../src/repo')(config) + + expect(repo.gc).to.be.a('function') + expect(repo.stat).to.be.a('function') + }) + + it('swarm', () => { + const swarm = require('../src/swarm')(config) + + expect(swarm.peers).to.be.a('function') + expect(swarm.connect).to.be.a('function') + expect(swarm.disconnect).to.be.a('function') + expect(swarm.addrs).to.be.a('function') + expect(swarm.localAddrs).to.be.a('function') + }) + + it('diag', () => { + const diag = require('../src/diag')(config) + + expect(diag.net).to.be.a('function') + expect(diag.sys).to.be.a('function') + expect(diag.cmds).to.be.a('function') + }) + + it('object', () => { + const object = require('../src/object')(config) + + expect(object.get).to.be.a('function') + expect(object.put).to.be.a('function') + expect(object.data).to.be.a('function') + expect(object.links).to.be.a('function') + expect(object.stat).to.be.a('function') + expect(object.new).to.be.a('function') + expect(object.patch.rmLink).to.be.a('function') + expect(object.patch.addLink).to.be.a('function') + expect(object.patch.setData).to.be.a('function') + expect(object.patch.appendData).to.be.a('function') + }) + + it('pubsub', () => { + const pubsub = require('../src/pubsub')(config) + + expect(pubsub.subscribe).to.be.a('function') + expect(pubsub.unsubscribe).to.be.a('function') + expect(pubsub.publish).to.be.a('function') + expect(pubsub.ls).to.be.a('function') + expect(pubsub.peers).to.be.a('function') + }) + + it('files', () => { + const files = require('../src/files')(config) + + expect(files.add).to.be.a('function') + expect(files.createAddStream).to.be.a('function') + expect(files.get).to.be.a('function') + expect(files.cat).to.be.a('function') + expect(files.cp).to.be.a('function') + expect(files.ls).to.be.a('function') + expect(files.mkdir).to.be.a('function') + expect(files.stat).to.be.a('function') + expect(files.rm).to.be.a('function') + expect(files.read).to.be.a('function') + expect(files.write).to.be.a('function') + expect(files.mv).to.be.a('function') + }) + + it('commands', () => { + const commands = require('../src/commands')(config) + + expect(commands).to.be.a('function') + }) + + it('ls', () => { + const ls = require('../src/ls')(config) + + expect(ls).to.be.a('function') + }) + + it('mount', () => { + const mount = require('../src/mount')(config) + + expect(mount).to.be.a('function') + }) + + it('fs-add', () => { + const fsAdd = require('../src/util/fs-add')(config) + + expect(fsAdd).to.be.a('function') + }) + + it('url-add', () => { + const urlAdd = require('../src/util/url-add')(config) + + expect(urlAdd).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') + }) + + it('add', () => { + const add = require('../src/add')(config) + + expect(add).to.be.a('function') + }) + + it('get', () => { + const get = require('../src/get')(config) + + expect(get).to.be.a('function') + }) + + it('cat', () => { + const cat = require('../src/cat')(config) + + expect(cat).to.be.a('function') + }) +})