Skip to content

Commit

Permalink
add error for hypercore feeds (#23)
Browse files Browse the repository at this point in the history
* check if metadata is already done

* add error for hypercore feeds

* delete dirs for bad links & add test

* add hyperdiscovery to dev dep

* remove fail

* remove fail

* fix tests
  • Loading branch information
joehand authored Dec 14, 2016
1 parent 6593b91 commit 8c4aaa4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 17 deletions.
45 changes: 32 additions & 13 deletions lib/download.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var assert = require('assert')
var logger = require('status-logger')
var prettyBytes = require('pretty-bytes')
var rimraf = require('rimraf')
var memdb = require('memdb')
var Dat = require('dat-node')
var ui = require('./ui')
Expand Down Expand Up @@ -69,6 +70,11 @@ module.exports = function (type, opts, dat) {
network.swarm.once('connection', function (peer) {
connected = true
progressOutput[2] = 'Starting Download...'

if (archive.metadata.blocksRemaining() === 0) {
metadataDownloaded = true
archive.metadata.on('update', onMetadataUpdate)
}
})
progressOutput[2] = 'Looking for Dat Archive in Network'

Expand All @@ -78,14 +84,7 @@ module.exports = function (type, opts, dat) {

// Live metadata updates
// TODO: this can be buggy if there are lots of metadata updates
archive.metadata.on('update', function () {
if (metadataDownloaded) {
metadataDownloaded = false
archive.metadata.once('download-finished', function () {
metadataDownloaded = true
})
}
})
archive.metadata.on('update', onMetadataUpdate)
})

// Content is populated
Expand All @@ -95,10 +94,28 @@ module.exports = function (type, opts, dat) {
contentPopulated = true
})
archive.open(function () {
if (!archive.content) return removeExit()
archive.content.once('download', function () {
contentPopulated = true
})
})

function onMetadataUpdate () {
if (metadataDownloaded) {
metadataDownloaded = false
archive.metadata.once('download-finished', function () {
metadataDownloaded = true
})
}
}

function removeExit () {
output[0] = ['']
output[1] = ['']
log.print()
rimraf.sync(dat.path)
return exit('Link is not a Dat Archive. Please check you have the correct link.')
}
}

function updateDownload () {
Expand Down Expand Up @@ -128,13 +145,15 @@ module.exports = function (type, opts, dat) {
var st = stats.get()
if (!connected || !metadataDownloaded || !contentPopulated) return false
if (st.blocksTotal !== archive.content.blocks) return false // TODO: hyperdrive-stats bug?
if (st.blocksProgress !== st.blocksTotal) return false
if (archive.content.blocksRemaining() !== 0) return false

progressOutput[2] = (type === 'sync') ? 'Files updated to latest!' : 'Download Finished!'
progressOutput[3] = `Total size: ${st.filesTotal} ${st.filesTotal === 1 ? 'file' : 'files'} (${prettyBytes(st.bytesTotal)})`
if (opts.exit !== false) {
output[1] = '' // remove network info
return exit()
}

if (!opts.exit) return true

// Exit!
output[1] = '' // remove network info
return exit()
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"memdb": "^1.3.1",
"pretty-bytes": "^4.0.2",
"progress-string": "^1.2.1",
"rimraf": "^2.5.4",
"status-logger": "^3.0.0",
"subcommand": "^2.0.4",
"township-client": "^1.1.0",
Expand All @@ -33,10 +34,11 @@
"devDependencies": {
"appa": "^5.0.0",
"homedir": "^0.6.0",
"hypercore": "^4.15.1",
"hyperdiscovery": "^1.0.1",
"memdb": "^1.3.1",
"mkdirp": "^0.5.1",
"recursive-readdir-sync": "^1.0.6",
"rimraf": "^2.5.4",
"standard": "^8.6.0",
"tap-spec": "^4.1.1",
"tape": "^4.6.3",
Expand Down
20 changes: 19 additions & 1 deletion tests/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ test('clone - errors on existing dir', function (t) {
return true
})
st.stdout.empty()
st.fails()
st.end()
})

Expand Down Expand Up @@ -197,6 +196,25 @@ test('clone - with --temp', function (t) {
})
})

test('clone - hypercore link', function (t) {
help.shareFeed(function (_, key, close) {
var cmd = dat + ' clone ' + key
var st = spawn(t, cmd, {cwd: baseTestDir})
var datDir = path.join(baseTestDir, key)
st.stderr.match(function (output) {
var error = output.indexOf('not a Dat Archive') > -1
if (!error) return false
t.ok(error, 'has error')
t.ok(!help.isDir(datDir), 'download dir removed')
st.kill()
return true
})
st.end(function () {
close()
})
})
})

test('close sharer', function (t) {
shareDat.close(function () {
rimraf.sync(path.join(shareDat.path, '.dat'))
Expand Down
20 changes: 20 additions & 0 deletions tests/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var recursiveReadSync = require('recursive-readdir-sync')
var Dat = require('dat-node')
var hypercore = require('hypercore')
var memdb = require('memdb')
var swarm = require('hyperdiscovery')

module.exports.matchLink = matchDatLink
module.exports.isDir = isDir
module.exports.testFolder = newTestFolder
module.exports.shareFixtures = shareFixtures
module.exports.shareFeed = shareFeed
module.exports.fileList = fileList

function shareFixtures (opts, cb) {
Expand Down Expand Up @@ -60,3 +64,19 @@ function isDir (dir) {
return false
}
}

function shareFeed (cb) {
var core = hypercore(memdb())
var feed = core.createFeed()
feed.append('hello world', function (err) {
if (err) throw err
cb(null, feed.key.toString('hex'), close)
})
var sw = swarm(feed)

function close (cb) {
feed.close(function () {
sw.close(cb)
})
}
}
4 changes: 2 additions & 2 deletions tests/sync-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ test('sync-remote - default opts', function (t) {
var cmd = dat + ' sync'
var st = spawn(t, cmd, {cwd: datDir})
st.stdout.match(function (output) {
var connected = output.indexOf('peer') > -1
if (!connected) return false
var updated = output.indexOf('Files updated') > -1
if (!updated) return false

var fileRe = new RegExp('2 files')
var bytesRe = new RegExp(/1\.\d{1,2} kB/)
Expand Down

0 comments on commit 8c4aaa4

Please sign in to comment.