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

[WIP] upgrade to new block interface #131

Merged
merged 3 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@
"bl": "^1.2.0",
"bs58": "^4.0.0",
"chai": "^3.5.0",
"cids": "^0.4.1",
"cids": "^0.4.2",
"concat-stream": "^1.6.0",
"detect-node": "^2.0.3",
"ipfs-block": "^0.5.5",
"ipld-dag-cbor": "^0.10.0",
"ipld-dag-pb": "^0.10.0",
"multiaddr": "^2.2.1",
"multihashes": "^0.4.3",
"ipfs-block": "^0.6.0",
"ipld-dag-cbor": "^0.11.0",
"ipld-dag-pb": "^0.11.0",
"multiaddr": "^2.2.2",
"multihashes": "^0.4.4",
"pull-stream": "^3.5.0",
"readable-stream": "1.1.14"
"readable-stream": "2.2.6"
},
"devDependencies": {
"aegir": "^10.0.0"
"aegir": "^11.0.1",
"dirty-chai": "^1.2.2"
},
"contributors": [
"David Dias <[email protected]>",
Expand All @@ -55,4 +56,4 @@
"haad <[email protected]>",
"nginnever <[email protected]>"
]
}
}
46 changes: 22 additions & 24 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@

'use strict'

const expect = require('chai').expect
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const Block = require('ipfs-block')
const multihash = require('multihashes')
const CID = require('cids')

function expectKey (block, expected, callback) {
block.key((err, key) => {
if (err) {
return callback(err)
}
expect(key).to.be.eql(expected)
callback()
})
expect(block.cid.multihash).to.be.eql(expected)
callback()
}

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

common.setup((err, factory) => {
expect(err).to.not.exist
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist
expect(err).to.not.exist()
ipfs = node
done()
})
Expand All @@ -43,34 +41,34 @@ module.exports = (common) => {
describe('callback API', () => {
it('.put a buffer', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(expectedHash)
const blob = Buffer('blorb')
// const cid = new CID(expectedHash)
const blob = new Buffer('blorb')

ipfs.block.put(blob, cid, (err, block) => {
expect(err).to.not.exist
expect(block).to.have.a.property('data', blob)
ipfs.block.put(blob, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.be.eql(blob)
expectKey(block, multihash.fromB58String(expectedHash), done)
})
})

it('.put a block', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const cid = new CID(expectedHash)
const blob = new Block(new Buffer('blorb'))
const blob = new Block(new Buffer('blorb'), cid)

ipfs.block.put(blob, cid, (err, block) => {
expect(err).to.not.exist
ipfs.block.put(blob, (err, block) => {
expect(err).to.not.exist()
expect(block.data).to.eql(new Buffer('blorb'))
expectKey(block, multihash.fromB58String(expectedHash), done)
})
})

it('.put a block (without using CID, legacy mode)', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const blob = new Block(new Buffer('blorb'))
const blob = new Block(new Buffer('blorb'), new CID(expectedHash))

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

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

ipfs.block.get(cid, (err, block) => {
expect(err).to.not.exist
expect(err).to.not.exist()
expect(block.data).to.eql(new Buffer('blorb'))
expectKey(block, cid.multihash, done)
})
Expand All @@ -100,7 +98,7 @@ module.exports = (common) => {
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'

ipfs.block.get(hash, (err, block) => {
expect(err).to.not.exist
expect(err).to.not.exist()
expect(block.data).to.eql(new Buffer('blorb'))
expectKey(block, multihash.fromB58String(hash), done)
})
Expand All @@ -111,7 +109,7 @@ module.exports = (common) => {
const cid = new CID(hash)

ipfs.block.stat(cid, (err, stats) => {
expect(err).to.not.exist
expect(err).to.not.exist()
expect(stats).to.have.property('key')
expect(stats).to.have.property('size')
done()
Expand Down
55 changes: 29 additions & 26 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

'use strict'

const expect = require('chai').expect
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

module.exports = (common) => {
describe('.config', () => {
let ipfs

before((done) => {
common.setup((err, factory) => {
expect(err).to.not.exist
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist
expect(err).to.not.exist()
ipfs = node
done()
})
Expand All @@ -28,59 +31,59 @@ module.exports = (common) => {
describe('.get', () => {
it('retrieve the whole config', (done) => {
ipfs.config.get((err, config) => {
expect(err).to.not.exist
expect(config).to.exist
expect(err).to.not.exist()
expect(config).to.exist()
done()
})
})

it('retrieve a value through a key', (done) => {
ipfs.config.get('Identity.PeerID', (err, peerId) => {
expect(err).to.not.exist
expect(peerId).to.exist
expect(err).to.not.exist()
expect(peerId).to.exist()
done()
})
})

it('retrieve a value through a nested key', (done) => {
ipfs.config.get('Addresses.Swarm', (err, swarmAddrs) => {
expect(err).to.not.exist
expect(swarmAddrs).to.exist
expect(err).to.not.exist()
expect(swarmAddrs).to.exist()
done()
})
})

it('fail on non valid key', (done) => {
ipfs.config.get(1234, (err, peerId) => {
expect(err).to.exist
expect(err).to.exist()
done()
})
})

it('fail on non existent key', (done) => {
it('fail on non exist()ent key', (done) => {
ipfs.config.get('Bananas', (err, peerId) => {
expect(err).to.exist
expect(err).to.exist()
done()
})
})
})
describe('.set', () => {
it('set a new key', (done) => {
ipfs.config.set('Fruit', 'banana', (err) => {
expect(err).to.not.exist
expect(err).to.not.exist()
ipfs.config.get('Fruit', (err, fruit) => {
expect(err).to.not.exist
expect(err).to.not.exist()
expect(fruit).to.equal('banana')
done()
})
})
})

it('set an already existing key', (done) => {
it('set an already exist()ing key', (done) => {
ipfs.config.set('Fruit', 'morango', (err) => {
expect(err).to.not.exist
expect(err).to.not.exist()
ipfs.config.get('Fruit', (err, fruit) => {
expect(err).to.not.exist
expect(err).to.not.exist()
expect(fruit).to.equal('morango')
done()
})
Expand All @@ -91,9 +94,9 @@ module.exports = (common) => {
const key = 'API.HTTPHeaders.Access-Control-Allow-Origin'
const val = ['http://example.io']
ipfs.config.set(key, val, function (err) {
expect(err).to.not.exist
expect(err).to.not.exist()
ipfs.config.get(key, function (err, result) {
expect(err).to.not.exist
expect(err).to.not.exist()
expect(result).to.deep.equal(val)
done()
})
Expand All @@ -102,14 +105,14 @@ module.exports = (common) => {

it('fail on non valid key', (done) => {
ipfs.config.set(new Buffer('heeey'), '', (err) => {
expect(err).to.exist
expect(err).to.exist()
done()
})
})

it('fail on non valid value', (done) => {
ipfs.config.set('Fruit', new Buffer('abc'), (err) => {
expect(err).to.exist
expect(err).to.exist()
done()
})
})
Expand All @@ -125,19 +128,19 @@ module.exports = (common) => {

it('replace the whole config', (done) => {
ipfs.config.replace(config, (err) => {
expect(err).to.not.exist
expect(err).to.not.exist()
ipfs.config.get((err, _config) => {
expect(err).to.not.exist
expect(err).to.not.exist()
expect(_config).to.deep.equal(config)
})
})
})

it('replace to empty config', (done) => {
ipfs.config.replace({}, (err) => {
expect(err).to.not.exist
expect(err).to.not.exist()
ipfs.config.get((err, _config) => {
expect(err).to.not.exist
expect(err).to.not.exist()
expect(_config).to.deep.equal(config)
})
})
Expand All @@ -150,7 +153,7 @@ module.exports = (common) => {
it('retrieve the whole config', () => {
return ipfs.config.get()
.then((config) => {
expect(config).to.exist
expect(config).to.exist()
})
})
})
Expand Down
Loading