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

Commit 5b9f761

Browse files
authored
Merge pull request #32 from ipfs/block-api
block API spec
2 parents 3e6fbbc + 488f61b commit 5b9f761

File tree

4 files changed

+158
-1
lines changed

4 files changed

+158
-1
lines changed

API/block/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
block API
2+
=========
3+
4+
#### `get`
5+
6+
> Get a raw IPFS block.
7+
8+
##### `Go` **WIP**
9+
10+
##### `JavaScript` - ipfs.block.get(multihash, [options, callback])
11+
12+
`multihash` is a [multihash][multihash] which can be passed as:
13+
14+
- Buffer, the raw Buffer of the multihash
15+
- String, the base58 encoded version of the multihash
16+
17+
`callback` must follow `function (err, block) {}` signature, where `err` is an error if the operation was not successful and `block` is a [Block][block] type object, containing both the data and the hash of the block.
18+
19+
```js
20+
ipfs.block.get(multihash, function (err, block) {
21+
if (err) {
22+
throw err
23+
}
24+
console.log(block.key, block.data)
25+
})
26+
```
27+
28+
If no `callback` is passed, a promise is returned.
29+
30+
#### `put`
31+
32+
> Stores input as an IPFS block.
33+
34+
##### `Go` **WIP**
35+
36+
##### `JavaScript` - ipfs.block.put(block, [callback])
37+
38+
Where `block` can be:
39+
40+
- `Buffer` - the raw bytes of the Block
41+
- [`Block`][block] instance
42+
43+
`callback` has the signature `function (err, block) {}`, where `err` is an error if the operation was not successful and `block` is a [Block][block] type object, containing both the data and the hash of the block.
44+
45+
If no `callback` is passed, a promise is returned.
46+
47+
#### `stat`
48+
49+
> Print information of a raw IPFS block.
50+
51+
##### `Go` **WIP**
52+
53+
##### `JavaScript` - ipfs.block.stat(multihash, [callback])
54+
55+
`multihash` is a [multihash][multihash] which can be passed as:
56+
57+
- `Buffer`, the raw Buffer of the multihash (or of and encoded version)
58+
- `String`, the toString version of the multihash (or of an encoded version)
59+
60+
`callback` must follow the signature `function (err, stats) {}`, where `err` is an error if the operation was not successful and `stats` is an object with the format:`
61+
62+
```JavaScript
63+
{
64+
Key: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD',
65+
Size: 10
66+
}
67+
```
68+
69+
If no `callback` is passed, a promise is returned.
70+
71+
[block](https://github.com/ipfs/js-ipfs-block)
72+
[multihash](https://github.com/multiformats/multihash)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
"greenkeeperio-bot <[email protected]>",
4949
"nginnever <[email protected]>"
5050
]
51-
}
51+
}

src/block.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const expect = require('chai').expect
7+
8+
module.exports = (common) => {
9+
describe('.block', () => {
10+
let ipfs
11+
12+
before(function (done) {
13+
// CI takes longer to instantiate the daemon,
14+
// so we need to increase the timeout for the
15+
// before step
16+
this.timeout(20 * 1000)
17+
18+
common.setup((err, factory) => {
19+
expect(err).to.not.exist
20+
factory.spawnNode((err, node) => {
21+
expect(err).to.not.exist
22+
ipfs = node
23+
done()
24+
})
25+
})
26+
})
27+
28+
after((done) => {
29+
common.teardown(done)
30+
})
31+
32+
describe('callback API', () => {
33+
it('.put', (done) => {
34+
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
35+
const blob = Buffer('blorb')
36+
37+
ipfs.block.put(blob, (err, res) => {
38+
expect(err).to.not.exist
39+
expect(res).to.have.a.property('Key', expectedHash)
40+
done()
41+
})
42+
})
43+
44+
it('.put error with array of blocks', () => {
45+
const blob = Buffer('blorb')
46+
47+
ipfs.block.put([blob, blob], (err) => {
48+
expect(err).to.be.an.instanceof(Error)
49+
})
50+
})
51+
52+
it('block.get', (done) => {
53+
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
54+
55+
ipfs.block.get(hash, (err, res) => {
56+
expect(err).to.not.exist
57+
58+
// TODO review this
59+
let buf = ''
60+
res
61+
.on('data', function (data) { buf += data })
62+
.on('end', function () {
63+
expect(buf).to.be.equal('blorb')
64+
done()
65+
})
66+
})
67+
})
68+
69+
it('block.stat', (done) => {
70+
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
71+
72+
ipfs.block.stat(hash, (err, res) => {
73+
expect(err).to.not.exist
74+
expect(res).to.have.property('Key')
75+
expect(res).to.have.property('Size')
76+
done()
77+
})
78+
})
79+
})
80+
81+
describe('promise API', () => {
82+
})
83+
})
84+
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ exports.config = require('./config')
66
exports.pin = require('./pin')
77
exports.generic = require('./generic')
88
exports.swarm = require('./swarm')
9+
exports.block = require('./block')

0 commit comments

Comments
 (0)