Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit a41ee1d

Browse files
committed
feat(dag): basics (get, put, rm)
1 parent 02efd7c commit a41ee1d

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

src/core/components/dag.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
const promisify = require('promisify-es6')
4+
5+
const dagPB = require('ipld-dag-pb')
6+
const dagCBOR = require('ipld-dag-cbor')
7+
8+
module.exports = function dag (self) {
9+
return {
10+
put: promisify((dagNode, multicodec, hashAlg, callback) => {
11+
switch (multicodec) {
12+
case 'dag-pb': dagPB.util.cid(dagNode, gotCid); break
13+
case 'dag-cbor': dagCBOR.util.cid(dagNode, gotCid); break
14+
default: callback(new Error('IPLD Format not supported'))
15+
}
16+
17+
function gotCid (err, cid) {
18+
if (err) {
19+
return callback(err)
20+
}
21+
self._ipldResolver.put({
22+
node: dagNode,
23+
cid: cid
24+
}, callback)
25+
}
26+
}),
27+
get: promisify((cid, callback) => {
28+
self._ipldResolver.get(cid, callback)
29+
}),
30+
rm: promisify((cid, callback) => {
31+
// TODO once pinning is complete, this remove operation has to first
32+
// verify that some pinning chain is not broken with the operation
33+
self._ipldResolver.remove(cid, callback)
34+
}),
35+
resolve: promisify((cid, path, callback) => {
36+
self._ipldResolver.resolve(cid, path, callback)
37+
})
38+
}
39+
}

src/core/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const bootstrap = require('./components/bootstrap')
1818
const config = require('./components/config')
1919
const block = require('./components/block')
2020
const object = require('./components/object')
21+
const dag = require('./components/dag')
2122
const libp2p = require('./components/libp2p')
2223
const swarm = require('./components/swarm')
2324
const ping = require('./components/ping')
@@ -64,6 +65,7 @@ function IPFS (repoInstance) {
6465
this.config = config(this)
6566
this.block = block(this)
6667
this.object = object(this)
68+
this.dag = dag(this)
6769
this.libp2p = libp2p(this)
6870
this.swarm = swarm(this)
6971
this.files = files(this)

test/core/interface/dag.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-env mocha */
2+
3+
'use strict'
4+
5+
const test = require('interface-ipfs-core')
6+
const IPFSFactory = require('../../utils/factory-core')
7+
8+
let factory
9+
10+
const common = {
11+
setup: function (cb) {
12+
factory = new IPFSFactory()
13+
cb(null, factory)
14+
},
15+
teardown: function (cb) {
16+
factory.dismantle(cb)
17+
}
18+
}
19+
20+
test.dag(common)

test/core/interface/interface.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('interface-ipfs-core tests', () => {
1010
require('./files')
1111
require('./generic')
1212
require('./object')
13+
require('./dag')
1314
if (isNode) {
1415
require('./swarm')
1516
require('./pubsub')

0 commit comments

Comments
 (0)