Skip to content

Instantly share code, notes, and snippets.

@alexindigo
Created July 14, 2017 15:38
Show Gist options
  • Save alexindigo/96fb493345bcda1b531099cfc2facda9 to your computer and use it in GitHub Desktop.
Save alexindigo/96fb493345bcda1b531099cfc2facda9 to your computer and use it in GitHub Desktop.
nodejs: 6.10.1
npm: 3.10.10
macos: 10.12.5
var net = require('net')
var hyperdrive = require('hyperdrive')
var archive = hyperdrive('./my-first-hyperdrive') // content will be stored in this folder
archive.writeFile('/hello2.txt', 'world2', function (err) {
if (err) throw err
archive.readdir('/', function (err, list) {
if (err) throw err
console.log(list) // prints ['hello.txt']
archive.readFile('/hello.txt', 'utf-8', function (err, data) {
if (err) throw err
console.log(data) // prints 'world'
})
})
});
// replicate
archive.on('ready', function()
{
console.log('ARCHIVE:', archive.key.toString('hex'));
console.log('DISCOVERY:', archive.discoveryKey.toString('hex'));
var server = net.createServer(function (socket) {
socket.pipe(archive.replicate()).pipe(socket)
})
server.listen(10000)
});
$ node one.js
ARCHIVE: bb9fcd5a9f629f83ec32b3a38a2ecda055c505956ceaf1e706aa0f7e8e713587
DISCOVERY: e5f9eb85f158c1ffa7daeec133b8df1f3563bfbe42644bbc188702eaeb071512
[ 'hello.txt', 'hello2.txt' ]
world
events.js:160
throw er; // Unhandled 'error' event
^
Error: First shared hypercore must be the same
at Protocol._sameKey (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:404:16)
at Protocol._onopen (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:272:15)
at Protocol._onmessage (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:311:17)
at Protocol._parseMessage (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:365:10)
at Protocol._parse (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:330:37)
at Protocol._write (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:238:8)
at doWrite (/Users/alex/Projects/hyperdrive/node_modules/readable-stream/lib/_stream_writable.js:406:64)
at writeOrBuffer (/Users/alex/Projects/hyperdrive/node_modules/readable-stream/lib/_stream_writable.js:395:5)
at Protocol.Writable.write (/Users/alex/Projects/hyperdrive/node_modules/readable-stream/lib/_stream_writable.js:322:11)
at Socket.ondata (_stream_readable.js:555:20)
$ node two.js
[]
events.js:160
throw er; // Unhandled 'error' event
^
Error: First shared hypercore must be the same
at Protocol._sameKey (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:404:16)
at Protocol._onopen (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:272:15)
at Protocol._onmessage (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:311:17)
at Protocol._parseMessage (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:365:10)
at Protocol._parse (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:330:37)
at Protocol._write (/Users/alex/Projects/hyperdrive/node_modules/hypercore-protocol/index.js:238:8)
at doWrite (/Users/alex/Projects/hyperdrive/node_modules/readable-stream/lib/_stream_writable.js:406:64)
at writeOrBuffer (/Users/alex/Projects/hyperdrive/node_modules/readable-stream/lib/_stream_writable.js:395:5)
at Protocol.Writable.write (/Users/alex/Projects/hyperdrive/node_modules/readable-stream/lib/_stream_writable.js:322:11)
at Socket.ondata (_stream_readable.js:555:20)
var hyperdrive = require('hyperdrive')
var net = require('net')
// discovery key from the previous script
var key = new Buffer('e5f9eb85f158c1ffa7daeec133b8df1f3563bfbe42644bbc188702eaeb071512', 'utf8');
var clonedArchive = hyperdrive('./my-cloned-hyperdrive-2', key)
var socket = net.connect(10000)
socket.pipe(clonedArchive.replicate()).pipe(socket)
clonedArchive.readdir('/', function (err, list) {
if (err) throw err
console.log(list) // prints ['hello.txt']
clonedArchive.readFile('/hello2.txt', 'utf-8', function (err, data) {
if (err) throw err
console.log(data) // prints 'world'
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment