diff --git a/cli.js b/cli.js index e52fddf..9e289a1 100755 --- a/cli.js +++ b/cli.js @@ -7,6 +7,7 @@ var progress = require('progress-string') var cliTruncate = require('cli-truncate') var neatLog = require('neat-log') var output = require('neat-log/output') +var networkSpeed = require('hyperdrive-network-speed') var dat = require('./') @@ -62,16 +63,12 @@ function trackProgress (state, bus) { }) function trackDownload () { - // var progress = state.progress + var progress = state.progress state.downloading = true - - // progress.on('put', function (src, dst) { - // state.fileDownload = { - // src: src, - // dst: dst, - // progress: 0 - // } - // }) + state.archive.content.on('sync', function () { + state.nsync = true + bus.emit('render') + }) } function trackImport () { @@ -136,14 +133,13 @@ function trackNetwork (state, bus) { }) }) - archive.content.on('upload', function (index, data) { - state.uploadSpeed = uploadSpeed(data.length) - bus.emit('render') - }) - archive.content.on('download', function (index, data) { - state.downloadSpeed = downloadSpeed(data.length) + var speed = networkSpeed(archive) + + setInterval(function () { + state.uploadSpeed = speed.uploadSpeed + state.downloadSpeed = speed.downloadSpeed bus.emit('render') - }) + }, 500) }) } @@ -192,6 +188,13 @@ function networkUI (state) { } function downloadUI (state) { + if (state.nsync) { + return output` + + Archive up to date with latest. + Exit if you'd like. + ` + } return output` Download progress TODO diff --git a/index.js b/index.js index 7600a70..87103eb 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,30 @@ var fs = require('fs') var path = require('path') var xtend = require('xtend') -var mkdirp = require('mkdirp') var hyperdrive = require('hyperdrive') var network = require('hyperdiscovery') -var ram = require('random-access-memory') -var secretStore = require('dat-secret-storage') var mirror = require('mirror-folder') var count = require('count-files') var datIgnore = require('dat-ignore') var encoding = require('dat-encoding') var debug = require('debug')('dat') +var storage = require('./storage') module.exports = run +/** + * Run dat-next + * @param {string} src directory or key (for downloading) + * @param {string} dest directory to download to, required for download + * @param {object} opts options + * @param {Function} cb callback(err, archive, swarm, progress) + */ function run (src, dest, opts, cb) { if (!opts) opts = {} var key if (dest) { + // Downloading try { // validate key + remove dat:// stuff key = encoding.toStr(src) @@ -26,36 +32,23 @@ function run (src, dest, opts, cb) { return cb(new Error('Invalid dat link')) } src = null - opts.sparse = true + } else { + opts.indexing = true } var progress var ignore = datIgnore(src || dest) - var archive = hyperdrive(storage(), key, opts) + var archive = hyperdrive(storage(src || dest, opts), key, opts) archive.on('ready', function () { if (!archive.metadata.writable && !dest) { return cb(new Error('Archive is not writable and no destination provided.')) } if (archive.metadata.writable) importFiles() - else downloadFiles() var swarm = joinNetwork() cb(null, archive, swarm, progress) }) - function storage () { - if (opts.temp) return ram - if (typeof opts.sleep === 'string') return secretStore(opts.sleep) - if (!src) { - mkdirp.sync(dest) - return path.join(dest, '.dat') // TODO: if dest is file - } - - var isDir = fs.statSync(src).isDirectory() - if (isDir) return secretStore(path.join(src, '.dat')) - return cb(new Error('Specify dir for sleep files: --sleep