Comparing version 3.0.4 to 3.0.5
20
index.js
@@ -15,2 +15,3 @@ var raf = require('random-access-file') | ||
if (!(this instanceof Multifeed)) return new Multifeed(hypercore, storage, opts) | ||
this._id = opts._id || Math.floor(Math.random() * 1000).toString(16) // for debugging | ||
this._feeds = {} | ||
@@ -43,3 +44,3 @@ this._feedKeyToFeed = {} | ||
if (self._opts.key) protocolEncryptionKey = Buffer.from(self._opts.key) | ||
else debug('Warning, running multifeed with unsecure default key') | ||
else debug(self._id + ' Warning, running multifeed with unsecure default key') | ||
@@ -51,3 +52,3 @@ var feed = hypercore(self._storage('_fake'), protocolEncryptionKey) | ||
self._loadFeeds(function () { | ||
debug('[INIT] finished loading feeds') | ||
debug(self._id + ' [INIT] finished loading feeds') | ||
done() | ||
@@ -107,3 +108,3 @@ }) | ||
function next (n) { | ||
debug('[INIT] loading feed #' + n) | ||
debug(self._id + ' [INIT] loading feed #' + n) | ||
var storage = self._storage(''+n) | ||
@@ -147,3 +148,3 @@ var st = storage('key') | ||
debug('[WRITER] creating new writer: ' + name) | ||
debug(self._id + ' [WRITER] creating new writer: ' + name) | ||
@@ -197,2 +198,3 @@ self.writerLock(function (release) { | ||
} | ||
if (!opts) opts = {} | ||
@@ -212,2 +214,3 @@ var self = this | ||
// Q(noffle): why do this? | ||
var key2feed = values(self._feeds).reduce(function(h,feed){ | ||
@@ -218,2 +221,3 @@ h[feed.key.toString('hex')] = feed | ||
// Q(noffle): does order matter to hypercore-protocol? | ||
var feeds = keys.map(function(k){ return key2feed[k] }) | ||
@@ -251,3 +255,3 @@ repl(feeds) | ||
var pending = 0 | ||
debug('[REPLICATION] recv\'d ' + keys.length + ' keys') | ||
debug(self._id + ' [REPLICATION] recv\'d ' + keys.length + ' keys') | ||
var filtered = keys.filter(function (key) { | ||
@@ -266,10 +270,10 @@ return !Number.isNaN(parseInt(key, 16)) && key.length === 64 | ||
try { | ||
debug('[REPLICATION] trying to create new local hypercore, key=' + key.toString('hex')) | ||
debug(self._id + ' [REPLICATION] trying to create new local hypercore, key=' + key.toString('hex')) | ||
feed = self._hypercore(storage, Buffer.from(key, 'hex'), self._opts) | ||
} catch (e) { | ||
debug('[REPLICATION] failed to create new local hypercore, key=' + key.toString('hex')) | ||
debug(self._id + ' [REPLICATION] failed to create new local hypercore, key=' + key.toString('hex')) | ||
if (!--pending) cb() | ||
return | ||
} | ||
debug('[REPLICATION] succeeded in creating new local hypercore, key=' + key.toString('hex')) | ||
debug(self._id + ' [REPLICATION] succeeded in creating new local hypercore, key=' + key.toString('hex')) | ||
self._addFeed(feed, String(numFeeds)) | ||
@@ -276,0 +280,0 @@ feed.ready(function () { |
45
mux.js
@@ -6,3 +6,2 @@ var protocol = require('hypercore-protocol') | ||
var debug = require('debug')('multifeed') | ||
var hypercore = require('hypercore') | ||
@@ -25,3 +24,5 @@ // constants | ||
if (!(this instanceof Multiplexer)) return new Multiplexer(key, opts) | ||
debug('[REPLICATION] New mux initialized', key.toString('hex'), opts) | ||
opts = opts || {} | ||
this._id = opts._id || Math.floor(Math.random() * 1000).toString(16) | ||
debug(this._id + ' [REPLICATION] New mux initialized', key.toString('hex'), opts) | ||
var self = this | ||
@@ -51,9 +52,9 @@ self._opts = opts = opts || {} | ||
} catch (err) { | ||
debug('[REPLICATION] Failed parsing JSON header', err) | ||
debug(self._id + ' [REPLICATION] Failed parsing JSON handshake', err) | ||
self._finalize(err) | ||
return | ||
} | ||
debug('[REPLICATION] recv\'d header: ', JSON.stringify(header)) | ||
debug(self._id + ' [REPLICATION] recv\'d handshake: ', JSON.stringify(header)) | ||
if (!compatibleVersions(header.version, PROTOCOL_VERSION)) { | ||
debug('[REPLICATION] aborting; version mismatch (us='+PROTOCOL_VERSION+')') | ||
debug(self._id + ' [REPLICATION] aborting; version mismatch (us='+PROTOCOL_VERSION+')') | ||
self._finalize(new Error('protocol version mismatch! us='+PROTOCOL_VERSION + ' them=' + header.version)) | ||
@@ -64,3 +65,3 @@ return | ||
if (header.client != MULTIFEED) { | ||
debug('[REPLICATION] aborting; Client mismatch! expected ', MULTIFEED, 'but got', header.client) | ||
debug(self._id + ' [REPLICATION] aborting; Client mismatch! expected ', MULTIFEED, 'but got', header.client) | ||
self._finalize(new Error('Client mismatch! expected ' + MULTIFEED + ' but got ' + header.client)) | ||
@@ -73,3 +74,3 @@ return | ||
feed.on('extension', function (type, message) { | ||
debug('Extension:', type, message.toString('utf8')) | ||
debug(self._id + ' Recv\'d extension:', type, message.toString('utf8')) | ||
switch(type) { | ||
@@ -90,3 +91,3 @@ case MANIFEST: | ||
self.stream.on('prefinalize', function(cb){ | ||
debug('[REPLICATION] feed finish/prefinalize', self.stream.expectedFeeds) | ||
debug(self._id + ' [REPLICATION] feed finish/prefinalize', self.stream.expectedFeeds) | ||
cb() | ||
@@ -96,6 +97,5 @@ }) | ||
this._ready = readify(function (done) { | ||
self.on('ready', function(remote){ | ||
debug('[REPLICATION] remote connected and ready') | ||
debug(self._id + ' [REPLICATION] remote connected and ready') | ||
done(remote) | ||
@@ -114,7 +114,7 @@ }) | ||
if (err) { | ||
debug('[REPLICATION] destroyed due to', err) | ||
debug(this._id + ' [REPLICATION] destroyed due to', err) | ||
this.emit('error', err) | ||
this.stream.destroy(err) | ||
} else { | ||
debug('[REPLICATION] finalized', err) | ||
debug(this._id + ' [REPLICATION] finalized', err) | ||
this.stream.finalize() | ||
@@ -133,3 +133,3 @@ } | ||
}) | ||
debug('[REPLICATON] sending manifest: ', opts) | ||
debug(this._id + ' [REPLICATON] sending manifest:', manifest) | ||
this._localHave = manifest.keys | ||
@@ -143,3 +143,3 @@ this._feed.extension(MANIFEST, Buffer.from(JSON.stringify(manifest))) | ||
keys = extractKeys(keys) | ||
debug('[REPLICATION] Sending feeds request', keys) | ||
debug(this._id + ' [REPLICATION] Sending feeds request', keys) | ||
this._feed.extension(REQUEST_FEEDS, Buffer.from(JSON.stringify(keys))) | ||
@@ -151,10 +151,11 @@ this._localWant = keys | ||
// this method is expected to be called twice, and will trigger | ||
// the 'replicate' event when both local and remote 'wants' are available. | ||
// calculating a sorted common denominator between both wants and availablility which | ||
// should result in two identical arrays being built on both ends using algorithm: | ||
// this method is expected to be called twice, and will trigger the 'replicate' | ||
// event when both local and remote 'wants' are available. calculating a sorted | ||
// common denominator between both wants and availablility which should result | ||
// in two identical arrays being built on both ends using algorithm: | ||
// | ||
// formula: feedsToReplicate = (lWant - (lWant - rHave)) + (rWant - (rWant - lHave )) | ||
// | ||
// The result honors that each node only shares what it offers and does not receive feeds that it didn't ask for. | ||
// The result honors that each node only shares what it offers and does not | ||
// receive feeds that it didn't ask for. | ||
Multiplexer.prototype._initRepl = function() { | ||
@@ -183,3 +184,3 @@ var self = this | ||
debug('[REPLICATION] _initRepl', keys.length, keys) | ||
debug(this._id + ' [REPLICATION] _initRepl', keys.length, keys) | ||
@@ -189,3 +190,3 @@ // End immedietly if there's nothing to replicate. | ||
this.emit('replicate', keys, startFeedReplication) | ||
this.emit('replicate', keys, startFeedReplication) | ||
@@ -202,3 +203,3 @@ return keys | ||
feed.ready(function() { // wait for each to be ready before replicating. | ||
debug('[REPLICATION] replicating feed:', feed.key.toString('hex')) | ||
debug(self._id + ' [REPLICATION] replicating feed:', feed.key.toString('hex')) | ||
feed.replicate(Object.assign({}, { | ||
@@ -205,0 +206,0 @@ live: self._opts.live, |
@@ -5,3 +5,3 @@ { | ||
"author": "Stephen Whitmore <sww@eight.net>", | ||
"version": "3.0.4", | ||
"version": "3.0.5", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "url": "git://github.com/noffle/multifeed.git" |
37680
1013