diff --git a/cli.js b/cli.js index 9e289a1..660fe88 100755 --- a/cli.js +++ b/cli.js @@ -7,7 +7,6 @@ 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('./') @@ -19,8 +18,6 @@ var argv = minimist(process.argv.slice(2), { var src = argv._[0] || process.cwd() var dest = argv._[1] var indexSpeed = speed() -var downloadSpeed = speed() -var uploadSpeed = speed() var neat = neatLog([mainView, progressView], {logspeed: 200}) // todo: opts.debug neat.use(runDat) @@ -31,24 +28,28 @@ function runDat (state, bus) { state.title = 'Starting Dat program...' bus.emit('render') - dat(src, dest, argv, function (err, archive, network, progress) { + dat(src, dest, argv, function (err, dat) { if (err) { - console.error(err) + console.error('ERROR:', err) process.exit(1) } - state.archive = archive - state.network = network - state.progress = progress - state.writable = archive.metadata.writable - archive.once('content', function () { + state.archive = dat.archive + state.network = dat.network + state.importer = dat.importer + state.stats = dat.stats + state.writable = dat.writable + if (dat.archive.content) { bus.emit('archive:content') - bus.emit('render') - }) - archive.metadata.on('append', function () { + } else { + dat.archive.once('content', function () { + bus.emit('archive:content') + }) + } + dat.archive.metadata.on('append', function () { bus.emit('render') }) - if (state.writable) state.title = `dat://${archive.key.toString('hex')}` + if (dat.writable) state.title = `dat://${dat.key.toString('hex')}` else state.title = 'Dat Download' bus.emit('archive') @@ -58,12 +59,11 @@ function runDat (state, bus) { function trackProgress (state, bus) { bus.on('archive:content', function () { - if (state.writable) trackImport() + if (state.archive.metadata.writable) trackImport() else trackDownload() }) function trackDownload () { - var progress = state.progress state.downloading = true state.archive.content.on('sync', function () { state.nsync = true @@ -72,7 +72,7 @@ function trackProgress (state, bus) { } function trackImport () { - var progress = state.progress + var progress = state.importer var counting = setInterval(function () { // Update file count while we are going (for big dirs) @@ -123,7 +123,6 @@ function trackProgress (state, bus) { function trackNetwork (state, bus) { bus.on('archive:content', function () { - var archive = state.archive var network = state.network network.on('connection', function (peer) { @@ -133,7 +132,7 @@ function trackNetwork (state, bus) { }) }) - var speed = networkSpeed(archive) + var speed = state.stats.network setInterval(function () { state.uploadSpeed = speed.uploadSpeed @@ -153,7 +152,7 @@ function mainView (state) { function progressView (state) { if (state.downloading) return downloadUI(state) - else if (state.importing) return importUI(state) + else if (state.importer) return importUI(state) return '' } @@ -202,7 +201,7 @@ function downloadUI (state) { } function importUI (state) { - if (!state.count) return `\nStarting import of ${state.progress.count.files} files ... (${pretty(state.progress.count.bytes)})` + if (!state.count) return `\nStarting import of ${state.importer.count.files} files ... (${pretty(state.importer.count.bytes)})` if (state.import.progress === state.count.bytes) return '\nAll files imported.' if (!state.totalBar) { var total = state.count.bytes diff --git a/index.js b/index.js index 87103eb..b8243b1 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,6 @@ -var fs = require('fs') -var path = require('path') -var xtend = require('xtend') -var hyperdrive = require('hyperdrive') -var network = require('hyperdiscovery') -var mirror = require('mirror-folder') -var count = require('count-files') -var datIgnore = require('dat-ignore') +var Dat = require('dat-node') var encoding = require('dat-encoding') -var debug = require('debug')('dat') +// var debug = require('debug')('dat') var storage = require('./storage') module.exports = run @@ -22,12 +15,11 @@ module.exports = run function run (src, dest, opts, cb) { if (!opts) opts = {} - var key if (dest) { // Downloading try { // validate key + remove dat:// stuff - key = encoding.toStr(src) + opts.key = encoding.toStr(src) } catch (e) { return cb(new Error('Invalid dat link')) } @@ -36,55 +28,21 @@ function run (src, dest, opts, cb) { opts.indexing = true } - var progress - var ignore = datIgnore(src || dest) - var archive = hyperdrive(storage(src || dest, opts), key, opts) - - archive.on('ready', function () { - if (!archive.metadata.writable && !dest) { + Dat(storage(src || dest, opts), opts, function (err, dat) { + if (err) return cb(err) + if (!dat.writable && !dest) { return cb(new Error('Archive is not writable and no destination provided.')) } - if (archive.metadata.writable) importFiles() - var swarm = joinNetwork() - cb(null, archive, swarm, progress) - }) - - function importFiles () { - if (!archive.metadata.writable) return - - progress = mirror(src, {name: '/', fs: archive}, { - live: opts.watch, - ignore: ignore, - dereference: true - }) - - progress.on('error', function (err) { - debug('IMPORT ERROR:', err) - }) - progress.count = count(src, { ignore: ignore, dereference: true }, function (err, data) { - if (err) return progress.emit('error', err) - progress.emit('count', data) - }) - - return progress - } + dat.joinNetwork(opts) + dat.trackStats() + if (dat.writable) { + dat.importFiles(src, { + watch: opts.watch, + dereference: true + }) + } - function joinNetwork () { - var swarm = network(archive, xtend({ - stream: function (peer) { - var stream = archive.replicate({ - live: true // !archive.metadata.writable && opts.sync - }) - stream.on('error', function (err) { - debug('Replication error:', err.message) - }) - stream.on('end', function () { - archive.downloaded = true - }) - return stream - } - }, opts)) - return swarm - } + cb(null, dat) + }) } diff --git a/package.json b/package.json index abf81c8..3a8e02b 100644 --- a/package.json +++ b/package.json @@ -20,23 +20,18 @@ "homepage": "https://github.com/joehand/dat-next#readme", "dependencies": { "cli-truncate": "^1.0.0", - "count-files": "^2.6.0", "dat-encoding": "^4.0.2", - "dat-ignore": "^1.0.0", + "dat-node": "^2.0.0", "dat-secret-storage": "^1.0.0", "dat-storage": "github:joehand/dat-storage#fix-write", "debug": "^2.4.5", - "hyperdiscovery": "^1.3.0", - "hyperdrive": "^8.0.0", "minimist": "^1.2.0", - "mirror-folder": "^1.2.2", "mkdirp": "^0.5.1", "neat-log": "^1.0.0", "prettier-bytes": "^1.0.3", "progress-string": "^1.2.1", "random-access-memory": "^2.3.0", - "speedometer": "^1.0.0", - "xtend": "^4.0.1" + "speedometer": "^1.0.0" }, "devDependencies": { "standard": "^8.6.0" diff --git a/storage.js b/storage.js index c78b3a3..00e2f65 100644 --- a/storage.js +++ b/storage.js @@ -1,9 +1,8 @@ var fs = require('fs') var ram = require('random-access-memory') -var secretStore = require('dat-secret-storage') +// var secretStore = require('dat-secret-storage') var datStore = require('dat-storage') var mkdirp = require('mkdirp') -var debug = require('debug')('dat-storage') module.exports = storage @@ -22,8 +21,8 @@ function storage (dir, opts) { throw e } - function error() { + function error () { // TODO: single file sleep storage throw new Error('Specify dir for sleep files: --sleep