scuttlebutt
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -11,5 +11,5 @@ var Scuttlebutt = require('./') | ||
function ReliableEventEmitter (id) { | ||
if(!(this instanceof ReliableEventEmitter)) return new ReliableEventEmitter(id) | ||
Scuttlebutt.call(this, id) | ||
function ReliableEventEmitter (opts) { | ||
if(!(this instanceof ReliableEventEmitter)) return new ReliableEventEmitter(opts) | ||
Scuttlebutt.call(this, opts) | ||
} | ||
@@ -16,0 +16,0 @@ |
85
index.js
@@ -24,19 +24,6 @@ //really simple data replication. | ||
function validate (data) { | ||
//must be an 4 element array | ||
//string, *, string, number | ||
//log a message and ignore if invalid. | ||
function error () { | ||
console.error('invalid update', data) | ||
} | ||
var key = data[0], ts = data[2], source = data[3] | ||
/*console.log(!Array.isArray(data) | ||
, data.length !== 4 | ||
, 'string' !== typeof key | ||
, 'string' !== typeof source | ||
, 'number' !== typeof ts | ||
)*/ | ||
if( !Array.isArray(data) | ||
|| data.length !== 4 | ||
|| data.length < 4 | ||
|| 'string' !== typeof key | ||
@@ -46,3 +33,3 @@ || 'string' !== typeof source | ||
) | ||
return error(), false | ||
return false | ||
@@ -54,6 +41,17 @@ return true | ||
function Scuttlebutt (id) { | ||
if(!(this instanceof Scuttlebutt)) return new Scuttlebutt(id) | ||
function Scuttlebutt (opts) { | ||
var secure = opts && opts.security | ||
if(!(this instanceof Scuttlebutt)) return new Scuttlebutt(opts) | ||
var id = 'string' === typeof opts ? opts : opts && opts.id | ||
this.sources = {} | ||
this.id = id = id || u.createID() | ||
if(secure) { | ||
// id should be camelcased "Id" not "ID". | ||
// as it's a abbreviation, not an acronym. | ||
this.id = id || secure.createId() | ||
this._sign = secure.sign | ||
this._verify = secure.verify | ||
} else { | ||
this.id = id || u.createId() | ||
} | ||
} | ||
@@ -78,3 +76,2 @@ | ||
var source = update[3] | ||
//if this message is old for it's source, | ||
@@ -90,14 +87,42 @@ //ignore it. it's out of order. | ||
//check if this message is older than | ||
//the value we already have. | ||
//do nothing if so | ||
//emit an 'old-data' event because i'll want to track how many | ||
//unnecessary messages are sent. | ||
if(this.applyUpdate(update)) { | ||
emit.call(this, '_update', update) | ||
return true | ||
var self = this | ||
function didVerification (err, verified) { | ||
// I'm not sure how what should happen if a async verification | ||
// errors. if it's an key not found - that is a verification fail, | ||
// not a error. if it's genunie error, really you should queue and | ||
// try again? or replay the message later | ||
// -- this should be done my the security plugin though, not scuttlebutt. | ||
if(err) | ||
self.emit('error', err) | ||
if(!verified) | ||
return EventEmitter.prototype.emit.call(self, 'unverified_data', update) | ||
// check if this message is older than | ||
// the value we already have. | ||
// do nothing if so | ||
// emit an 'old-data' event because i'll want to track how many | ||
// unnecessary messages are sent. | ||
if(self.applyUpdate(update)) | ||
emit.call(self, '_update', update) | ||
} | ||
//key, value, timestamp, source | ||
return false | ||
if(source !== this.id) { | ||
if(this._verify) | ||
this._verify(update, didVerification) | ||
else | ||
didVerification(null, true) | ||
} else { | ||
if(this._sign) { | ||
//could make this async easily enough. | ||
update[4] = this._sign(update) | ||
} | ||
didVerification(null, true) | ||
} | ||
return true | ||
} | ||
@@ -171,3 +196,3 @@ | ||
var data = out.shift() | ||
console.log('>>', data) | ||
if(!data && !opts.tail) | ||
@@ -174,0 +199,0 @@ return this.emit('end'), null |
@@ -10,5 +10,5 @@ var Scuttlebutt = require('./index') | ||
function Model (id) { | ||
if(!(this instanceof Model)) return new Model(id) | ||
Scuttlebutt.call(this, id) | ||
function Model (opts) { | ||
if(!(this instanceof Model)) return new Model(opts) | ||
Scuttlebutt.call(this, opts) | ||
this.store = {} | ||
@@ -15,0 +15,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "replicate data via scuttlebutt protocol", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"homepage": "https://github.com/dominictarr/scuttlebutt", | ||
@@ -16,4 +16,7 @@ "repository": { | ||
"stream-serializer": "0.0.0", | ||
"readable-stream" :"0.0.0" | ||
"readable-stream" :"~0.0.1" | ||
}, | ||
"devDependencies": { | ||
"macgyver" : "~1.8.0" | ||
}, | ||
"bundleDependencies": ["readable-stream"], | ||
@@ -20,0 +23,0 @@ "scripts": { |
var ReliableEventEmitter = require('../events') | ||
var assert = require('assert') | ||
var mac = require('macgyver')() | ||
var A = new ReliableEventEmitter() | ||
var B = new ReliableEventEmitter() | ||
process.on('exit', mac.validate) | ||
function log (data) { | ||
console.log(this.id, data) | ||
function allow (update, cb) { | ||
return cb(null, true) | ||
} | ||
var insecure = { | ||
sign: Math.random, verify: allow | ||
} | ||
var A = new ReliableEventEmitter({ id: 'a', security: insecure }) | ||
var B = new ReliableEventEmitter({ id: 'b', security: insecure }) | ||
function log (data) { | ||
console.log('LOG', this.id, data) | ||
} | ||
function old (data) { | ||
@@ -21,6 +31,6 @@ console.log('OLD', data, | ||
A.on('a', log) | ||
A.on('a', function (data) { _a.push(data) }) | ||
A.on('a', mac(function (data) { _a.push(data) }).times(6)) | ||
B.on('a', log) | ||
B.on('a', function (data) { _b.push(data) }) | ||
B.on('a', mac(function (data) { _b.push(data) }).times(6)) | ||
@@ -27,0 +37,0 @@ A.emit('a', 'aardvark') |
@@ -6,3 +6,3 @@ | ||
var timestamp = require('../util').timestamp | ||
var createID = require('../util').createID | ||
var createId = require('../util').createId | ||
@@ -23,3 +23,3 @@ function test(name, test) { | ||
, true | ||
, 'write returns true to indicate update applied') | ||
, 'update returns true to indicate was not old') | ||
@@ -44,5 +44,5 @@ console.log(g.store) | ||
test('can filter histroy with {sources: timestamps}', function (g) { | ||
var A = createID() | ||
var B = createID() | ||
var C = createID() | ||
var A = createId() | ||
var B = createId() | ||
var C = createId() | ||
var ts = timestamp() | ||
@@ -49,0 +49,0 @@ |
var map = require('iterate').map | ||
exports.createID = | ||
function createID () { | ||
exports.createId = | ||
function () { | ||
return map(3, function (i) { | ||
@@ -6,0 +6,0 @@ return Math.random().toString(16).substring(2).toUpperCase() |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
55090
38
1029
0
1
5
1
Updatedreadable-stream@~0.0.1