Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

feat: DAG API basics (get, put, rm) #112

Merged
merged 5 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions API/dag/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
dag API
=======

> The dag API comes to replace the `object API`, it support the creation and manipulation of dag-pb object, as well as other IPLD formats (i.e dag-cbor, ethereum-block, git, etc)

#### `dag.put`

> Store an IPLD format node
Expand All @@ -9,13 +11,10 @@ dag API

##### `JavaScript` - ipfs.dag.put(dagNode, formatMulticodec, hashAlg, callback)

`dagNode` - a DAG node that follows one of the supported IPLD formats.

`formatMulticodec` - The IPLD format multicodec.

`hashAlg` - The hash algorithm to be used over the serialized dagNode.

`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
- `dagNode` - a DAG node that follows one of the supported IPLD formats.
- `formatMulticodec` - The IPLD format multicodec.
- `hashAlg` - The hash algorithm to be used over the serialized dagNode.
- `callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.

If no `callback` is passed, a [promise][] is returned.

Expand All @@ -25,9 +24,9 @@ If no `callback` is passed, a [promise][] is returned.

##### `Go` **WIP**

##### `JavaScript` - ipfs.object.get(cid, callback)
##### `JavaScript` - ipfs.dag.get(cid, callback)

`cid` is a [CID][https://github.com/ipfs/js-cid] instance.
- `cid` is a [CID][https://github.com/ipfs/js-cid] instance.

`callback` must follow `function (err, dagNode) {}` signature, where `err` is an error if the operation was not successful and `dagNode` is the IPLD format DAG node retrieved.

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"concat-stream": "^1.6.0",
"detect-node": "^2.0.3",
"ipfs-block": "^0.5.4",
"ipld-dag-cbor": "^0.8.5",
"ipld-dag-pb": "^0.9.3",
"multiaddr": "^2.1.1",
"multihashes": "^0.3.1",
Expand All @@ -52,4 +53,4 @@
"haad <haad@headbanggames.com>",
"nginnever <ginneversource@gmail.com>"
]
}
}
141 changes: 141 additions & 0 deletions src/dag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */

'use strict'

const expect = require('chai').expect
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const dagCBOR = require('ipld-dag-cbor')

module.exports = (common) => {
describe.only('.dag (basics)', () => {
let ipfs

before(function (done) {
// CI is slow
this.timeout(20 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist
factory.spawnNode((err, node) => {
expect(err).to.not.exist
ipfs = node
done()
})
})
})

after((done) => {
common.teardown(done)
})

describe('callback API', () => {
let pbNode
let cborNode

before((done) => {
const someData = new Buffer('some data')

pbNode = DAGNode.create(someData, (err, node) => {
expect(err).to.not.exist
pbNode = node
done()
})

cborNode = {
data: someData
}
})

describe('.put', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also get these nodes to check that they have been actually put in the correct way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is what happens in the get tests, this way put is tested in isolation and in conjunction with get

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But not for all the formats as far as I can see.

it('dag-pb with default hash func (sha2-256)', (done) => {
ipfs.dag.put(pbNode, 'dag-pb', 'sha2-256', done)
})

it('dag-pb with custom hash func (sha3-512)', (done) => {
ipfs.dag.put(pbNode, 'dag-pb', 'sha3-512', done)
})

/*
* This works because dag-cbor will just treat pbNode as a regular object
it.skip('dag-pb node with wrong multicodec', (done) => {
// This works because dag-cbor will just treat pbNode as a
// regular object
ipfs.dag.put(pbNode, 'dag-cbor', 'sha3-512', (err) => {
expect(err).to.exist
done()
})
})
*/

it('dag-cbor with default hash func (sha2-256)', (done) => {
ipfs.dag.put(cborNode, 'dag-cbor', 'sha2-256', done)
})

it('dag-cbor with custom hash func (sha3-512)', (done) => {
ipfs.dag.put(cborNode, 'dag-cbor', 'sha3-512', done)
})

it('dag-cbor node with wrong multicodec', (done) => {
ipfs.dag.put(cborNode, 'dag-pb', 'sha3-512', (err) => {
expect(err).to.exist
done()
})
})
})

describe('.get', () => {
let pbNode
let cborNode

before((done) => {
const someData = new Buffer('some other data')

pbNode = DAGNode.create(someData, (err, node) => {
expect(err).to.not.exist
pbNode = node
done()
})

cborNode = {
data: someData
}
})

it('dag-pb node', (done) => {
ipfs.dag.put(pbNode, 'dag-pb', 'sha2-256', (err) => {
expect(err).to.not.exist
dagPB.util.cid(pbNode, (err, cid) => {
expect(err).to.not.exist
ipfs.dag.get(cid, (err, node) => {
expect(err).to.not.exist
expect(pbNode.toJSON()).to.eql(node.toJSON())
done()
})
})
})
})

it('dag-cbor node', (done) => {
ipfs.dag.put(cborNode, 'dag-cbor', 'sha2-256', (err) => {
expect(err).to.not.exist
dagCBOR.util.cid(cborNode, (err, cid) => {
expect(err).to.not.exist
ipfs.dag.get(cid, (err, node) => {
expect(err).to.not.exist
expect(cborNode).to.eql(node)
done()
})
})
})
})
})
})

describe('promise API', () => {
describe('.put', () => {})
describe('.get', () => {})
})
})
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ exports.generic = require('./generic')
exports.swarm = require('./swarm')
exports.block = require('./block')
exports.dht = require('./dht')
exports.dag = require('./dag')
exports.pubsub = require('./pubsub')