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

Commit e32090e

Browse files
nginneverdaviddias
authored andcommitted
feat(block): spec
1 parent e72068a commit e32090e

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

API/block/README.md

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

src/block.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint-env mocha */
2+
/* globals apiClients */
3+
'use strict'
4+
5+
const expect = require('chai').expect
6+
7+
module.exports = (common) => {
8+
describe.only('.block', () => {
9+
const blorbKey = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
10+
const blorb = Buffer('blorb')
11+
12+
it('returns an error when putting an array of files', () => {
13+
return apiClients.a.block.put([blorb, blorb], (err) => {
14+
console.log(err)
15+
expect(err).to.be.an.instanceof(Error)
16+
})
17+
})
18+
19+
it('block.put', (done) => {
20+
apiClients.a.block.put(blorb, (err, res) => {
21+
expect(err).to.not.exist
22+
expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ')
23+
done()
24+
})
25+
})
26+
27+
it('block.get', (done) => {
28+
apiClients.a.block.get(blorbKey, (err, res) => {
29+
expect(err).to.not.exist
30+
31+
let buf = ''
32+
res
33+
.on('data', function (data) { buf += data })
34+
.on('end', function () {
35+
expect(buf).to.be.equal('blorb')
36+
done()
37+
})
38+
})
39+
})
40+
41+
it('block.stat', (done) => {
42+
apiClients.a.block.stat(blorbKey, (err, res) => {
43+
expect(err).to.not.exist
44+
expect(res).to.have.property('Key')
45+
expect(res).to.have.property('Size')
46+
done()
47+
})
48+
})
49+
50+
describe('promise', () => {
51+
it('returns an error when putting an array of files', () => {
52+
return apiClients.a.block.put([blorb, blorb])
53+
.catch((err) => {
54+
expect(err).to.be.an.instanceof(Error)
55+
})
56+
})
57+
58+
it('block.put', () => {
59+
return apiClients.a.block.put(blorb)
60+
.then((res) => {
61+
expect(res).to.have.a.property('Key', 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ')
62+
})
63+
})
64+
65+
it('block.get', (done) => {
66+
return apiClients.a.block.get(blorbKey)
67+
.then((res) => {
68+
let buf = ''
69+
res
70+
.on('data', function (data) { buf += data })
71+
.on('end', function () {
72+
expect(buf).to.be.equal('blorb')
73+
done()
74+
})
75+
})
76+
})
77+
78+
it('block.stat', () => {
79+
return apiClients.a.block.stat(blorbKey)
80+
.then((res) => {
81+
expect(res).to.have.property('Key')
82+
expect(res).to.have.property('Size')
83+
})
84+
})
85+
})
86+
})
87+
}

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)