Skip to content

add tests for replicating with hypercore and with multifeed #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
"devDependencies": {
"hypercore": "^8.4.0",
"hypercore-crypto": "^1.0.0",
"multifeed": "^5.1.3",
"pump": "^3.0.0",
"random-access-memory": "^3.1.1",
"tape": "^4.11.0"
},
"dependencies": {
"sodium-universal": "^2.0.0",
"hypercore-protocol": "^7.7.1"
"hypercore-protocol": "^7.7.1",
"sodium-universal": "^2.0.0"
},
"description": ""
}
100 changes: 100 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const Protocol = require('hypercore-protocol')
const crypto = require('hypercore-crypto')
const { pipeline } = require('stream')
const authenticatedProtocol = require('.')
const hypercore = require('hypercore')
const ram = require('random-access-memory')
const multifeed = require('multifeed')

tape('basics', t => {
t.plan(2)
Expand Down Expand Up @@ -39,3 +42,100 @@ tape('basics', t => {
return protocol
}
})

tape('with replication of a hypercore', t => {
const auth1 = crypto.keyPair()
const auth2 = crypto.keyPair()

const feed1 = hypercore(ram)
feed1.on('ready', () => {
const feed2 = hypercore(ram, feed1.key)
feed1.append(Buffer.from('boop'), (err, seq) => {
t.error(err, 'No error on append')
const stream1 = init(true, auth1, 'one', feed1)
const stream2 = init(false, auth2, 'two', feed2)
stream1.pipe(stream2).pipe(stream1)
stream1.on('error', (err) => {
console.log(err)
})
stream1.on('end', () => {
t.error(err, 'no error on replicate')
t.ok(feed2.length, 'feed has replicated')
t.end()
})
})
})

function init (isInitiator, authKeyPair, name, feed) {
const protocol = new Protocol(isInitiator)
authenticatedProtocol(protocol, {
authKeyPair,
onauthenticate (publicKey, cb) {
if (name === 'one' && publicKey.equals(auth2.publicKey)) {
return cb(null, true)
}
if (name === 'two' && publicKey.equals(auth1.publicKey)) {
return cb(null, true)
}
cb(null, false)
},
onprotocol (protocol) {
t.ok(protocol, 'Protocol ok')
feed.replicate(isInitiator, { stream: protocol })
}
})
return protocol
}
})

tape('using multifeed', t => {
const auth1 = crypto.keyPair()
const auth2 = crypto.keyPair()

const multi1 = multifeed(ram)
const multi2 = multifeed(ram)

multi1.writer('test', (err, feed1) => {
t.error(err, 'No error on create writer')
feed1.append(Buffer.from('boop'), (err, seq) => {
t.error(err, 'No error on append')
const stream1 = init(true, auth1, 'one', multi1)
const stream2 = init(false, auth2, 'two', multi2)
stream1.pipe(stream2).pipe(stream1)
stream1.on('error', (err) => {
console.log(err)
})
stream1.on('end', () => {
t.error(err, 'no error on replicate')
multi2.feeds()[0].get(0, (err, data) => {
t.error(err, 'no error on get')
t.equals(data.toString(), 'boop', 'data has replicated')
t.end()
})
})
})
})

function init (isInitiator, authKeyPair, name, multifeed) {
const protocol = new Protocol(isInitiator)
authenticatedProtocol(protocol, {
authKeyPair,
onauthenticate (publicKey, cb) {
if (name === 'one' && publicKey.equals(auth2.publicKey)) {
return cb(null, true)
}
if (name === 'two' && publicKey.equals(auth1.publicKey)) {
return cb(null, true)
}
cb(null, false)
},
onprotocol (protocol) {
t.ok(protocol, 'Protocol ok')
// multifeed.replicate() will not take a stream as an option
// so this doesn't work
multifeed.replicate(isInitiator, { stream: protocol })
}
})
return protocol
}
})