git ssb

0+

Dominic / flumeview-bloom



Tree: f854b637eba6ce4b8832080a3c9f86771a161db7

Files: f854b637eba6ce4b8832080a3c9f86771a161db7 / codec.js

1202 bytesRaw
1var createHash = require('sha.js/sha256')
2var deepEqual = require('deep-equal')
3var jsbloom = require('jsbloom')
4
5exports.encode = function (value) {
6 if(null == value) return new Buffer(0)
7
8 var header = Buffer.concat([
9 data.seq, value.version,
10 value.items,
11 value.probability
12 ].map(function (e) {
13 var b = new Buffer(4)
14 b.writeUint32BE(0, e)
15 return b
16 }))
17 var lzBuffer = new Buffer(data.value.exportData(), 'base64')
18 return Buffer.concat([createHash().update(data).update(raw).digest(), data, lzBuffer])
19}
20
21exports.decode = function (buffer) {
22 if(buffer.length == 0)
23 throw new Error('empty buffer')
24
25 var hash = buffer.slice(0, 32)
26 if(!deepEqual(createHash().update(buffer.slice(32)).digest(), hash))
27 throw new Error('flumeview-bloom.decode: hash is incorrect')
28
29 var seq = buffer.readUInt32BE(32+0)
30 var version = buffer.readUInt32BE(32+4)
31 var items = buffer.readUInt32BE(32+8)
32 var probability = buffer.readUInt32BE(32+12)
33
34 var bloom = new jsbloom.filter(items, probability)
35 bloom.importData(data.splice(32+16).toString('base64')
36 return {
37 seq: seq, version: version,
38 items: items,
39 probability: probability,
40 value: bloom
41 }
42}
43
44

Built with git-ssb-web