Ind.ie is now Small Technology Foundation.
Commit d3724543 authored by Aral Balkan's avatar Aral Balkan

Seeing same behaviour for replication over native as with WebRTC: odd

parent 89cd4ca0
......@@ -220,6 +220,9 @@ function generateKeys() {
const remoteStream = webSocketStream(`wss://localhost/hypha/${dbKeyInHex}`)
const localStream = db.replicate({
// If we remove the encrypt: false, we get an error on the server:
// Pipe closed for c4a99bc919c23d9c12b1fe440a41488141263e59fb98288388b578e105ad2523 Remote message is larger than 8MB (max allowed)
// Why is this and what’s the encryption that we’re turning off here and what effects does this have on privacy and security? (TODO: file issue)
encrypt: false,
live: true
})
......@@ -265,7 +268,7 @@ function generateKeys() {
//
// TEST
//
const NUMBER_TO_APPEND = 3
const NUMBER_TO_APPEND = 100
let counter = 0
const intervalToUpdateInMS = 500
......
......@@ -2,6 +2,7 @@
// Hypha: A native client for testing replication of a single hypercore.
//
const hypercore = require('hypercore')
const hyperdb = require('hyperdb')
const ram = require('random-access-memory')
const hyperswarm = require('@hyperswarm/network')
const { pipeline } = require('stream')
......@@ -17,34 +18,34 @@ if (process.argv.length !== 3) {
}
const readKeyInHex = process.argv[2]
console.log(`\nAttempting to find and replicate hypercore with read key:\n${readKeyInHex}\n`)
console.log(`\nAttempting to find and replicate hyperdb with read key:\n${readKeyInHex}\n`)
const readKeyBuffer = Buffer.from(readKeyInHex, 'hex')
const discoveryKeyBuffer = discoveryKey(readKeyBuffer)
const discoveryKeyInHex = discoveryKeyBuffer.toString('hex')
// Create the local hypercore instance
const localCore = hypercore((filename) => ram(filename), readKeyBuffer, {
// Create the local hyperdb instance
// NOTE: But this says do *NOT* pass the read key ???
// See https://github.com/mafintosh/hyperdb/issues/153
const db = hyperdb((filename) => ram(filename), readKeyBuffer, {
createIfMissing: false,
overwrite: false,
valueEncoding: 'json',
onwrite: (index, data, peer, next) => {
// console.log(`Feed: [onWrite] index = ${index}, peer = ${peer}, data:`)
// console.log(data)
next()
}
valueEncoding: 'json'
})
const localReadStream = localCore.createReadStream({live: true})
localReadStream.on('data', (data) => {
console.log('[Replicate]', data)
})
localCore.on('ready', () => {
console.log('Local core ready.')
const watcher = db.watch('/table', () => {
db.get('/table', (error, values) => {
// New data is available on the db. Display it on the page.
const obj = values[0].value
for (let [key, value] of Object.entries(obj)) {
console.log(`[Replicate] ${key}: ${value}`)
}
})
})
// HACK: Just for now, make sure we only connect once
let connected = false
db.on('ready', () => {
console.log('Local hyperdb ready.')
//
// Join the swarm
......@@ -55,9 +56,6 @@ localCore.on('ready', () => {
})
swarm.on('connection', (remoteNativeStream, details) => {
// HACK: only handle first connection
if (connected) return
connected = true
console.log(`Joined swarm for read key ${readKeyInHex}, discovery key ${discoveryKeyInHex}`)
......@@ -65,13 +63,7 @@ localCore.on('ready', () => {
console.log('About to replicate!')
// Create the local replication stream.
const localReplicationStream = localCore.replicate({
// TODO: why is Jim’s shopping list example setting encrypt to false?
// The encryption of __what__ does this affect?
// (I haven’t even tested this yet with it set to true to limit the variables.)
encrypt: false,
live: true
})
const localReplicationStream = db.replicate({encrypt: false, live: true})
pipeline(
remoteNativeStream,
......
......@@ -104,24 +104,26 @@ server.on('connect', (event) => {
//
// Connect to the hyperswarm for this hyperdb.
//
const nativePeers = {}
const swarm = hyperswarm()
const discoveryKey = db.discoveryKey
const discoveryKeyInHex = discoveryKey.toString('hex')
console.log(discoveryKeyInHex)
console.log(`Joining hyperswarm for discovery key ${discoveryKeyInHex}.`)
console.log(`Joining hyperswarm for discovery key ${discoveryKeyInHex}`)
// Join the swarm
swarm.join(db.discoveryKey, {
lookup: true, // find and connect to Heers.
swarm.join(discoveryKey, {
lookup: true, // find and connect to peers.
announce: true // optional: announce self as a connection target.
})
swarm.on('connection', (remoteNativeStream, details) => {
console.log(`Got peer for ${readKey} (discovery key: ${discoveryKeyInHex})`)
console.log('About to replicate!')
// Create a new replication stream
const nativeReplicationStream = db.replicate({
encrypt: false,
......@@ -137,6 +139,7 @@ server.on('connect', (event) => {
console.log(`(Native stream from swarm) Pipe closed for ${readKey}`, error && error.message)
}
)
})
})
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment