Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

es6ify #2

Merged
merged 1 commit into from
Jan 29, 2016
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
76 changes: 36 additions & 40 deletions src/block-service.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,79 @@
var Block = require('./block')
var bl = require('bl')
var async = require('async')
const Block = require('./block')
const bl = require('bl')
const async = require('async')

module.exports = BlockService

// BlockService is a hybrid block datastore. It stores data in a local
// datastore and may retrieve data from a remote Exchange.
// It uses an internal `datastore.Datastore` instance to store values.
function BlockService (ipfsRepo, exchange) {
this.addBlock = addBlock
function addBlock (block, cb) {
this.addBlock = (block, callback) => {
var ws = ipfsRepo.datastore.createWriteStream(block.key)
ws.write(block.data)
ws.on('finish', cb)
ws.on('finish', callback)
ws.end()
}

this.addBlocks = function (blocks, cb) {
this.addBlocks = (blocks, callback) => {
if (!Array.isArray(blocks)) {
return cb(new Error('expects an array of Blocks'))
return callback(new Error('expects an array of Blocks'))
}

async.each(blocks, function (block, next) {
addBlock(block, next)
}, function (err) {
cb(err)
async.each(blocks, (block, next) => {
this.addBlock(block, next)
}, (err) => {
callback(err)
})
}

this.getBlock = getBlock
function getBlock (multihash, cb) {
this.getBlock = (multihash, callback) => {
if (!multihash) {
return cb(new Error('Invalid multihash'))
return callback(new Error('Invalid multihash'))
}

ipfsRepo.datastore.createReadStream(multihash)
.pipe(bl(function (err, data) {
if (err) {
return cb(err)
}
cb(null, new Block(data))
.pipe(bl((err, data) => {
if (err) { return callback(err) }
callback(null, new Block(data))
}))
}

this.getBlocks = function (multihashes, cb) {
this.getBlocks = (multihashes, callback) => {
if (!Array.isArray(multihashes)) {
return cb(new Error('Invalid batch of multihashes'))
return callback(new Error('Invalid batch of multihashes'))
}

var blocks = []
async.each(multihashes, function (multihash, next) {
getBlock(multihash, function (err, block) {
if (err) {
return next(err)
}

async.each(multihashes, (multihash, next) => {
this.getBlock(multihash, (err, block) => {
if (err) { return next(err) }
blocks.push(block)
})
}, function (err) {
cb(err, blocks)
}, (err) => {
callback(err, blocks)
})
}

this.deleteBlock = deleteBlock
function deleteBlock (multihash, cb) {
this.deleteBlock = (multihash, callback) => {
if (!multihash) {
return cb(new Error('Invalid multihash'))
return callback(new Error('Invalid multihash'))
}

ipfsRepo.datastore.remove(multihash, cb)
ipfsRepo.datastore.remove(multihash, callback)
}

this.deleteBlocks = function (multihashes, cb) {
this.deleteBlocks = (multihashes, callback) => {
if (!Array.isArray(multihashes)) {
return cb('Invalid batch of multihashes')
return callback('Invalid batch of multihashes')
}

async.each(multihashes, function (multihash, next) {
deleteBlock(multihash, next)
}, function (err) {
cb(err)
async.each(multihashes, (multihash, next) => {
this.deleteBlock(multihash, next)
}, (err) => {
callback(err)
})
}
}
module.exports = BlockService

13 changes: 5 additions & 8 deletions src/block.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
var util = require('./util')
const util = require('./util')

// Immutable block of data

module.exports = Block

function Block (data) {
if (!data) {
throw new Error('Block must be constructed with data')
}
if (!data) { throw new Error('Block must be constructed with data') }

if (!(this instanceof Block)) {
return new Block(data)
}
if (!(this instanceof Block)) { return new Block(data) }

this.data = new Buffer(data)
this.key = util.hash(this.data)
}
module.exports = Block
11 changes: 3 additions & 8 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
var multihashing = require('multihashing')

var util = {}
exports = module.exports

// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
util.hash = function (data) {
return multihashing(data, 'sha2-256')
}

util.isBrowser = function () { return !!global.window }

module.exports = util
exports.hash = (data) => { return multihashing(data, 'sha2-256') }
exports.isBrowser = () => { return !!global.window }
60 changes: 30 additions & 30 deletions tests/block-service-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ const BlockService = require('../src').BlockService

const IPFSRepo = require('ipfs-repo')

describe('block-service', function () {
describe('block-service', () => {
var bs

it('create a block-service', function (done) {
var repo = new IPFSRepo(process.env.IPFS_PATH)
it('create a block-service', (done) => {
const repo = new IPFSRepo(process.env.IPFS_PATH)
bs = new BlockService(repo)
expect(bs).to.exist
done()
})

it('store a block', function (done) {
var b = new Block('A random data block')
bs.addBlock(b, function (err) {
it('store a block', (done) => {
const b = new Block('A random data block')
bs.addBlock(b, (err) => {
expect(err).to.not.exist
bs.getBlock(b.key, function (err, block) {
bs.getBlock(b.key, (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)
Expand All @@ -31,63 +31,63 @@ describe('block-service', function () {
})
})

it('get a non existent block', function (done) {
var b = new Block('Not stored')
bs.getBlock(b.key, function (err, block) {
it('get a non existent block', (done) => {
const b = new Block('Not stored')
bs.getBlock(b.key, (err, block) => {
expect(err).to.exist
done()
})
})

it('store many blocks', function (done) {
var b1 = new Block('1')
var b2 = new Block('2')
var b3 = new Block('3')
it('store many blocks', (done) => {
const b1 = new Block('1')
const b2 = new Block('2')
const b3 = new Block('3')

var blocks = []
const blocks = []
blocks.push(b1)
blocks.push(b2)
blocks.push(b3)

bs.addBlocks(blocks, function (err) {
bs.addBlocks(blocks, (err) => {
expect(err).to.not.exist
done()
})
})

it('delete a block', function (done) {
var b = new Block('Will not live that much')
bs.addBlock(b, function (err) {
it('delete a block', (done) => {
const b = new Block('Will not live that much')
bs.addBlock(b, (err) => {
expect(err).to.not.exist
bs.deleteBlock(b.key, function (err) {
bs.deleteBlock(b.key, (err) => {
expect(err).to.not.exist
bs.getBlock(b.key, function (err, block) {
bs.getBlock(b.key, (err, block) => {
expect(err).to.exist
done()
})
})
})
})

it('block-service: \t delete a non existent block', function (done) {
var b = new Block('I do not exist')
bs.deleteBlock(b.key, function (err) {
it('delete a non existent block', (done) => {
const b = new Block('I do not exist')
bs.deleteBlock(b.key, (err) => {
expect(err).to.not.exist
done()
})
})

it('block-service: \t delete many blocks', function (done) {
var b1 = new Block('1')
var b2 = new Block('2')
var b3 = new Block('3')
it('delete many blocks', (done) => {
const b1 = new Block('1')
const b2 = new Block('2')
const b3 = new Block('3')

var blocks = []
const blocks = []
blocks.push(b1.key)
blocks.push(b2.key)
blocks.push(b3.key)

bs.deleteBlocks(blocks, function (err) {
bs.deleteBlocks(blocks, (err) => {
expect(err).to.not.exist
done()
})
Expand Down
18 changes: 9 additions & 9 deletions tests/block-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
const expect = require('chai').expect
const Block = require('../src').Block

describe('block', function () {
it('block: \t\t create a new block', function (done) {
var b = new Block('random-data')
describe('block', () => {
it('block: \t\t create a new block', (done) => {
const b = new Block('random-data')
expect(b.key).to.exist
expect(b.data).to.exist
done()
})

it('fail to create an empty block', function (done) {
it('fail to create an empty block', (done) => {
var b
try {
b = new Block()
Expand All @@ -23,17 +23,17 @@ describe('block', function () {
}
})

it('2 different blocks have different hashes', function (done) {
var b1 = new Block('random-data')
var b2 = new Block('more-random-data')
it('2 different blocks have different hashes', (done) => {
const b1 = new Block('random-data')
const b2 = new Block('more-random-data')
expect(b1).to.not.deep.equal(b2)
done()
})

it.skip('block stays immutable', function (done) {
it.skip('block stays immutable', (done) => {
// it from the original implementation
// It doesn't stricly verify the immutability of the Block object
var block = new Block("Can't change this!")
const block = new Block("Can't change this!")
var key = block.key
key = new Buffer('new key')

Expand Down