Comparing version 5.1.2 to 5.1.3
26
index.js
@@ -15,3 +15,3 @@ var hypercore = require('hypercore') | ||
// Key-less constant hypercore to bootstrap hypercore-protocol replication. | ||
var defaultEncryptionKey = new Buffer('bee80ff3a4ee5e727dc44197cb9d25bf8f19d50b0f3ad2984cfe5b7d14e75de7', 'hex') | ||
var defaultEncryptionKey = Buffer.from('bee80ff3a4ee5e727dc44197cb9d25bf8f19d50b0f3ad2984cfe5b7d14e75de7', 'hex') | ||
@@ -22,3 +22,3 @@ module.exports = Multifeed | ||
if (!(this instanceof Multifeed)) return new Multifeed(storage, opts) | ||
this._id = (opts||{})._id || Math.floor(Math.random() * 1000).toString(16) // for debugging | ||
this._id = (opts || {})._id || Math.floor(Math.random() * 1000).toString(16) // for debugging | ||
debug(this._id, 'multifeed @ ' + version) | ||
@@ -64,3 +64,2 @@ this._feeds = {} | ||
var storageName = encryptionKey.toString('hex') | ||
var feed = hypercore(ram, encryptionKey) | ||
@@ -143,6 +142,6 @@ | ||
function next (n) { | ||
var storage = self._storage(''+n) | ||
var storage = self._storage('' + n) | ||
var st = storage('key') | ||
st.read(0, 4, function (err) { | ||
if (err) return done() // means there are no more feeds to read | ||
if (err) return done() // means there are no more feeds to read | ||
debug(self._id + ' [INIT] loading feed #' + n) | ||
@@ -206,3 +205,3 @@ pending++ | ||
var len = Object.keys(self._feeds).length | ||
var storage = self._storage(''+len) | ||
var storage = self._storage('' + len) | ||
@@ -257,3 +256,3 @@ var idx = name || String(len) | ||
var self = this | ||
var mux = multiplexer(isInitiator, self._root.key, Object.assign({}, opts, {_id:this._id})) | ||
var mux = multiplexer(isInitiator, self._root.key, Object.assign({}, opts, {_id: this._id})) | ||
@@ -286,6 +285,6 @@ // Add key exchange listener | ||
// Start streaming | ||
this.ready(function(err){ | ||
this.ready(function (err) { | ||
if (err) return mux.stream.destroy(err) | ||
if (mux.stream.destroyed) return | ||
mux.ready(function(){ | ||
mux.ready(function () { | ||
var keys = values(self._feeds).map(function (feed) { return feed.key.toString('hex') }) | ||
@@ -366,10 +365,8 @@ mux.offerFeeds(keys) | ||
if (!this._streams.length) return // no-op if no live-connections | ||
var self = this | ||
var hexKey = feed.key.toString('hex'); | ||
var hexKey = feed.key.toString('hex') | ||
// Tell each remote that we have a new key available unless | ||
// it's already being replicated | ||
this._streams.forEach(function(mux) { | ||
this._streams.forEach(function (mux) { | ||
if (mux.knownFeeds().indexOf(hexKey) === -1) { | ||
self._streams | ||
debug("Forwarding new feed to existing peer:", hexKey) | ||
debug('Forwarding new feed to existing peer:', hexKey) | ||
mux.offerFeeds([hexKey]) | ||
@@ -404,5 +401,4 @@ } | ||
function values (obj) { | ||
return Object.keys(obj).map(function (k) { return obj[k] }) | ||
} |
52
mux.js
@@ -1,2 +0,1 @@ | ||
var crypto = require('hypercore-crypto') | ||
var Protocol = require('hypercore-protocol') | ||
@@ -37,3 +36,2 @@ var readify = require('./ready') | ||
var discoveryKey = crypto.discoveryKey(key) | ||
var onFirstKey = true | ||
@@ -46,3 +44,2 @@ var stream = this.stream = new Protocol(isInitiator, Object.assign({}, opts, { | ||
self._finalize(new Error('Exchange key did not match remote')) | ||
return | ||
} | ||
@@ -63,6 +60,7 @@ } | ||
debug(self._id + ' [REPLICATION] recv\'d handshake: ', JSON.stringify(header)) | ||
var err | ||
if (!compatibleVersions(header.version, PROTOCOL_VERSION)) { | ||
debug(self._id + ' [REPLICATION] aborting; version mismatch (us='+PROTOCOL_VERSION+')') | ||
var err = new Error('protocol version mismatch! us='+PROTOCOL_VERSION + ' them=' + header.version) | ||
debug(self._id + ' [REPLICATION] aborting; version mismatch (us=' + PROTOCOL_VERSION + ')') | ||
err = new Error('protocol version mismatch! us=' + PROTOCOL_VERSION + ' them=' + header.version) | ||
err.code = ERR_VERSION_MISMATCH | ||
@@ -75,5 +73,5 @@ err.usVersion = PROTOCOL_VERSION | ||
if (header.client != MULTIFEED) { | ||
if (header.client !== MULTIFEED) { | ||
debug(self._id + ' [REPLICATION] aborting; Client mismatch! expected ', MULTIFEED, 'but got', header.client) | ||
var err = new Error('Client mismatch! expected ' + MULTIFEED + ' but got ' + header.client) | ||
err = new Error('Client mismatch! expected ' + MULTIFEED + ' but got ' + header.client) | ||
err.code = ERR_CLIENT_MISMATCH | ||
@@ -93,3 +91,3 @@ err.usClient = MULTIFEED | ||
// Open a virtual feed that has the key set to the shared key. | ||
var feed = this._feed = stream.open(key, { | ||
this._feed = stream.open(key, { | ||
onopen: function () { | ||
@@ -146,3 +144,3 @@ onFirstKey = false | ||
if (!self._opts.live ) { | ||
if (!self._opts.live) { | ||
self.stream.on('prefinalize', function () { | ||
@@ -155,3 +153,3 @@ self._feed.close() | ||
this._ready = readify(function (done) { | ||
self.on('ready', function(remote){ | ||
self.on('ready', function (remote) { | ||
debug(self._id + ' [REPLICATION] remote connected and ready') | ||
@@ -165,7 +163,7 @@ done(remote) | ||
Multiplexer.prototype.ready = function(cb) { | ||
Multiplexer.prototype.ready = function (cb) { | ||
this._ready(cb) | ||
} | ||
Multiplexer.prototype._finalize = function(err) { | ||
Multiplexer.prototype._finalize = function (err) { | ||
if (err) { | ||
@@ -206,3 +204,3 @@ debug(this._id + ' [REPLICATION] destroyed due to', err) | ||
var self = this | ||
var filtered = keys.filter(function(key) { | ||
var filtered = keys.filter(function (key) { | ||
if (self._localOffer.indexOf(key) === -1) { | ||
@@ -227,3 +225,3 @@ debug('[REPLICATION] Warning, remote requested feed that is not in offer', key) | ||
var self = this | ||
var filtered = keys.filter(function(key) { | ||
var filtered = keys.filter(function (key) { | ||
return self._requestedFeeds.indexOf(key) !== -1 | ||
@@ -238,3 +236,3 @@ }) | ||
// the main stream. | ||
Multiplexer.prototype._replicateFeeds = function(keys) { | ||
Multiplexer.prototype._replicateFeeds = function (keys) { | ||
var self = this | ||
@@ -254,3 +252,3 @@ keys = uniq(keys) | ||
function startFeedReplication(feeds){ | ||
function startFeedReplication (feeds) { | ||
if (!Array.isArray(feeds)) feeds = [feeds] | ||
@@ -263,4 +261,4 @@ | ||
// hypercore-protocol has built in protection against receiving unexpected/not asked for data. | ||
feeds.forEach(function(feed) { | ||
feed.ready(function() { // wait for each feed to be ready before replicating. | ||
feeds.forEach(function (feed) { | ||
feed.ready(function () { // wait for each feed to be ready before replicating. | ||
var hexKey = feed.key.toString('hex') | ||
@@ -286,7 +284,7 @@ | ||
var cleanup = function(err, res) { | ||
var cleanup = function (_, res) { | ||
if (!self._activeFeedStreams[hexKey]) return | ||
// delete feed stream reference | ||
delete self._activeFeedStreams[hexKey] | ||
debug(self._id, "[REPLICATION] feedStream closed:", hexKey.substr(0,8)) | ||
debug(self._id, '[REPLICATION] feedStream closed:', hexKey.substr(0, 8)) | ||
} | ||
@@ -297,2 +295,7 @@ fStream.once('end', cleanup) | ||
}) | ||
if (feeds.length === 0) { | ||
debug('[REPLICATION] terminating mux: no feeds to sync') | ||
self._feed.close() | ||
} | ||
} | ||
@@ -316,3 +319,3 @@ } | ||
if (!Array.isArray(keys)) keys = [keys] | ||
return keys.map(function(o) { | ||
return keys.map(function (o) { | ||
if (typeof o === 'string') return o | ||
@@ -322,11 +325,10 @@ if (typeof o === 'object' && o.key) return o.key.toString('hex') | ||
}) | ||
.filter(function(o) { return !!o }) // remove invalid entries | ||
.filter(function (o) { return !!o }) // remove invalid entries | ||
} | ||
function uniq (arr) { | ||
return Object.keys(arr.reduce(function(m, i) { | ||
m[i]=true | ||
return Object.keys(arr.reduce(function (m, i) { | ||
m[i] = true | ||
return m | ||
}, {})).sort() | ||
} | ||
@@ -5,3 +5,3 @@ { | ||
"author": "Stephen Whitmore <sww@eight.net>", | ||
"version": "5.1.2", | ||
"version": "5.1.3", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "url": "git://github.com/noffle/multifeed.git" |
@@ -17,3 +17,3 @@ # multifeed | ||
have locally, and choose which of the remote feeds they'd like to download in | ||
exchange. Right now, the replication mechanism defauls to sharing all local | ||
exchange. Right now, the replication mechanism defaults to sharing all local | ||
feeds and downloading all remote feeds. | ||
@@ -20,0 +20,0 @@ |
27610
631