From 5e6387d4586a42e673c4c14d156a282c78aae1ac Mon Sep 17 00:00:00 2001 From: David Dias Date: Wed, 24 Aug 2016 16:49:16 +0100 Subject: [PATCH] feat(block-core): add compliance with interface-ipfs-core on block-API --- package.json | 4 +- src/core/ipfs/block.js | 41 +++++++-- test/core/both/test-bitswap.js | 10 +-- test/core/both/test-block.js | 85 +++---------------- .../test-block.js | 20 +++++ 5 files changed, 73 insertions(+), 87 deletions(-) diff --git a/package.json b/package.json index 146d8fb187..d60b1618c0 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "form-data": "^1.0.0-rc4", "gulp": "^3.9.1", "idb-plus-blob-store": "^1.1.2", - "interface-ipfs-core": "^0.13.0", + "interface-ipfs-core": "^0.14.0", "left-pad": "^1.1.1", "lodash": "^4.14.1", "ncp": "^2.0.0", @@ -67,7 +67,7 @@ "fs-blob-store": "^5.2.1", "glob": "^7.0.5", "hapi": "^14.0.0", - "ipfs-api": "^7.0.0", + "ipfs-api": "^8.0.1", "ipfs-bitswap": "^0.6.0", "ipfs-block": "^0.3.0", "ipfs-block-service": "^0.4.0", diff --git a/src/core/ipfs/block.js b/src/core/ipfs/block.js index 0add8633ea..22c3c0be11 100644 --- a/src/core/ipfs/block.js +++ b/src/core/ipfs/block.js @@ -1,24 +1,47 @@ 'use strict' +const Block = require('ipfs-block') +const multihash = require('multihashes') + module.exports = function block (self) { return { - get: (multihash, callback) => { - self._blockS.getBlock(multihash, callback) + get: (hash, callback) => { + if (typeof hash === 'string') { + hash = multihash.fromB58String(hash) + } + self._blockS.getBlock(hash, callback) }, put: (block, callback) => { - self._blockS.addBlock(block, callback) + if (Array.isArray(block)) { + return callback(new Error('Array is not supported')) + } + if (Buffer.isBuffer(block)) { + block = new Block(block) + } + + self._blockS.addBlock(block, (err) => { + callback(err, block) + }) }, - del: (multihash, callback) => { - self._blockS.deleteBlock(multihash, callback) + del: (hash, callback) => { + if (typeof hash === 'string') { + hash = multihash.fromB58String(hash) + } + + self._blockS.deleteBlock(hash, callback) }, - stat: (multihash, callback) => { - self._blockS.getBlock(multihash, (err, block) => { + stat: (hash, callback) => { + if (typeof hash === 'string') { + hash = multihash.fromB58String(hash) + } + + self._blockS.getBlock(hash, (err, block) => { if (err) { return callback(err) } callback(null, { - Key: multihash, - Size: block.data.length + key: hash, + size: block.data.length }) }) } diff --git a/test/core/both/test-bitswap.js b/test/core/both/test-bitswap.js index b75330260f..530380e0fa 100644 --- a/test/core/both/test-bitswap.js +++ b/test/core/both/test-bitswap.js @@ -119,7 +119,7 @@ describe('bitswap', () => { cb(err) }), (cb) => { - remoteNode.block.put(block.data, cb) + remoteNode.block.put(block, cb) }, (cb) => { inProcNode.block.get(block.key, (err, b) => { @@ -146,10 +146,10 @@ describe('bitswap', () => { cb(err) }), (cb) => connectNodes(remoteNodes[0], remoteNodes[1], cb), - (cb) => remoteNodes[0].block.put(blocks[0].data, cb), - (cb) => remoteNodes[0].block.put(blocks[1].data, cb), - (cb) => remoteNodes[1].block.put(blocks[2].data, cb), - (cb) => remoteNodes[1].block.put(blocks[3].data, cb), + (cb) => remoteNodes[0].block.put(blocks[0], cb), + (cb) => remoteNodes[0].block.put(blocks[1], cb), + (cb) => remoteNodes[1].block.put(blocks[2], cb), + (cb) => remoteNodes[1].block.put(blocks[3], cb), (cb) => inProcNode.block.put(blocks[4], cb), (cb) => inProcNode.block.put(blocks[5], cb), // 3. Fetch blocks on all nodes diff --git a/test/core/both/test-block.js b/test/core/both/test-block.js index d9d9d04839..e2950dd8ea 100644 --- a/test/core/both/test-block.js +++ b/test/core/both/test-block.js @@ -1,77 +1,20 @@ /* eslint-env mocha */ -'use strict' - -const expect = require('chai').expect -const base58 = require('bs58') -const fs = require('fs') -const IPFS = require('../../../src/core') -const Block = require('ipfs-block') -const path = require('path') - -const isNode = require('detect-node') -const fileA = isNode - ? fs.readFileSync(path.join(__dirname, '../../go-ipfs-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data')) - : require('buffer!./../../go-ipfs-repo/blocks/12207028/122070286b9afa6620a66f715c7020d68af3d10e1a497971629c07606bfdb812303d.data') - -// TODO use arrow funtions again when https://github.com/webpack/webpack/issues/1944 is fixed -describe('block', function () { - var ipfs - - before((done) => { - ipfs = new IPFS(require('../../utils/repo-path')) - ipfs.load(done) - }) +'use strict' - it('get', function (done) { - const b58mh = 'QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe' - const mh = new Buffer(base58.decode(b58mh)) - ipfs.block.get(mh, (err, block) => { - expect(err).to.not.exist - const eq = fileA.equals(block.data) - expect(eq).to.equal(true) - done() - }) - }) +const test = require('interface-ipfs-core') +const IPFSFactory = require('../../utils/factory-core') - it('put', (done) => { - var b = new Block('random data') - ipfs.block.put(b, function (err) { - expect(err).to.not.exist - ipfs.block.get(b.key, function (err, block) { - expect(err).to.not.exist - expect(b.data.equals(block.data)).to.equal(true) - expect(b.key.equals(block.key)).to.equal(true) - done() - }) - }) - }) +let factory - it('rm', (done) => { - var b = new Block('I will not last long enough') - ipfs.block.put(b, function (err) { - expect(err).to.not.exist - ipfs.block.get(b.key, function (err, block) { - expect(err).to.not.exist - ipfs.block.del(b.key, function (err) { - expect(err).to.not.exist - ipfs.block.get(b.key, function (err, block) { - expect(err).to.exist - done() - }) - }) - }) - }) - }) +const common = { + setup: function (cb) { + factory = new IPFSFactory() + cb(null, factory) + }, + teardown: function (cb) { + factory.dismantle(cb) + } +} - it('stat', function (done) { - const mh = new Buffer(base58 - .decode('QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe')) - ipfs.block.stat(mh, (err, stats) => { - expect(err).to.not.exist - expect(stats.Key.equals(mh)).to.equal(true) - expect(stats.Size).to.equal(309) - done() - }) - }) -}) +test.block(common) diff --git a/test/http-api/interface-ipfs-core-over-ipfs-api/test-block.js b/test/http-api/interface-ipfs-core-over-ipfs-api/test-block.js index e69de29bb2..57d625c99a 100644 --- a/test/http-api/interface-ipfs-core-over-ipfs-api/test-block.js +++ b/test/http-api/interface-ipfs-core-over-ipfs-api/test-block.js @@ -0,0 +1,20 @@ +/* eslint-env mocha */ + +'use strict' + +const test = require('interface-ipfs-core') +const FactoryClient = require('./../../utils/factory-http') + +let fc + +const common = { + setup: function (callback) { + fc = new FactoryClient() + callback(null, fc) + }, + teardown: function (callback) { + fc.dismantle(callback) + } +} + +test.block(common)