diff --git a/package.json b/package.json index beba89cc54..fc9fee0d5d 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "form-data": "^2.1.2", "fs-pull-blob-store": "^0.4.1", "gulp": "^3.9.1", - "interface-ipfs-core": "^0.23.5", + "interface-ipfs-core": "^0.23.8", "ipfsd-ctl": "^0.18.1", "left-pad": "^1.1.3", "lodash": "^4.17.4", @@ -171,4 +171,4 @@ "nginnever ", "npmcdn-to-unpkg-bot " ] -} \ No newline at end of file +} diff --git a/src/core/components/dag.js b/src/core/components/dag.js new file mode 100644 index 0000000000..21a203d67c --- /dev/null +++ b/src/core/components/dag.js @@ -0,0 +1,34 @@ +'use strict' + +const promisify = require('promisify-es6') + +const dagPB = require('ipld-dag-pb') +const dagCBOR = require('ipld-dag-cbor') + +module.exports = function dag (self) { + return { + put: promisify((dagNode, multicodec, hashAlg, callback) => { + switch (multicodec) { + case 'dag-pb': dagPB.util.cid(dagNode, gotCid); break + case 'dag-cbor': dagCBOR.util.cid(dagNode, gotCid); break + default: callback(new Error('IPLD Format not supported')) + } + + function gotCid (err, cid) { + if (err) { + return callback(err) + } + self._ipldResolver.put({ + node: dagNode, + cid: cid + }, callback) + } + }), + get: promisify((cid, callback) => { + self._ipldResolver.get(cid, callback) + }), + resolve: promisify((cid, path, callback) => { + self._ipldResolver.resolve(cid, path, callback) + }) + } +} diff --git a/src/core/components/index.js b/src/core/components/index.js index 6d8193c5e4..28193af597 100644 --- a/src/core/components/index.js +++ b/src/core/components/index.js @@ -12,6 +12,7 @@ exports.bootstrap = require('./bootstrap') exports.config = require('./config') exports.block = require('./block') exports.object = require('./object') +exports.dag = require('./dag') exports.libp2p = require('./libp2p') exports.swarm = require('./swarm') exports.ping = require('./ping') diff --git a/src/core/index.js b/src/core/index.js index 3dc676a019..3c6d3470e3 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -52,6 +52,7 @@ class IPFS { this.config = components.config(this) this.block = components.block(this) this.object = components.object(this) + this.dag = components.dag(this) this.libp2p = components.libp2p(this) this.swarm = components.swarm(this) this.files = components.files(this) diff --git a/test/core/interface/dag.js b/test/core/interface/dag.js new file mode 100644 index 0000000000..1a617691c6 --- /dev/null +++ b/test/core/interface/dag.js @@ -0,0 +1,20 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const IPFSFactory = require('../../utils/factory-core') + +let factory + +const common = { + setup: function (cb) { + factory = new IPFSFactory() + cb(null, factory) + }, + teardown: function (cb) { + factory.dismantle(cb) + } +} + +test.dag(common) diff --git a/test/core/interface/interface.spec.js b/test/core/interface/interface.spec.js index a3cfae8350..bf749464d1 100644 --- a/test/core/interface/interface.spec.js +++ b/test/core/interface/interface.spec.js @@ -10,6 +10,7 @@ describe('interface-ipfs-core tests', () => { require('./files') require('./generic') require('./object') + require('./dag') if (isNode) { require('./swarm') require('./pubsub')