From 9463d3a52ce8ab13e0ae9686a3e9308d373a28fc Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 9 Mar 2017 16:06:32 +0000 Subject: [PATCH] feat: dag.put --- package.json | 1 + src/dag/dag.js | 79 ++++++++++++++++++++++++++++++++++++++ test/interface/dag.spec.js | 34 ++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 src/dag/dag.js create mode 100644 test/interface/dag.spec.js diff --git a/package.json b/package.json index 0fabfe21c..9e86be21d 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "glob": "^7.1.2", "ipfs-block": "~0.6.1", "ipfs-unixfs": "~0.1.14", + "ipld-dag-cbor": "^0.12.0", "ipld-dag-pb": "~0.13.1", "is-ipfs": "^0.3.2", "is-stream": "^1.1.0", diff --git a/src/dag/dag.js b/src/dag/dag.js new file mode 100644 index 000000000..ab81f041c --- /dev/null +++ b/src/dag/dag.js @@ -0,0 +1,79 @@ +'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) => { + 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.hashAlg || '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 () { + if (format === 'dag-cbor') { + // TODO change this once + // https://github.com/ipfs/go-ipfs/issues/3771 is finished + format = 'cbor' + + inputEnc = 'cbor' + dagCBOR.util.serialize(dagNode, finalize) + } + if (format === 'dag-pb') { + // TODO change this once + // https://github.com/ipfs/go-ipfs/issues/3771 is finished + format = 'protobuf' + + inputEnc = 'protobuf' + dagPB.util.serialize(dagNode, finalize) + } + } + + function finalize (err, serialized) { + if (err) { return callback(err) } + + send({ + path: 'dag/put', + qs: { + hashAlg: hashAlg, // not implemented in go yet https://github.com/ipfs/go-ipfs/issues/3771 + format: format, + inputenc: inputEnc + }, + files: serialized + }, (err, result) => { + if (err) { + return callback(err) + } + // TODO handle the result + }) + } + }), + get: promisify((cid, path, options, callback) => { + // TODO + }) + } + + return api +} diff --git a/test/interface/dag.spec.js b/test/interface/dag.spec.js new file mode 100644 index 000000000..6c68680e7 --- /dev/null +++ b/test/interface/dag.spec.js @@ -0,0 +1,34 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const parallel = require('async/parallel') + +const IPFSApi = require('../../src') + +const DaemonFactory = require('ipfsd-ctl') +const df = DaemonFactory.create() + +const nodes = [] +const common = { + setup: function (callback) { + callback(null, { + spawnNode: (cb) => { + df.spawn((err, _ipfsd) => { + if (err) { + return cb(err) + } + + nodes.push(_ipfsd) + cb(null, IPFSApi(_ipfsd.apiAddr)) + }) + } + }) + }, + teardown: function (callback) { + parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) + } +} + +test.dag(common)