Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Created February 28, 2016 22:38
Show Gist options
  • Save max-mapper/e63104ca72a26d428690 to your computer and use it in GitHub Desktop.
Save max-mapper/e63104ca72a26d428690 to your computer and use it in GitHub Desktop.
dat minimal example
var Swarm = require('discovery-swarm')
var Hyperdrive = require('hyperdrive')
var Level = require('level')
// run this like: node thisfile.js 4c325f7874b4070blahblahetc
// the dat link someone sent us, we want to download the data from it
var link = new Buffer(process.argv[2], 'hex')
// here are the default config dat uses:
// used for MDNS and also as the dns 'app name', you prob shouldnt change this
var DAT_DOMAIN = 'dat.local'
// dat will try this first and pick the first open port if its taken
var DEFAULT_LOCAL_PORT = 3282
// we run the servers below you can use if you want or run your own
var DEFAULT_DISCOVERY = [
'discovery1.publicbits.org',
'discovery2.publicbits.org'
]
var DEFAULT_BOOTSTRAP = [
'bootstrap1.publicbits.org:6881',
'bootstrap2.publicbits.org:6881',
'bootstrap3.publicbits.org:6881',
'bootstrap4.publicbits.org:6881'
]
var db = Level('./dat.db')
var drive = Hyperdrive(db)
var swarm = Swarm({
id: drive.core.id,
dns: {server: DEFAULT_DISCOVERY, domain: DAT_DOMAIN},
dht: {bootstrap: DEFAULT_BOOTSTRAP},
stream: function () {
// this is how the swarm and hyperdrive interface
console.log('new peer stream')
return drive.createPeerStream()
}
})
swarm.once('listening', function () {
console.log('joining', link)
// join the swarm
swarm.join(link)
// tell hyperdrive to start downloading/uploading in ./data
var archive = drive.get(link, process.cwd() + '/data')
archive.ready(function (err) {
console.log('archive ready')
// a stream of all metadata. after retrieving each entry metadata will be cached locally
// but the first time it has to fetch it from the swarm
var metadata = archive.createEntryStream()
// start downloading all entries, or choose your own filter logic to download specific entries
// entries will be either files or directories
metadata.on('data', function (entry) {
var dl = archive.download(entry)
console.log('downloading', entry.name, dl)
dl.on('ready', function () {
console.log('download started', entry.name, dl)
})
})
})
})
swarm.listen(DEFAULT_LOCAL_PORT)
{
"name": "testdat",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"discovery-swarm": "^3.0.0",
"hyperdrive": "^3.4.1",
"level": "^1.4.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment