Skip to content

Instantly share code, notes, and snippets.

@gerhardberger
Created January 10, 2017 21:10
Show Gist options
  • Save gerhardberger/9a9080154619925008a3570ab32a09a2 to your computer and use it in GitHub Desktop.
Save gerhardberger/9a9080154619925008a3570ab32a09a2 to your computer and use it in GitHub Desktop.
const { createWriteStream } = require('fs')
const hyperdrive = require('hyperdrive')
const raf = require('random-access-file')
const level = require('level')
const argv = require('minimist')(process.argv.slice(2))
const swarm = require('discovery-swarm')({
dns: {
server: [
'discovery1.publicbits.org',
'discovery2.publicbits.org'
]
},
dht: {
bootstrap: [
'bootstrap1.publicbits.org:6881',
'bootstrap2.publicbits.org:6881',
'bootstrap3.publicbits.org:6881',
'bootstrap4.publicbits.org:6881'
]
}
})
let link
let archive
if (argv.link) {
const db = level('./hyperdrive-receiver.db')
const drive = hyperdrive(db)
link = argv.link
archive = drive.createArchive(link, { file: name => raf(name) })
}
if (argv.file) {
const db = level('./hyperdrive-sender.db')
const drive = hyperdrive(db)
archive = drive.createArchive({ file: name => raf(name) })
archive.append(argv.file)
link = archive.key.toString('hex')
console.log(`Your hyperdrive link: ${link}`)
}
if (link) {
swarm.listen()
swarm.join(new Buffer(link, 'hex'))
swarm.on('connection', connection => {
console.log('New connection!')
connection.pipe(archive.replicate()).pipe(connection)
if (argv.link) {
archive.get(0, (err, entry) => {
if (err) return console.error(err)
const stream = archive.createFileReadStream(entry)
const ws = createWriteStream(`./downloads/${entry.name}`)
stream.pipe(ws)
stream.on('error', err => console.error(err))
stream.on('end', () => {
console.log(`Written file to downloads/${entry.name}`)
})
})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment