Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
use new hypercore-archiver (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
Karissa authored May 24, 2017
1 parent 4829a86 commit da57c25
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 31 deletions.
14 changes: 8 additions & 6 deletions client/js/models/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ module.exports = {
})
},
getMetadata: function (state, data, send, done) {
if (!state.key) return done()
http({url: `/metadata/${state.key}?timeout=${data.timeout}`, method: 'GET', json: true}, function (err, resp, json) {
if (err) return send('archive:update', {error: {message: err.message}}, done)
if (json.error) return send('archive:update', json, done)
if (json.entries) json.error = null
send('archive:update', json, done)
if (!state.key || state.fetched) return done()
send('archive:update', {fetched: true}, function () {
http({url: `/metadata/${state.key}?timeout=${data.timeout}`, method: 'GET', json: true}, function (err, resp, json) {
if (err) return send('archive:update', {error: {message: err.message}}, done)
if (json.error) return send('archive:update', json, done)
if (json.entries) json.error = null
send('archive:update', json, done)
})
})
},
delete: function (state, data, send, done) {
Expand Down
2 changes: 1 addition & 1 deletion client/js/pages/archive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const archivePage = (state, prev, send) => {
}
}
if (err.message === 'timed out') {
if (!module.parent) send('archive:getMetadata', {timeout: 60000})
err.message = 'Looking for dat.json metadata...'
}
}
if (!module.parent) send('archive:getMetadata', {timeout: 60000})
var peers = Math.max(state.archive.peers - 1, 0) // we don't count
var size = state.archive.size
var meta = state.archive.metadata
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"from2": "^2.3.0",
"get-form-data": "^1.2.5",
"gravatar": "^1.6.0",
"hyperdiscovery": "^6.0.4",
"hypercore-archiver": "^4.1.0",
"hyperdrive": "^9.2.3",
"intro.js": "^2.1.0",
"is-my-json-valid": "^2.15.0",
Expand All @@ -115,7 +115,6 @@
"relative-date": "^1.1.3",
"render-data": "^2.2.0",
"response": "^0.18.0",
"run-parallel": "^1.1.6",
"serialize-javascript": "^1.3.0",
"sheetify": "^6.0.1",
"sheetify-nested": "^1.0.2",
Expand Down
33 changes: 11 additions & 22 deletions server/dats.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
const mkdirp = require('mkdirp')
const parallel = require('run-parallel')
const hyperdiscovery = require('hyperdiscovery')
const ram = require('random-access-memory')
const encoding = require('dat-encoding')
const hyperdrive = require('hyperdrive')
const archiver = require('hypercore-archiver')
const swarm = require('hypercore-archiver/swarm')

module.exports = Dats

function Dats (dir) {
if (!(this instanceof Dats)) return new Dats(dir)
mkdirp.sync(dir)
this.archives = {}
this.ar = archiver(dir, {sparse: true})
this.swarm = swarm(this.ar)
}

Dats.prototype.get = function (key, opts, cb) {
var self = this
if (typeof opts === 'function') return this.get(key, {}, opts)
key = encoding.toStr(key)
if (this.archives[key]) return cb(null, this.archives[key])
var buf = encoding.toBuf(key)
var archive = hyperdrive('./archiver/ ' + key, buf, {sparse: true, latest: false})
archive.once('ready', function () {
var swarm = hyperdiscovery(archive)
archive.swarm = swarm
self.archives[key] = archive
return cb(null, archive)
this.ar.add(key, function () {
})
self.ar.get(key, function (err, metadataFeed, contentFeed) {
if (err) return cb(err)
return cb(null, hyperdrive(ram, {metadata: metadataFeed, content: contentFeed}))
})
}

Expand Down Expand Up @@ -86,15 +85,5 @@ Dats.prototype.metadata = function (archive, opts, cb) {
}

Dats.prototype.close = function (cb) {
var tasks = []
for (var i in this.archives) {
var archive = this.archives[i]
var swarm = archive.swarm
tasks.push(function (next) {
swarm.leave(archive.discoveryKey)
swarm.destroy(next)
})
}

parallel(tasks, cb)
this.swarm.destroy(cb)
}

0 comments on commit da57c25

Please sign in to comment.