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

Commit e1f6438

Browse files
dignifiedquiredaviddias
authored andcommitted
[WIP] upgrade to new block interface (#131)
* feat: new ipfs-block api * refactor: upgrade to latest standard
1 parent cee7033 commit e1f6438

File tree

11 files changed

+299
-273
lines changed

11 files changed

+299
-273
lines changed

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@
3030
"bl": "^1.2.0",
3131
"bs58": "^4.0.0",
3232
"chai": "^3.5.0",
33-
"cids": "^0.4.1",
33+
"cids": "^0.4.2",
3434
"concat-stream": "^1.6.0",
3535
"detect-node": "^2.0.3",
36-
"ipfs-block": "^0.5.5",
37-
"ipld-dag-cbor": "^0.10.0",
38-
"ipld-dag-pb": "^0.10.0",
39-
"multiaddr": "^2.2.1",
40-
"multihashes": "^0.4.3",
36+
"ipfs-block": "^0.6.0",
37+
"ipld-dag-cbor": "^0.11.0",
38+
"ipld-dag-pb": "^0.11.0",
39+
"multiaddr": "^2.2.2",
40+
"multihashes": "^0.4.4",
4141
"pull-stream": "^3.5.0",
42-
"readable-stream": "1.1.14"
42+
"readable-stream": "2.2.6"
4343
},
4444
"devDependencies": {
45-
"aegir": "^10.0.0"
45+
"aegir": "^11.0.1",
46+
"dirty-chai": "^1.2.2"
4647
},
4748
"contributors": [
4849
"David Dias <[email protected]>",
@@ -56,4 +57,4 @@
5657
5758
"nginnever <[email protected]>"
5859
]
59-
}
60+
}

src/block.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@
33

44
'use strict'
55

6-
const expect = require('chai').expect
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
710
const Block = require('ipfs-block')
811
const multihash = require('multihashes')
912
const CID = require('cids')
1013

1114
function expectKey (block, expected, callback) {
12-
block.key((err, key) => {
13-
if (err) {
14-
return callback(err)
15-
}
16-
expect(key).to.be.eql(expected)
17-
callback()
18-
})
15+
expect(block.cid.multihash).to.be.eql(expected)
16+
callback()
1917
}
2018

2119
module.exports = (common) => {
@@ -29,9 +27,9 @@ module.exports = (common) => {
2927
this.timeout(20 * 1000)
3028

3129
common.setup((err, factory) => {
32-
expect(err).to.not.exist
30+
expect(err).to.not.exist()
3331
factory.spawnNode((err, node) => {
34-
expect(err).to.not.exist
32+
expect(err).to.not.exist()
3533
ipfs = node
3634
done()
3735
})
@@ -43,34 +41,34 @@ module.exports = (common) => {
4341
describe('callback API', () => {
4442
it('.put a buffer', (done) => {
4543
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
46-
const cid = new CID(expectedHash)
47-
const blob = Buffer('blorb')
44+
// const cid = new CID(expectedHash)
45+
const blob = new Buffer('blorb')
4846

49-
ipfs.block.put(blob, cid, (err, block) => {
50-
expect(err).to.not.exist
51-
expect(block).to.have.a.property('data', blob)
47+
ipfs.block.put(blob, (err, block) => {
48+
expect(err).to.not.exist()
49+
expect(block.data).to.be.eql(blob)
5250
expectKey(block, multihash.fromB58String(expectedHash), done)
5351
})
5452
})
5553

5654
it('.put a block', (done) => {
5755
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
5856
const cid = new CID(expectedHash)
59-
const blob = new Block(new Buffer('blorb'))
57+
const blob = new Block(new Buffer('blorb'), cid)
6058

61-
ipfs.block.put(blob, cid, (err, block) => {
62-
expect(err).to.not.exist
59+
ipfs.block.put(blob, (err, block) => {
60+
expect(err).to.not.exist()
6361
expect(block.data).to.eql(new Buffer('blorb'))
6462
expectKey(block, multihash.fromB58String(expectedHash), done)
6563
})
6664
})
6765

6866
it('.put a block (without using CID, legacy mode)', (done) => {
6967
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
70-
const blob = new Block(new Buffer('blorb'))
68+
const blob = new Block(new Buffer('blorb'), new CID(expectedHash))
7169

7270
ipfs.block.put(blob, (err, block) => {
73-
expect(err).to.not.exist
71+
expect(err).to.not.exist()
7472
expect(block.data).to.eql(new Buffer('blorb'))
7573
expectKey(block, multihash.fromB58String(expectedHash), done)
7674
})
@@ -79,7 +77,7 @@ module.exports = (common) => {
7977
it('.put error with array of blocks', (done) => {
8078
const blob = Buffer('blorb')
8179

82-
ipfs.block.put([blob, blob], 'fake cids', (err) => {
80+
ipfs.block.put([blob, blob], (err) => {
8381
expect(err).to.be.an.instanceof(Error)
8482
done()
8583
})
@@ -90,7 +88,7 @@ module.exports = (common) => {
9088
const cid = new CID(hash)
9189

9290
ipfs.block.get(cid, (err, block) => {
93-
expect(err).to.not.exist
91+
expect(err).to.not.exist()
9492
expect(block.data).to.eql(new Buffer('blorb'))
9593
expectKey(block, cid.multihash, done)
9694
})
@@ -100,7 +98,7 @@ module.exports = (common) => {
10098
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
10199

102100
ipfs.block.get(hash, (err, block) => {
103-
expect(err).to.not.exist
101+
expect(err).to.not.exist()
104102
expect(block.data).to.eql(new Buffer('blorb'))
105103
expectKey(block, multihash.fromB58String(hash), done)
106104
})
@@ -111,7 +109,7 @@ module.exports = (common) => {
111109
const cid = new CID(hash)
112110

113111
ipfs.block.stat(cid, (err, stats) => {
114-
expect(err).to.not.exist
112+
expect(err).to.not.exist()
115113
expect(stats).to.have.property('key')
116114
expect(stats).to.have.property('size')
117115
done()

src/config.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33

44
'use strict'
55

6-
const expect = require('chai').expect
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
710

811
module.exports = (common) => {
912
describe('.config', () => {
1013
let ipfs
1114

1215
before((done) => {
1316
common.setup((err, factory) => {
14-
expect(err).to.not.exist
17+
expect(err).to.not.exist()
1518
factory.spawnNode((err, node) => {
16-
expect(err).to.not.exist
19+
expect(err).to.not.exist()
1720
ipfs = node
1821
done()
1922
})
@@ -28,59 +31,59 @@ module.exports = (common) => {
2831
describe('.get', () => {
2932
it('retrieve the whole config', (done) => {
3033
ipfs.config.get((err, config) => {
31-
expect(err).to.not.exist
32-
expect(config).to.exist
34+
expect(err).to.not.exist()
35+
expect(config).to.exist()
3336
done()
3437
})
3538
})
3639

3740
it('retrieve a value through a key', (done) => {
3841
ipfs.config.get('Identity.PeerID', (err, peerId) => {
39-
expect(err).to.not.exist
40-
expect(peerId).to.exist
42+
expect(err).to.not.exist()
43+
expect(peerId).to.exist()
4144
done()
4245
})
4346
})
4447

4548
it('retrieve a value through a nested key', (done) => {
4649
ipfs.config.get('Addresses.Swarm', (err, swarmAddrs) => {
47-
expect(err).to.not.exist
48-
expect(swarmAddrs).to.exist
50+
expect(err).to.not.exist()
51+
expect(swarmAddrs).to.exist()
4952
done()
5053
})
5154
})
5255

5356
it('fail on non valid key', (done) => {
5457
ipfs.config.get(1234, (err, peerId) => {
55-
expect(err).to.exist
58+
expect(err).to.exist()
5659
done()
5760
})
5861
})
5962

60-
it('fail on non existent key', (done) => {
63+
it('fail on non exist()ent key', (done) => {
6164
ipfs.config.get('Bananas', (err, peerId) => {
62-
expect(err).to.exist
65+
expect(err).to.exist()
6366
done()
6467
})
6568
})
6669
})
6770
describe('.set', () => {
6871
it('set a new key', (done) => {
6972
ipfs.config.set('Fruit', 'banana', (err) => {
70-
expect(err).to.not.exist
73+
expect(err).to.not.exist()
7174
ipfs.config.get('Fruit', (err, fruit) => {
72-
expect(err).to.not.exist
75+
expect(err).to.not.exist()
7376
expect(fruit).to.equal('banana')
7477
done()
7578
})
7679
})
7780
})
7881

79-
it('set an already existing key', (done) => {
82+
it('set an already exist()ing key', (done) => {
8083
ipfs.config.set('Fruit', 'morango', (err) => {
81-
expect(err).to.not.exist
84+
expect(err).to.not.exist()
8285
ipfs.config.get('Fruit', (err, fruit) => {
83-
expect(err).to.not.exist
86+
expect(err).to.not.exist()
8487
expect(fruit).to.equal('morango')
8588
done()
8689
})
@@ -91,9 +94,9 @@ module.exports = (common) => {
9194
const key = 'API.HTTPHeaders.Access-Control-Allow-Origin'
9295
const val = ['http://example.io']
9396
ipfs.config.set(key, val, function (err) {
94-
expect(err).to.not.exist
97+
expect(err).to.not.exist()
9598
ipfs.config.get(key, function (err, result) {
96-
expect(err).to.not.exist
99+
expect(err).to.not.exist()
97100
expect(result).to.deep.equal(val)
98101
done()
99102
})
@@ -102,14 +105,14 @@ module.exports = (common) => {
102105

103106
it('fail on non valid key', (done) => {
104107
ipfs.config.set(new Buffer('heeey'), '', (err) => {
105-
expect(err).to.exist
108+
expect(err).to.exist()
106109
done()
107110
})
108111
})
109112

110113
it('fail on non valid value', (done) => {
111114
ipfs.config.set('Fruit', new Buffer('abc'), (err) => {
112-
expect(err).to.exist
115+
expect(err).to.exist()
113116
done()
114117
})
115118
})
@@ -125,19 +128,19 @@ module.exports = (common) => {
125128

126129
it('replace the whole config', (done) => {
127130
ipfs.config.replace(config, (err) => {
128-
expect(err).to.not.exist
131+
expect(err).to.not.exist()
129132
ipfs.config.get((err, _config) => {
130-
expect(err).to.not.exist
133+
expect(err).to.not.exist()
131134
expect(_config).to.deep.equal(config)
132135
})
133136
})
134137
})
135138

136139
it('replace to empty config', (done) => {
137140
ipfs.config.replace({}, (err) => {
138-
expect(err).to.not.exist
141+
expect(err).to.not.exist()
139142
ipfs.config.get((err, _config) => {
140-
expect(err).to.not.exist
143+
expect(err).to.not.exist()
141144
expect(_config).to.deep.equal(config)
142145
})
143146
})
@@ -150,7 +153,7 @@ module.exports = (common) => {
150153
it('retrieve the whole config', () => {
151154
return ipfs.config.get()
152155
.then((config) => {
153-
expect(config).to.exist
156+
expect(config).to.exist()
154157
})
155158
})
156159
})

0 commit comments

Comments
 (0)