Skip to content

Commit

Permalink
start sleeping (#120)
Browse files Browse the repository at this point in the history
* start sleeping

* usage + updates

* upgrade dat-ignore

* update mirror folder to PR api

* use hyperdiscovery

* remove cast from index

* cleanup

* standard

* support single file sharing

* parse dat key

* forgot mkdrip

* add live option

* use secret store

* add ui for live file watching

* update status-logger

* add dereference options

* update cli to try neat-log

* use hyperdrive 8 release and add downloads

* update readme

* standard
  • Loading branch information
joehand authored Apr 5, 2017
1 parent 77a032e commit 4fba82c
Show file tree
Hide file tree
Showing 48 changed files with 598 additions and 2,772 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
node_modules
tests/fixtures/.dat
tests/fixtures/dat.json
tests/**.db
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@ node_js:
- "4"
- "6"
- "7"

sudo: false

script:
- npm test
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 Joe Hand
Copyright (c) 2017 Joe Hand

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:

Expand Down
107 changes: 0 additions & 107 deletions bin/cli.js

This file was deleted.

202 changes: 202 additions & 0 deletions cli-old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
#!/usr/bin/env node

var minimist = require('minimist')
var logger = require('status-logger')
var pretty = require('prettier-bytes')
var speed = require('speedometer')
var progressBar = require('progress-string')
var dat = require('./')

process.title = 'dat-next'

var argv = minimist(process.argv.slice(2), {
alias: {sleep: 's', quiet: 'q', watch: 'w'}
})
var src = argv._[0] || process.cwd()
var dest = argv._[1]

var output = [
['', ''], // Key/Msg + Peer Count
['', ''], // Total Import/Download Progress
['', ''] // File Import Progress
]
var log = logger(output, { quiet: argv.quiet })
var indexSpeed = speed()
var downloadSpeed = speed()
var hasContent
var imported = 0
var downloaded = 0
var total = 0
var fileImported = 0
var bar
var totalBar
var watchTimeout

dat(src, dest, argv, function (archive, swarm, progress) {
output[0][0] = 'Here we go!'
setInterval(function () {
networkUI()
log.print()
if (archive.downloaded) {
log.clear()
console.log('Done! Bye bye.')
process.exit(0)
}
}, 200)
log.print()

archive.once('content', function () {
hasContent = true
imported = archive.content.byteLength
if (!archive.metadata.writable) downloadUI()
})
swarm.once('connection', function () {
output[0].push('') // add space for peers
})

if (!archive.metadata.writable) {
output[0][0] = 'Connecting...'
return
}

output[0][0] = `dat://${archive.key.toString('hex')}`

progress.once('count', function (count) {
total = count.bytes
if (!argv.watch) {
totalBar = progressBar({
total: total,
style: function (a, b) {
return `[${a}${b}] ${pretty(imported)} / ${pretty(total)}`
}
})
output[1][1] = totalBar(imported)
output[1].push('') // Import Speed
output[1].push('') // Spacer
}
updateImportTotal()
})

progress.on('put', function (src, dst) {
// Show progress for files only
if (src.stat.isDirectory()) return
clearTimeout(watchTimeout)

var name = (dst.name === '/') ? src.name : dst.name // use prettier names if available
output[2][0] = `ADD: ${name}`
fileImported = 0

// Avoid flashing progress bar of small files
if (src.stat.size > Math.pow(10, 7)) {
bar = progressBar({
total: src.stat.size,
style: function (a, b) {
return `[${a}${b}] ${pretty(fileImported)} / ${pretty(src.stat.size)}`
}
})
output[2][1] = bar(fileImported)
}
})

progress.on('put-data', function (chunk, src, dst) {
imported += chunk.length
fileImported += chunk.length

if (bar) {
output[2][1] = bar(fileImported)
if (!totalBar) output[2][2] = `${pretty(indexSpeed(chunk.length))}/s`
}
updateImportTotal(chunk.length)
})

progress.on('put-end', function (src, dst) {
// Remove put file progress
if (bar) {
output[2][1] = ''
if (!totalBar) output[2][2] = ''
}
fileImported = 0
bar = null
updateImportTotal()

if (argv.watch) {
watchTimeout = setTimeout(function () {
if (argv.watch) output[2] = [''] // clear output for idle watching
}, 1200)
}
})

progress.on('del', function (dst) {
output[2][0] = `DEL: ${dst.name}`
clearTimeout(watchTimeout)

if (argv.watch) {
watchTimeout = setTimeout(function () {
if (argv.watch) output[2] = [''] // clear output for idle watching
}, 1200)
}
})

progress.on('end', function (src, dst) {
// Only fires if argv.watch === false
totalBar = null
output[1] = [output[1][0]] // Clear total bar + import speed
output[2] = [`\nImport complete`]
setTimeout(function () {
output.pop()
}, 5000)
})

function updateImportTotal (size) {
size = size || 0
var verb = !argv.watch
? imported === total
? 'Sharing'
: 'Importing to'
: 'Syncing'

output[1][0] = `${verb} Archive: ${archive.metadata.length - 1} files (${pretty(archive.content.byteLength)})`
if (totalBar) {
output[1][1] = totalBar(imported)
output[1][2] = `${pretty(indexSpeed(size))}/s`
}
}

function downloadUI () {
var bar = downloadBar()
archive.content.ready(function () {
total = archive.content.length
output[0][0] = `Downloading ${pretty(archive.content.byteLength)}`
output[1][0] = bar(downloaded)
for (var i = 0; i < archive.content.length; i++) {
if (archive.content.has(i)) downloaded++
}
})

archive.content.on('download', function (index, data) {
if (archive.content.length !== total) {
output[0][0] = `Downloading ${pretty(archive.content.byteLength)}`
total = archive.content.length
bar = downloadBar()
}
downloaded++
var per = (downloaded / total * 100).toFixed(2)
if (bar) output[1][0] = bar(downloaded) + ' ' + per + '%'
output[1][1] = pretty(downloadSpeed(data.length)) + '/s'
})

function downloadBar () {
return progressBar({
total: total,
style: function (a, b) {
return `[${a}${b}]`
}
})
}
}

function networkUI () {
if (!swarm.connected || !hasContent) return
output[0][1] = `${archive.content.peers.length} peers`
}
})
Loading

0 comments on commit 4fba82c

Please sign in to comment.