Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 5e6387d

Browse files
committed
feat(block-core): add compliance with interface-ipfs-core on block-API
1 parent f7a668d commit 5e6387d

File tree

5 files changed

+73
-87
lines changed

5 files changed

+73
-87
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"form-data": "^1.0.0-rc4",
4848
"gulp": "^3.9.1",
4949
"idb-plus-blob-store": "^1.1.2",
50-
"interface-ipfs-core": "^0.13.0",
50+
"interface-ipfs-core": "^0.14.0",
5151
"left-pad": "^1.1.1",
5252
"lodash": "^4.14.1",
5353
"ncp": "^2.0.0",
@@ -67,7 +67,7 @@
6767
"fs-blob-store": "^5.2.1",
6868
"glob": "^7.0.5",
6969
"hapi": "^14.0.0",
70-
"ipfs-api": "^7.0.0",
70+
"ipfs-api": "^8.0.1",
7171
"ipfs-bitswap": "^0.6.0",
7272
"ipfs-block": "^0.3.0",
7373
"ipfs-block-service": "^0.4.0",

src/core/ipfs/block.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
11
'use strict'
22

3+
const Block = require('ipfs-block')
4+
const multihash = require('multihashes')
5+
36
module.exports = function block (self) {
47
return {
5-
get: (multihash, callback) => {
6-
self._blockS.getBlock(multihash, callback)
8+
get: (hash, callback) => {
9+
if (typeof hash === 'string') {
10+
hash = multihash.fromB58String(hash)
11+
}
12+
self._blockS.getBlock(hash, callback)
713
},
814
put: (block, callback) => {
9-
self._blockS.addBlock(block, callback)
15+
if (Array.isArray(block)) {
16+
return callback(new Error('Array is not supported'))
17+
}
18+
if (Buffer.isBuffer(block)) {
19+
block = new Block(block)
20+
}
21+
22+
self._blockS.addBlock(block, (err) => {
23+
callback(err, block)
24+
})
1025
},
11-
del: (multihash, callback) => {
12-
self._blockS.deleteBlock(multihash, callback)
26+
del: (hash, callback) => {
27+
if (typeof hash === 'string') {
28+
hash = multihash.fromB58String(hash)
29+
}
30+
31+
self._blockS.deleteBlock(hash, callback)
1332
},
14-
stat: (multihash, callback) => {
15-
self._blockS.getBlock(multihash, (err, block) => {
33+
stat: (hash, callback) => {
34+
if (typeof hash === 'string') {
35+
hash = multihash.fromB58String(hash)
36+
}
37+
38+
self._blockS.getBlock(hash, (err, block) => {
1639
if (err) {
1740
return callback(err)
1841
}
1942
callback(null, {
20-
Key: multihash,
21-
Size: block.data.length
43+
key: hash,
44+
size: block.data.length
2245
})
2346
})
2447
}

test/core/both/test-bitswap.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe('bitswap', () => {
119119
cb(err)
120120
}),
121121
(cb) => {
122-
remoteNode.block.put(block.data, cb)
122+
remoteNode.block.put(block, cb)
123123
},
124124
(cb) => {
125125
inProcNode.block.get(block.key, (err, b) => {
@@ -146,10 +146,10 @@ describe('bitswap', () => {
146146
cb(err)
147147
}),
148148
(cb) => connectNodes(remoteNodes[0], remoteNodes[1], cb),
149-
(cb) => remoteNodes[0].block.put(blocks[0].data, cb),
150-
(cb) => remoteNodes[0].block.put(blocks[1].data, cb),
151-
(cb) => remoteNodes[1].block.put(blocks[2].data, cb),
152-
(cb) => remoteNodes[1].block.put(blocks[3].data, cb),
149+
(cb) => remoteNodes[0].block.put(blocks[0], cb),
150+
(cb) => remoteNodes[0].block.put(blocks[1], cb),
151+
(cb) => remoteNodes[1].block.put(blocks[2], cb),
152+
(cb) => remoteNodes[1].block.put(blocks[3], cb),
153153
(cb) => inProcNode.block.put(blocks[4], cb),
154154
(cb) => inProcNode.block.put(blocks[5], cb),
155155
// 3. Fetch blocks on all nodes

test/core/both/test-block.js

Lines changed: 14 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,20 @@
11
/* eslint-env mocha */
2-
'use strict'
3-
4-
const expect = require('chai').expect
5-
const base58 = require('bs58')
6-
const fs = require('fs')
7-
const IPFS = require('../../../src/core')
8-
const Block = require('ipfs-block')
9-
const path = require('path')
10-
11-
const isNode = require('detect-node')
122

13-
const fileA = isNode
14-
? fs.readFileSync(path.join(__dirname, '../../go-ipfs-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data'))
15-
: require('buffer!./../../go-ipfs-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data')
16-
17-
// TODO use arrow funtions again when https://github.com/webpack/webpack/issues/1944 is fixed
18-
describe('block', function () {
19-
var ipfs
20-
21-
before((done) => {
22-
ipfs = new IPFS(require('../../utils/repo-path'))
23-
ipfs.load(done)
24-
})
3+
'use strict'
254

26-
it('get', function (done) {
27-
const b58mh = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'
28-
const mh = new Buffer(base58.decode(b58mh))
29-
ipfs.block.get(mh, (err, block) => {
30-
expect(err).to.not.exist
31-
const eq = fileA.equals(block.data)
32-
expect(eq).to.equal(true)
33-
done()
34-
})
35-
})
5+
const test = require('interface-ipfs-core')
6+
const IPFSFactory = require('../../utils/factory-core')
367

37-
it('put', (done) => {
38-
var b = new Block('random data')
39-
ipfs.block.put(b, function (err) {
40-
expect(err).to.not.exist
41-
ipfs.block.get(b.key, function (err, block) {
42-
expect(err).to.not.exist
43-
expect(b.data.equals(block.data)).to.equal(true)
44-
expect(b.key.equals(block.key)).to.equal(true)
45-
done()
46-
})
47-
})
48-
})
8+
let factory
499

50-
it('rm', (done) => {
51-
var b = new Block('I will not last long enough')
52-
ipfs.block.put(b, function (err) {
53-
expect(err).to.not.exist
54-
ipfs.block.get(b.key, function (err, block) {
55-
expect(err).to.not.exist
56-
ipfs.block.del(b.key, function (err) {
57-
expect(err).to.not.exist
58-
ipfs.block.get(b.key, function (err, block) {
59-
expect(err).to.exist
60-
done()
61-
})
62-
})
63-
})
64-
})
65-
})
10+
const common = {
11+
setup: function (cb) {
12+
factory = new IPFSFactory()
13+
cb(null, factory)
14+
},
15+
teardown: function (cb) {
16+
factory.dismantle(cb)
17+
}
18+
}
6619

67-
it('stat', function (done) {
68-
const mh = new Buffer(base58
69-
.decode('QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe'))
70-
ipfs.block.stat(mh, (err, stats) => {
71-
expect(err).to.not.exist
72-
expect(stats.Key.equals(mh)).to.equal(true)
73-
expect(stats.Size).to.equal(309)
74-
done()
75-
})
76-
})
77-
})
20+
test.block(common)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-env mocha */
2+
3+
'use strict'
4+
5+
const test = require('interface-ipfs-core')
6+
const FactoryClient = require('./../../utils/factory-http')
7+
8+
let fc
9+
10+
const common = {
11+
setup: function (callback) {
12+
fc = new FactoryClient()
13+
callback(null, fc)
14+
},
15+
teardown: function (callback) {
16+
fc.dismantle(callback)
17+
}
18+
}
19+
20+
test.block(common)

0 commit comments

Comments
 (0)