epidemic-broadcast-trees
Advanced tools
Comparing version 6.0.1 to 6.1.0
@@ -16,2 +16,19 @@ 'use strict' | ||
function isEmpty (o) { | ||
for(var k in o) return false | ||
return true | ||
} | ||
function isObject (o) { | ||
return o && 'object' === typeof o | ||
} | ||
function isBlocked(state, id, target) { | ||
return state.blocks[id] && state.blocks[id][target] | ||
} | ||
function isShared (state, id, peer_id) { | ||
return state.follows[id] && !isBlocked(state, id, peer_id) | ||
} | ||
//check if a feed is already being replicated on another peer from ignore_id | ||
@@ -35,2 +52,3 @@ function isAlreadyReplicating(state, feed_id, ignore_id) { | ||
var peer = state.peers[peer_id] | ||
//BLOCK: check wether id has blocked this peer | ||
if((peer.clock[feed_id] || 0) > (state.clock[feed_id] || 0)) { | ||
@@ -71,2 +89,3 @@ return true | ||
follows: {}, | ||
blocks: {}, | ||
peers: {}, | ||
@@ -84,2 +103,4 @@ receive: [] | ||
if(state.peers[ev.id]) throw new Error('already connected to peer:'+ev.id) | ||
if(isBlocked(state, state.id, ev.id)) return state | ||
state.peers[ev.id] = { | ||
@@ -125,3 +146,4 @@ clock: null, | ||
var seq = clock[id], lseq = state.clock[id] || 0 | ||
if(state.follows[id] && seq !== -1 && seq !== lseq) { | ||
//BLOCK: check wether id has blocked this peer | ||
if(isShared(state, id, ev.id) && seq !== -1 && seq !== lseq) { | ||
@@ -151,3 +173,6 @@ //if we are already replicating, and this feed is at zero, ask for it anyway, | ||
var peer = state.peers[id] | ||
if(!peer.clock || !peer.replicating) continue | ||
if(!peer.clock || !peer.replicating || !isShared(state, ev.id, id)) continue | ||
//BLOCK: check wether this feed has has blocked this peer. | ||
//..... don't replicate feeds with peers that have blocked them at all? | ||
//cases: | ||
@@ -183,3 +208,5 @@ // don't have feed | ||
if(!peer.replicating) continue; | ||
//BLOCK: check wether id has blocked this peer | ||
var rep = peer.replicating[msg.author] | ||
if(rep && rep.tx && rep.sent === msg.sequence - 1) { | ||
@@ -211,3 +238,5 @@ rep.sent ++ | ||
var peer = state.peers[id] | ||
if(!peer.clock || !peer.replicating) continue | ||
if(!peer.clock || !peer.replicating || !isShared(state, msg.author, id)) continue | ||
//BLOCK: check wether msg.author has blocked this peer | ||
var seq = peer.clock[msg.author] | ||
@@ -275,2 +304,8 @@ | ||
var clock = ev.value | ||
//support sending clocks inside a thing with additional properties. | ||
//this is to allow room for backwards compatible upgrades. | ||
if(isObject(ev.value.clock)) | ||
clock = ev.value.clock | ||
var peer = state.peers[ev.id] | ||
@@ -289,2 +324,3 @@ if(!peer) throw new Error('lost state of peer:'+ev.id) | ||
count ++ | ||
var seq = peer.clock[id] = getSequence(clock[id]) | ||
@@ -297,3 +333,4 @@ var tx = getReceive(clock[id]) //seq >= 0 | ||
//check if we are not following this feed. | ||
if(!state.follows[id]) { | ||
//BLOCK: or wether id has blocked this peer | ||
if(!isShared(state, id, ev.id)) { | ||
if(!peer.replicating[id]) | ||
@@ -389,2 +426,24 @@ setNotes(peer, id, -1) | ||
exports.block = function (state, ev) { | ||
if(!ev.value) { | ||
if(state.blocks[ev.id]) delete state.blocks[ev.id][ev.target] | ||
if(isEmpty(state.blocks[ev.id])) | ||
delete state.blocks[ev.id] | ||
} | ||
else { | ||
state.blocks[ev.id] = state.blocks[ev.id] || {} | ||
state.blocks[ev.id][ev.target] = true | ||
} | ||
for(var id in state.peers) { | ||
var peer = state.peers[id] | ||
if(!peer.replicating) continue | ||
if(id === ev.target && peer.replicating[ev.id]) | ||
setNotes(peer, ev.id, -1, false) | ||
} | ||
return state | ||
} | ||
return exports | ||
@@ -391,0 +450,0 @@ |
11
index.js
@@ -25,2 +25,6 @@ var events = require('./events')(require('./v3')) | ||
}, | ||
block: function (id, target, value) { | ||
self.state = events.block(self.state, {id: id, target: target, value: value !== false, ts: timestamp()}) | ||
self.update() | ||
}, | ||
createStream: function (remote_id, version, client) { | ||
@@ -56,5 +60,5 @@ if(this.streams[remote_id]) | ||
}, | ||
_append: function (err, msg) { | ||
if(msg) { | ||
self.onAppend(msg) | ||
_append: function (err, data) { | ||
if(data) { | ||
self.onAppend(data.value ? data.value : data) | ||
} | ||
@@ -97,1 +101,2 @@ else | ||
{ | ||
"name": "epidemic-broadcast-trees", | ||
"description": "", | ||
"version": "6.0.1", | ||
"version": "6.1.0", | ||
"homepage": "https://github.com/dominictarr/epidemic-broadcast-trees", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -140,2 +140,4 @@ var test = require('tape') | ||
clock: { alice: 1 }, | ||
follows: { alice: true }, | ||
blocks: {}, | ||
peers: { | ||
@@ -200,2 +202,3 @@ bob: { | ||
follows: { alice: true, bob: true, charles: true, darlene: false }, | ||
blocks: {}, | ||
peers: {} | ||
@@ -219,4 +222,4 @@ } | ||
clock: { alice: 3, bob: 2}, | ||
follows: { alice: true, bob: true}, | ||
peers: {} | ||
follows: { alice: true, bob: true}, blocks: {}, | ||
peers: {}, | ||
} | ||
@@ -242,3 +245,3 @@ | ||
clock: { alice: 3, bob: 2}, | ||
follows: { alice: true, bob: true}, | ||
follows: { alice: true, bob: true}, blocks: {}, | ||
peers: {} | ||
@@ -272,3 +275,3 @@ } | ||
clock: { alice: 3, bob: 2}, | ||
follows: { alice: true, bob: true}, | ||
follows: { alice: true, bob: true}, blocks: {}, | ||
peers: { | ||
@@ -307,3 +310,3 @@ bob: { | ||
clock: { alice: 3, bob: 2}, | ||
follows: { alice: true, bob: true}, | ||
follows: { alice: true, bob: true}, blocks: {}, | ||
peers: {} | ||
@@ -327,3 +330,3 @@ } | ||
clock: { alice: 3, bob: 2}, | ||
follows: { alice: true, bob: true}, | ||
follows: { alice: true, bob: true}, blocks: {}, | ||
peers: {} | ||
@@ -349,3 +352,3 @@ } | ||
clock: { alice: 3, bob: 2}, | ||
follows: { alice: true, bob: true}, | ||
follows: { alice: true, bob: true}, blocks: {}, | ||
peers: {} | ||
@@ -374,3 +377,3 @@ } | ||
clock: { alice: 3, bob: 2}, | ||
follows: {}, | ||
follows: {}, blocks: {}, | ||
peers: {} | ||
@@ -407,3 +410,3 @@ } | ||
clock: { alice: 3, bob: 2}, | ||
follows: {alice: true}, | ||
follows: {alice: true}, blocks: {}, | ||
peers: {} | ||
@@ -421,3 +424,20 @@ } | ||
test('notes can be passed inside {clock:{}} object', function (t) { | ||
var state = { | ||
clock: { alice: 3, bob: 2}, | ||
follows: {alice: true}, blocks: {}, | ||
peers: {} | ||
} | ||
state = events.connect(state, {id: 'bob'}) | ||
state = events.peerClock(state, {id: 'bob', value:{alice: -1}}) | ||
state = events.notes(state, {id: 'bob', value: { | ||
clock: {bob: note(4, true)} | ||
}}) | ||
t.equal(state.peers.bob.clock.bob, 4) | ||
t.end() | ||
}) | ||
} | ||
@@ -424,0 +444,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77819
29
1820