Last active
January 9, 2017 00:18
-
-
Save joehand/1e644adc3cc43ae05855 to your computer and use it in GitHub Desktop.
Dat Download Basic
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
var Hyperdrive = require('hyperdrive') | |
var Level = require('level') | |
var Swarm = require('discovery-swarm') | |
var swarmDefaults = require('datland-swarm-defaults') | |
// run this like: node thisfile.js 4c325f7874b4070blahblahetc | |
var link = new Buffer(process.argv[2], 'hex') | |
var LOCAL_PORT = Number(process.argv[3]) || 3282 // specify port or use 3282 | |
var db = Level('./dat.db') | |
var drive = Hyperdrive(db) | |
var config = swarmDefaults({ | |
// use the Dat default dns + discovery servers | |
stream: function () { | |
return drive.createPeerStream() | |
} | |
}) | |
var swarm = Swarm(config) | |
swarm.once('listening', function () { | |
console.log('joining swarm: ', link) | |
swarm.join(link) | |
// start downloading to ./data | |
var archive = drive.get(link, process.cwd() + '/data') | |
archive.ready(function (err) { | |
// a stream of all metadata. needs to download once then will cache | |
var metadata = archive.createEntryStream() | |
// start downloading all entries | |
metadata.on('data', function (entry) { | |
var dl = archive.download(entry) | |
dl.on('end', function () { | |
console.log('download finished', entry.name) | |
}) | |
}) | |
}) | |
}) | |
swarm.listen(LOCAL_PORT) |
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
{ | |
"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.1.0", | |
"hyperdrive": "^3.5.1", | |
"level": "^1.4.0", | |
"datland-swarm-default": "^1.0.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment