diff --git a/src/dag/dag.js b/src/dag/dag.js deleted file mode 100644 index 666437ed6..000000000 --- a/src/dag/dag.js +++ /dev/null @@ -1,127 +0,0 @@ -'use strict' - -const dagPB = require('ipld-dag-pb') -const dagCBOR = require('ipld-dag-cbor') -const promisify = require('promisify-es6') -const CID = require('cids') -const multihash = require('multihashes') -const block = require('./block') - -function noop () {} - -module.exports = (send) => { - const api = { - put: promisify((dagNode, options, callback) => { - if (typeof options === 'function') { - return setImmediate(() => callback(new Error('no options were passed'))) - } - - callback = callback || noop - - let hashAlg = options.hash || 'sha2-256' - let format - let inputEnc - - if (options.cid && CID.isCID(options.cid)) { - format = options.cid.codec - hashAlg = multihash.decode(options.cid.multihash).name - prepare() - } else if (options.format) { - format = options.format - prepare() - } else { - callback(new Error('Invalid arguments')) - } - - function prepare () { - inputEnc = 'raw' - - if (format === 'dag-cbor') { - dagCBOR.util.serialize(dagNode, finalize) - } - if (format === 'dag-pb') { - dagPB.util.serialize(dagNode, finalize) - } - } - - function finalize (err, serialized) { - if (err) { return callback(err) } - - send({ - path: 'dag/put', - qs: { - hash: hashAlg, - format: format, - 'input-enc': inputEnc - }, - files: serialized - }, (err, result) => { - if (err) { - return callback(err) - } - if (result.Cid) { - return callback(null, new CID(result.Cid['/'])) - } else { - return callback(result) - } - }) - } - }), - get: promisify((cid, path, options, callback) => { - if (typeof path === 'function') { - callback = path - path = undefined - } - - if (typeof options === 'function') { - callback = options - options = {} - } - - options = options || {} - - if (CID.isCID(cid)) { - cid = cid.toBaseEncodedString() - } - - if (typeof cid === 'string') { - const split = cid.split('/') - cid = split[0] - split.shift() - - if (split.length > 0) { - path = split.join('/') - } else { - path = '/' - } - } - - send({ - path: 'dag/resolve', - args: cid + '/' + path, - qs: options - }, (err, resolved) => { - if (err) { - return callback(err) - } - - let resolvedCid = new CID(resolved['Cid']['/']) - - block(send).get(resolvedCid, (err, blk) => { - if (err) { - return callback(err) - } - - if (resolvedCid.codec === 'dag-cbor') { - dagCBOR.resolver.resolve(blk, resolved['RemPath'], callback) - } - if (resolvedCid.codec === 'dag-pb') { - dagCBOR.resolver.resolve(blk, resolved['RemPath'], callback) - } - }) - }) - }) - } - - return api -} diff --git a/src/dag/get.js b/src/dag/get.js new file mode 100644 index 000000000..af3fa55e7 --- /dev/null +++ b/src/dag/get.js @@ -0,0 +1,61 @@ +'use strict' + +const dagPB = require('ipld-dag-pb') +const dagCBOR = require('ipld-dag-cbor') +const promisify = require('promisify-es6') +const CID = require('cids') +const waterfall = require('async/waterfall') +const block = require('../block') + +module.exports = (send) => { + return promisify((cid, path, options, callback) => { + if (typeof path === 'function') { + callback = path + path = undefined + } + + if (typeof options === 'function') { + callback = options + options = {} + } + + options = options || {} + + if (CID.isCID(cid)) { + cid = cid.toBaseEncodedString() + } + + if (typeof cid === 'string') { + const split = cid.split('/') + cid = split[0] + split.shift() + + if (split.length > 0) { + path = split.join('/') + } else { + path = '/' + } + } + + waterfall([ + cb => { + send({ + path: 'dag/resolve', + args: cid + '/' + path, + qs: options + }, cb) + }, + (resolved, cb) => { + block(send).get(new CID(resolved['Cid']['/']), (err, blk) => cb(err, blk, resolved['RemPath'])) + }, + (blk, path, cb) => { + if (blk.cid.codec === 'dag-cbor') { + dagCBOR.resolver.resolve(blk, path, cb) + } + if (blk.cid.codec === 'dag-pb') { + dagPB.resolver.resolve(blk, path, cb) + } + } + ], callback) + }) +} diff --git a/src/dag/index.js b/src/dag/index.js new file mode 100644 index 000000000..bb6b1333c --- /dev/null +++ b/src/dag/index.js @@ -0,0 +1,12 @@ +'use strict' + +const moduleConfig = require('../utils/module-config') + +module.exports = (arg) => { + const send = moduleConfig(arg) + + return { + get: require('./get')(send), + put: require('./put')(send) + } +} diff --git a/src/dag/put.js b/src/dag/put.js new file mode 100644 index 000000000..fbe2d00f5 --- /dev/null +++ b/src/dag/put.js @@ -0,0 +1,68 @@ +'use strict' + +const dagPB = require('ipld-dag-pb') +const dagCBOR = require('ipld-dag-cbor') +const promisify = require('promisify-es6') +const CID = require('cids') +const multihash = require('multihashes') + +function noop () {} + +module.exports = (send) => { + return promisify((dagNode, options, callback) => { + if (typeof options === 'function') { + return setImmediate(() => callback(new Error('no options were passed'))) + } + + callback = callback || noop + + let hashAlg = options.hash || 'sha2-256' + let format + let inputEnc + + if (options.cid && CID.isCID(options.cid)) { + format = options.cid.codec + hashAlg = multihash.decode(options.cid.multihash).name + prepare() + } else if (options.format) { + format = options.format + prepare() + } else { + callback(new Error('Invalid arguments')) + } + + function prepare () { + inputEnc = 'raw' + + if (format === 'dag-cbor') { + dagCBOR.util.serialize(dagNode, finalize) + } + if (format === 'dag-pb') { + dagPB.util.serialize(dagNode, finalize) + } + } + + function finalize (err, serialized) { + if (err) { return callback(err) } + + send({ + path: 'dag/put', + qs: { + hash: hashAlg, + format: format, + 'input-enc': inputEnc + }, + files: serialized + }, (err, result) => { + if (err) { + return callback(err) + } + if (result.Cid) { + return callback(null, new CID(result.Cid['/'])) + } else { + return callback(result) + } + }) + } + }) +} diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 860a8f7f1..183b76d2a 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -11,6 +11,7 @@ function requireCommands () { bootstrap: require('../bootstrap'), commands: require('../commands'), config: require('../config'), + dag: require('../dag'), dht: require('../dht'), diag: require('../diag'), id: require('../id'),