Skip to content

Commit

Permalink
hyperdrive + storage updates and small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
joehand committed Apr 29, 2017
1 parent a49a8af commit a7389b6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 226 deletions.
202 changes: 0 additions & 202 deletions cli-old.js

This file was deleted.

73 changes: 59 additions & 14 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,29 @@ var dat = require('./')
process.title = 'dat-next'

var argv = minimist(process.argv.slice(2), {
alias: {temp: 't', quiet: 'q', watch: 'w', sleep: 's'}
alias: {temp: 't', help: 'h', watch: 'w', sleep: 's'}
})

if (argv.help) return usage()

var src = argv._[0] || process.cwd()
var dest = argv._[1]
var indexSpeed = speed()
var logspeed = 200

var neat = neatLog([mainView, progressView], {logspeed: 200}) // todo: opts.debug
var neat = neatLog([mainView, progressView], {logspeed: logspeed})
neat.use(runDat)
neat.use(trackNetwork)
neat.use(trackProgress)

function runDat (state, bus) {

state.title = 'Starting Dat program...'
bus.emit('render')

dat(src, dest, argv, function (err, dat) {
if (err) {
bus.clear()
console.error('ERROR:', err)
process.exit(1)
}
Expand All @@ -50,7 +56,7 @@ function runDat (state, bus) {
})

if (dat.writable) state.title = `dat://${dat.key.toString('hex')}`
else state.title = 'Dat Download'
else state.title = 'Dat!'

bus.emit('archive')
bus.emit('render')
Expand All @@ -65,8 +71,28 @@ function trackProgress (state, bus) {

function trackDownload () {
state.downloading = true
state.archive.content.on('sync', function () {
state.modified = false

state.archive.content.on('clear', function () {
state.modified = true
})

state.archive.content.on('download', function (index, data) {
state.modified = true
})

state.archive.on('sync', function () {
state.nsync = true
if (state.modified && !argv.live) {
state.downloadExit = true
bus.render()
process.exit()
}
bus.emit('render')
})

state.archive.on('update', function () {
state.nsync = false
bus.emit('render')
})
}
Expand All @@ -77,7 +103,7 @@ function trackProgress (state, bus) {
var counting = setInterval(function () {
// Update file count while we are going (for big dirs)
bus.emit('render')
}, 200)
}, logspeed)

state.importing = true
state.import = {
Expand Down Expand Up @@ -138,7 +164,7 @@ function trackNetwork (state, bus) {
state.uploadSpeed = speed.uploadSpeed
state.downloadSpeed = speed.downloadSpeed
bus.emit('render')
}, 500)
}, logspeed)
})
}

Expand Down Expand Up @@ -167,7 +193,8 @@ function archiveUI (state) {
}

function networkUI (state) {
if (!state.network) return ''
// state.exiting = last render before download exit
if (!state.network || state.downloadExit) return ''
if (!state.network.connected || !state.archive.content) {
if (state.writable) return '\nWaiting for Connections...'
return '\nConnecting...'
Expand All @@ -181,7 +208,8 @@ function networkUI (state) {
function speed () {
var output = ''
if (state.uploadSpeed) output += `Uploading ${pretty(state.uploadSpeed)}/s`
if (state.downloadSpeed) output += `Downloading ${pretty(state.downloadSpeed)}/s`
// !state.nsync hack so speed doesn't display when done
if (!state.nsync && state.downloadSpeed) output += `Downloading ${pretty(state.downloadSpeed)}/s`
return output
}
}
Expand All @@ -191,9 +219,12 @@ function downloadUI (state) {
return output`
Archive up to date with latest.
Exit if you'd like.
${argv.live ? 'Waiting for updates ...' : ''}
`
}
if (!state.stats.get().blocksTotal) {
return '' // no metadata yet
}
if (!state.downloaded) {
var feed = state.archive.content
state.downloaded = 0
Expand All @@ -204,7 +235,17 @@ function downloadUI (state) {
state.downloaded += 1
})
}

if (!state.downloadBar) {
makeBar()
state.archive.metadata.update(makeBar)
}
return output`
${state.downloadBar(state.downloaded)}
`

function makeBar () {
var total = state.stats.get().blocksTotal
state.downloadBar = progress({
total: total,
Expand All @@ -213,10 +254,6 @@ function downloadUI (state) {
}
})
}
return output`
${state.downloadBar(state.downloaded)}
`
}

function importUI (state) {
Expand All @@ -241,12 +278,13 @@ function importUI (state) {
`

function fileImport () {
if (!state.fileImport) return ``
if (!state.fileImport) return ''
if (state.fileImport.type === 'del') return `\nDEL: ${state.fileImport.src.name}`
if (!state.fileImport.bar) {
var total = state.fileImport.src.stat.size
state.fileImport.bar = progress({
total: total,
width: 35,
style: function (a, b) {
return `[${a}${b}] ${pretty(state.fileImport.progress)} / ${pretty(total)}`
}
Expand All @@ -260,3 +298,10 @@ function importUI (state) {
`
}
}

function usage () {
console.error('dat-next!')
console.error(' dat-next <dir> SHARE directory')
console.error(' dat-next <key> <dir> DOWNLOAD key to dir (directory required)')
process.exit(0)
}
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module.exports = run
* @param {Function} cb callback(err, archive, swarm, progress)
*/
function run (src, dest, opts, cb) {
if (!opts) opts = {}
opts = Object.assign({
latest: true
}, opts)

if (dest) {
// Downloading
Expand All @@ -24,6 +26,7 @@ function run (src, dest, opts, cb) {
return cb(new Error('Invalid dat link'))
}
src = null
opts.secretKey = false // turns off dat secret storage for local download testing
} else {
opts.indexing = true
}
Expand Down
Loading

0 comments on commit a7389b6

Please sign in to comment.