Comparing version 1.3.0 to 1.3.1
42
index.js
@@ -11,6 +11,6 @@ var raf = require('random-access-file') | ||
module.exports = Multicore | ||
module.exports = Multifeed | ||
function Multicore (hypercore, storage, opts) { | ||
if (!(this instanceof Multicore)) return new Multicore(hypercore, storage, opts) | ||
function Multifeed (hypercore, storage, opts) { | ||
if (!(this instanceof Multifeed)) return new Multifeed(hypercore, storage, opts) | ||
@@ -47,5 +47,5 @@ this._feeds = {} | ||
inherits(Multicore, events.EventEmitter) | ||
inherits(Multifeed, events.EventEmitter) | ||
Multicore.prototype._addFeed = function (feed, name) { | ||
Multifeed.prototype._addFeed = function (feed, name) { | ||
this._feeds[name] = feed | ||
@@ -56,7 +56,7 @@ this._feedKeyToFeed[feed.key.toString('hex')] = feed | ||
Multicore.prototype.ready = function (cb) { | ||
Multifeed.prototype.ready = function (cb) { | ||
this._ready(cb) | ||
} | ||
Multicore.prototype._loadFeeds = function (cb) { | ||
Multifeed.prototype._loadFeeds = function (cb) { | ||
var self = this | ||
@@ -84,5 +84,3 @@ | ||
// TODO: wrap this in a mutex in case of two writers being created at the same | ||
// time | ||
Multicore.prototype.writer = function (name, cb) { | ||
Multifeed.prototype.writer = function (name, cb) { | ||
if (typeof name === 'function' && !cb) { | ||
@@ -127,7 +125,7 @@ cb = name | ||
Multicore.prototype.feeds = function () { | ||
Multifeed.prototype.feeds = function () { | ||
return Object.values(this._feeds) | ||
} | ||
Multicore.prototype.feed = function (key) { | ||
Multifeed.prototype.feed = function (key) { | ||
if (Buffer.isBuffer(key)) key = key.toString('hex') | ||
@@ -138,10 +136,10 @@ if (typeof key === 'string') return this._feedKeyToFeed[key] | ||
Multicore.prototype.replicate = function (opts) { | ||
Multifeed.prototype.replicate = function (opts) { | ||
if (!opts) opts = {} | ||
var self = this | ||
opts.expectedFeeds = Object.keys(this._feeds).length | ||
opts.expectedFeeds = Object.keys(this._feeds).length + 1 | ||
var expectedFeeds = opts.expectedFeeds | ||
opts.download = true | ||
opts.encrypt = false | ||
opts.stream = protocol(opts) | ||
@@ -159,3 +157,3 @@ | ||
self._addFeed(feed, String(numFeeds)) | ||
replicate() | ||
feed.replicate(opts) | ||
} | ||
@@ -193,3 +191,3 @@ }) | ||
opts.stream.on('prefinalize', function (cb) { | ||
var numFeeds = Object.keys(self._feeds).length | ||
var numFeeds = Object.keys(self._feeds).length + 1 | ||
opts.stream.expectedFeeds += (numFeeds - expectedFeeds) | ||
@@ -205,8 +203,2 @@ expectedFeeds = numFeeds | ||
function replicate () { | ||
Object.values(self._feeds).forEach(function (feed) { | ||
feed.replicate(opts) | ||
}) | ||
} | ||
function onready (err) { | ||
@@ -218,3 +210,5 @@ if (err) return stream.destroy(err) | ||
replicate() | ||
Object.values(self._feeds).forEach(function (feed) { | ||
feed.replicate(opts) | ||
}) | ||
} | ||
@@ -221,0 +215,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"author": "Stephen Whitmore <sww@eight.net>", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "url": "git://github.com/noffle/multifeed.git" |
@@ -127,4 +127,10 @@ # multifeed | ||
## See Also | ||
- [multifeed-index](https://github.com/noffle/multifeed-index) | ||
- [hypercore](https://github.com/mafintosh/hypercore) | ||
- [kappa-core](https://github.com/noffle/kappa-core) | ||
## License | ||
ISC |
var test = require('tape') | ||
var hypercore = require('hypercore') | ||
var multicore = require('..') | ||
var multifeed = require('..') | ||
var ram = require('random-access-memory') | ||
@@ -8,3 +8,3 @@ var tmp = require('tmp').tmpNameSync | ||
test('no feeds', function (t) { | ||
var multi = multicore(hypercore, ram, { valueEncoding: 'json' }) | ||
var multi = multifeed(hypercore, ram, { valueEncoding: 'json' }) | ||
@@ -18,3 +18,3 @@ t.deepEquals(multi.feeds(), []) | ||
var multi = multicore(hypercore, ram, { valueEncoding: 'json' }) | ||
var multi = multifeed(hypercore, ram, { valueEncoding: 'json' }) | ||
@@ -37,3 +37,3 @@ multi.writer(function (err, w) { | ||
var multi = multicore(hypercore, ram, { valueEncoding: 'json' }) | ||
var multi = multifeed(hypercore, ram, { valueEncoding: 'json' }) | ||
@@ -52,3 +52,3 @@ multi.writer(function (err, w) { | ||
var multi = multicore(hypercore, ram, { valueEncoding: 'json' }) | ||
var multi = multifeed(hypercore, ram, { valueEncoding: 'json' }) | ||
@@ -64,7 +64,7 @@ multi.writer('bob', function (err, w) { | ||
test('replicate two multicores', function (t) { | ||
test('replicate two multifeeds', function (t) { | ||
t.plan(22) | ||
var m1 = multicore(hypercore, ram, { valueEncoding: 'json' }) | ||
var m2 = multicore(hypercore, ram, { valueEncoding: 'json' }) | ||
var m1 = multifeed(hypercore, ram, { valueEncoding: 'json' }) | ||
var m2 = multifeed(hypercore, ram, { valueEncoding: 'json' }) | ||
@@ -127,3 +127,3 @@ var feedEvents1 = 0 | ||
var multi = multicore(hypercore, storage, { valueEncoding: 'json' }) | ||
var multi = multifeed(hypercore, storage, { valueEncoding: 'json' }) | ||
@@ -137,3 +137,3 @@ multi.writer('minuette', function (err, w) { | ||
w._storage.close(function () { | ||
var multi2 = multicore(hypercore, storage, { valueEncoding: 'json' }) | ||
var multi2 = multifeed(hypercore, storage, { valueEncoding: 'json' }) | ||
multi.writer('minuette', function (err, w2) { | ||
@@ -148,3 +148,3 @@ t.error(err) | ||
test('regression test: concurrency of writer creation', function (t) { | ||
test('regression: concurrency of writer creation', function (t) { | ||
t.plan(3) | ||
@@ -155,3 +155,3 @@ | ||
var multi = multicore(hypercore, storage, { valueEncoding: 'json' }) | ||
var multi = multifeed(hypercore, storage, { valueEncoding: 'json' }) | ||
@@ -168,1 +168,82 @@ multi.writer('minuette', function (err, w) { | ||
}) | ||
test('regression: MF with no writer replicate to MF with 1 writer', function (t) { | ||
var m1 = multifeed(hypercore, ram, { valueEncoding: 'json' }) | ||
var m2 = multifeed(hypercore, ram, { valueEncoding: 'json' }) | ||
function setup1 (m, buf, cb) { | ||
m.writer(function (err, w) { | ||
t.error(err) | ||
var bufs = [] | ||
for(var i=0; i < 1000; i++) { | ||
bufs.push(buf) | ||
} | ||
w.append(bufs, function (err) { | ||
t.error(err) | ||
w.get(13, function (err, data) { | ||
t.error(err) | ||
t.equals(data, buf) | ||
t.deepEquals(m.feeds(), [w], 'read matches write') | ||
cb() | ||
}) | ||
}) | ||
}) | ||
} | ||
function setup2 (m, buf, cb) { | ||
m.writer(function (err, w) { | ||
t.error(err) | ||
var bufs = [] | ||
for(var i=0; i < 10; i++) { | ||
bufs.push(buf) | ||
} | ||
w.append(bufs, function (err) { | ||
t.error(err) | ||
w.get(3, function (err, data) { | ||
t.error(err) | ||
t.equals(data, buf) | ||
t.deepEquals(m.feeds(), [w], 'read matches write') | ||
cb() | ||
}) | ||
}) | ||
}) | ||
//cb() | ||
//m.writer(function (err, w) { | ||
// t.error(err) | ||
// cb() | ||
//}) | ||
} | ||
setup1(m1, 'foo', function () { | ||
setup2(m2, 'bar', function () { | ||
var r = m1.replicate() | ||
r.once('end', done) | ||
var s = m2.replicate() | ||
s.once('end', done) | ||
r.pipe(s).pipe(r) | ||
var pending = 2 | ||
function done () { | ||
if (!--pending) check() | ||
} | ||
}) | ||
}) | ||
function check () { | ||
t.equals(m1.feeds().length, 2, '2 feeds') | ||
t.equals(m2.feeds().length, 2, '2 feeds') | ||
t.equals(m1.feeds()[0].length, 1000, 'writer sees 1000 entries') | ||
t.equals(m1.feeds()[1].length, 10, 'writer sees 10 entries') | ||
t.equals(m2.feeds()[0].length, 10, 'receiver sees 10 entries') | ||
t.equals(m2.feeds()[1].length, 1000, 'receiver sees 1000 entries') | ||
m1.feeds()[1].get(0, function (err, data) { | ||
t.error(err) | ||
t.equals(data, 'bar', 'feed 1 has feed 2 data') | ||
m2.feeds()[1].get(0, function (err, data) { | ||
t.error(err) | ||
t.equals(data, 'foo', 'feed 2 has feed 1 data') | ||
t.end() | ||
}) | ||
}) | ||
} | ||
}) |
18203
449
136