├── .travis.yml ├── example.js ├── .gitignore ├── LICENSE.md ├── package.json ├── README.md ├── test └── index.js └── index.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '4' 4 | - '6' 5 | - '8' 6 | cache: 7 | directories: 8 | - node_modules 9 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var datDownload = require('dat-download') 2 | 3 | datDownload('dat://beakerbrowser.com/index.html', process.cwd(), function (err) { 4 | if (err) throw err 5 | console.log('done downloading! thanks') 6 | }) 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # [MIT License](https://spdx.org/licenses/MIT) 2 | 3 | Copyright (c) 2017 Joe Hand 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dat-download", 3 | "description": "download a file or directory from a dat", 4 | "version": "1.0.1", 5 | "author": "Joe Hand ", 6 | "bugs": { 7 | "url": "https://github.com/joehand/dat-download/issues" 8 | }, 9 | "devDependencies": { 10 | "dependency-check": "^2.9.1", 11 | "install": "^0.10.1", 12 | "standard": "*", 13 | "tap-spec": "^4.0.2", 14 | "tape": "^4.0.0", 15 | "temporary-directory": "^1.0.2" 16 | }, 17 | "homepage": "https://github.com/joehand/dat-download", 18 | "keywords": [ 19 | "dat", 20 | "hyperdrive" 21 | ], 22 | "license": "MIT", 23 | "main": "index.js", 24 | "repository": { 25 | "type": "git", 26 | "url": "https://github.com/joehand/dat-download.git" 27 | }, 28 | "scripts": { 29 | "test": "standard && dependency-check ./package.json && tape test/*.js | tap-spec" 30 | }, 31 | "dependencies": { 32 | "dat-node": "^3.5.1", 33 | "debug": "^3.0.1", 34 | "mirror-folder": "^2.1.1", 35 | "mkdirp": "^0.5.1", 36 | "pump": "^1.0.2", 37 | "random-access-memory": "^2.4.0" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dat-download 2 | 3 | One-time file downloads via Dat. Download a single file or subdirectory from a dat. Automatically joins the network and handles everything for you. 4 | 5 | [![npm][npm-image]][npm-url] 6 | [![travis][travis-image]][travis-url] 7 | [![standard][standard-image]][standard-url] 8 | 9 | ## Example 10 | 11 | ```js 12 | var datDownload = require('dat-download') 13 | 14 | datDownload('dat://778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639/dat.json', process.cwd(), function (err) { 15 | if (err) throw err 16 | console.log('done downloading! thanks') 17 | }) 18 | ``` 19 | 20 | Works with DNS-type dat keys too! 21 | 22 | ```js 23 | var datDownload = require('dat-download') 24 | 25 | datDownload('dat://beakerbrowser.com/index.html', process.cwd(), function (err) { 26 | if (err) throw err 27 | console.log('done downloading! thanks') 28 | }) 29 | ``` 30 | 31 | Can also download a whole dat: 32 | 33 | ```js 34 | var datDownload = require('dat-download') 35 | 36 | datDownload('dat://beakerbrowser.com/', process.cwd(), function (err) { 37 | if (err) throw err 38 | console.log('done downloading! thanks') 39 | }) 40 | ``` 41 | 42 | ## Install 43 | 44 | ``` 45 | npm install dat-download 46 | ``` 47 | 48 | ## API 49 | 50 | ### `datDownload(datPath, [destination], callback)` 51 | 52 | * `datPath` - dat key with subdirectory or file path. If the whole key is specified, it will download to `destination/key`. 53 | * `destination` - download folder 54 | 55 | ## License 56 | 57 | [MIT](LICENSE.md) 58 | 59 | [npm-image]: https://img.shields.io/npm/v/dat-download.svg?style=flat-square 60 | [npm-url]: https://www.npmjs.com/package/dat-download 61 | [travis-image]: https://img.shields.io/travis/joehand/dat-download.svg?style=flat-square 62 | [travis-url]: https://travis-ci.org/joehand/dat-download 63 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 64 | [standard-url]: http://npm.im/standard 65 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs') 2 | var path = require('path') 3 | var test = require('tape') 4 | var tmp = require('temporary-directory') 5 | var downloadDat = require('..') 6 | 7 | var testdats = { 8 | fullDat: 'dat://778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639/', 9 | fullDat2: 'dat://778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639', 10 | file: 'dat://778f8d955175c92e4ced5e4f5563f69bfec0c86cc6f670352c457943666fe639/dat.json', 11 | subdir: 'dat://43bafc583c0c42fcc712b35a5f6bce62cc92567a5c719d10ea6acfca4903b843/knight-publicbits-ppt' 12 | } 13 | 14 | test('Download full dat', function (t) { 15 | tmp(function (_, dir, cleanup) { 16 | downloadDat(testdats.fullDat, dir, function (err) { 17 | t.error(err, 'no error') 18 | var key = testdats.fullDat.split('//')[1] 19 | fs.stat(path.join(dir, key), function (err, stat) { 20 | t.error(err, 'no error') 21 | t.ok(stat.isDirectory(), 'directory exists') 22 | cleanup(function () { 23 | t.end() 24 | }) 25 | }) 26 | }) 27 | }) 28 | }) 29 | 30 | test('Download full dat 2', function (t) { 31 | tmp(function (_, dir, cleanup) { 32 | downloadDat(testdats.fullDat2, dir, function (err) { 33 | t.error(err, 'no error') 34 | var key = testdats.fullDat.split('//')[1] 35 | fs.stat(path.join(dir, key), function (err, stat) { 36 | t.error(err, 'no error') 37 | t.ok(stat.isDirectory(), 'directory exists') 38 | cleanup(function () { 39 | t.end() 40 | }) 41 | }) 42 | }) 43 | }) 44 | }) 45 | 46 | test('Download subdir', function (t) { 47 | tmp(function (_, dir, cleanup) { 48 | downloadDat(testdats.subdir, dir, function (err) { 49 | t.error(err, 'no error') 50 | fs.readdir(dir, function (err, entries) { 51 | t.error(err, 'no error') 52 | t.ok(entries.length === 1, 'directory exists') 53 | t.ok(entries[0] === 'knight-publicbits-ppt') 54 | cleanup(function () { 55 | t.end() 56 | }) 57 | }) 58 | }) 59 | }) 60 | }) 61 | 62 | test('Download single file', function (t) { 63 | tmp(function (_, dir, cleanup) { 64 | downloadDat(testdats.file, dir, function (err) { 65 | t.error(err, 'no error') 66 | fs.stat(path.join(dir, 'dat.json'), function (err, stat) { 67 | t.error(err, 'no error') 68 | t.ok(stat, 'file exists') 69 | cleanup(function () { 70 | t.end() 71 | }) 72 | }) 73 | }) 74 | }) 75 | }) 76 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | var fs = require('fs') 3 | var path = require('path') 4 | var Dat = require('dat-node') 5 | var mirror = require('mirror-folder') 6 | var pump = require('pump') 7 | var ram = require('random-access-memory') 8 | var mkdirp = require('mkdirp') 9 | var debug = require('debug')('dat-download') 10 | 11 | module.exports = function (datPath, downloadDest, cb) { 12 | assert.equal(typeof datPath, 'string', 'dat-download: string path required') 13 | if (typeof downloadDest === 'function') { 14 | cb = downloadDest 15 | downloadDest = process.cwd() 16 | } 17 | assert.equal(typeof cb, 'function', 'dat-download: callback required') 18 | 19 | if (datPath.indexOf('//') > -1) datPath = datPath.split('//')[1] 20 | var key = 'dat://' + datPath.split('/')[0] 21 | var entryPath = '/' + datPath.split('/').slice(1).join('/') 22 | if (entryPath === '/') downloadDest = path.join(downloadDest, datPath.split('/')[0]) 23 | debug('downloadDir', downloadDest) 24 | debug('dat key', key) 25 | debug('dat path', entryPath) 26 | 27 | Dat(ram, {key: key, sparse: true}, function (err, dat) { 28 | if (err) return cb(err) 29 | var archive = dat.archive 30 | dat.joinNetwork() 31 | archive.metadata.update(function () { 32 | download(entryPath, function (err) { 33 | if (err) return cb(err) 34 | dat.close(cb) 35 | }) 36 | }) 37 | 38 | function download (entryPath, cb) { 39 | archive.stat(entryPath, function (err, stat) { 40 | if (err) return cb(err) 41 | if (stat.isDirectory()) downloadDir(entryPath, cb) 42 | if (stat.isFile()) downloadFile(entryPath, cb) 43 | }) 44 | } 45 | 46 | function downloadDir (dirname, cb) { 47 | debug('downloading dir', dirname) 48 | var dest = path.join(downloadDest, dirname) 49 | fs.stat(dest, function (_, stat) { 50 | // throw if dest exists 51 | if (stat && stat.isDirectory()) return cb(new Error('Destination path exists:' + dest)) 52 | mkdirp(dest, function (err) { 53 | if (err) return cb(err) 54 | mirror({fs: archive, name: dirname}, dest, cb) 55 | }) 56 | }) 57 | } 58 | 59 | function downloadFile (file, cb) { 60 | mkdirp(downloadDest, function (err) { 61 | if (err) return cb(err) 62 | var dest = path.join(downloadDest, file) 63 | debug('downloading file', file, 'to', dest) 64 | var rs = archive.createReadStream(file) 65 | var ws = fs.createWriteStream(path.join(downloadDest, file)) 66 | pump(rs, ws, cb) 67 | }) 68 | } 69 | }) 70 | } 71 | --------------------------------------------------------------------------------