From da57c259733a283737bf902bbc25a4561e237e6a Mon Sep 17 00:00:00 2001 From: Karissa McKelvey Date: Tue, 23 May 2017 19:43:56 -0700 Subject: [PATCH] use new hypercore-archiver (#551) --- client/js/models/archive.js | 14 ++++++++------ client/js/pages/archive/index.js | 2 +- package.json | 3 +-- server/dats.js | 33 +++++++++++--------------------- 4 files changed, 21 insertions(+), 31 deletions(-) diff --git a/client/js/models/archive.js b/client/js/models/archive.js index 63c24dff..097251f4 100644 --- a/client/js/models/archive.js +++ b/client/js/models/archive.js @@ -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) { diff --git a/client/js/pages/archive/index.js b/client/js/pages/archive/index.js index 27f07c0d..a63052a9 100644 --- a/client/js/pages/archive/index.js +++ b/client/js/pages/archive/index.js @@ -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 diff --git a/package.json b/package.json index 2f420e57..0f58ad41 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/server/dats.js b/server/dats.js index d04a3b3d..4d2ba3e4 100644 --- a/server/dats.js +++ b/server/dats.js @@ -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})) }) } @@ -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) }