Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.

Commit 4b376e1

Browse files
author
Joe Hand
authored
Merge pull request #4 from millette/more-tests
Large change to resolve URLs with paths or versions.
2 parents fa48f9b + 0de355b commit 4b376e1

3 files changed

Lines changed: 86 additions & 38 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ resolve urls, links to a dat key using common methods
1212
* URLs with keys in them (`datproject.org/6161616161616161616161616161616161616161616161616161616161616161`)
1313
* `hyperdrive-key` or `dat-key` headers
1414
* Url to JSON http request that returns `{key: <dat-key>}`
15+
* Dat-DNS resolution ([details](https://github.com/beakerbrowser/beaker/wiki/Authenticated-Dat-URLs-and-HTTPS-to-Dat-Discovery))
1516

1617
## Install
1718

@@ -40,6 +41,7 @@ Resolution order:
4041
1. Validate buffers or any strings with 64 character hashes in them via [dat-encoding](https://github.com/juliangruber/dat-encoding)
4142
2. Check headers in http request
4243
3. Check JSON request response for `key`
44+
4. Dat-DNS resolution via [dat-dns](https://github.com/datprotocol/dat-dns)
4345

4446
## Contributing
4547

index.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var assert = require('assert')
2-
var url = require('url')
32
var stringKey = require('dat-encoding').toStr
43
var nets = require('nets')
54
var datDns = require('dat-dns')()
@@ -19,46 +18,45 @@ function resolve (link, cb) {
1918
key = stringKey(link)
2019
cb(null, key)
2120
} catch (e) {
22-
return lookup()
21+
lookup()
2322
}
2423

2524
function lookup () {
26-
debug('lookup', link)
27-
var parsed = url.parse(link)
28-
29-
// parsed.host check b/c url.parse('beakerbrowser.com') returns parsed.pathname = 'beakerbrowser.com'
30-
if (parsed.host && parsed.path && parsed.path !== '/') return resolveKey()
31-
32-
// If no path, check .well-known first
33-
datDns.resolveName(link, function (err, key) {
34-
if (key) return cb(null, key)
35-
if (err) debug('datDns.resolveName() error', err)
36-
resolveKey()
37-
})
38-
39-
function resolveKey () {
40-
var urlLink = link.indexOf('http') > -1 ? link : 'http://' + link
41-
nets({ url: urlLink, json: true }, function (err, resp, body) {
42-
if (err) return cb(err)
43-
if (resp.statusCode !== 200) return cb(body.message)
44-
45-
// first check if key is in header response
46-
key = resp.headers['hyperdrive-key'] || resp.headers['dat-key']
47-
if (key) {
48-
debug('Received key from http header:', key)
49-
return cb(null, key)
50-
}
51-
52-
// else fall back to parsing the body
53-
try {
54-
key = stringKey(body.url)
55-
debug('Received key via json:', key)
56-
if (key) return cb(null, key)
57-
} catch (e) {
58-
cb(new Error(e))
59-
}
60-
cb(new Error('Unable to lookup key from http link.'))
25+
// if it starts with http or dat: use as is, otherwise prepend http://
26+
var urlLink = (link.indexOf('http') && link.indexOf('dat:')) ? ('http://' + link) : link
27+
28+
function resolveName () {
29+
datDns.resolveName(urlLink, function (err, key) {
30+
debug('resolveName', urlLink, err, key)
31+
if (key) return cb(null, key)
32+
if (err) debug('datDns.resolveName() error')
33+
cb(err)
6134
})
6235
}
36+
37+
debug('resolveKey', link, urlLink)
38+
nets({ url: urlLink, json: true }, function (err, resp, body) {
39+
// no ressource at given URL
40+
if (err || resp.statusCode !== 200) {
41+
return resolveName()
42+
}
43+
44+
// first check if key is in header response
45+
key = resp.headers['hyperdrive-key'] || resp.headers['dat-key']
46+
if (key) {
47+
debug('Received key from http header:', key)
48+
return cb(null, key)
49+
}
50+
51+
// else fall back to parsing the body
52+
try {
53+
key = stringKey(body.url)
54+
debug('Received key via json:', key, typeof body, body && typeof body.url)
55+
if (key) return cb(null, key)
56+
} catch (e) {
57+
// fall back to datDns
58+
resolveName()
59+
}
60+
})
6361
}
6462
}

test/index.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var test = require('tape')
2-
var enc = require('dat-encoding')
32
var datResolve = require('..')
3+
var enc = require('dat-encoding')
44

55
// Strings that do not require lookup
66
var stringKeys = [
@@ -18,6 +18,54 @@ var stringBadKeys = [
1818
{type: 'invalid', key: '61616161616161616161616161616161616161616161616161616161616161612'}
1919
]
2020

21+
test('resolve key with path', function (t) {
22+
t.plan(2)
23+
datResolve('87ed2e3b160f261a032af03921a3bd09227d0a4cde73466c17114816cae43336/path', function (err, newKey) {
24+
t.notOk(err, 'not expected error')
25+
t.ok(newKey, 'is a key')
26+
})
27+
})
28+
29+
test('resolve https hostname with path', function (t) {
30+
t.plan(2)
31+
datResolve('https://beakerbrowser.com/path', function (err, newKey) {
32+
t.notOk(err, 'not expected error')
33+
t.ok(newKey, 'is a key')
34+
})
35+
})
36+
37+
test('resolve dat hostname with path', function (t) {
38+
t.plan(2)
39+
datResolve('dat://beakerbrowser.com/path', function (err, newKey) {
40+
t.notOk(err, 'not expected error')
41+
t.ok(newKey, 'is a key')
42+
})
43+
})
44+
45+
test('resolve hostname with path', function (t) {
46+
t.plan(2)
47+
datResolve('beakerbrowser.com/path', function (err, newKey) {
48+
t.notOk(err, 'not expected error')
49+
t.ok(newKey, 'is a key')
50+
})
51+
})
52+
53+
test('resolve key with version', function (t) {
54+
t.plan(2)
55+
datResolve('87ed2e3b160f261a032af03921a3bd09227d0a4cde73466c17114816cae43336+5', function (err, newKey) {
56+
t.notOk(err, 'not expected error')
57+
t.ok(newKey, 'is a key')
58+
})
59+
})
60+
61+
test('resolve hostname with version', function (t) {
62+
t.plan(2)
63+
datResolve('beakerbrowser.com+5', function (err, newKey) {
64+
t.notOk(err, 'not expected error')
65+
t.ok(newKey, 'is a key')
66+
})
67+
})
68+
2169
test('resolve bad key without http', function (t) {
2270
t.plan(2 * stringBadKeys.length) // 2 tests for 2 keys
2371
stringBadKeys.forEach(function (key) {

0 commit comments

Comments
 (0)