Last active
July 27, 2016 01:50
-
-
Save emilbayes/4297344e730d26b554a24a1e9b40e175 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hyperdrive = require('hyperdrive') | |
const level = require('level-party') | |
const raf = require('random-access-file') | |
const path = require('path') | |
const chokidar = require('chokidar') | |
const swarm = require('discovery-swarm')() | |
const archiveSwarm = require('hyperdrive-archive-swarm') | |
const lpm = require('length-prefixed-message') | |
const createArchive = require('./load-or-create-archive.js') | |
const db = level('./hyperdrive.db') | |
const drive = hyperdrive(db) | |
const boxLink = new Buffer('137cdd378ddbfc57a0f14819c24a0123c41c8aa4') | |
createArchive(drive, db, 'my-peers', { | |
live: true, | |
file: name => raf(path.resolve('.', name)) | |
}, function (err, localArchive) { | |
if (err) throw err | |
chokidar | |
.watch('./send') | |
.on('add', file => localArchive.append(file)) | |
.on('change', file => localArchive.append(file)) | |
archiveSwarm(localArchive) | |
swarm.listen() | |
swarm.join(boxLink) | |
swarm.on('connection', function (peer) { | |
lpm.write(peer, localArchive.key) | |
lpm.read(peer, function (link) { | |
archiveSwarm(drive.createArchive(link, { | |
live: true, | |
file: name => raf(path.resolve('./receive/', name)) | |
})) | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const sub = require('subleveldown') | |
module.exports = function (drive, db, name, opts, cb) { | |
if (name == null) { | |
name = 'default' | |
} | |
const archives = sub(sub(db, 'simple-dropbox'), 'archives') | |
archives.get(name, function (err, link) { | |
if (link != null) { | |
return cb(null, drive.createArchive(new Buffer(link, 'hex'), opts)) | |
} | |
if (err.notFound) { | |
var archive = drive.createArchive(opts) | |
return archives.put(name, archive.key.toString('hex'), function (err) { | |
if (err) return cb(err) | |
return cb(null, archive) | |
}) | |
} | |
return cb(err) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mkdir -p peer-{1,2}/send | |
(cd peer-1 && node ../index.js) & | |
(cd peer-2 && node ../index.js) & | |
sleep 2; | |
touch peer-{1,2}/send/test.js | |
cat >> peer-{1,2}/send/test.js <<EOF | |
Hello world | |
Multi-line string | |
May remove lines | |
Bye! | |
EOF; | |
# Try editing either of test.js and remove/add lines. random-access-file doesn't fit this usecase, but what does? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment