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

Commit da57c25

Browse files
authored
use new hypercore-archiver (#551)
1 parent 4829a86 commit da57c25

File tree

4 files changed

+21
-31
lines changed

4 files changed

+21
-31
lines changed

client/js/models/archive.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ module.exports = {
2828
})
2929
},
3030
getMetadata: function (state, data, send, done) {
31-
if (!state.key) return done()
32-
http({url: `/metadata/${state.key}?timeout=${data.timeout}`, method: 'GET', json: true}, function (err, resp, json) {
33-
if (err) return send('archive:update', {error: {message: err.message}}, done)
34-
if (json.error) return send('archive:update', json, done)
35-
if (json.entries) json.error = null
36-
send('archive:update', json, done)
31+
if (!state.key || state.fetched) return done()
32+
send('archive:update', {fetched: true}, function () {
33+
http({url: `/metadata/${state.key}?timeout=${data.timeout}`, method: 'GET', json: true}, function (err, resp, json) {
34+
if (err) return send('archive:update', {error: {message: err.message}}, done)
35+
if (json.error) return send('archive:update', json, done)
36+
if (json.entries) json.error = null
37+
send('archive:update', json, done)
38+
})
3739
})
3840
},
3941
delete: function (state, data, send, done) {

client/js/pages/archive/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ const archivePage = (state, prev, send) => {
3333
}
3434
}
3535
if (err.message === 'timed out') {
36-
if (!module.parent) send('archive:getMetadata', {timeout: 60000})
3736
err.message = 'Looking for dat.json metadata...'
3837
}
3938
}
39+
if (!module.parent) send('archive:getMetadata', {timeout: 60000})
4040
var peers = Math.max(state.archive.peers - 1, 0) // we don't count
4141
var size = state.archive.size
4242
var meta = state.archive.metadata

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"from2": "^2.3.0",
9191
"get-form-data": "^1.2.5",
9292
"gravatar": "^1.6.0",
93-
"hyperdiscovery": "^6.0.4",
93+
"hypercore-archiver": "^4.1.0",
9494
"hyperdrive": "^9.2.3",
9595
"intro.js": "^2.1.0",
9696
"is-my-json-valid": "^2.15.0",
@@ -115,7 +115,6 @@
115115
"relative-date": "^1.1.3",
116116
"render-data": "^2.2.0",
117117
"response": "^0.18.0",
118-
"run-parallel": "^1.1.6",
119118
"serialize-javascript": "^1.3.0",
120119
"sheetify": "^6.0.1",
121120
"sheetify-nested": "^1.0.2",

server/dats.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
const mkdirp = require('mkdirp')
2-
const parallel = require('run-parallel')
3-
const hyperdiscovery = require('hyperdiscovery')
2+
const ram = require('random-access-memory')
43
const encoding = require('dat-encoding')
54
const hyperdrive = require('hyperdrive')
5+
const archiver = require('hypercore-archiver')
6+
const swarm = require('hypercore-archiver/swarm')
67

78
module.exports = Dats
89

910
function Dats (dir) {
1011
if (!(this instanceof Dats)) return new Dats(dir)
1112
mkdirp.sync(dir)
12-
this.archives = {}
13+
this.ar = archiver(dir, {sparse: true})
14+
this.swarm = swarm(this.ar)
1315
}
1416

1517
Dats.prototype.get = function (key, opts, cb) {
1618
var self = this
1719
if (typeof opts === 'function') return this.get(key, {}, opts)
1820
key = encoding.toStr(key)
19-
if (this.archives[key]) return cb(null, this.archives[key])
20-
var buf = encoding.toBuf(key)
21-
var archive = hyperdrive('./archiver/ ' + key, buf, {sparse: true, latest: false})
22-
archive.once('ready', function () {
23-
var swarm = hyperdiscovery(archive)
24-
archive.swarm = swarm
25-
self.archives[key] = archive
26-
return cb(null, archive)
21+
this.ar.add(key, function () {
22+
})
23+
self.ar.get(key, function (err, metadataFeed, contentFeed) {
24+
if (err) return cb(err)
25+
return cb(null, hyperdrive(ram, {metadata: metadataFeed, content: contentFeed}))
2726
})
2827
}
2928

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

8887
Dats.prototype.close = function (cb) {
89-
var tasks = []
90-
for (var i in this.archives) {
91-
var archive = this.archives[i]
92-
var swarm = archive.swarm
93-
tasks.push(function (next) {
94-
swarm.leave(archive.discoveryKey)
95-
swarm.destroy(next)
96-
})
97-
}
98-
99-
parallel(tasks, cb)
88+
this.swarm.destroy(cb)
10089
}

0 commit comments

Comments
 (0)