Skip to content

Instantly share code, notes, and snippets.

@Frando
Last active January 26, 2020 16:58
Show Gist options
  • Save Frando/e123c29160d0d995ef2149e8e96a6717 to your computer and use it in GitHub Desktop.
Save Frando/e123c29160d0d995ef2149e8e96a6717 to your computer and use it in GitHub Desktop.
hypercore-protocol in rust - first steps
# in one terminal:
node hypercore-demo.js server 8080
# in another
git clone https://github.com/Frando/hypercore-protocol-rust-experiments
cargo run --example basic -- client 8080
# this will print:
recv: Open(Open { discovery_key: [184, 247, 61, 143, 233, 194, 150, 241, 106, 107, 132, 78, 106, 85, 102, 241, 250, 223, 3, 228, 39, 56, 171, 83, 111, 70, 25, 193, 212, 113, 39, 143], capability: None })
send 1 Open(Open { discovery_key: [184, 247, 61, 143, 233, 194, 150, 241, 106, 107, 132, 78, 106, 85, 102, 241, 250, 223, 3, 228, 39, 56, 171, 83, 111, 70, 25, 193, 212, 113, 39, 143], capability: None })
send 1 Want(Want { start: 0, length: Some(100) })
send 1 Request(Request { index: 0, bytes: None, hash: None, nodes: None })
recv: Status(Status { uploading: Some(true), downloading: Some(false) })
recv: Data(Data { index: 0, value: Some([104, 101, 108, 108, 111]), nodes: [Node { index: 2, hash: [180, 147, 64, 191, 105, 136, 120, 34, 225, 194, 130, 146, 158, 44, 129, 18, 94, 199, 174, 219, 144, 43, 52, 247, 202, 59, 161, 219, 122, 171, 222, 165], size: 5 }], signature: Some([59, 147, 139, 20, 8, 44, 155, 41, 10, 173, 217, 83, 178, 26, 93, 109, 156, 73, 41, 241, 32, 252, 201, 98, 147, 62, 204, 100, 190, 211, 10, 194, 213, 212, 198, 9, 86, 58, 4, 144, 115, 246, 249, 78, 160, 72, 127, 206, 146, 213, 188, 179, 244, 212, 215, 118, 31, 40, 189, 93, 8, 13, 181, 8]) })
// NOTE: Use with this PR: https://github.com/mafintosh/hypercore-protocol/pull/50
// Plus the linked PR of dependencies (hypercore-protocol and simple-hypercore-protocol)
const net = require('net')
const hypercore = require('hypercore')
const ram = require('random-access-memory')
const [mode, port, key] = process.argv.slice(2)
if (['client', 'server'].indexOf(mode) === -1 || !port) {
exit('usage: node index.js PORT [client|server] [KEY]')
}
const hostname = 'localhost'
const feed = hypercore(ram, key, { valueEncoding: 'utf8' })
feed.ready(() => {
console.log(`key: ${feed.key.toString('hex')}`)
console.log(`dkey: ${feed.discoveryKey.toString('hex')}`)
if (!key) {
feed.append('hello')
feed.append('world')
}
})
if (mode === 'client') {
const socket = net.connect(port, hostname)
onconnection(socket, true)
} else {
const server = net.createServer(socket => onconnection(socket, false))
server.listen(port, hostname, () => {
console.log(`server listening on ${server.address().address}:${server.address().port}`)
})
}
function onconnection (socket, isInitiator) {
const remoteAddr = `${socket.remoteAddress}:${socket.remotePort}`
console.log(`new connection from ${remoteAddr}`)
console.log(`starting to replicate`, feed)
const proto = feed.replicate(isInitiator, { live: true, noise: false })
proto.pipe(socket).pipe(proto)
socket.on('close', () => console.log(`connection closed from ${remoteAddr}`))
}
function exit (msg) {
console.error(msg)
process.exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment