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

Commit

Permalink
Merge pull request #78 from ipfs/awesome-ipld
Browse files Browse the repository at this point in the history
Awesome IPLD endeavour
  • Loading branch information
daviddias committed Oct 28, 2016
2 parents 3fdad1c + 4281d6f commit 59e4958
Show file tree
Hide file tree
Showing 5 changed files with 659 additions and 337 deletions.
34 changes: 34 additions & 0 deletions API/dag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
dag API
=======

#### `dag.put`

> Store an IPLD format node
##### `Go` **WIP**

##### `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.

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

#### `dag.get`

> Retrieve an IPLD format node
##### `Go` **WIP**

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

`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.

If no `callback` is passed, a [promise][] is returned.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
},
"homepage": "https://github.com/ipfs/interface-ipfs-core#readme",
"dependencies": {
"async": "^2.1.2",
"bl": "^1.1.2",
"bs58": "^3.0.0",
"chai": "^3.5.0",
"concat-stream": "^1.5.1",
"concat-stream": "^1.5.2",
"detect-node": "^2.0.3",
"ipfs-block": "^0.3.0",
"ipfs-merkle-dag": "^0.7.0",
"ipfs-block": "^0.4.0",
"ipld-dag-pb": "^0.1.3",
"multihashes": "^0.2.2",
"readable-stream": "1.1.13",
"run-series": "^1.1.4"
"readable-stream": "1.1.13"
},
"devDependencies": {
"aegir": "^8.0.0"
"aegir": "^8.1.2"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand All @@ -51,4 +51,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"nginnever <ginneversource@gmail.com>"
]
}
}
40 changes: 34 additions & 6 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const expect = require('chai').expect
const Block = require('ipfs-block')
const multihash = require('multihashes')
const CID = require('cids')

module.exports = (common) => {
describe('.block', () => {
Expand Down Expand Up @@ -34,23 +35,37 @@ module.exports = (common) => {
describe('callback API', () => {
it('.put a buffer', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(expectedHash)
const blob = Buffer('blorb')

ipfs.block.put(blob, (err, block) => {
ipfs.block.put(blob, cid, (err, block) => {
expect(err).to.not.exist
expect(block.key).to.eql(multihash.fromB58String(expectedHash))
expect(block.key('sha2-256')).to.eql(multihash.fromB58String(expectedHash))
expect(block).to.have.a.property('data', blob)
done()
})
})

it('.put a block', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(expectedHash)
const blob = new Block(new Buffer('blorb'))

ipfs.block.put(blob, cid, (err, block) => {
expect(err).to.not.exist
expect(block.key('sha2-256')).to.eql(multihash.fromB58String(expectedHash))
expect(block.data).to.eql(new Buffer('blorb'))
done()
})
})

it('.put a block (without using CID, legacy mode)', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const blob = new Block(new Buffer('blorb'))

ipfs.block.put(blob, (err, block) => {
expect(err).to.not.exist
expect(block.key).to.eql(multihash.fromB58String(expectedHash))
expect(block.key('sha2-256')).to.eql(multihash.fromB58String(expectedHash))
expect(block.data).to.eql(new Buffer('blorb'))
done()
})
Expand All @@ -59,26 +74,39 @@ module.exports = (common) => {
it('.put error with array of blocks', () => {
const blob = Buffer('blorb')

ipfs.block.put([blob, blob], (err) => {
ipfs.block.put([blob, blob], 'fake cids', (err) => {
expect(err).to.be.an.instanceof(Error)
})
})

it('block.get', (done) => {
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(hash)

ipfs.block.get(cid, (err, block) => {
expect(err).to.not.exist
expect(block.key('sha2-256')).to.eql(cid.multihash)
expect(block.data).to.eql(new Buffer('blorb'))
done()
})
})

it('block.get (without using CID, legacy mode)', (done) => {
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'

ipfs.block.get(hash, (err, block) => {
expect(err).to.not.exist
expect(block.key).to.eql(multihash.fromB58String(hash))
expect(block.key('sha2-256')).to.eql(multihash.fromB58String(hash))
expect(block.data).to.eql(new Buffer('blorb'))
done()
})
})

it('block.stat', (done) => {
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(hash)

ipfs.block.stat(hash, (err, stats) => {
ipfs.block.stat(cid, (err, stats) => {
expect(err).to.not.exist
expect(stats).to.have.property('key')
expect(stats).to.have.property('size')
Expand Down
Loading

0 comments on commit 59e4958

Please sign in to comment.