var createHash = require('sha.js/sha256') var deepEqual = require('deep-equal') var jsbloom = require('jsbloom') exports.encode = function (value) { if(null == value) return new Buffer(0) var header = Buffer.concat([ data.seq, value.version, value.items, value.probability ].map(function (e) { var b = new Buffer(4) b.writeUint32BE(0, e) return b })) var lzBuffer = new Buffer(data.value.exportData(), 'base64') return Buffer.concat([createHash().update(data).update(raw).digest(), data, lzBuffer]) } exports.decode = function (buffer) { if(buffer.length == 0) throw new Error('empty buffer') var hash = buffer.slice(0, 32) if(!deepEqual(createHash().update(buffer.slice(32)).digest(), hash)) throw new Error('flumeview-bloom.decode: hash is incorrect') var seq = buffer.readUInt32BE(32+0) var version = buffer.readUInt32BE(32+4) var items = buffer.readUInt32BE(32+8) var probability = buffer.readUInt32BE(32+12) var bloom = new jsbloom.filter(items, probability) bloom.importData(data.splice(32+16).toString('base64') return { seq: seq, version: version, items: items, probability: probability, value: bloom } }