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

Commit

Permalink
Publish better (#594)
Browse files Browse the repository at this point in the history
* move dat handling to dat-registry-api module

* Refactor by moving the archiver from here to dat-registry-api. The new version
of dat-registry-api will check to make sure the dat is available and cache it's
metadata.

* remove unnecessary dependency

* fix tests

* no dat encoding

* bug

* depend on 6.0.0 dat-registry-api
  • Loading branch information
Karissa committed Jun 20, 2017
1 parent 2a64cb3 commit 88238c8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 170 deletions.
3 changes: 0 additions & 3 deletions client/js/pages/archive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ const archivePage = (state, emit) => {
`
}
}
if (err.message === 'timed out') {
err.message = 'Looking for dat.json metadata...'
}
}
// var owner = (meta && state.township) && meta.username === state.township.username
var meta = state.archive.metadata
Expand Down
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@
"dat-colors": "^3.4.0",
"dat-design": "^5.0.0",
"dat-icons": "^2.5.0",
"dat-link-resolve": "^1.0.0",
"dat-registry": "^3.0.2",
"dat-registry-api": "^5.0.0",
"dat-registry-api": "^6.0.0",
"dat-swarm-defaults": "^1.0.0",
"debug": "^2.6.8",
"discovery-swarm": "^4.2.0",
Expand All @@ -92,9 +91,6 @@
"from2": "^2.3.0",
"get-form-data": "^1.2.5",
"gravatar": "^1.6.0",
"hypercore-archiver": "^4.1.0",
"hyperdrive": "^9.2.3",
"hyperhealth": "^2.0.0",
"mime": "^1.3.4",
"minifier": "^0.8.0",
"mixpanel": "^0.7.0",
Expand All @@ -104,8 +100,6 @@
"node-version-assets": "^1.1.0",
"pretty-bytes": "^3.0.1",
"pump": "^1.0.2",
"random-access-file": "^1.5.0",
"random-access-memory": "^2.4.0",
"range-parser": "^1.2.0",
"relative-date": "^1.1.3",
"render-data": "^2.2.0",
Expand Down
119 changes: 0 additions & 119 deletions server/dats.js

This file was deleted.

1 change: 0 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module.exports = function (config) {

server.on('close', function () {
router.api.close(function () {
router.dats.close()
})
})

Expand Down
26 changes: 13 additions & 13 deletions server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ const Mixpanel = require('mixpanel')
const app = require('../client/js/app')
const page = require('./page')
const Api = require('dat-registry-api')
const Dats = require('./dats')

module.exports = function (config) {
config = config || {}

const dats = config.dats || Dats(config.archiver)
const mx = Mixpanel.init(config.mixpanel)
const api = Api(config)
const db = api.db
const archiver = api.archiver

var router = express()
router.use(compression())
Expand Down Expand Up @@ -99,6 +98,7 @@ module.exports = function (config) {
router.get('/blog/*', function (req, res) {
res.redirect(301, 'http://bdatproject.org')
})
// TODO: move a lot of this junk below to some other api file so it can be more easily read

function onfile (archive, name, req, res) {
archive.stat(name, function (err, st) {
Expand Down Expand Up @@ -131,30 +131,30 @@ module.exports = function (config) {

router.get('/download/:archiveKey/*', function (req, res) {
debug('getting file contents', req.params)
dats.get(req.params.archiveKey, function (err, archive) {
archiver.get(req.params.archiveKey, function (err, archive) {
if (err) return onerror(err, res)
var filename = req.params[0]
return onfile(archive, filename, req, res)
})
})

router.get('/health/:archiveKey', function (req, res) {
dats.get(req.params.archiveKey, function (err, archive, key) {
archiver.get(req.params.archiveKey, function (err, archive, key) {
if (err) return onerror(err, res)
archive.ready(function () {
return res.status(200).json(dats.health(archive))
return res.status(200).json(archiver.health(archive))
})
})
})

router.get('/metadata/:archiveKey', function (req, res) {
const timeout = parseInt(req.query.timeout) || 1000
debug('requesting metadata for key', req.params.archiveKey)
dats.get(req.params.archiveKey, {timeout}, function (err, archive) {
archiver.get(req.params.archiveKey, {timeout}, function (err, archive) {
if (err) return onerror(err, res)
dats.metadata(archive, {timeout}, function (err, info) {
archiver.metadata(archive, {timeout}, function (err, info) {
if (err) info.error = {message: err.message}
info.health = dats.health(archive)
info.health = archiver.health(archive)
debug('got', info)
return res.status(200).json(info)
})
Expand Down Expand Up @@ -240,7 +240,7 @@ module.exports = function (config) {
debug('getting file contents', req.params)
var filename = req.params[0]
archiveRoute(req.params.archiveKey, function (state) {
dats.get(req.params.archiveKey, function (err, archive) {
archiver.get(req.params.archiveKey, function (err, archive) {
if (err) return onerror(err, res)
archive.stat(filename, function (err, entry) {
if (err) {
Expand Down Expand Up @@ -294,16 +294,16 @@ module.exports = function (config) {
var state = getDefaultAppState()
mx.track('archive viewed', {key: key})

dats.get(key, function (err, archive, key) {
archiver.get(key, function (err, archive, key) {
if (err) return onerror(err)
archive.ready(function () {
debug('got archive key', key)
state.archive.health = dats.health(archive)
state.archive.health = archiver.health(archive)
clearTimeout(timeout)
if (cancelled) return
cancelled = true

dats.metadata(archive, {timeout: 1000}, function (err, info) {
archiver.metadata(archive, {timeout: 1000}, function (err, info) {
if (err) state.archive.error = {message: err.message}
state.archive = xtend(state.archive, info)
state.archive.key = key
Expand All @@ -313,7 +313,7 @@ module.exports = function (config) {
})
}

router.dats = dats
router.archiver = archiver
router.api = api
return router

Expand Down
6 changes: 3 additions & 3 deletions tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ module.exports = {
admin: {name: 'pam spam', username: 'pam', password: 'secret123', email: '[email protected]', description: 'i dont eat it', token: null, role: 'admin'}
},
dats: {
cats: {name: 'cats', url: 'dat://ahashfordats', title: 'all of the cats', description: 'live on the corner of washington and 7th', keywords: 'furry, fluffy'},
penguins: {name: 'penguins', url: 'dat://ahashforpenguins', title: 'all of the penguins', description: 'lives in your house', keywords: 'sloppy, loud'},
dogs: {name: 'dogs', url: 'dat://ahashfordogs', title: 'all of the dogs', description: 'lives in your house', keywords: 'sloppy, loud'}
cats: {name: 'cats', url: 'dat://96cf4957539aff4fc856fa0804e613181064ed193e5f7882c9623ec7bed38deb', title: 'all of the cats', description: 'live on the corner of washington and 7th', keywords: 'furry, fluffy'},
penguins: {name: 'penguins', url: 'dat://96cf4957539aff4fc856fa0804e613181064ed193e5f7882c9623ec7bed38deb', title: 'all of the penguins', description: 'lives in your house', keywords: 'sloppy, loud'},
dogs: {name: 'dogs', url: 'dat://96cf4957539aff4fc856fa0804e613181064ed193e5f7882c9623ec7bed38deb', title: 'all of the dogs', description: 'lives in your house', keywords: 'sloppy, loud'}
}
}
40 changes: 16 additions & 24 deletions tests/unit/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const test = require('tape')
const TownshipClient = require('township-client')
const request = require('request')
const ram = require('random-access-memory')
const hyperdrive = require('hyperdrive')
const xtend = require('xtend')
const helpers = require('../helpers')
const Config = require('../../server/config')
Expand Down Expand Up @@ -145,18 +143,15 @@ test('api', function (t) {
})

test('api can create a dat', function (t) {
var archive = hyperdrive(ram)
archive.ready(function () {
client.secureRequest({method: 'POST', url: '/dats', body: dats.cats, json: true}, function (err, resp, body) {
client.secureRequest({method: 'POST', url: '/dats', body: dats.cats, json: true}, function (err, resp, body) {
t.ifError(err)
t.ok(body.id, 'has an id')
dats.cats.id = body.id
dats.cats.user_id = body.user_id
client.secureRequest({url: '/dats', json: true}, function (err, resp, body) {
t.ifError(err)
t.ok(body.id, 'has an id')
dats.cats.id = body.id
dats.cats.user_id = body.user_id
client.secureRequest({url: '/dats', json: true}, function (err, resp, body) {
t.ifError(err)
t.same(body.length, 1)
t.end()
})
t.same(body.length, 1)
t.end()
})
})
})
Expand Down Expand Up @@ -206,18 +201,15 @@ test('api', function (t) {
})

test('api can create another dat', function (t) {
var archive = hyperdrive(ram)
archive.ready(function () {
client.secureRequest({method: 'POST', url: '/dats', body: dats.penguins, json: true}, function (err, resp, body) {
client.secureRequest({method: 'POST', url: '/dats', body: dats.penguins, json: true}, function (err, resp, body) {
t.ifError(err)
t.ok(body.id, 'has an id')
dats.penguins.id = body.id
dats.penguins.user_id = body.user_id
client.secureRequest({url: '/dats', json: true}, function (err, resp, body) {
t.ifError(err)
t.ok(body.id, 'has an id')
dats.penguins.id = body.id
dats.penguins.user_id = body.user_id
client.secureRequest({url: '/dats', json: true}, function (err, resp, body) {
t.ifError(err)
t.same(body.length, 2)
t.end()
})
t.same(body.length, 2)
t.end()
})
})
})
Expand Down

0 comments on commit 88238c8

Please sign in to comment.